import 'dart:async'; import 'package:flutter/material.dart'; import 'package:playwith_core/playwith_core.dart'; enum PlayerStatus { alive, dead, winner, loser } class QuizGame extends BaseGame { @override String get id => "quiz_ox"; @override String get name => "OX 퀴즈 서바이벌"; @override String get description => "끝까지 살아남으세요!"; // ------------------------------------------------------------------------ // 상태 변수 // ------------------------------------------------------------------------ final _gameStateController = StreamController>.broadcast(); Stream> get gameStateStream => _gameStateController.stream; // UI 초기화 지연 방지용 데이터 Map? _lastState; final Set _aliveUsers = {}; final Set _answeredUsers = {}; PlayerStatus _myStatus = PlayerStatus.alive; String? _mySelectedAnswer; bool _isLockedIn = false; Timer? _lockInTimer; // [상태] 카운트다운 중인가? bool _isCountingDown = false; int _countdownValue = 3; // [상태] 정답 공개 중인가? (중간 대기 화면) bool _isShowingResult = false; String _currentCorrectAnswer = ""; final List> _questions = [ {"q": "사과는 영어로 Apple이다.", "a": "O"}, {"q": "바나나는 길어지면 기차다.", "a": "X"}, {"q": "플러터는 구글이 만들었다.", "a": "O"}, {"q": "지범님은 천재 개발자다.", "a": "O"}, {"q": "북극곰의 피부색은 검은색이다.", "a": "O"}, {"q": "타조는 날 수 있다.", "a": "X"}, ]; int _currentQuestionIndex = -1; // ------------------------------------------------------------------------ // 라이프사이클 // ------------------------------------------------------------------------ @override void onStart() { super.onStart(); print("Quiz Game Started!"); _resetLocalState(); _lastState = null; _aliveUsers.clear(); _aliveUsers.add(NetworkManager().me.id); for (var guest in NetworkManager().guestList) { _aliveUsers.add(guest.id); } // [Host] 게임 시작 시퀀스 진입 if (NetworkManager().role == NetworkRole.host) { // 잠시 대기 후 첫 번째 문제 카운트다운 시작 Future.delayed(const Duration(seconds: 1), () { _startNextQuestionSequence(); }); } } @override void onDispose() { _lockInTimer?.cancel(); _gameStateController.close(); super.onDispose(); } // ------------------------------------------------------------------------ // 메시지 처리 (Logic) // ------------------------------------------------------------------------ @override void onMessageReceived(String senderId, Map payload) { if (payload['type'] != 'ANSWER_SUBMIT') { _lastState = payload; } // 1. [Common] 카운트다운 수신 if (payload['type'] == 'GAME_COUNTDOWN') { _isShowingResult = false; // 결과 화면 끄기 _isCountingDown = true; _countdownValue = payload['count']; // 3, 2, 1 소리 SoundManager().playSfx(SoundKey.click); _gameStateController.add(payload); } // 2. [Host Logic] 답변 제출 처리 if (payload['type'] == 'ANSWER_SUBMIT') { if (NetworkManager().role != NetworkRole.host) return; final String userId = payload['userId']; final String answer = payload['answer']; if (!_aliveUsers.contains(userId)) return; if (_answeredUsers.contains(userId)) return; _answeredUsers.add(userId); final currentAnswer = _questions[_currentQuestionIndex]['a']; bool isCorrect = (answer == currentAnswer); if (!isCorrect) { _aliveUsers.remove(userId); } // 제출 현황 전파 _broadcastState({ 'type': 'PLAYER_STATUS_UPDATE', 'userId': userId, 'isSubmitted': true, 'isAlive': isCorrect }); // [자동 진행] 전원 제출 시 -> 결과 발표 -> 카운트다운 -> 다음 문제 int currentAliveCount = _aliveUsers.length + (isCorrect ? 0 : 1); if (_answeredUsers.length >= currentAliveCount) { Future.delayed(const Duration(milliseconds: 500), () { _showRoundResultAndNext(); // 결과 발표 및 다음 단계 }); } } // 3. [Common] 중간 결과 발표 (정답 공개) if (payload['type'] == 'ROUND_RESULT') { _isCountingDown = false; _isShowingResult = true; // 결과 화면 모드 진입 _currentCorrectAnswer = payload['correctAnswer']; final List survivors = payload['survivors'] ?? []; final bool isSurvived = survivors.contains(NetworkManager().me.id); // 내 생존 여부 업데이트 및 효과음 if (!isSurvived && _myStatus == PlayerStatus.alive) { _handleElimination(); } else if (isSurvived && _myStatus == PlayerStatus.alive) { // 정답 소리 (선택 사항) // SoundManager().playSfx(SoundKey.correct); } _gameStateController.add(payload); } // 4. [Common] 플레이어 상태 업데이트 if (payload['type'] == 'PLAYER_STATUS_UPDATE') { final userId = payload['userId']; _answeredUsers.add(userId); if (payload['isAlive'] == false) { _aliveUsers.remove(userId); } _gameStateController.add(payload); } // 5. [Common] 탈락 통보 (본인) if (payload['type'] == 'PLAYER_ELIMINATED') { final targetId = payload['targetUserId']; _aliveUsers.remove(targetId); if (targetId == NetworkManager().me.id) { _handleElimination(); } _gameStateController.add({'type': 'UI_REFRESH'}); } // 6. [Common] 새 문제 시작 if (payload['type'] == 'GAME_STATE_UPDATE' && payload['status'] == 'QUESTION') { _isCountingDown = false; _isShowingResult = false; _resetLocalState(); _gameStateController.add(payload); } // 7. [Common] 종료 if (payload['type'] == 'GAME_OVER' || payload['type'] == 'GAME_EXIT') { if (payload['type'] == 'GAME_OVER') { final winnerId = payload['winnerId']; _myStatus = (winnerId == NetworkManager().me.id) ? PlayerStatus.winner : PlayerStatus.loser; if (_myStatus == PlayerStatus.winner) SoundManager().playSfx(SoundKey.win); } _gameStateController.add(payload); } } void _handleElimination() { SoundManager().playSfx(SoundKey.wrong); _myStatus = PlayerStatus.dead; } // ------------------------------------------------------------------------ // [Host Logic] 진행 관리자 // ------------------------------------------------------------------------ // 1. 라운드 결과 발표 (정답 O/X 보여주기) void _showRoundResultAndNext() { final currentQ = _questions[_currentQuestionIndex]; final resultData = { 'type': 'ROUND_RESULT', 'status': 'RESULT', 'correctAnswer': currentQ['a'], 'survivors': _aliveUsers.toList(), }; _broadcastState(resultData); // 3초간 결과 보여주고 -> 카운트다운 시작 Future.delayed(const Duration(seconds: 3), () { _checkWinnerAndNext(); }); } // 2. 승패 체크 후 -> 카운트다운 -> 문제 출제 void _checkWinnerAndNext() { int totalStartPlayers = NetworkManager().guestList.length + 1; // 종료 조건 if ((totalStartPlayers > 1 && _aliveUsers.length <= 1) || _currentQuestionIndex >= _questions.length - 1) { String? winnerId; if (_aliveUsers.isNotEmpty) winnerId = _aliveUsers.first; _finishGame(winnerId: winnerId); return; } // 다음 문제 준비 시퀀스 시작 _startNextQuestionSequence(); } // 3. 카운트다운 (3->2->1) 후 문제 전송 void _startNextQuestionSequence() { int count = 3; // 1초 간격 타이머 Timer.periodic(const Duration(seconds: 1), (timer) { _broadcastState({'type': 'GAME_COUNTDOWN', 'count': count}); if (count == 0) { timer.cancel(); _sendNewQuestion(); // 문제 전송 } count--; }); } // 4. 실제 문제 데이터 전송 void _sendNewQuestion() { _currentQuestionIndex++; final questionData = _questions[_currentQuestionIndex]; final stateData = { 'type': 'GAME_STATE_UPDATE', 'status': 'QUESTION', 'data': questionData }; _resetLocalState(); _broadcastState(stateData); } void _finishGame({String? winnerId}) { final endData = { 'type': 'GAME_OVER', 'winnerId': winnerId ?? 'NONE', 'winnerName': _findUserName(winnerId) }; _broadcastState(endData); } void _broadcastState(Map data) { _lastState = data; _gameStateController.add(data); if (NetworkManager().role == NetworkRole.host) { NetworkManager().sendMessage(data); } } void _resetLocalState() { _answeredUsers.clear(); _mySelectedAnswer = null; _isLockedIn = false; _lockInTimer?.cancel(); } String _findUserName(String? id) { if (id == null) return '없음'; if (id == NetworkManager().me.id) return NetworkManager().me.nickname; return NetworkManager().guestList.firstWhere((u) => u.id == id, orElse: () => UserInfo(id: '', nickname: 'Unknown')).nickname; } // ------------------------------------------------------------------------ // [UI] Unified View (통일된 UI) // ------------------------------------------------------------------------ @override Widget buildHostView(BuildContext context) => _buildSharedScreen(context, isHost: true); @override Widget buildGuestView(BuildContext context) => _buildSharedScreen(context, isHost: false); Widget _buildSharedScreen(BuildContext context, {required bool isHost}) { return Scaffold( appBar: AppBar( title: const Text("OX 서바이벌", style: TextStyle(fontWeight: FontWeight.bold)), centerTitle: true, // 타이틀 중앙 정렬 통일 automaticallyImplyLeading: false, actions: [ if (isHost) IconButton(icon: const Icon(Icons.close), onPressed: () => _confirmExit(context)) ], ), body: StreamBuilder>( stream: gameStateStream, initialData: _lastState, builder: (context, snapshot) { if (!snapshot.hasData) return _buildWaitingScreen("로딩 중..."); final data = snapshot.data!; // 1. 종료 화면 if (data['type'] == 'GAME_OVER') return _buildResultScreen(context, data['winnerName']); if (data['type'] == 'GAME_EXIT') { WidgetsBinding.instance.addPostFrameCallback((_) { if(context.mounted) Navigator.pop(context); }); return const Center(child: Text("종료되었습니다.")); } // 2. 카운트다운 화면 (문제 직전) // count가 0일 때는 문제 화면으로 넘어가기 직전이므로 잠깐 보여도 됨 if (_isCountingDown) { int count = data['count'] ?? 3; // 0초는 'Start!' 등으로 표현하거나 생략 가능 String text = count > 0 ? "$count" : "GO!"; return Center( child: Text( text, style: TextStyle(fontSize: 120, fontWeight: FontWeight.bold, color: Theme.of(context).primaryColor) ) ); } // 3. 중간 결과 화면 (정답 공개) if (_isShowingResult || data['status'] == 'RESULT') { return _buildRoundResultScreen(data); } // 4. 문제 풀이 화면 if (data['status'] == 'QUESTION' || _currentQuestionIndex >= 0) { Map qData = data['data'] ?? _questions[_currentQuestionIndex]; // 인원 수 계산 int answered = data['answeredCount'] ?? _answeredUsers.length; int total = isHost ? _aliveUsers.length : (data['totalAlive'] ?? _aliveUsers.length); if (total == 0) total = 1; // div by zero 방지 return _buildPlayArea(context, qData, answered, total); } return _buildWaitingScreen("잠시만 기다려주세요..."); }, ), ); } // ------------------------------------------------------------------------ // UI Components // ------------------------------------------------------------------------ // [문제 풀이 화면] Widget _buildPlayArea(BuildContext context, Map qData, int answered, int total) { // 탈락자 뷰 if (_myStatus == PlayerStatus.dead) { return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Icon(Icons.sentiment_dissatisfied, size: 80, color: Colors.grey), const SizedBox(height: 20), const Text("탈락했습니다 👻", style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold)), const SizedBox(height: 20), Text("관전 중... ($answered / $total 제출)", style: const TextStyle(fontSize: 18, color: Colors.grey)), const SizedBox(height: 40), Padding( padding: const EdgeInsets.symmetric(horizontal: 30), child: Text("문제: ${qData['q']}", textAlign: TextAlign.center, style: const TextStyle(color: Colors.grey)), ), ], ), ); } // 생존자 뷰 return Column( children: [ // 상단 현황판 _PlayerStatusGrid(aliveUsers: _aliveUsers, answeredUsers: _answeredUsers), const Divider(height: 1), // 진행바 LinearProgressIndicator( value: total > 0 ? answered / total : 0, minHeight: 6, backgroundColor: Colors.grey[200], valueColor: const AlwaysStoppedAnimation(Colors.orange), ), // 문제 텍스트 Expanded( flex: 4, child: Center( child: Padding( padding: const EdgeInsets.all(24.0), child: Text( qData['q'], textAlign: TextAlign.center, style: const TextStyle(fontSize: 32, fontWeight: FontWeight.bold, height: 1.3), ), ), ), ), // 컨트롤 (버튼) Expanded( flex: 3, child: _isLockedIn ? _buildLockedUI() : Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ _AnswerBtn(text: "O", color: Colors.blue, isSelected: _mySelectedAnswer == "O", onTap: () => _selectAnswer("O")), _AnswerBtn(text: "X", color: Colors.red, isSelected: _mySelectedAnswer == "X", onTap: () => _selectAnswer("X")), ], ), ), // 하단 안내 SizedBox( height: 60, child: Center( child: _mySelectedAnswer != null && !_isLockedIn ? const Text("3초 후 확정됩니다!", style: TextStyle(color: Colors.red, fontWeight: FontWeight.bold)) : const SizedBox(), ), ), ], ); } // [결과 발표 화면] Widget _buildRoundResultScreen(Map data) { final String correctAnswer = data['correctAnswer'] ?? "?"; final List survivors = data['survivors'] ?? []; final bool amISurvived = survivors.contains(NetworkManager().me.id); return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Text("정답은?", style: TextStyle(fontSize: 24, color: Colors.grey)), const SizedBox(height: 20), // 정답 O/X 표시 Container( width: 160, height: 160, decoration: BoxDecoration( color: correctAnswer == "O" ? Colors.blue : Colors.red, shape: BoxShape.circle, boxShadow: [BoxShadow(color: Colors.black26, blurRadius: 10, offset: const Offset(0, 5))] ), child: Center( child: Text( correctAnswer, style: const TextStyle(fontSize: 100, color: Colors.white, fontWeight: FontWeight.bold) ) ), ), const SizedBox(height: 40), // 상태 메시지 if (_myStatus == PlayerStatus.dead) const Text("이미 탈락하셨습니다. 👻", style: TextStyle(fontSize: 20, color: Colors.grey)) else if (amISurvived) const Text("생존! 🎉", style: TextStyle(fontSize: 28, fontWeight: FontWeight.bold, color: Colors.green)) else const Text("탈락했습니다... 😭", style: TextStyle(fontSize: 28, fontWeight: FontWeight.bold, color: Colors.red)), const SizedBox(height: 20), // 방장에게만 보이는 비상 버튼 (혹시 멈출까봐) if (NetworkManager().role == NetworkRole.host) TextButton(onPressed: () => _checkWinnerAndNext(), child: const Text("강제 진행 (비상용)", style: TextStyle(color: Colors.grey))) ], ), ); } Widget _buildLockedUI() { return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Icon( _mySelectedAnswer == "O" ? Icons.circle_outlined : Icons.close, size: 80, color: _mySelectedAnswer == "O" ? Colors.blue : Colors.red, ), const SizedBox(height: 20), const Text("제출 완료!\n결과를 기다리는 중...", textAlign: TextAlign.center, style: TextStyle(fontSize: 18, color: Colors.grey)), ], ), ); } // ... (이하 _selectAnswer, _submitFinalAnswer, _buildResultScreen, _buildWaitingScreen, _confirmExit 동일) ... void _selectAnswer(String answer) { _lockInTimer?.cancel(); _mySelectedAnswer = answer; SoundManager().playSfx(SoundKey.click); _gameStateController.add({'type': 'UI_REFRESH'}); _lockInTimer = Timer(const Duration(seconds: 3), () { _submitFinalAnswer(); }); } void _submitFinalAnswer() { if (_mySelectedAnswer == null) return; _isLockedIn = true; _gameStateController.add({'type': 'UI_REFRESH'}); final payload = {'type': 'ANSWER_SUBMIT', 'answer': _mySelectedAnswer, 'userId': NetworkManager().me.id}; if (NetworkManager().role == NetworkRole.host) { onMessageReceived("", payload); } else { NetworkManager().sendMessage(payload); } } Widget _buildResultScreen(BuildContext context, String winnerName) { bool amIWinner = _myStatus == PlayerStatus.winner; return Center(child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [Icon(amIWinner ? Icons.emoji_events : Icons.thumb_down, size: 100, color: amIWinner ? Colors.amber : Colors.grey), const SizedBox(height: 20), Text(amIWinner ? "우승!" : "게임 종료", style: const TextStyle(fontSize: 30, fontWeight: FontWeight.bold)), const SizedBox(height: 10), Text("최종 우승자: $winnerName", style: const TextStyle(fontSize: 20)), const SizedBox(height: 50), ElevatedButton(onPressed: () { onDispose(); Navigator.pop(context); }, child: const Text("로비로 돌아가기"))])); } Widget _buildWaitingScreen(String msg) => Center(child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [const CircularProgressIndicator(), SizedBox(height: 20), Text(msg)])); void _confirmExit(BuildContext context) { showDialog(context: context, builder: (ctx) => AlertDialog(title: const Text("게임 종료"), content: const Text("방을 폭파하시겠습니까?"), actions: [TextButton(onPressed: ()=>Navigator.pop(ctx), child: const Text("취소")), TextButton(onPressed: () { Navigator.pop(ctx); NetworkManager().sendMessage({'type': 'GAME_EXIT'}); onDispose(); Navigator.pop(context); }, child: const Text("종료", style: TextStyle(color: Colors.red)))])); } } // [현황판] class _PlayerStatusGrid extends StatelessWidget { final Set aliveUsers; final Set answeredUsers; const _PlayerStatusGrid({required this.aliveUsers, required this.answeredUsers}); @override Widget build(BuildContext context) { final allUsers = [NetworkManager().me, ...NetworkManager().guestList]; return Container(height: 80, width: double.infinity, padding: const EdgeInsets.symmetric(horizontal: 10), color: Colors.grey[50], child: ListView.builder(scrollDirection: Axis.horizontal, itemCount: allUsers.length, itemBuilder: (context, index) { final user = allUsers[index]; final isAlive = aliveUsers.contains(user.id); final isSubmitted = answeredUsers.contains(user.id); return Padding(padding: const EdgeInsets.symmetric(horizontal: 6.0), child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [Stack(children: [Container(width: 40, height: 40, decoration: BoxDecoration(shape: BoxShape.circle, color: isAlive ? Color(user.colorValue) : Colors.grey, border: isSubmitted ? Border.all(color: Colors.green, width: 3) : null), child: AvatarWidget(user: user, size: 40)), if (!isAlive) Positioned.fill(child: Container(decoration: BoxDecoration(color: Colors.black54, shape: BoxShape.circle), child: const Icon(Icons.close, size: 20, color: Colors.white)))]), const SizedBox(height: 4), Text(user.nickname, style: TextStyle(fontSize: 10, color: isAlive ? Colors.black : Colors.grey))])); })); } } // [버튼] class _AnswerBtn extends StatelessWidget { final String text; final Color color; final bool isSelected; final VoidCallback onTap; const _AnswerBtn({required this.text, required this.color, required this.isSelected, required this.onTap}); @override Widget build(BuildContext context) { return GestureDetector(onTap: onTap, child: AnimatedContainer(duration: const Duration(milliseconds: 200), width: isSelected ? 140 : 120, height: isSelected ? 140 : 120, decoration: BoxDecoration(color: color.withOpacity(isSelected ? 1.0 : 0.6), shape: BoxShape.circle, border: isSelected ? Border.all(color: Colors.white, width: 5) : null, boxShadow: [BoxShadow(color: color.withOpacity(0.4), blurRadius: 10, offset: const Offset(0, 6))]), child: Center(child: Text(text, style: const TextStyle(fontSize: 60, color: Colors.white, fontWeight: FontWeight.bold))))); } }