From 283f08786eaf39b365b0e096ce99f74df4e52eb6 Mon Sep 17 00:00:00 2001 From: lunaticbum Date: Tue, 25 Nov 2025 17:25:16 +0900 Subject: [PATCH] ... --- apps/app/lib/lobby_screen.dart | 400 +++++---- packages/games/quiz/lib/model/quiz_model.dart | 108 +++ packages/games/quiz/lib/quiz_game.dart | 845 ++++++++++-------- 3 files changed, 817 insertions(+), 536 deletions(-) create mode 100644 packages/games/quiz/lib/model/quiz_model.dart diff --git a/apps/app/lib/lobby_screen.dart b/apps/app/lib/lobby_screen.dart index ef8cb69..09bdce7 100644 --- a/apps/app/lib/lobby_screen.dart +++ b/apps/app/lib/lobby_screen.dart @@ -2,7 +2,7 @@ import 'dart:convert'; import 'package:bonsoir/bonsoir.dart'; import 'package:flutter/material.dart'; import 'package:mobile_scanner/mobile_scanner.dart'; -import 'package:playwith_core/playwith_core.dart'; // GameChatOverlay 포함됨 +import 'package:playwith_core/playwith_core.dart'; // Core (AvatarWidget, NetworkManager 등) import 'package:playwith_game_quiz/quiz_game.dart'; import 'package:qr_flutter/qr_flutter.dart'; @@ -22,6 +22,7 @@ class _LobbyScreenState extends State { void initState() { super.initState(); + // 로그 리스너 _net.logStream.listen((log) { if (!mounted) return; setState(() { @@ -39,6 +40,7 @@ class _LobbyScreenState extends State { }); }); + // 게임 시작 신호 감지 _net.messageStream.listen((data) { if (data['type'] == 'GAME_START') { final String gameId = data['gameId']; @@ -49,10 +51,8 @@ class _LobbyScreenState extends State { }); } - // [핵심] 채팅 오버레이 적용 void _startGameAndNavigate(BaseGame game) { if (!mounted) return; - game.onStart(); Navigator.push( @@ -65,13 +65,11 @@ class _LobbyScreenState extends State { gameView = game.buildGuestView(context); } - // 플랫폼 구조: 게임 화면 위에 채팅창 오버레이 + // 게임 위 채팅 오버레이 return Stack( children: [ - gameView, // 1. 게임 화면 - const SafeArea( - child: GameChatOverlay(), // 2. 채팅창 (Core 제공) - ), + gameView, + const SafeArea(child: GameChatOverlay()), ], ); }), @@ -85,25 +83,26 @@ class _LobbyScreenState extends State { builder: (context, child) { return Scaffold( appBar: AppBar( - title: Text('대기실: ${_net.me.nickname}'), + title: const Text('대기실'), + centerTitle: true, actions: [ - if (_net.role == NetworkRole.host) - IconButton( - icon: const Icon(Icons.qr_code, size: 30), - onPressed: () => _showHostQRDialog(), - ), - + // 연결된 상태라면 나가기 버튼 표시 if (_net.role != NetworkRole.none) IconButton( - icon: const Icon(Icons.exit_to_app), + icon: const Icon(Icons.exit_to_app, color: Colors.red), + tooltip: "나가기", onPressed: () => _net.stopNetwork(), ) ], ), body: Column( children: [ - Expanded(flex: 3, child: _buildBody()), - const Divider(thickness: 2, color: Colors.grey), + // 메인 바디 (연결 상태에 따라 분기) + Expanded(flex: 3, child: _buildMainBody()), + + const Divider(thickness: 1, height: 1), + + // 하단 디버그 로그 (개발용) _buildDebugConsole(), ], ), @@ -112,93 +111,85 @@ class _LobbyScreenState extends State { ); } - Widget _buildDebugConsole() { - return Column( - children: [ - Container( - width: double.infinity, - padding: const EdgeInsets.all(8.0), - color: Colors.black87, - child: const Text("DEBUG LOGS", style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold)), - ), - SizedBox( - height: 150, - child: Container( - color: Colors.black, - child: ListView.builder( - controller: _scrollController, - itemCount: _logs.length, - itemBuilder: (context, index) { - return Padding( - padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 2.0), - child: Text( - _logs[index], - style: const TextStyle(color: Colors.greenAccent, fontSize: 12, fontFamily: 'Courier'), - ), - ); - }, + // [Main Body] 상태에 따라 초기화면 vs 대기실 화면 분기 + Widget _buildMainBody() { + // 1. 아직 연결 안 됨 (초기 화면) + if (_net.role == NetworkRole.none) { + return _buildInitView(); + } + + // 2. 연결됨 (대기실 - 방장/참가자 통합 UI) + return _buildLobbyView(); + } + + // ------------------------------------------------------------------------ + // 1. 초기 화면 (방 만들기 / 찾기) + // ------------------------------------------------------------------------ + Widget _buildInitView() { + return Center( + child: SingleChildScrollView( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + const Text("게임을 시작해볼까요?", style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold)), + const SizedBox(height: 40), + + Row( + mainAxisAlignment: MainAxisAlignment.spaceEvenly, + children: [ + _BigButton( + title: "방 만들기\n(Host)", + color: Colors.blue[100]!, + icon: Icons.add_home_work, + onTap: () { + _net.startHosting("${_net.me.nickname}의 방"); + // 방장은 방 만들자마자 QR 팝업 + Future.delayed(const Duration(milliseconds: 500), () { + if (mounted && _net.role == NetworkRole.host) _showHostQRDialog(); + }); + }, + ), + _BigButton( + title: "방 찾기\n(Guest)", + color: Colors.green[100]!, + icon: Icons.search, + onTap: () => _showRoomListDialog(), + ), + ], ), - ), + const SizedBox(height: 30), + + ElevatedButton.icon( + icon: const Icon(Icons.qr_code_scanner), + label: const Text("QR 코드로 접속하기"), + style: ElevatedButton.styleFrom(padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 15)), + onPressed: () => _openQRScanner(), + ), + const SizedBox(height: 10), + TextButton( + onPressed: () => _showManualJoinDialog(), + child: const Text("IP 주소 직접 입력 (비상용)", style: TextStyle(color: Colors.grey)), + ), + ], ), - ], + ), ); } - Widget _buildBody() { - if (_net.role == NetworkRole.none) { - return Center( - child: SingleChildScrollView( - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Row( - mainAxisAlignment: MainAxisAlignment.spaceEvenly, - children: [ - _BigButton( - title: "방 만들기\n(Host)", - color: Colors.blue[100]!, - icon: Icons.add_home_work, - onTap: () { - _net.startHosting("${_net.me.nickname}의 방"); - Future.delayed(const Duration(milliseconds: 500), () { - if (mounted && _net.role == NetworkRole.host) _showHostQRDialog(); - }); - }, - ), - _BigButton( - title: "방 찾기\n(Guest)", - color: Colors.green[100]!, - icon: Icons.search, - onTap: () => _showRoomListDialog(), - ), - ], - ), - const SizedBox(height: 30), - ElevatedButton.icon( - icon: const Icon(Icons.qr_code_scanner, size: 28), - label: const Text("QR 코드로 접속하기"), - style: ElevatedButton.styleFrom( - padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 15), - ), - onPressed: () => _openQRScanner(), - ), - const SizedBox(height: 10), - TextButton( - onPressed: () => _showManualJoinDialog(), - child: const Text("IP 주소 직접 입력 (비상용)", style: TextStyle(color: Colors.grey)), - ), - ], - ), - ), - ); - } - + // ------------------------------------------------------------------------ + // 2. 대기실 화면 (통합 UI) + // ------------------------------------------------------------------------ + Widget _buildLobbyView() { return Column( children: [ + // A. 상단 정보 카드 Container( - padding: const EdgeInsets.all(20.0), - color: Colors.grey[100], width: double.infinity, + padding: const EdgeInsets.all(20), + decoration: BoxDecoration( + color: Colors.grey[100], + border: const Border(bottom: BorderSide(color: Colors.black12)), + ), child: Column( children: [ Row( @@ -213,69 +204,90 @@ class _LobbyScreenState extends State { ], ), + // 방장일 경우 접속 정보 표시 if (_net.role == NetworkRole.host) ...[ - const SizedBox(height: 10), - Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - SelectableText( - "IP: ${_net.hostIp} / Port: ${_net.hostPort}", - style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16), - ), - const SizedBox(width: 10), - InkWell( - onTap: () => _showHostQRDialog(), - child: const Icon(Icons.qr_code, color: Colors.black87), - ) - ], + const SizedBox(height: 15), + Container( + padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), + decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(20), border: Border.all(color: Colors.blue.shade100)), + child: Row( + mainAxisSize: MainAxisSize.min, + children: [ + Text("IP: ${_net.hostIp} : ${_net.hostPort}", style: const TextStyle(fontWeight: FontWeight.bold)), + const SizedBox(width: 10), + InkWell( + onTap: () => _showHostQRDialog(), + child: const Icon(Icons.qr_code, color: Colors.black87), + ) + ], + ), ), const SizedBox(height: 5), - const Text("QR 버튼을 눌러 친구들을 초대하세요!", style: TextStyle(fontSize: 12, color: Colors.grey)), - ], + const Text("QR 코드를 눌러 친구를 초대하세요!", style: TextStyle(fontSize: 12, color: Colors.grey)), + ] else ...[ + // 참가자일 경우 방장 정보 표시 + const SizedBox(height: 10), + Text("방장 IP: ${_net.hostIp ?? '...'}", style: const TextStyle(color: Colors.grey)), + ] ], ), ), - const Divider(height: 1), - + // B. 참가자 리스트 (나 + 게스트) Expanded( child: ListView( padding: const EdgeInsets.all(16), children: [ - const Text("참가자 목록", style: TextStyle(color: Colors.grey)), + const Text("대기 중인 참가자", style: TextStyle(color: Colors.grey, fontWeight: FontWeight.bold)), const SizedBox(height: 10), - _buildUserTile(_net.me), - ..._net.guestList.map((guest) => _buildUserTile(guest)), + // 1. 나 (항상 맨 위) + _buildUserTile(_net.me, isMe: true), + + // 2. 다른 참가자들 + ..._net.guestList.map((guest) => _buildUserTile(guest, isMe: false)), + + // 대기 문구 if (_net.guestList.isEmpty && _net.role == NetworkRole.host) - const Padding( - padding: EdgeInsets.all(40.0), - child: Center(child: Text("참가자를 기다리는 중...\nQR 코드를 보여주세요.", textAlign: TextAlign.center, style: TextStyle(color: Colors.grey))), + Padding( + padding: const EdgeInsets.only(top: 40.0), + child: Center( + child: Column( + children: const [ + CircularProgressIndicator(), + SizedBox(height: 20), + Text("친구를 기다리는 중...", style: TextStyle(color: Colors.grey)), + ], + ), + ), ), ], ), ), + // C. 하단 레디 버튼 _buildReadyButton(), ], ); } - Widget _buildUserTile(UserInfo user) { - bool isMe = user.id == _net.me.id; + Widget _buildUserTile(UserInfo user, {required bool isMe}) { return Card( - elevation: 2, + elevation: user.isReady ? 4 : 1, color: user.isReady ? Colors.green[50] : Colors.white, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(12), + side: user.isReady ? const BorderSide(color: Colors.green, width: 2) : BorderSide.none, + ), margin: const EdgeInsets.symmetric(vertical: 6), child: ListTile( - leading: CircleAvatar( - backgroundColor: Color(user.colorValue), - child: Text(user.nickname[0], style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold)), - ), + contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8), + leading: AvatarWidget(user: user, size: 50), // Core의 AvatarWidget 사용 title: Text( user.nickname + (isMe ? " (나)" : ""), - style: const TextStyle(fontWeight: FontWeight.bold), + style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16), ), + subtitle: Text(isMe ? "준비 버튼을 눌러주세요" : (user.isReady ? "준비 완료!" : "준비 중..."), style: TextStyle(color: Colors.grey[600], fontSize: 12)), trailing: user.isReady ? const Icon(Icons.check_circle, color: Colors.green, size: 32) : const Icon(Icons.hourglass_empty, color: Colors.grey, size: 32), @@ -285,9 +297,9 @@ class _LobbyScreenState extends State { Widget _buildReadyButton() { bool isReady = _net.me.isReady; - bool canReady = _net.role == NetworkRole.host - ? _net.guestList.isNotEmpty - : true; + // 조건: 방장이라도 게스트가 없으면 레디 불가 (혼자 게임 불가) + // 게스트는 들어오자마자 레디 가능 + bool canReady = _net.role == NetworkRole.host ? _net.guestList.isNotEmpty : true; return Container( width: double.infinity, @@ -300,27 +312,79 @@ class _LobbyScreenState extends State { onPressed: canReady ? () => _net.toggleReady() : () { - ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("참가자가 들어와야 게임을 시작할 수 있습니다."))); + ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("친구를 초대해야 시작할 수 있습니다!"))); }, style: ElevatedButton.styleFrom( backgroundColor: !canReady - ? Colors.grey + ? Colors.grey[300] : (isReady ? Colors.redAccent : Colors.blueAccent), padding: const EdgeInsets.symmetric(vertical: 18), - shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)), + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15)), elevation: canReady ? 5 : 0, ), child: Text( isReady ? "준비 취소 (WAIT)" : "준비 완료 (READY)", - style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold, color: Colors.white), + style: TextStyle( + fontSize: 22, + fontWeight: FontWeight.bold, + color: !canReady ? Colors.grey : Colors.white + ), ), ), ); } + // ------------------------------------------------------------------------ + // [Components] 디버그 콘솔 + // ------------------------------------------------------------------------ + Widget _buildDebugConsole() { + return Column( + children: [ + GestureDetector( + onTap: () => _scrollController.jumpTo(_scrollController.position.maxScrollExtent), + child: Container( + width: double.infinity, + padding: const EdgeInsets.symmetric(vertical: 5, horizontal: 10), + color: Colors.black87, + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: const [ + Text("DEBUG LOGS", style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 10)), + Icon(Icons.keyboard_arrow_down, color: Colors.white, size: 14) + ], + ), + ), + ), + SizedBox( + height: 100, + child: Container( + color: Colors.black, + child: ListView.builder( + controller: _scrollController, + itemCount: _logs.length, + itemBuilder: (context, index) { + return Padding( + padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 1.0), + child: Text( + _logs[index], + style: const TextStyle(color: Colors.greenAccent, fontSize: 10, fontFamily: 'Courier'), + ), + ); + }, + ), + ), + ), + ], + ); + } + + // ------------------------------------------------------------------------ + // [Dialogs] QR, 검색, 수동입력 + // ------------------------------------------------------------------------ void _showHostQRDialog() { if (_net.hostIp == null || _net.hostPort == null) return; final qrData = jsonEncode({'ip': _net.hostIp, 'port': _net.hostPort}); + showDialog( context: context, builder: (context) => Dialog( @@ -330,7 +394,7 @@ class _LobbyScreenState extends State { child: Column( mainAxisSize: MainAxisSize.min, children: [ - const Text("초대 QR 코드", style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold)), + const Text("친구 초대", style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold)), const SizedBox(height: 20), Container( padding: const EdgeInsets.all(10), @@ -338,7 +402,7 @@ class _LobbyScreenState extends State { child: QrImageView(data: qrData, version: QrVersions.auto, size: 220.0), ), const SizedBox(height: 20), - Text("IP: ${_net.hostIp} / Port: ${_net.hostPort}", style: const TextStyle(color: Colors.grey)), + SelectableText("IP: ${_net.hostIp}\nPort: ${_net.hostPort}", textAlign: TextAlign.center, style: const TextStyle(color: Colors.grey)), const SizedBox(height: 20), ElevatedButton(onPressed: () => Navigator.pop(context), child: const Text("닫기")) ], @@ -352,20 +416,18 @@ class _LobbyScreenState extends State { bool isScanCompleted = false; Navigator.of(context).push(MaterialPageRoute( builder: (context) => Scaffold( - appBar: AppBar(title: const Text("QR 코드 스캔")), + appBar: AppBar(title: const Text("QR 스캔")), body: MobileScanner( onDetect: (capture) { if (isScanCompleted) return; - final List barcodes = capture.barcodes; - for (final barcode in barcodes) { - final String? code = barcode.rawValue; - if (code != null) { + for (final barcode in capture.barcodes) { + if (barcode.rawValue != null) { try { - final data = jsonDecode(code); + final data = jsonDecode(barcode.rawValue!); if (data['ip'] != null && data['port'] != null) { isScanCompleted = true; Navigator.pop(context); - ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("QR 인식 성공! 접속 중..."))); + ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("접속 중..."))); _net.joinRoom(data['ip'], data['port']); return; } @@ -382,7 +444,7 @@ class _LobbyScreenState extends State { showDialog( context: context, builder: (context) => AlertDialog( - title: const Text("방 찾는 중..."), + title: const Text("방 찾기"), content: SizedBox( width: double.maxFinite, height: 300, @@ -390,19 +452,17 @@ class _LobbyScreenState extends State { stream: _net.discoverRooms(), builder: (context, snapshot) { if (!snapshot.hasData || snapshot.data!.isEmpty) { - return const Center(child: Text("검색 중... (로그를 확인하세요)")); + return const Center(child: Text("검색 중...\n같은 와이파이인지 확인하세요.")); } - final services = snapshot.data!; return ListView.builder( - itemCount: services.length, + itemCount: snapshot.data!.length, itemBuilder: (context, index) { - final service = services[index]; - final displayName = service.name.split('#').first; + final service = snapshot.data![index]; final ip = service.attributes?['ip'] ?? '알 수 없음'; return ListTile( leading: const Icon(Icons.meeting_room), - title: Text(displayName), - subtitle: Text("IP: $ip"), + title: Text(service.name.split('#').first), + subtitle: Text(ip), onTap: () { Navigator.pop(context); if (service.attributes != null && service.attributes!['ip'] != null) { @@ -421,26 +481,25 @@ class _LobbyScreenState extends State { } void _showManualJoinDialog() { - final ipController = TextEditingController(text: "192.168."); - final portController = TextEditingController(); + final ipCtrl = TextEditingController(text: "192.168."); + final portCtrl = TextEditingController(); showDialog( context: context, builder: (context) => AlertDialog( - title: const Text("수동 접속"), + title: const Text("직접 입력"), content: Column( mainAxisSize: MainAxisSize.min, children: [ - const Text("방장 화면의 IP/Port를 입력하세요."), - TextField(controller: ipController, decoration: const InputDecoration(labelText: "IP")), - TextField(controller: portController, decoration: const InputDecoration(labelText: "Port")), + TextField(controller: ipCtrl, decoration: const InputDecoration(labelText: "IP Address")), + TextField(controller: portCtrl, decoration: const InputDecoration(labelText: "Port")), ], ), actions: [ TextButton(onPressed: () => Navigator.pop(context), child: const Text("취소")), ElevatedButton( onPressed: () { - final ip = ipController.text.trim(); - final port = int.tryParse(portController.text.trim()); + final ip = ipCtrl.text.trim(); + final port = int.tryParse(portCtrl.text.trim()); if (ip.isNotEmpty && port != null) { Navigator.pop(context); _net.joinRoom(ip, port); @@ -455,29 +514,16 @@ class _LobbyScreenState extends State { } class _BigButton extends StatelessWidget { - final String title; - final Color color; - final IconData icon; - final VoidCallback onTap; + final String title; final Color color; final IconData icon; final VoidCallback onTap; const _BigButton({required this.title, required this.color, required this.icon, required this.onTap}); @override Widget build(BuildContext context) { return GestureDetector( onTap: onTap, child: Container( - width: 140, height: 140, - decoration: BoxDecoration( - color: color, borderRadius: BorderRadius.circular(20), - boxShadow: [BoxShadow(color: Colors.black12, blurRadius: 10, offset: const Offset(0, 5))], - ), - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Icon(icon, size: 40, color: Colors.black54), - const SizedBox(height: 10), - Text(title, textAlign: TextAlign.center, style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold)), - ], - ), + width: 130, height: 130, + decoration: BoxDecoration(color: color, borderRadius: BorderRadius.circular(20), boxShadow: [BoxShadow(color: Colors.black12, blurRadius: 10, offset: const Offset(0, 5))]), + child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [Icon(icon, size: 40, color: Colors.black54), const SizedBox(height: 10), Text(title, textAlign: TextAlign.center, style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold))]), ), ); } diff --git a/packages/games/quiz/lib/model/quiz_model.dart b/packages/games/quiz/lib/model/quiz_model.dart new file mode 100644 index 0000000..90c9c50 --- /dev/null +++ b/packages/games/quiz/lib/model/quiz_model.dart @@ -0,0 +1,108 @@ +enum QuizType { text, image } // 문제 유형 + +class QuizItem { + final QuizType type; + final String question; // 질문 텍스트 + final String answer; // 정답 + final List options; // 보기 (객관식용, 없으면 주관식/OX) + + QuizItem({ + required this.type, + required this.question, + required this.answer, + required this.options, + }); + + Map toJson() => { + 'type': type.name, + 'question': question, + 'answer': answer, + 'options': options, + }; + + factory QuizItem.fromJson(Map json) { + return QuizItem( + type: json['type'] == 'image' ? QuizType.image : QuizType.text, + question: json['question'], + answer: json['answer'], + options: List.from(json['options'] ?? []), + ); + } +} + +class QuizSet { + static List getDummy10() { + return [ + // 1. [OX] 기본 + QuizItem( + type: QuizType.text, + question: "사과는 영어로 Apple이다.", + answer: "O", + options: ["O", "X"], + ), + // 2. [OX] 상식 + QuizItem( + type: QuizType.text, + question: "북극곰의 피부색은 흰색이다.", + answer: "X", // 검은색임 + options: ["O", "X"], + ), + // 3. [OX] 생물 + QuizItem( + type: QuizType.text, + question: "돌고래는 '어류(물고기)'다.", + answer: "X", // 포유류 + options: ["O", "X"], + ), + // 4. [4지선다] 역사 + QuizItem( + type: QuizType.text, + question: "임진왜란이 일어난 해는?", + answer: "1592년", + options: ["1392년", "1492년", "1592년", "1950년"], + ), + // 5. [4지선다] 넌센스 + QuizItem( + type: QuizType.text, + question: "왕이 넘어지면?", + answer: "킹콩", + options: ["왕콩", "킹콩", "전하", "꽈당"], + ), + // 6. [주관식/음성] 속담 (보기를 줘서 터치도 가능하게 함) + QuizItem( + type: QuizType.text, + question: "가는 말이 고와야 [ ? ]가 곱다.", + answer: "오는 말", + options: ["오는 말", "가는 발", "너의 말", "우리 말"], + ), + // 7. [수학] 연산 + QuizItem( + type: QuizType.text, + question: "5 + 5 × 5 = ?", + answer: "30", + options: ["25", "30", "50", "10"], + ), + // 8. [상식] 수도 맞추기 + QuizItem( + type: QuizType.text, + question: "미국의 수도는 어디일까요?", + answer: "워싱턴 D.C.", + options: ["뉴욕", "LA", "워싱턴 D.C.", "시카고"], + ), + // 9. [넌센스] + QuizItem( + type: QuizType.text, + question: "세상에서 가장 뜨거운 바다는?", + answer: "열바다", + options: ["불바다", "열바다", "사랑해", "동해"], + ), + // 10. [OX] 마지막 + QuizItem( + type: QuizType.text, + question: "개발자님은 이 앱을 완성할 수 있다!", + answer: "O", + options: ["O", "X"], + ), + ]; + } +} \ No newline at end of file diff --git a/packages/games/quiz/lib/quiz_game.dart b/packages/games/quiz/lib/quiz_game.dart index 4b3b07e..0ec7e4a 100644 --- a/packages/games/quiz/lib/quiz_game.dart +++ b/packages/games/quiz/lib/quiz_game.dart @@ -1,53 +1,58 @@ import 'dart:async'; +import 'dart:math'; import 'package:flutter/material.dart'; import 'package:playwith_core/playwith_core.dart'; +import 'model/quiz_model.dart'; // QuizSet, QuizItem 모델 필요 +// --- Enums --- enum PlayerStatus { alive, dead, winner, loser } +enum GamePhase { voteRule, voteInput, playing, result } +enum InputMode { touch, voice } +enum GameRule { survival, suddenDeath, scoreAttack, relay } class QuizGame extends BaseGame { @override - String get id => "quiz_ox"; - + String get id => "quiz_mix"; // main.dart 등록 ID와 일치해야 함 @override - String get name => "OX 퀴즈 서바이벌"; - + String get name => "멀티 모드 퀴즈"; @override - String get description => "끝까지 살아남으세요!"; + String get description => "투표로 룰을 정하고 승리하세요!"; // ------------------------------------------------------------------------ // 상태 변수 // ------------------------------------------------------------------------ final _gameStateController = StreamController>.broadcast(); Stream> get gameStateStream => _gameStateController.stream; - - // UI 초기화 지연 방지용 데이터 Map? _lastState; + // 진행 상태 + GamePhase _phase = GamePhase.voteRule; + GameRule _selectedRule = GameRule.survival; + InputMode _selectedInputMode = InputMode.touch; + + // 데이터 final Set _aliveUsers = {}; final Set _answeredUsers = {}; + final Map _scores = {}; + final Map _votes = {}; + + // 릴레이 모드 전용 + List _turnOrder = []; + int _currentTurnIndex = 0; + // 내 상태 PlayerStatus _myStatus = PlayerStatus.alive; String? _mySelectedAnswer; bool _isLockedIn = false; Timer? _lockInTimer; - // [상태] 카운트다운 중인가? + // UI 상태 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"}, - ]; - + // 문제 데이터 + List _questions = []; int _currentQuestionIndex = -1; // ------------------------------------------------------------------------ @@ -55,27 +60,47 @@ class QuizGame extends BaseGame { // ------------------------------------------------------------------------ @override void onStart() { - super.onStart(); + super.onStart(); print("Quiz Game Started!"); + _resetGame(); - _resetLocalState(); - _lastState = null; - _aliveUsers.clear(); - - _aliveUsers.add(NetworkManager().me.id); - for (var guest in NetworkManager().guestList) { - _aliveUsers.add(guest.id); + // 문제 로드 (QuizModel이 없다면 하드코딩된 리스트 사용 가능) + try { + _questions = QuizSet.getDummy10(); + } catch (e) { + // Fallback Dummy + _questions = [ + QuizItem(type: QuizType.text, question: "사과는 영어로 Apple?", answer: "O", options: ["O", "X"]), + QuizItem(type: QuizType.text, question: "바나나는 길어지면 기차?", answer: "X", options: ["O", "X"]), + ]; } - // [Host] 게임 시작 시퀀스 진입 + // [Host] 1단계: 룰 투표 시작 if (NetworkManager().role == NetworkRole.host) { - // 잠시 대기 후 첫 번째 문제 카운트다운 시작 - Future.delayed(const Duration(seconds: 1), () { - _startNextQuestionSequence(); + Future.delayed(const Duration(milliseconds: 1000), () { + _broadcastState({'type': 'PHASE_CHANGE', 'phase': 'VOTE_RULE'}); }); } } + void _resetGame() { + _phase = GamePhase.voteRule; + _lastState = null; + _aliveUsers.clear(); + _scores.clear(); + _votes.clear(); + _turnOrder.clear(); + _currentQuestionIndex = -1; + _resetLocalState(); + + final allUsers = [NetworkManager().me, ...NetworkManager().guestList]; + for (var u in allUsers) { + _aliveUsers.add(u.id); + _scores[u.id] = 0; + } + _myStatus = PlayerStatus.alive; + } + @override void onDispose() { _lockInTimer?.cancel(); @@ -84,193 +109,290 @@ class QuizGame extends BaseGame { } // ------------------------------------------------------------------------ - // 메시지 처리 (Logic) + // 메시지 처리 (Logic Hub) // ------------------------------------------------------------------------ @override void onMessageReceived(String senderId, Map payload) { - - if (payload['type'] != 'ANSWER_SUBMIT') { - _lastState = payload; + if (!['ANSWER_SUBMIT', 'VOTE_SUBMIT'].contains(payload['type'])) { + _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); + switch (payload['type']) { + case 'PHASE_CHANGE': _handlePhaseChange(payload); break; + case 'VOTE_SUBMIT': _handleVoteSubmit(payload); break; + case 'GAME_COUNTDOWN': _handleCountdown(payload); break; + case 'ANSWER_SUBMIT': _handleAnswerSubmit(payload); break; + case 'PLAYER_STATUS_UPDATE': _handleStatusUpdate(payload); break; + case 'PLAYER_ELIMINATED': _handleEliminated(payload); break; + case 'ROUND_RESULT': _handleRoundResult(payload); break; + case 'GAME_STATE_UPDATE': _handleNewQuestion(payload); break; + case 'GAME_OVER': _handleGameOver(payload); break; + case 'GAME_EXIT': _gameStateController.add(payload); break; } } - void _handleElimination() { + // --- Handlers --- + + void _handlePhaseChange(Map payload) { + final phaseStr = payload['phase']; + if (phaseStr == 'VOTE_RULE') _phase = GamePhase.voteRule; + else if (phaseStr == 'VOTE_INPUT') { + _phase = GamePhase.voteInput; + _selectedRule = GameRule.values.firstWhere((e) => e.name == payload['rule'], orElse: () => GameRule.survival); + } + else if (phaseStr == 'PLAYING') { + _phase = GamePhase.playing; + _selectedInputMode = payload['inputMode'] == 'voice' ? InputMode.voice : InputMode.touch; + if (_selectedRule == GameRule.relay) { + _turnOrder = List.from(payload['turnOrder'] ?? []); + _currentTurnIndex = 0; + } + } + _gameStateController.add(payload); + _votes.clear(); + } + + void _handleVoteSubmit(Map payload) { + if (NetworkManager().role != NetworkRole.host) return; + _votes[payload['userId']] = payload['vote']; + + // 전원 투표 완료 체크 + // (중간에 나간 사람 고려하여 aliveUsers 기준으로 체크하거나 타임아웃 필요. MVP는 단순 크기 비교) + if (_votes.length >= _aliveUsers.length) { + if (_phase == GamePhase.voteRule) _decideRule(); + else if (_phase == GamePhase.voteInput) _decideInputAndStart(); + } + } + + void _handleCountdown(Map payload) { + _isShowingResult = false; + _isCountingDown = true; + _countdownValue = payload['count']; + if (_countdownValue > 0) SoundManager().playSfx(SoundKey.click); + _gameStateController.add(payload); + } + + void _handleAnswerSubmit(Map payload) { + if (NetworkManager().role != NetworkRole.host) return; + + final String userId = payload['userId']; + final String answer = payload['answer']; + + if (_answeredUsers.contains(userId)) return; + if (_selectedRule == GameRule.relay && _turnOrder[_currentTurnIndex] != userId) return; + + _answeredUsers.add(userId); + + final currentQ = _questions[_currentQuestionIndex]; + bool isCorrect = false; + if (_selectedInputMode == InputMode.voice) { + isCorrect = VoiceManager().checkAnswer(answer, currentQ.answer); + } else { + isCorrect = (answer == currentQ.answer); + } + + // 룰별 탈락 처리 + if (_selectedRule == GameRule.scoreAttack) { + if (isCorrect) _scores[userId] = (_scores[userId] ?? 0) + 1; + } else { + if (!isCorrect) { + _aliveUsers.remove(userId); + NetworkManager().sendMessage({'type': 'PLAYER_ELIMINATED', 'targetUserId': userId}); + if (userId == NetworkManager().me.id) _handleLocalElimination(); + + if (_selectedRule == GameRule.suddenDeath || _selectedRule == GameRule.relay) { + _broadcastState({'type': 'PLAYER_STATUS_UPDATE', 'userId': userId, 'isSubmitted': true, 'isAlive': false}); + Future.delayed(const Duration(milliseconds: 1000), () => _finishGame(winnerId: null)); + return; + } + } + } + + _broadcastState({ + 'type': 'PLAYER_STATUS_UPDATE', + 'userId': userId, + 'isSubmitted': true, + 'isAlive': _aliveUsers.contains(userId), + 'score': _scores[userId] + }); + + // 다음 진행 판단 + bool shouldAdvance = false; + if (_selectedRule == GameRule.relay) { + shouldAdvance = true; + } else { + int targetCount = _selectedRule == GameRule.scoreAttack + ? NetworkManager().guestList.length + 1 + : _aliveUsers.length + (isCorrect ? 0 : 1); + if (_answeredUsers.length >= targetCount) shouldAdvance = true; + } + + if (shouldAdvance) { + Future.delayed(const Duration(milliseconds: 1000), () => _showRoundResultAndNext()); + } + } + + void _handleStatusUpdate(Map payload) { + _answeredUsers.add(payload['userId']); + if (payload['isAlive'] == false) _aliveUsers.remove(payload['userId']); + if (payload['score'] != null) _scores[payload['userId']] = payload['score']; + _gameStateController.add(payload); + } + + void _handleEliminated(Map payload) { + if (payload['targetUserId'] == NetworkManager().me.id) _handleLocalElimination(); + _gameStateController.add({'type': 'UI_REFRESH'}); + } + + void _handleLocalElimination() { 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); + void _handleRoundResult(Map payload) { + _isCountingDown = false; + _isShowingResult = true; + if (_selectedRule == GameRule.relay) _currentTurnIndex = payload['nextTurnIndex'] ?? 0; - // 3초간 결과 보여주고 -> 카운트다운 시작 - Future.delayed(const Duration(seconds: 3), () { - _checkWinnerAndNext(); + final survivors = payload['survivors'] ?? []; + bool amISurvived = survivors.contains(NetworkManager().me.id); + + if (!amISurvived && _myStatus == PlayerStatus.alive && _selectedRule != GameRule.scoreAttack) { + _handleLocalElimination(); + } + _gameStateController.add(payload); + } + + void _handleNewQuestion(Map payload) { + _isCountingDown = false; + _isShowingResult = false; + _resetLocalState(); + _gameStateController.add(payload); + } + + void _handleGameOver(Map payload) { + final winnerId = payload['winnerId']; + if (winnerId == NetworkManager().me.id) { + _myStatus = PlayerStatus.winner; + SoundManager().playSfx(SoundKey.win); + } else { + _myStatus = PlayerStatus.loser; + if (winnerId == 'ALL_LOSE') SoundManager().playSfx(SoundKey.wrong); + } + _gameStateController.add(payload); + } + + // ------------------------------------------------------------------------ + // [Host Logic] + // ------------------------------------------------------------------------ + void _decideRule() { + final counts = {}; + for (var v in _votes.values) { counts[v] = (counts[v] ?? 0) + 1; } + String topRule = counts.entries.reduce((a, b) => a.value >= b.value ? a : b).key; + + _selectedRule = GameRule.values.firstWhere((e) => e.name == topRule, orElse: () => GameRule.survival); + + _broadcastState({ + 'type': 'PHASE_CHANGE', + 'phase': 'VOTE_INPUT', + 'rule': _selectedRule.name }); } - // 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; + void _decideInputAndStart() { + int touch = _votes.values.where((v) => v == 'touch').length; + int voice = _votes.values.where((v) => v == 'voice').length; + InputMode mode = (touch >= voice) ? InputMode.touch : InputMode.voice; + + List? turnOrder; + if (_selectedRule == GameRule.relay) { + turnOrder = _aliveUsers.toList()..shuffle(); } - // 다음 문제 준비 시퀀스 시작 - _startNextQuestionSequence(); + _broadcastState({ + 'type': 'PHASE_CHANGE', + 'phase': 'PLAYING', + 'inputMode': mode.name, + 'turnOrder': turnOrder + }); + + _selectedInputMode = mode; + _turnOrder = turnOrder ?? []; + _phase = GamePhase.playing; + + Future.delayed(const Duration(seconds: 2), () => _startCountdownSequence()); } - // 3. 카운트다운 (3->2->1) 후 문제 전송 - void _startNextQuestionSequence() { + void _showRoundResultAndNext() { + final currentQ = _questions[_currentQuestionIndex]; + int nextTurn = _currentTurnIndex; + if (_selectedRule == GameRule.relay) { + nextTurn = (_currentTurnIndex + 1) % _aliveUsers.length; + } + + _broadcastState({ + 'type': 'ROUND_RESULT', + 'status': 'RESULT', + 'correctAnswer': currentQ.answer, + 'survivors': _aliveUsers.toList(), + 'scores': _scores, + 'nextTurnIndex': nextTurn + }); + + _currentTurnIndex = nextTurn; + + Future.delayed(const Duration(seconds: 3), () => _checkWinnerAndNext()); + } + + void _checkWinnerAndNext() { + int totalPlayers = NetworkManager().guestList.length + 1; + bool isEnd = false; + String? winnerId; + + if (_currentQuestionIndex >= _questions.length - 1) { + isEnd = true; + if (_selectedRule == GameRule.scoreAttack) { + if (_scores.isNotEmpty) { + winnerId = _scores.entries.reduce((a, b) => a.value >= b.value ? a : b).key; + } + } else { + winnerId = _aliveUsers.isNotEmpty ? _aliveUsers.first : null; + } + } + else if (_selectedRule != GameRule.scoreAttack && _aliveUsers.length <= 1) { + if (_aliveUsers.isNotEmpty) { + isEnd = true; + winnerId = _aliveUsers.first; + } else { + isEnd = true; + winnerId = null; + } + } + + if (isEnd) { + _finishGame(winnerId: winnerId); + } else { + _startCountdownSequence(); + } + } + + void _startCountdownSequence() { int count = 3; - // 1초 간격 타이머 Timer.periodic(const Duration(seconds: 1), (timer) { _broadcastState({'type': 'GAME_COUNTDOWN', 'count': count}); - if (count == 0) { timer.cancel(); - _sendNewQuestion(); // 문제 전송 + _sendNewQuestion(); } count--; }); } - // 4. 실제 문제 데이터 전송 void _sendNewQuestion() { _currentQuestionIndex++; - final questionData = _questions[_currentQuestionIndex]; - - final stateData = { - 'type': 'GAME_STATE_UPDATE', - 'status': 'QUESTION', - 'data': questionData - }; - + final qData = _questions[_currentQuestionIndex]; _resetLocalState(); - _broadcastState(stateData); + _broadcastState({'type': 'GAME_STATE_UPDATE', 'status': 'QUESTION', 'data': qData.toJson()}); } void _finishGame({String? winnerId}) { @@ -285,9 +407,7 @@ class QuizGame extends BaseGame { void _broadcastState(Map data) { _lastState = data; _gameStateController.add(data); - if (NetworkManager().role == NetworkRole.host) { - NetworkManager().sendMessage(data); - } + if (NetworkManager().role == NetworkRole.host) NetworkManager().sendMessage(data); } void _resetLocalState() { @@ -304,160 +424,176 @@ class QuizGame extends BaseGame { } // ------------------------------------------------------------------------ - // [UI] Unified View (통일된 UI) + // [UI] // ------------------------------------------------------------------------ @override - Widget buildHostView(BuildContext context) => _buildSharedScreen(context, isHost: true); - + Widget buildHostView(BuildContext context) => _buildScreen(context, true); @override - Widget buildGuestView(BuildContext context) => _buildSharedScreen(context, isHost: false); + Widget buildGuestView(BuildContext context) => _buildScreen(context, false); - Widget _buildSharedScreen(BuildContext context, {required bool isHost}) { + Widget _buildScreen(BuildContext context, bool isHost) { return Scaffold( appBar: AppBar( - title: const Text("OX 서바이벌", style: TextStyle(fontWeight: FontWeight.bold)), - centerTitle: true, // 타이틀 중앙 정렬 통일 + title: const Text("PlayWith 퀴즈"), + 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("로딩 중..."); + body: Padding( + padding: const EdgeInsets.only(bottom: 140.0), + child: StreamBuilder>( + stream: gameStateStream, + initialData: _lastState, + builder: (context, snapshot) { + if (!snapshot.hasData) return _buildWaitingScreen("로딩 중..."); + final data = snapshot.data!; - 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("종료되었습니다.")); - } + if (_phase == GamePhase.voteRule || (data['type'] == 'PHASE_CHANGE' && data['phase'] == 'VOTING')) return _buildRuleVotingView(context); + if (_phase == GamePhase.voteInput || (data['type'] == 'PHASE_CHANGE' && data['phase'] == 'VOTE_INPUT')) return _buildInputVotingView(context); + + if (_isCountingDown || (data['type'] == 'GAME_COUNTDOWN')) { + int count = data['count'] ?? 3; + return Center(child: Text(count > 0 ? "$count" : "START!", style: const TextStyle(fontSize: 90, fontWeight: FontWeight.bold, color: Colors.blue))); + } - // 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) - ) - ); - } + 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']); + + if (_isShowingResult || data['status'] == 'RESULT') return _buildRoundResultScreen(data); - // 3. 중간 결과 화면 (정답 공개) - if (_isShowingResult || data['status'] == 'RESULT') { - return _buildRoundResultScreen(data); - } + if (data['status'] == 'QUESTION' || _currentQuestionIndex >= 0) { + Map qData = data['data'] ?? _questions[_currentQuestionIndex].toJson(); + return _buildPlayArea(context, qData); + } - // 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("잠시만 기다려주세요..."); - }, + 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)), - ), - ], - ), - ); + // --- UI Parts --- + + Widget _buildRuleVotingView(BuildContext context) { + if (_votes.containsKey(NetworkManager().me.id)) return _buildWaitingScreen("룰 투표 완료! 대기 중..."); + return Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + const Text("어떤 게임을 할까요?", style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold)), + const SizedBox(height: 30), + Wrap( + spacing: 15, runSpacing: 15, alignment: WrapAlignment.center, + children: [ + _VoteButton(icon: Icons.local_fire_department, label: "서바이벌", color: Colors.red, onTap: () => _submitVote('survival')), + _VoteButton(icon: Icons.dangerous, label: "단체 한방", color: Colors.black, onTap: () => _submitVote('suddenDeath')), + _VoteButton(icon: Icons.score, label: "점수 내기", color: Colors.blue, onTap: () => _submitVote('scoreAttack')), + _VoteButton(icon: Icons.directions_run, label: "이어 달리기", color: Colors.green, onTap: () => _submitVote('relay')), + ], + ), + ], + ), + ); + } + + Widget _buildInputVotingView(BuildContext context) { + if (_votes.containsKey(NetworkManager().me.id)) return _buildWaitingScreen("입력 방식 투표 완료! 대기 중..."); + return Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + const Text("어떻게 맞출까요?", style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold)), + const SizedBox(height: 30), + Row( + mainAxisAlignment: MainAxisAlignment.spaceEvenly, + children: [ + _VoteButton(icon: Icons.touch_app, label: "터치", color: Colors.blue, onTap: () => _submitVote('touch')), + _VoteButton(icon: Icons.mic, label: "음성", color: Colors.orange, onTap: () => _submitVote('voice')), + ], + ), + ], + ), + ); + } + + void _submitVote(String vote) { + _votes[NetworkManager().me.id] = vote; + _gameStateController.add({'type': 'UI_REFRESH'}); + final payload = {'type': 'VOTE_SUBMIT', 'userId': NetworkManager().me.id, 'vote': vote}; + if (NetworkManager().role == NetworkRole.host) onMessageReceived("", payload); + else NetworkManager().sendMessage(payload); + } + + Widget _buildPlayArea(BuildContext context, Map qData) { + bool isMyTurn = true; + String currentTurnName = ""; + if (_selectedRule == GameRule.relay) { + String currentUserId = _turnOrder.isNotEmpty ? _turnOrder[_currentTurnIndex] : ""; + isMyTurn = currentUserId == NetworkManager().me.id; + currentTurnName = _findUserName(currentUserId); + } + + if (_myStatus == PlayerStatus.dead && _selectedRule != GameRule.scoreAttack) { + return Center(child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [const Icon(Icons.sentiment_dissatisfied, size: 70, color: Colors.grey), const SizedBox(height: 10), const Text("탈락했습니다 👻", style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold)), const SizedBox(height: 20), Text("문제: ${qData['question']}", style: const TextStyle(color: Colors.grey))])); } - // 생존자 뷰 return Column( children: [ - // 상단 현황판 - _PlayerStatusGrid(aliveUsers: _aliveUsers, answeredUsers: _answeredUsers), + Container( + padding: const EdgeInsets.all(10), + color: Colors.grey[100], + child: _selectedRule == GameRule.scoreAttack + ? _ScoreBoard(scores: _scores) + : _PlayerStatusGrid(aliveUsers: _aliveUsers, answeredUsers: _answeredUsers), + ), + + if (_selectedRule == GameRule.relay) + Container( + width: double.infinity, + padding: const EdgeInsets.all(8), + color: isMyTurn ? Colors.blueAccent : Colors.grey[300], + child: Text(isMyTurn ? "내 차례입니다!" : "$currentTurnName님의 차례", textAlign: TextAlign.center, style: TextStyle(color: isMyTurn ? Colors.white : Colors.black, fontWeight: FontWeight.bold)), + ), + 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), - ), - ), - ), + child: Center(child: Padding(padding: const EdgeInsets.all(20), child: Text(qData['question'], textAlign: TextAlign.center, style: const TextStyle(fontSize: 28, fontWeight: FontWeight.bold)))), ), - // 컨트롤 (버튼) 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(), - ), + child: !isMyTurn + ? const Center(child: Text("다른 사람이 푸는 중...", style: TextStyle(fontSize: 18, color: Colors.grey))) + : (_selectedInputMode == InputMode.touch + ? _buildTouchInput(qData['options'] != null ? List.from(qData['options']) : ["O", "X"]) + : _buildVoiceInput()), ), ], ); } - // [결과 발표 화면] + Widget _buildTouchInput(List options) { + if (_isLockedIn) return _buildLockedUI(); + return Center(child: Wrap(spacing: 20, runSpacing: 20, alignment: WrapAlignment.center, children: options.map((opt) => _AnswerBtn(text: opt, color: Colors.blueAccent, isSelected: _mySelectedAnswer == opt, onTap: () => _selectAnswer(opt))).toList())); + } + + Widget _buildVoiceInput() { + if (_isLockedIn) return _buildLockedUI(); + return Column(mainAxisAlignment: MainAxisAlignment.center, children: [VoiceWidget(isListening: VoiceManager().isListening), const SizedBox(height: 20), GestureDetector(onLongPressStart: (_) async { await VoiceManager().startListening(onResult: (text) {}); }, onLongPressEnd: (_) async { await VoiceManager().stopListening(); _selectAnswer("O"); }, child: Container(padding: const EdgeInsets.all(20), decoration: const BoxDecoration(color: Colors.redAccent, shape: BoxShape.circle), child: const Icon(Icons.mic, size: 40, color: Colors.white))), const SizedBox(height: 10), const Text("버튼을 누르고 정답을 말하세요!", style: TextStyle(color: Colors.grey))]); + } + + Widget _buildLockedUI() { + return Center(child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [Icon(Icons.check, size: 80, color: Colors.blue), const SizedBox(height: 20), const Text("제출 완료!", style: TextStyle(fontSize: 22))])); + } + Widget _buildRoundResultScreen(Map data) { final String correctAnswer = data['correctAnswer'] ?? "?"; final List survivors = data['survivors'] ?? []; @@ -469,59 +605,21 @@ class QuizGame extends BaseGame { 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) - ) - ), + 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: 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: 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))) + 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)), ], ), ); } - 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 동일) ... + // Helper methods void _selectAnswer(String answer) { _lockInTimer?.cancel(); _mySelectedAnswer = answer; @@ -539,21 +637,42 @@ class QuizGame extends BaseGame { } 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("로비로 돌아가기"))])); + return Center(child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [Icon(Icons.emoji_events, size: 100, color: Colors.amber), const SizedBox(height: 20), const Text("게임 종료", style: 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)))])); + 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)))])); + } +} + +// [Components] +class _ScoreBoard extends StatelessWidget { + final Map scores; + const _ScoreBoard({required this.scores}); + @override + Widget build(BuildContext context) { + return SizedBox( + height: 60, + child: ListView.builder( + scrollDirection: Axis.horizontal, + itemCount: scores.length, + itemBuilder: (context, index) { + final uid = scores.keys.elementAt(index); + final score = scores[uid]; + String name = "?"; + if (uid == NetworkManager().me.id) name = NetworkManager().me.nickname; + else { try { name = NetworkManager().guestList.firstWhere((u) => u.id == uid).nickname; } catch(_) {} } + return Container(margin: const EdgeInsets.symmetric(horizontal: 8), padding: const EdgeInsets.all(8), decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(10), border: Border.all(color: Colors.blue.shade100)), child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [Text(name, style: const TextStyle(fontSize: 10)), Text("$score점", style: const TextStyle(fontWeight: FontWeight.bold, color: Colors.blue))])); + }, + ), + ); } } -// [현황판] class _PlayerStatusGrid extends StatelessWidget { - final Set aliveUsers; - final Set answeredUsers; + final Set aliveUsers; final Set answeredUsers; const _PlayerStatusGrid({required this.aliveUsers, required this.answeredUsers}); @override Widget build(BuildContext context) { @@ -562,12 +681,20 @@ class _PlayerStatusGrid extends StatelessWidget { } } -// [버튼] +class _VoteButton extends StatelessWidget { + final IconData icon; final String label; final Color color; final VoidCallback onTap; + const _VoteButton({required this.icon, required this.label, required this.color, required this.onTap}); + @override + Widget build(BuildContext context) { + return GestureDetector(onTap: onTap, child: Column(children: [Container(width: 80, height: 80, decoration: BoxDecoration(color: color.withOpacity(0.1), borderRadius: BorderRadius.circular(20), border: Border.all(color: color, width: 2)), child: Icon(icon, size: 40, color: color)), const SizedBox(height: 5), Text(label, style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold, color: color))])); + } +} + 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))))); + 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: 30, color: Colors.white, fontWeight: FontWeight.bold))))); } } \ No newline at end of file