import 'dart:async'; import 'dart:developer'; import 'package:flutter/material.dart'; import 'package:sudoku_app/models/game_level.dart'; // ๐Ÿ‘ˆ [์ถ”๊ฐ€] ๋ ˆ๋ฒจ ๋ชจ๋ธ ์ž„ํฌํŠธ import 'package:sudoku_app/models/sudoku_game_dto.dart'; import 'package:sudoku_app/models/sudoku_theme.dart'; import 'package:sudoku_app/models/unified_rank_dto.dart'; import 'package:sudoku_app/services/puzzle_service.dart'; import 'package:sudoku_app/services/identity_service.dart'; import 'package:sudoku_app/widgets/ad_banner_widget.dart'; import 'package:sudoku_app/widgets/number_pad.dart'; import 'package:sudoku_app/widgets/sudoku_board.dart'; import 'package:sudoku_app/models/game_rank_dto.dart'; // ๋žญํ‚น ํŒ์—…์˜ 2๋‹จ๊ณ„ UI ์ƒํƒœ๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ enum enum _RankSubmissionStep { enterName, submitting, showList } class GameScreen extends StatefulWidget { final SudokuGameDto gameData; final String themeName; final String userId; final String? userName; final int levelIndex; // ๐Ÿ‘ˆ HomeScreen์—์„œ ์ „๋‹ฌ๋ฐ›์€ ํ˜„์žฌ ๋ ˆ๋ฒจ const GameScreen({ super.key, required this.gameData, required this.themeName, required this.userId, required this.userName, required this.levelIndex, }); @override State createState() => _GameScreenState(); } class _GameScreenState extends State { final PuzzleService _puzzleService = PuzzleService(); final IdentityService _identityService = IdentityService(); late final GameLevel currentLevel; // ๐Ÿ‘ˆ ํ˜„์žฌ ๋ ˆ๋ฒจ ์ •๋ณด late final int blockSize; late final int gridSize; late final SudokuTheme activeTheme; // ๐Ÿ‘ˆ ์ด๋ฒˆ ๊ฒŒ์ž„์˜ '๋™์ ' ํ…Œ๋งˆ // ๋ชจ๋“  ๋‚ด๋ถ€ ๋กœ์ง์€ 'int'๋กœ ๊ด€๋ฆฌ late List puzzleCells; late List solutionCells; late List originalCells; int? selectedIndex; int score = 5; int secondsElapsed = 0; Timer? timer; int? selectedNumberPad; Set incorrectCells = {}; bool isValidating = false; _RankSubmissionStep _rankStep = _RankSubmissionStep.enterName; List _rankingList = []; String _submittedPlayerName = ""; // "A" -> 10 (ํŒŒ์‹ฑ์šฉ) int _charToInt(String char) { if (char == '0') return 0; if (char.codeUnitAt(0) >= '1'.codeUnitAt(0) && char.codeUnitAt(0) <= '9'.codeUnitAt(0)) { return int.parse(char); } if (char.codeUnitAt(0) >= 'A'.codeUnitAt(0) && char.codeUnitAt(0) <= 'Z'.codeUnitAt(0)) { return char.codeUnitAt(0) - 'A'.codeUnitAt(0) + 10; } return -1; } // 10 -> "A" (์ „์†ก์šฉ) String _intToChar(int num) { if (num == 0) return '0'; if (num >= 1 && num <= 9) return num.toString(); if (num >= 10 && num <= 35) return String.fromCharCode('A'.codeUnitAt(0) + (num - 10)); return '?'; } @override void initState() { super.initState(); // 1. HomeScreen์—์„œ ๋ฐ›์€ levelIndex๋กœ ํ˜„์žฌ ๋ ˆ๋ฒจ ์ •๋ณด ๋กœ๋“œ currentLevel = AppLevels.getLevel(widget.levelIndex); blockSize = currentLevel.blockSize; gridSize = blockSize * blockSize; // 2. ๐Ÿ”ฝ [์ˆ˜์ •] ํ…Œ๋งˆ ์ •์ฑ… ์ ์šฉ String themeForThisGame = widget.themeName; // ๐Ÿ”ฝ [์ˆ˜์ •] 'isEasyMode' ๋ณ€์ˆ˜๋ฅผ 'GameLevel'์˜ ์†์„ฑ๋“ค๋กœ ์กฐํ•ฉ bool isEasyMode = currentLevel.isSequentialNumbers || currentLevel.isSequentialLetters; if (currentLevel.isSequentialNumbers) { themeForThisGame = AppThemes.numbers; // ๐Ÿ‘ˆ ์ˆซ์ž ํ…Œ๋งˆ ๊ฐ•์ œ } else if (currentLevel.isSequentialLetters) { themeForThisGame = AppThemes.letters; // ๐Ÿ‘ˆ ์•ŒํŒŒ๋ฒณ ํ…Œ๋งˆ ๊ฐ•์ œ } // 3. '์ด๋ฒˆ ๊ฒŒ์ž„'์˜ ํ…Œ๋งˆ ๊ฐ์ฒด ์ƒ์„ฑ activeTheme = AppThemes.buildGameTheme( themeForThisGame, gridSize, isEasyMode: isEasyMode, // ๐Ÿ‘ˆ [์ˆ˜์ •] ์กฐํ•ฉ๋œ bool ๊ฐ’ ์ „๋‹ฌ ); // 4. ์„œ๋ฒ„ ๋ฐ์ดํ„ฐ(String)๋ฅผ ๋‚ด๋ถ€ ๋กœ์ง(int)์œผ๋กœ ๋ณ€ํ™˜ puzzleCells = widget.gameData.question.split('').map(_charToInt).toList(); solutionCells = widget.gameData.solution.split('').map(_charToInt).toList(); originalCells = widget.gameData.question.split('').map(_charToInt).toList(); startTimer(); } @override void dispose() { timer?.cancel(); super.dispose(); } void startTimer() { timer = Timer.periodic(const Duration(seconds: 1), (timer) { setState(() { secondsElapsed++; }); }); } void onCellTapped(int index) { if (originalCells[index] == 0) { if (incorrectCells.isNotEmpty && !incorrectCells.contains(index)) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('ํ‹€๋ฆฐ ๊ฐ’์„ ๋จผ์ € ์ˆ˜์ •ํ•ด์ฃผ์„ธ์š”. (๋˜๋Œ๋ฆฌ๊ธฐ โ†ฉ)'), duration: Duration(seconds: 1), ), ); return; } setState(() { selectedIndex = index; if (selectedNumberPad != null) { final int numberValue = selectedNumberPad!; puzzleCells[index] = numberValue; if (numberValue != solutionCells[index]) { if (!incorrectCells.contains(index)) { if (score > 0) { score--; } incorrectCells.add(index); } } else { incorrectCells.remove(index); } _checkIfBoardIsFull(); } }); } } void _checkIfBoardIsFull() { if (!puzzleCells.contains(0) && !isValidating) { _validateGame(); } } void onNumberTapped(int numberValue) { setState(() { if (selectedNumberPad == numberValue) { selectedNumberPad = null; } else { selectedNumberPad = numberValue; } }); } void onUndoTapped() { if (incorrectCells.isNotEmpty) { int errorIndex = incorrectCells.first; setState(() { puzzleCells[errorIndex] = 0; incorrectCells.remove(errorIndex); selectedIndex = errorIndex; }); } else if (selectedIndex != null && originalCells[selectedIndex!] == 0) { setState(() { puzzleCells[selectedIndex!] = 0; }); } } void onHintTapped() { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('ํžŒํŠธ ๊ธฐ๋Šฅ์€ ์ค€๋น„ ์ค‘์ž…๋‹ˆ๋‹ค.')), ); } void _onRestartGameTapped() { setState(() { puzzleCells = originalCells.toList(); incorrectCells.clear(); selectedIndex = null; selectedNumberPad = null; score = 5; timer?.cancel(); secondsElapsed = 0; startTimer(); }); } void _onQuitGameTapped() { Navigator.of(context).pop(); } // ์ •๋‹ต ํ™•์ธ API ํ˜ธ์ถœ Future _validateGame() async { if (isValidating) return; setState(() { isValidating = true; }); timer?.cancel(); String currentAnswer = puzzleCells.map(_intToChar).join(''); try { final bool result = await _puzzleService.validateSolution( widget.gameData.puzzleId, currentAnswer, ); if (result) { if(mounted) _showRankingDialog(); } else { if(mounted) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('๐Ÿค” ํ‹€๋ฆฐ ๋ถ€๋ถ„์ด ์žˆ์Šต๋‹ˆ๋‹ค.')), ); } startTimer(); } } catch (e) { if(mounted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('์˜ค๋ฅ˜: $e')), ); } startTimer(); } finally { if(mounted) { setState(() { isValidating = false; }); } } } // ๋žญํ‚น ๋“ฑ๋ก ํŒ์—… (2๋‹จ๊ณ„ UI + ๋ ˆ๋ฒจ ์ž ๊ธˆ ํ•ด์ œ) void _showRankingDialog() { final nameController = TextEditingController(text: widget.userName); bool isSubmitting = false; String? dialogErrorMessage; _rankStep = _RankSubmissionStep.enterName; _rankingList = []; _submittedPlayerName = ""; final String contextId = currentLevel.contextId; showDialog( context: context, barrierDismissible: false, builder: (ctx) { return StatefulBuilder( builder: (context, setDialogState) { Widget closeButton = TextButton( onPressed: () { Navigator.of(ctx).pop(); Navigator.of(context).pop(); }, child: const Text('๋‹ซ๊ธฐ'), ); Widget rankListWidget = Expanded( child: _rankingList.isEmpty ? const Center(child: Text("ํ˜„์žฌ ๋žญํ‚น์ด ์—†์Šต๋‹ˆ๋‹ค.")) : ListView.builder( itemCount: _rankingList.length, shrinkWrap: true, itemBuilder: (context, index) { final rank = _rankingList[index]; final bool isMe = rank.playerName == _submittedPlayerName; int displayScore = 5 - (rank.secondaryScore ?? 5); final min = (rank.primaryScore ~/ 60).toString().padLeft(2, '0'); final sec = (rank.primaryScore % 60).toString().padLeft(2, '0'); final time = '$min:$sec'; return ListTile( selected: isMe, selectedTileColor: Colors.blue.shade100, leading: Text('${index + 1}.', style: const TextStyle(fontWeight: FontWeight.bold)), title: Text(rank.playerName, style: TextStyle(fontWeight: isMe ? FontWeight.bold : FontWeight.normal)), trailing: Text('$time (Score: $displayScore)', style: const TextStyle(fontWeight: FontWeight.bold, color: Colors.black87)), ); }, ), ); Widget nameEntryWidget = Column( mainAxisSize: MainAxisSize.min, children: [ Text('์™„๋ฃŒ ์‹œ๊ฐ„: $secondsElapsed ์ดˆ / ๋‚จ์€ ์ ์ˆ˜: $score ์ '), const SizedBox(height: 20), TextField( controller: nameController, readOnly: false, decoration: InputDecoration( labelText: '์ด๋ฆ„ (10์ž ์ด๋‚ด)', border: const OutlineInputBorder(), errorText: dialogErrorMessage, ), maxLength: 10, ), ], ); Widget submitButton = ElevatedButton( onPressed: () async { final playerName = nameController.text.trim(); if (playerName.isEmpty) { setDialogState(() { dialogErrorMessage = "์ด๋ฆ„์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."; }); return; } setDialogState(() { _rankStep = _RankSubmissionStep.submitting; _submittedPlayerName = playerName; dialogErrorMessage = null; }); final rankDto = UnifiedRankDto( userId: widget.userId, gameType: 'SUDOKU', contextId: contextId, playerName: playerName, primaryScore: secondsElapsed, secondaryScore: (5 - score), ); try { await _puzzleService.submitRank(rankDto); await _identityService.saveUserName(playerName); final int currentMaxLevel = await _identityService.getMaxUnlockedLevel(); if (currentMaxLevel < 99) { if (widget.levelIndex >= currentMaxLevel) { int nextLevel = widget.levelIndex + 1; if (nextLevel > AppLevels.allLevels.length) { await _identityService.saveMaxUnlockedLevel(99); } else { await _identityService.saveMaxUnlockedLevel(nextLevel); } } } final ranks = await _puzzleService.fetchRanks('SUDOKU', contextId); setDialogState(() { _rankingList = ranks; _rankStep = _RankSubmissionStep.showList; }); } catch (e) { log("!!! ๋žญํ‚น ๋“ฑ๋ก ์‹คํŒจ !!!", error: e); setDialogState(() { _rankStep = _RankSubmissionStep.enterName; dialogErrorMessage = e.toString().replaceFirst("Exception: ", ""); }); } }, child: const Text('๋žญํ‚น ๋“ฑ๋ก'), ); Widget dialogContent; if (_rankStep == _RankSubmissionStep.showList) { dialogContent = rankListWidget; } else { dialogContent = nameEntryWidget; } List dialogActions; if (_rankStep == _RankSubmissionStep.enterName) { dialogActions = [ TextButton( onPressed: () { Navigator.of(ctx).pop(); Navigator.of(context).pop(); }, child: const Text('๋‹ซ๊ธฐ'), ), submitButton ]; } else if (_rankStep == _RankSubmissionStep.submitting) { dialogActions = [const CircularProgressIndicator()]; } else { dialogActions = [closeButton]; } return AlertDialog( title: Text(_rankStep == _RankSubmissionStep.showList ? '๐Ÿ† ์ƒ์œ„ 10๊ฐœ ๋žญํ‚น ($contextId)' : '๐ŸŽ‰ ์„ฑ๊ณต! ๊ธฐ๋ก์„ ๋‚จ๊ฒจ์ฃผ์„ธ์š”.'), content: SizedBox( width: 400, height: _rankStep == _RankSubmissionStep.showList ? 400 : null, child: dialogContent, ), actions: dialogActions, ); }, ); }, ); } // ๋žญํ‚น ID ์ƒ์„ฑ์„ ์œ„ํ•ด, ์›๋ณธ ๋ฌธ์ œ์˜ ๋นˆ์นธ ๋น„์œจ๋กœ ๋ ˆ๋ฒจ(1~5)์„ ์—ญ์‚ฐ int _difficultyLevel(String question) { int holes = question.split('').where((c) => c == '0').length; double holeRatio = holes / (gridSize * gridSize); if (holeRatio <= 0.51) return 1; if (holeRatio <= 0.58) return 2; if (holeRatio <= 0.63) return 3; if (holeRatio <= 0.68) return 4; return 5; } @override Widget build(BuildContext context) { String formattedTime = '${(secondsElapsed ~/ 60).toString().padLeft(2, '0')}:${(secondsElapsed % 60).toString().padLeft(2, '0')}'; final Map numberCounts = {}; for (int i = 1; i <= gridSize; i++) { numberCounts[i] = 0; } for (int cellValue in puzzleCells) { if (cellValue > 0) { numberCounts[cellValue] = (numberCounts[cellValue] ?? 0) + 1; } } return Scaffold( body: SafeArea( child: Column( children: [ Expanded( child: LayoutBuilder( builder: (context, constraints) { bool isLandscape = constraints.maxWidth > constraints.maxHeight; if (isLandscape) { return _buildLandscapeLayout(context, numberCounts, constraints, formattedTime); } else { return _buildPortraitLayout(context, numberCounts, constraints, formattedTime); } }, ), ), const AdBannerWidget(), ], ), ), ); } Widget _buildPortraitLayout(BuildContext context, Map numberCounts, BoxConstraints constraints, String formattedTime) { final double boardWidth = (constraints.maxWidth > 600) ? 600 : constraints.maxWidth; return Center( child: ConstrainedBox( constraints: BoxConstraints(maxWidth: boardWidth), child: Column( children: [ Padding( padding: const EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 0), child: _buildGameInfoWidget(formattedTime), ), Expanded( child: Center( child: SingleChildScrollView( child: Padding( padding: const EdgeInsets.all(16.0), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ _buildSudokuBoardWidget(), const SizedBox(height: 15), _buildNumberPadWidget(context, numberCounts, isLandscape: false, boardWidth: boardWidth), ], ), ), ), ), ), ], ), ), ); } Widget _buildLandscapeLayout(BuildContext context, Map numberCounts, BoxConstraints constraints, String formattedTime) { const double infoBarHeight = 60.0; double boardWidth = constraints.maxHeight - infoBarHeight - 32.0; const double numberPadScaleRatio = 0.6; double padWidth = boardWidth * numberPadScaleRatio; if (padWidth < 200) padWidth = 200; if (padWidth > 350) padWidth = 350; double totalWidth = boardWidth + (padWidth + 100) + 16.0; if (totalWidth > (constraints.maxWidth - 32.0)) { double scale = (constraints.maxWidth - 32.0) / totalWidth; boardWidth *= scale; padWidth *= scale; } return Padding( padding: const EdgeInsets.all(16.0), child: Column( children: [ _buildGameInfoWidget(formattedTime), Expanded( child: Row( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, children: [ SizedBox( width: boardWidth, child: _buildSudokuBoardWidget(), ), const SizedBox(width: 16), SizedBox( width: padWidth + 100, child: SingleChildScrollView( child: Column( children: [ _buildNumberPadWidget(context, numberCounts, isLandscape: true, boardWidth: boardWidth), ], ), ), ), ], ), ), ], ), ); } Widget _buildGameInfoWidget(String formattedTime) { return Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.center, children: [ Text('SCORE: $score', style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold)), Text(formattedTime, style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold)), Row( mainAxisSize: MainAxisSize.min, children: [ IconButton( onPressed: onHintTapped, icon: const Icon(Icons.lightbulb_outline, color: Colors.orange), iconSize: 30, ), IconButton( onPressed: onUndoTapped, icon: const Icon(Icons.undo, color: Colors.red), iconSize: 30, ), ], ) ], ); } Widget _buildSudokuBoardWidget() { return SudokuBoard( blockSize: blockSize, theme: activeTheme, cells: puzzleCells, originalCells: originalCells, selectedIndex: selectedIndex, selectedNumberPad: selectedNumberPad, incorrectCells: incorrectCells, onCellTapped: onCellTapped, ); } Widget _buildNumberPadWidget(BuildContext context, Map numberCounts, {required bool isLandscape, required double boardWidth}) { const double numberPadScaleRatio = 0.6; double? padMaxWidth; if (!isLandscape) { padMaxWidth = boardWidth * numberPadScaleRatio; } else { padMaxWidth = null; } Widget numberPadGrid = ConstrainedBox( constraints: BoxConstraints(maxWidth: padMaxWidth ?? double.infinity), child: NumberPad( blockSize: blockSize, theme: activeTheme, numberCounts: numberCounts, selectedNumber: selectedNumberPad, onNumberTapped: onNumberTapped, isLandscape: isLandscape, ), ); Widget quitButton = IconButton( icon: Icon(Icons.close, color: Colors.red.shade700, size: 30), onPressed: _onQuitGameTapped, tooltip: "๊ฒŒ์ž„ ์ข…๋ฃŒ", ); Widget restartButton = IconButton( icon: Icon(Icons.refresh, color: Colors.blue.shade700, size: 30), onPressed: _onRestartGameTapped, tooltip: "๋‹ค์‹œํ•˜๊ธฐ", ); if (isLandscape) { return Column( mainAxisSize: MainAxisSize.min, children: [ numberPadGrid, const SizedBox(height: 10), Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [quitButton, restartButton], ) ], ); } else { return Row( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ quitButton, Expanded(child: numberPadGrid), restartButton, ], ); } } }