From 59a8a78a7a5e537b573d0710e0818b1751e4bf78 Mon Sep 17 00:00:00 2001 From: Andrew nuark G Date: Fri, 3 Mar 2023 21:28:11 +0700 Subject: [PATCH] Implemented game room as chat for now --- lib/main.dart | 182 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 174 insertions(+), 8 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 338d045..22dabc9 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,4 @@ -import 'dart:developer'; - +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:socket_io_client/socket_io_client.dart' as io; @@ -20,6 +19,7 @@ void main() { getPages: [ GetPage(name: '/auth', page: () => const AuthPage()), GetPage(name: '/home', page: () => const HomePage()), + GetPage(name: '/game', page: () => const GamePage()), ], )); } @@ -39,7 +39,6 @@ class AuthPage extends StatefulWidget { } class _AuthPageState extends State { - final wsUrl = Uri.parse("ws://localhost:9800/ws"); late io.Socket socket; TextEditingController loginController = TextEditingController(); @@ -48,7 +47,10 @@ class _AuthPageState extends State { @override void initState() { super.initState(); - socket = io.io("http://localhost:9800/", { + + const serverUrl = + kDebugMode ? "http://localhost:9800" : "https://huacu.nuark.xyz"; + socket = io.io("https://huacu.nuark.xyz", { "transports": ["websocket"], }); } @@ -167,6 +169,14 @@ class _AuthPageState extends State { } } +class Game { + final String id; + final String player1; + final String player2; + + Game(this.id, this.player1, this.player2); +} + class HomePage extends StatefulWidget { const HomePage({super.key}); @@ -175,8 +185,8 @@ class HomePage extends StatefulWidget { } class _HomePageState extends State { - late io.Socket socket; - late AuthData authData = Get.find(); + final io.Socket socket = Get.find(); + final AuthData authData = Get.find(); var guessersPlayers = [].obs; var suggestersPlayers = [].obs; @@ -185,7 +195,6 @@ class _HomePageState extends State { @override void initState() { super.initState(); - socket = Get.find(); socket.on("hello", (idky) { socket.dispose(); @@ -217,6 +226,23 @@ class _HomePageState extends State { }); }); + socket.on("requestResponseResult", (data) { + bool ok = data[0]; + if (ok) { + socket.off("hello"); + socket.off("update"); + socket.off("updateNeeded"); + socket.off("gameRequest"); + socket.off("requestResponseResult"); + Get.put(authData); + Get.put(socket); + Get.put(Game(data[1]["id"], data[1]["player1"], data[1]["player2"])); + Get.offNamed("/game"); + } else { + Get.snackbar("Request response", data[1]); + } + }); + socket.emit("getUpdate"); } @@ -380,5 +406,145 @@ class _HomePageState extends State { socket.emit("sendRequest", player); } - void _handleRequestResponseAction(String data, bool bool) {} + void _handleRequestResponseAction(String data, bool response) { + socket.emit("requestResponse", [data, response]); + setState(() { + incommingRequests.remove(data); + }); + } +} + +class ChatEntry { + final String from; + final String message; + + ChatEntry(this.from, this.message); +} + +class GamePage extends StatefulWidget { + const GamePage({Key? key}) : super(key: key); + + @override + State createState() => _GamePageState(); +} + +class _GamePageState extends State { + final io.Socket socket = Get.find(); + final AuthData authData = Get.find(); + final Game gameInfo = Get.find(); + + var chat = [].obs; + + final _chatController = TextEditingController(); + + @override + void initState() { + super.initState(); + + socket.on("hello", (idky) { + socket.dispose(); + Get.offAllNamed("/auth"); + }); + + socket.on("chatResponse", (data) { + bool ok = data[0]; + if (ok) { + chat.add(ChatEntry(data[1]["from"], data[1]["message"])); + } else { + int statusCode = data[1]; + Get.put(socket); + Get.put(authData); + switch (statusCode) { + case 404: + Get.snackbar("Error", "Other player not found"); + Get.offAllNamed("/home"); + break; + case 410: + Get.snackbar("Error", "Other player left game"); + Get.offAllNamed("/home"); + break; + } + } + }); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text("HUACU"), + ), + body: Center( + child: _buildGame(chat), + ), + ); + } + + Widget _buildGame(RxList chat) { + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Expanded( + child: ObxValue( + (data) => ListView.builder( + itemCount: data.length, + itemBuilder: (context, index) { + return Card( + child: ListTile( + title: Text(data[index].message), + subtitle: Text("from ${data[index].from}"), + subtitleTextStyle: const TextStyle( + fontStyle: FontStyle.italic, fontSize: 12), + ), + ); + }, + ), + chat, + ), + ), + Padding( + padding: const EdgeInsets.all(16), + child: Row( + mainAxisSize: MainAxisSize.max, + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Expanded( + child: TextField( + controller: _chatController, + onSubmitted: (value) { + final text = _chatController.text; + if (text.isNotEmpty) { + socket.emit("chat", [gameInfo.id, text.trim()]); + } + _chatController.clear(); + }, + decoration: const InputDecoration( + enabledBorder: OutlineInputBorder( + borderSide: BorderSide(color: Colors.grey, width: 0.5), + borderRadius: BorderRadius.all(Radius.circular(32)), + ), + focusedBorder: OutlineInputBorder( + borderSide: BorderSide(color: Colors.amber, width: 2.0), + borderRadius: BorderRadius.all(Radius.circular(32)), + ), + hintText: "Your message", + ), + ), + ), + IconButton( + onPressed: () { + final text = _chatController.text; + if (text.isNotEmpty) { + socket.emit("chat", [gameInfo.id, text.trim()]); + } + _chatController.clear(); + }, + icon: const Icon(Icons.send), + ), + ], + ), + ), + ], + ); + } }