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 => "방장도 플레이어! 3초 안에 선택하세요."; // ------------------------------------------------------------------------ // 상태 변수 // ------------------------------------------------------------------------ final _gameStateController = StreamController>.broadcast(); Stream> get gameStateStream => _gameStateController.stream; StreamSubscription? _networkSubscription; // UI 초기화 지연 방지용 데이터 Map? _lastState; // 게임 데이터 final Set _aliveUsers = {}; // 생존자 ID final Set _answeredUsers = {}; // 답변 제출자 ID // 나의 상태 PlayerStatus _myStatus = PlayerStatus.alive; String? _mySelectedAnswer; bool _isLockedIn = false; Timer? _lockInTimer; // 카운트다운 상태 bool _isCountingDown = false; int _countdownValue = 3; 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() { print("Quiz Game Started!"); _resetLocalState(); _lastState = null; _aliveUsers.clear(); // 참가자 명단 초기화 (나 + 게스트) _aliveUsers.add(NetworkManager().me.id); for (var guest in NetworkManager().guestList) { _aliveUsers.add(guest.id); } _networkSubscription = NetworkManager().messageStream.listen((payload) { onMessageReceived("", payload); }); // [Host] 잠시 후 카운트다운 시작 if (NetworkManager().role == NetworkRole.host) { Future.delayed(const Duration(milliseconds: 1000), () { _startCountdown(); }); } } @override void onDispose() { _lockInTimer?.cancel(); _networkSubscription?.cancel(); _gameStateController.close(); } // [Host] 카운트다운 void _startCountdown() { if (_currentQuestionIndex != -1) return; Timer.periodic(const Duration(seconds: 1), (timer) { int nextCount = 3 - timer.tick; if (nextCount > 0) { _broadcastState({'type': 'GAME_COUNTDOWN', 'count': nextCount}); } else { timer.cancel(); _nextQuestion(); // 첫 문제 출제 } }); _broadcastState({'type': 'GAME_COUNTDOWN', 'count': 3}); } // ------------------------------------------------------------------------ // 메시지 처리 (Logic) // ------------------------------------------------------------------------ @override void onMessageReceived(String senderId, Map payload) { if (payload['type'] != 'ANSWER_SUBMIT') { _lastState = payload; } // 1. [Common] 카운트다운 if (payload['type'] == 'GAME_COUNTDOWN') { _isCountingDown = true; _countdownValue = payload['count']; 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': true // 아직은 살아있는 척 (결과 화면에서 공개) }); // [핵심 변경] 전원 제출 완료 시 -> 결과 발표 화면으로 이동 // (방금 죽은 사람 포함해서 이번 라운드 시작 인원만큼 답변이 왔는지 체크) int currentRoundPlayers = _aliveUsers.length + (isCorrect ? 0 : 1); // 방금 뺀 사람 포함 // 더 정확히는: answeredUsers가 이번 라운드 참가자 수에 도달하면 진행 // (여기선 간단히 answeredUsers가 더이상 늘어날 수 없을 때로 판단) // 타임아웃 로직이 없으므로, 현재 살아있는 사람들이 다 냈으면 진행 // (로직이 복잡해질 수 있으므로, 간단히 '살아있는 사람 수 == 답변 수'가 아니라 // '이번 라운드 시작 시점의 생존자 수'를 별도 변수로 관리하는 게 정석이지만, // 여기서는 생존자 수 + 이번에 틀린 사람 수로 계산) // 간단 로직: 1초 뒤 체크해서 더 낼 사람이 없으면 진행 (혹은 모두 냈으면 바로) Future.delayed(const Duration(milliseconds: 500), () { // 대충 모두 냈다고 판단되면 (추가 보정 필요할 수 있음) if (_answeredUsers.length >= (_aliveUsers.length + (isCorrect?0:1))) { _showRoundResult(); } }); } // 3. [Common] 중간 결과 발표 (NEW) if (payload['type'] == 'ROUND_RESULT') { _isCountingDown = false; final bool isSurvived = payload['survivors'].contains(NetworkManager().me.id); // 내 생존 여부 업데이트 if (!isSurvived && _myStatus == PlayerStatus.alive) { _handleElimination(); } _gameStateController.add(payload); } // 4. [Common] 플레이어 상태 업데이트 if (payload['type'] == 'PLAYER_STATUS_UPDATE') { final userId = payload['userId']; _answeredUsers.add(userId); _gameStateController.add(payload); } // 5. [Common] 새 문제 시작 if (payload['type'] == 'GAME_STATE_UPDATE' && payload['status'] == 'QUESTION') { _isCountingDown = false; _resetLocalState(); _gameStateController.add(payload); } // 6. [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] // ------------------------------------------------------------------------ // [NEW] 결과 발표 단계 void _showRoundResult() { 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), () { _nextQuestion(); }); } void _nextQuestion() { // 승패 판정 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; } _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 // ------------------------------------------------------------------------ @override Widget buildHostView(BuildContext context) => _buildGameScreen(context, isHost: true); @override Widget buildGuestView(BuildContext context) => _buildGameScreen(context, isHost: false); Widget _buildGameScreen(BuildContext context, {required bool isHost}) { return Scaffold( appBar: AppBar( title: const Text("OX 서바이벌"), automaticallyImplyLeading: false, actions: [ if (isHost) IconButton(icon: const Icon(Icons.power_settings_new), onPressed: () => _confirmExit(context)) ], ), body: StreamBuilder>( stream: gameStateStream, initialData: _lastState, builder: (context, snapshot) { if (!snapshot.hasData) return _buildWaitingScreen("로딩 중..."); final data = snapshot.data!; if (data['type'] == 'GAME_COUNTDOWN') { int count = data['count'] ?? 3; return Center(child: Text("$count", style: const TextStyle(fontSize: 120, fontWeight: FontWeight.bold, color: Colors.blueAccent))); } if (data['type'] == 'GAME_EXIT') { WidgetsBinding.instance.addPostFrameCallback((_) { if(context.mounted) Navigator.pop(context); }); return const Center(child: Text("종료 중...")); } if (data['type'] == 'GAME_OVER') { return _buildResultScreen(context, data['winnerName']); } // [NEW] 중간 결과 화면 if (data['status'] == 'RESULT') { return _buildRoundResultScreen(data); } // 문제 풀이 화면 if (data['status'] == 'QUESTION' || _currentQuestionIndex >= 0) { Map qData = data['data'] ?? _questions[_currentQuestionIndex]; int answered = data['answeredCount'] ?? _answeredUsers.length; // 총인원 계산 (생존자 기준이 아님, 이번 라운드 참여자 기준이어야 함. 여기선 간단히 전체 인원 사용) int total = NetworkManager().guestList.length + 1; return _buildPlayArea(context, qData, answered, total); } return _buildWaitingScreen("대기 중..."); }, ), ); } // [NEW] 중간 결과 화면 위젯 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), // 정답 표시 Container( width: 150, height: 150, decoration: BoxDecoration( color: correctAnswer == "O" ? Colors.blue : Colors.red, shape: BoxShape.circle, ), child: Center(child: Text(correctAnswer, style: const TextStyle(fontSize: 80, 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: 22, fontWeight: FontWeight.bold, color: Colors.green)) else const Text("탈락했습니다... 😭", style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold, color: Colors.red)), const SizedBox(height: 20), Text("잠시 후 다음 문제가 시작됩니다.", style: TextStyle(color: Colors.grey[600])), ], ), ); } 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명 제출)", style: const TextStyle(fontSize: 18, color: Colors.grey)), const SizedBox(height: 40), Text("문제: ${qData['q']}", style: const TextStyle(color: Colors.grey)), ], ), ); } return Column( children: [ // 상단 현황판 _PlayerStatusGrid(aliveUsers: _aliveUsers, answeredUsers: _answeredUsers), const Divider(), 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(), ), ), ], ); } // ... (이하 _buildLockedUI, _selectAnswer 등 기존 함수들 유지) ... 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("제출 완료! 결과를 기다리는 중...", style: TextStyle(fontSize: 20, color: Colors.grey)), ], ), ); } void _selectAnswer(String answer) { _lockInTimer?.cancel(); _mySelectedAnswer = answer; SoundManager().playSfx(SoundKey.click); _updateLocalState({'type': 'UI_REFRESH'}); _lockInTimer = Timer(const Duration(seconds: 3), () { _submitFinalAnswer(); }); } void _submitFinalAnswer() { if (_mySelectedAnswer == null) return; _isLockedIn = true; _updateLocalState({'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); } } void _updateLocalState(Map data) { _gameStateController.add(data); } 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))), ] )); } } // ------------------------------------------------------------------------ // [Widget] 현황판 (기존 코드와 동일하지만 함께 제공) // ------------------------------------------------------------------------ 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: 90, width: double.infinity, padding: const EdgeInsets.all(10), color: Colors.grey[100], 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); final isMe = user.id == NetworkManager().me.id; return Padding( padding: const EdgeInsets.symmetric(horizontal: 8.0), child: Column( children: [ Stack( children: [ Container( width: 50, height: 50, decoration: BoxDecoration( shape: BoxShape.circle, color: isAlive ? Color(user.colorValue) : Colors.grey, border: isSubmitted ? Border.all(color: Colors.green, width: 3) : null, ), child: Center( child: isAlive ? Text(user.nickname[0], style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold)) : const Icon(Icons.close, color: Colors.white), ), ), if (isMe) Positioned(top:0, right:0, child: Container(width: 10, height: 10, decoration: const BoxDecoration(color: Colors.red, shape: BoxShape.circle))), ], ), 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))), ), ); } }