Fixed sub/unsub problem

This commit is contained in:
Andrew 2023-03-05 20:12:51 +07:00
parent 57004048e5
commit 94d6cf984f
2 changed files with 146 additions and 131 deletions

View file

@ -32,59 +32,14 @@ class _GamePageState extends State<GamePage> {
void initState() {
super.initState();
socket.on("hello", (idky) {
socket.dispose();
Get.offAllNamed("/auth");
});
socket.on("gameStatus", (data) {
bool won = data[0];
Get.defaultDialog(
title: "Game ended",
middleText: won ? "You won!" : "You lost!",
barrierDismissible: false,
onConfirm: () {
Get.put(authData);
Get.put(socket);
Get.offAllNamed("/home");
},
);
});
socket.on("leaveGameResponse", (data) {
if (data[0] == true && data[1] == 410) {
Get.defaultDialog(
title: "Game ended",
middleText: "Your opponent left the game",
barrierDismissible: false,
onConfirm: () {
Get.put(authData);
Get.put(socket);
Get.offAllNamed("/home");
},
);
} else {
Get.put(authData);
Get.put(socket);
Get.offAllNamed("/home");
}
});
socket.on("chatResponse", (data) {
bool ok = data[0];
if (ok) {
chat.add(ChatEntry(data[1]["from"], data[1]["message"]));
}
});
socket.on("hello", _onHelloEvent);
socket.on("gameStatus", _onGameStatusEvent);
socket.on("leaveGameResponse", _onLeaveGameResponseEvent);
socket.on("chatResponse", _onChatResponseEvent);
if (isGuesser) {
socket.on("guessResponse", _onGuessResponse);
socket.on("guessResponse", _onGuessResponseEvent);
} else {
socket.on("guess", (data) {
setState(() {
guessedColors.add(data);
});
});
socket.on("guess", _onGuessEvent);
}
chat.listen((data) {
@ -93,14 +48,74 @@ class _GamePageState extends State<GamePage> {
});
}
void _onHelloEvent(dynamic idky) {
socket.dispose();
Get.offAllNamed("/auth");
}
void _onGameStatusEvent(dynamic data) {
bool won = data[0];
Get.defaultDialog(
title: "Game ended",
middleText: won ? "You won!" : "You lost!",
barrierDismissible: false,
onConfirm: () {
Get.put(authData);
Get.put(socket);
Get.offAllNamed("/home");
},
);
}
void _onLeaveGameResponseEvent(dynamic data) {
if (data[0] == true && data[1] == 410) {
Get.defaultDialog(
title: "Game ended",
middleText: "Your opponent left the game",
barrierDismissible: false,
onConfirm: () {
Get.put(authData);
Get.put(socket);
Get.offAllNamed("/home");
},
);
} else {
Get.put(authData);
Get.put(socket);
Get.offAllNamed("/home");
}
}
void _onChatResponseEvent(dynamic data) {
bool ok = data[0];
if (ok) {
chat.add(ChatEntry(data[1]["from"], data[1]["message"]));
}
}
void _onGuessEvent(dynamic data) {
setState(() {
guessedColors.add(data);
});
}
void _onGuessResponseEvent(dynamic data) {
bool ok = data[0];
if (ok) {
Get.snackbar("Success", "Guess sent successfully");
} else {
Get.snackbar("Error", "Guess failed to send");
}
}
@override
void dispose() {
socket.off("hello");
socket.off("gameStatus");
socket.off("leaveGameResponse");
socket.off("chatResponse");
socket.off("guessResponse");
socket.off("guess");
socket.off("hello", _onHelloEvent);
socket.off("gameStatus", _onGameStatusEvent);
socket.off("leaveGameResponse", _onLeaveGameResponseEvent);
socket.off("chatResponse", _onChatResponseEvent);
socket.off("guessResponse", _onGuessResponseEvent);
socket.off("guess", _onGuessEvent);
super.dispose();
}
@ -352,15 +367,6 @@ class _GamePageState extends State<GamePage> {
socket.emit("guess", [gameInfo.id, name]);
}
void _onGuessResponse(dynamic data) {
bool ok = data[0];
if (ok) {
Get.snackbar("Success", "Guess sent successfully");
} else {
Get.snackbar("Error", "Guess failed to send");
}
}
void _openChat() {
Get.bottomSheet(BottomSheet(
onClosing: () {},