Implemented game room as chat for now
This commit is contained in:
parent
7eeba12968
commit
59a8a78a7a
1 changed files with 174 additions and 8 deletions
182
lib/main.dart
182
lib/main.dart
|
|
@ -1,5 +1,4 @@
|
||||||
import 'dart:developer';
|
import 'package:flutter/foundation.dart';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
import 'package:socket_io_client/socket_io_client.dart' as io;
|
import 'package:socket_io_client/socket_io_client.dart' as io;
|
||||||
|
|
@ -20,6 +19,7 @@ void main() {
|
||||||
getPages: [
|
getPages: [
|
||||||
GetPage(name: '/auth', page: () => const AuthPage()),
|
GetPage(name: '/auth', page: () => const AuthPage()),
|
||||||
GetPage(name: '/home', page: () => const HomePage()),
|
GetPage(name: '/home', page: () => const HomePage()),
|
||||||
|
GetPage(name: '/game', page: () => const GamePage()),
|
||||||
],
|
],
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
@ -39,7 +39,6 @@ class AuthPage extends StatefulWidget {
|
||||||
}
|
}
|
||||||
|
|
||||||
class _AuthPageState extends State<AuthPage> {
|
class _AuthPageState extends State<AuthPage> {
|
||||||
final wsUrl = Uri.parse("ws://localhost:9800/ws");
|
|
||||||
late io.Socket socket;
|
late io.Socket socket;
|
||||||
|
|
||||||
TextEditingController loginController = TextEditingController();
|
TextEditingController loginController = TextEditingController();
|
||||||
|
|
@ -48,7 +47,10 @@ class _AuthPageState extends State<AuthPage> {
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
socket = io.io("http://localhost:9800/", <String, dynamic>{
|
|
||||||
|
const serverUrl =
|
||||||
|
kDebugMode ? "http://localhost:9800" : "https://huacu.nuark.xyz";
|
||||||
|
socket = io.io("https://huacu.nuark.xyz", <String, dynamic>{
|
||||||
"transports": ["websocket"],
|
"transports": ["websocket"],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -167,6 +169,14 @@ class _AuthPageState extends State<AuthPage> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class Game {
|
||||||
|
final String id;
|
||||||
|
final String player1;
|
||||||
|
final String player2;
|
||||||
|
|
||||||
|
Game(this.id, this.player1, this.player2);
|
||||||
|
}
|
||||||
|
|
||||||
class HomePage extends StatefulWidget {
|
class HomePage extends StatefulWidget {
|
||||||
const HomePage({super.key});
|
const HomePage({super.key});
|
||||||
|
|
||||||
|
|
@ -175,8 +185,8 @@ class HomePage extends StatefulWidget {
|
||||||
}
|
}
|
||||||
|
|
||||||
class _HomePageState extends State<HomePage> {
|
class _HomePageState extends State<HomePage> {
|
||||||
late io.Socket socket;
|
final io.Socket socket = Get.find();
|
||||||
late AuthData authData = Get.find();
|
final AuthData authData = Get.find();
|
||||||
|
|
||||||
var guessersPlayers = <String>[].obs;
|
var guessersPlayers = <String>[].obs;
|
||||||
var suggestersPlayers = <String>[].obs;
|
var suggestersPlayers = <String>[].obs;
|
||||||
|
|
@ -185,7 +195,6 @@ class _HomePageState extends State<HomePage> {
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
socket = Get.find();
|
|
||||||
|
|
||||||
socket.on("hello", (idky) {
|
socket.on("hello", (idky) {
|
||||||
socket.dispose();
|
socket.dispose();
|
||||||
|
|
@ -217,6 +226,23 @@ class _HomePageState extends State<HomePage> {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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");
|
socket.emit("getUpdate");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -380,5 +406,145 @@ class _HomePageState extends State<HomePage> {
|
||||||
socket.emit("sendRequest", player);
|
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<GamePage> createState() => _GamePageState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _GamePageState extends State<GamePage> {
|
||||||
|
final io.Socket socket = Get.find();
|
||||||
|
final AuthData authData = Get.find();
|
||||||
|
final Game gameInfo = Get.find();
|
||||||
|
|
||||||
|
var chat = <ChatEntry>[].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<ChatEntry> 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),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue