This commit is contained in:
2025-11-19 11:17:33 +09:00
parent 3b053530f5
commit 2008c377f4
17 changed files with 1416 additions and 966 deletions
@@ -32,12 +32,13 @@ class SpiderGameController with ChangeNotifier {
List<SpiderCard> _cardsToDealAnimate = [];
List<SpiderCard> get cardsToDealAnimate => _cardsToDealAnimate;
void clearDealAnimationTrigger() {
debugPrint("[LOG] clearDealAnimationTrigger: CALLED. Clearing animation queue (Current count: ${_cardsToDealAnimate.length}).");
debugPrint(
"[LOG] clearDealAnimationTrigger: CALLED. Clearing animation queue (Current count: ${_cardsToDealAnimate.length}).");
_cardsToDealAnimate.clear();
}
List<SpiderCard> _cardsToAnimateStack = [];
List<SpiderCard> get cardsToAnimateStack => _cardsToAnimateStack;
int _animationSourcePileIndex = -1;
@@ -47,8 +48,8 @@ class SpiderGameController with ChangeNotifier {
bool get canUndo {
return _undoHistory.isNotEmpty &&
!_isGameCompleted &&
_undoCount < maxUndoCount;
!_isGameCompleted &&
_undoCount < maxUndoCount;
}
void setUserInfo(String userId, String? userName) {
@@ -99,6 +100,7 @@ class SpiderGameController with ChangeNotifier {
}
return deck;
}
(List<List<SpiderCard>>, List<SpiderCard>) _dealCards(
List<SpiderCard> shuffledDeck, String distribution) {
final List<List<SpiderCard>> tableau = List.generate(10, (_) => []);
@@ -118,6 +120,7 @@ class SpiderGameController with ChangeNotifier {
}
return (tableau, stock);
}
void _startTimer() {
_timer?.cancel();
_secondsElapsed = 0;
@@ -126,6 +129,7 @@ class SpiderGameController with ChangeNotifier {
notifyListeners();
});
}
void stopTimer() {
_timer?.cancel();
}
@@ -133,7 +137,7 @@ class SpiderGameController with ChangeNotifier {
/// 🔽 덱 분배 (애니메이션 트리거)
void dealFromStock() {
debugPrint("[LOG] dealFromStock: CALLED. Checking conditions...");
if (_currentState.stock.isEmpty) {
debugPrint("[LOG] dealFromStock: FAILED (Stock is empty)");
return;
@@ -152,8 +156,9 @@ class SpiderGameController with ChangeNotifier {
}
final bool hasEmptyPile = _currentState.tableau.any((pile) => pile.isEmpty);
debugPrint("[LOG] dealFromStock: Checking for empty piles... Result: $hasEmptyPile");
debugPrint(
"[LOG] dealFromStock: Checking for empty piles... Result: $hasEmptyPile");
if (hasEmptyPile) {
debugPrint("[LOG] dealFromStock: FAILED (Empty pile found)");
return;
@@ -163,52 +168,66 @@ class SpiderGameController with ChangeNotifier {
_saveUndoState();
final int cardsToDealCount = min(10, _currentState.stock.length);
debugPrint("[LOG] dealFromStock: Preparing ${cardsToDealCount} cards for animation.");
debugPrint(
"[LOG] dealFromStock: Preparing ${cardsToDealCount} cards for animation.");
for (int i = 0; i < cardsToDealCount; i++) {
_cardsToDealAnimate.add(_currentState.stock.removeLast());
}
debugPrint("[LOG] dealFromStock: Cards moved to _cardsToDealAnimate queue (Total: ${_cardsToDealAnimate.length}). Notifying listeners...");
debugPrint(
"[LOG] dealFromStock: Cards moved to _cardsToDealAnimate queue (Total: ${_cardsToDealAnimate.length}). Notifying listeners...");
notifyListeners();
}
/// 🔽 덱 분배 애니메이션이 끝난 후 UI가 호출
void finalizeDealFromStock(List<SpiderCard> dealtCards) {
debugPrint("[LOG] finalizeDealFromStock: CALLED. Finalizing ${dealtCards.length} cards.");
debugPrint(
"[LOG] finalizeDealFromStock: CALLED. Finalizing ${dealtCards.length} cards.");
for (int i = 0; i < dealtCards.length; i++) {
final card = dealtCards[i];
card.isFaceUp = true;
_currentState.tableau[i].add(card);
}
_currentState = _currentState.copyWith(moves: _currentState.moves + 1);
debugPrint("[LOG] finalizeDealFromStock: FINISHED. Calling _checkCompletedStacks...");
debugPrint(
"[LOG] finalizeDealFromStock: FINISHED. Calling _checkCompletedStacks...");
_checkCompletedStacks();
}
// ( onDragStarted, onDragCancelled, onCardsDropped, _moveCards 는 동일 )
void onDragStarted(List<SpiderCard> cards) {
_draggedCards = cards;
for (var card in cards) { card.isBeingDragged = true; }
for (var card in cards) {
card.isBeingDragged = true;
}
notifyListeners();
}
void onDragCancelled() {
for (var card in _draggedCards) { card.isBeingDragged = false; }
for (var card in _draggedCards) {
card.isBeingDragged = false;
}
_draggedCards = [];
notifyListeners();
}
void onCardsDropped(List<SpiderCard> cards, int targetPileIndex) {
final int sourcePileIndex = _findPileIndexForCard(cards.first);
for (var card in cards) { card.isBeingDragged = false; }
for (var card in cards) {
card.isBeingDragged = false;
}
_draggedCards = [];
_moveCards(cards, sourcePileIndex, targetPileIndex);
}
void _moveCards(List<SpiderCard> cards, int fromIndex, int toIndex) {
if (fromIndex == toIndex) {
notifyListeners(); return;
notifyListeners();
return;
}
_saveUndoState();
final sourcePile = _currentState.tableau[fromIndex];
@@ -221,17 +240,17 @@ class SpiderGameController with ChangeNotifier {
_currentState = _currentState.copyWith(moves: _currentState.moves + 1);
_checkCompletedStacks();
}
int undo() {
if (!canUndo) return _undoCount;
final prevState = _undoHistory.removeLast();
_currentState = SpiderGameState.fromHistory(prevState);
_undoCount++;
notifyListeners();
return _undoCount;
}
// ( canPickUpCard, getDraggableStack, isValidMove 는 동일 )
bool canPickUpCard(SpiderCard card) {
for (final pile in _currentState.tableau) {
@@ -239,6 +258,7 @@ class SpiderGameController with ChangeNotifier {
}
return false;
}
List<SpiderCard> getDraggableStack(SpiderCard tappedCard) {
final int pileIndex = _findPileIndexForCard(tappedCard);
if (pileIndex == -1) return [];
@@ -251,15 +271,15 @@ class SpiderGameController with ChangeNotifier {
final currentCard = pile[i];
if (currentCard.isFaceUp &&
prevCard.rank == currentCard.rank + 1 &&
prevCard.suit == currentCard.suit)
{
prevCard.suit == currentCard.suit) {
draggableStack.add(currentCard);
} else {
return [];
return [];
}
}
return draggableStack;
}
bool isValidMove(List<SpiderCard> cardsToMove, int targetPileIndex) {
if (cardsToMove.isEmpty) return false;
final targetPile = _currentState.tableau[targetPileIndex];
@@ -272,7 +292,7 @@ class SpiderGameController with ChangeNotifier {
/// 🔽 _checkCompletedStacks (애니메이션 트리거)
void _checkCompletedStacks() {
// 🔽 [수정] 애니메이션이 실행 중이면 중복 검사 방지
if (_cardsToAnimateStack.isNotEmpty) return;
if (_cardsToAnimateStack.isNotEmpty) return;
bool stackCompleted = false;
for (int i = 0; i < _currentState.tableau.length; i++) {
@@ -295,12 +315,12 @@ class SpiderGameController with ChangeNotifier {
_cardsToAnimateStack = last13Cards;
_animationSourcePileIndex = i;
_animationTargetFoundationIndex = _currentState.foundation.length;
stackCompleted = true;
break;
break;
}
}
if (stackCompleted) {
notifyListeners(); // 👈 UI에 애니메이션을 그리라고 알림
} else {
@@ -309,38 +329,43 @@ class SpiderGameController with ChangeNotifier {
}
/// 🔽 [수정] 스택 완성 애니메이션이 끝난 후 UI가 호출 (인자 받도록 변경)
void finalizeStackCompletion(List<SpiderCard> cardsToAnimate, int sourceIndex) {
debugPrint("[LOG] finalizeStackCompletion: CALLED. Source Index: $sourceIndex");
void finalizeStackCompletion(
List<SpiderCard> cardsToAnimate, int sourceIndex) {
debugPrint(
"[LOG] finalizeStackCompletion: CALLED. Source Index: $sourceIndex");
// 🔽 [수정] 크래시 방지
if (sourceIndex < 0 || sourceIndex >= _currentState.tableau.length) {
debugPrint("[LOG] finalizeStackCompletion: FAILED. Invalid Source Index: $sourceIndex");
debugPrint(
"[LOG] finalizeStackCompletion: FAILED. Invalid Source Index: $sourceIndex");
return;
}
_currentState.foundation.add(cardsToAnimate);
final pile = _currentState.tableau[sourceIndex];
if (pile.length >= cardsToAnimate.length) {
pile.removeRange(pile.length - cardsToAnimate.length, pile.length);
} else {
debugPrint("[LOG] finalizeStackCompletion: WARNING. Pile length was ${pile.length}, expected >= ${cardsToAnimate.length}.");
debugPrint(
"[LOG] finalizeStackCompletion: WARNING. Pile length was ${pile.length}, expected >= ${cardsToAnimate.length}.");
}
if (pile.isNotEmpty && !pile.last.isFaceUp) {
pile.last.isFaceUp = true;
}
// 🔽 [삭제] 인덱스 리셋 불필요 (지역 변수로 처리됨)
// _animationSourcePileIndex = -1;
// _animationTargetFoundationIndex = -1;
_checkGameCompletion(); // 👈 [핵심] 게임 완료 검사
}
// 🔽 [복원됨]
void clearStackAnimationTrigger() {
debugPrint("[LOG] clearStackAnimationTrigger: CALLED. Clearing animation queue (Current count: ${_cardsToAnimateStack.length}).");
debugPrint(
"[LOG] clearStackAnimationTrigger: CALLED. Clearing animation queue (Current count: ${_cardsToAnimateStack.length}).");
_cardsToAnimateStack.clear();
}
@@ -348,7 +373,8 @@ class SpiderGameController with ChangeNotifier {
if (_currentState.foundation.length == 8 && !_isGameCompleted) {
_isGameCompleted = true;
stopTimer();
debugPrint("게임 완료! 이동: ${_currentState.moves}, 시간: $_secondsElapsed");
debugPrint(
"게임 완료! 이동: ${_currentState.moves}, 시간: $_secondsElapsed");
notifyListeners(); // 👈 [수정] 게임이 '완료'되었을 때만 notify
} else if (!_isGameCompleted) {
// 🔽 [수정] 게임이 완료되지 '않았을' 때도 notify (카드 이동 등을 반영하기 위해)
@@ -357,42 +383,21 @@ class SpiderGameController with ChangeNotifier {
// (게임이 완료된 후에는 더 이상 notify하지 않음)
}
// ( _saveUndoState, _findPileIndexForCard, submitRank, dispose 는 동일 )
// ( _saveUndoState, _findPileIndexForCard 는 동일 )
void _saveUndoState() {
_undoHistory.add(SpiderGameHistory.fromState(_currentState));
if (_undoHistory.length > 20) {
_undoHistory.removeAt(0);
}
}
int _findPileIndexForCard(SpiderCard card) {
return _currentState.tableau.indexWhere((pile) => pile.contains(card));
}
Future<RankSubmissionResult> submitRank(String playerName) async {
final puzzleService = PuzzleService();
final identityService = IdentityService();
final rankDto = UnifiedRankDto(
userId: userId,
gameType: 'SPIDER',
contextId: difficulty.contextId,
playerName: playerName,
primaryScore: _currentState.moves,
secondaryScore: _secondsElapsed,
);
final result = await puzzleService.submitRank(rankDto);
await identityService.saveUserName(playerName);
final int currentMaxLevel = await identityService.getMaxUnlockedLevel(gameType: 'SPIDER');
if (currentMaxLevel < 99) {
if (difficulty.levelIndex >= currentMaxLevel) {
int nextLevel = difficulty.levelIndex + 1;
if (nextLevel > SpiderDifficulties.allDifficulties.length) {
await identityService.saveMaxUnlockedLevel(99, gameType: 'SPIDER');
} else {
await identityService.saveMaxUnlockedLevel(nextLevel, gameType: 'SPIDER');
}
}
}
return result;
}
// ❌ [삭제] submitRank 메서드 (약 20줄) 삭제
// Future<RankSubmissionResult> submitRank(String playerName) async { ... }
@override
void dispose() {
_timer?.cancel();