From f65da457bc15680a4608e3832ce0b68436757f11 Mon Sep 17 00:00:00 2001 From: Halit Aksoy Date: Sun, 31 Mar 2024 17:54:07 +0300 Subject: [PATCH] feature: cancel new game --- backend/src/consts/channel_names.ts | 1 + backend/src/game/GameManager.ts | 5 +++++ backend/src/matchmaker/MatchMaker.ts | 1 + backend/src/matchmaker/MatchMakerImpl.ts | 9 ++++++++ frontend/src/const/channel_names.ts | 1 + frontend/src/routes/LobyPage.tsx | 27 ++++++++++++++++++++---- mobile/src/const/channel_names.ts | 1 + mobile/src/screens/LobyScreen.tsx | 21 ++++++++++++++---- 8 files changed, 58 insertions(+), 8 deletions(-) diff --git a/backend/src/consts/channel_names.ts b/backend/src/consts/channel_names.ts index d088b0b..af41238 100644 --- a/backend/src/consts/channel_names.ts +++ b/backend/src/consts/channel_names.ts @@ -1,4 +1,5 @@ export const channel_new_game = "new_game" +export const channel_cancel_new_game = "cancel_new_game" export const channel_on_game_start = "on_game_start" export const channel_game_move = "game_move" export const channel_on_game_update = "on_game_update" diff --git a/backend/src/game/GameManager.ts b/backend/src/game/GameManager.ts index a92c4d8..f52c0e7 100644 --- a/backend/src/game/GameManager.ts +++ b/backend/src/game/GameManager.ts @@ -3,6 +3,7 @@ import { RTMT } from "../rtmt/rtmt"; import { GameStore } from "./gamestore/GameStore"; import { generateKey } from "../util/key_factory"; import { + channel_cancel_new_game, channel_game_move, channel_leave_game, channel_listen_game_events, @@ -42,6 +43,10 @@ export class GameManager { this.matchMaker.join(userKey); }); + this.rtmt.listenMessage(channel_cancel_new_game, (userKey: string, message: Object) => { + this.matchMaker.cancel(userKey); + }); + this.rtmt.listenMessage(channel_game_move, (userKey: string, message: Object) => { this.onGameMove(userKey, message as GameMove); }); diff --git a/backend/src/matchmaker/MatchMaker.ts b/backend/src/matchmaker/MatchMaker.ts index bdc84db..737bf37 100644 --- a/backend/src/matchmaker/MatchMaker.ts +++ b/backend/src/matchmaker/MatchMaker.ts @@ -2,5 +2,6 @@ export type OnPlayersPaired = (player1Id: string, player2Id: string)=> void; export interface MatchMaker { join(playerId : string): void; + cancel(playerId : string): boolean; listenOnPlayersPaired(onPlayersPaired: OnPlayersPaired ) : void; } \ No newline at end of file diff --git a/backend/src/matchmaker/MatchMakerImpl.ts b/backend/src/matchmaker/MatchMakerImpl.ts index a9f1ca2..ec4faf9 100644 --- a/backend/src/matchmaker/MatchMakerImpl.ts +++ b/backend/src/matchmaker/MatchMakerImpl.ts @@ -29,6 +29,15 @@ export class MatchMakerImpl implements MatchMaker { this.waitingPlayerId = newcomerPlayerId } } + + public cancel(playerId: string): boolean { + if(playerId === this.waitingPlayerId) { + this.waitingPlayerId = undefined; + return true; + } + + return false; + } public listenOnPlayersPaired(onPlayersPaired: OnPlayersPaired): void { this.onPlayersPaired = onPlayersPaired; diff --git a/frontend/src/const/channel_names.ts b/frontend/src/const/channel_names.ts index d088b0b..af41238 100644 --- a/frontend/src/const/channel_names.ts +++ b/frontend/src/const/channel_names.ts @@ -1,4 +1,5 @@ export const channel_new_game = "new_game" +export const channel_cancel_new_game = "cancel_new_game" export const channel_on_game_start = "on_game_start" export const channel_game_move = "game_move" export const channel_on_game_update = "on_game_update" diff --git a/frontend/src/routes/LobyPage.tsx b/frontend/src/routes/LobyPage.tsx index dcccaa9..71f2785 100644 --- a/frontend/src/routes/LobyPage.tsx +++ b/frontend/src/routes/LobyPage.tsx @@ -10,10 +10,11 @@ import HeaderbarTitle from '../components/headerbar/HeaderbarTitle'; import ThemeSwitchMenu from '../components/headerbar/ThemeSwitchMenu'; import PageContainer from '../components/PageContainer'; import Row from '../components/Row'; -import { channel_on_game_start } from '../const/channel_names'; +import { channel_cancel_new_game, channel_on_game_start } from '../const/channel_names'; import { Context } from '../context/context'; import { Theme } from '../theme/Theme'; import { getColorByBrightness } from '../util/ColorUtil'; +import Button from "../components/Button"; const LobyPage: FunctionComponent<{ context: Context, @@ -28,6 +29,12 @@ const LobyPage: FunctionComponent<{ navigate(`/game/${newGame.id}`) } + const onCancelNewGameClick = () => { + if (context.rtmt.connectionState === "connected") { + context.rtmt.sendMessage(channel_cancel_new_game, ""); + } + navigate("/") + } useEffect(() => { context.rtmt.addMessageListener(channel_on_game_start, onGameStart); context.rtmt.sendMessage("new_game", {}); @@ -62,9 +69,21 @@ const LobyPage: FunctionComponent<{
- -

{`${context.texts.SearchingOpponent} ${context.texts.PleaseWait}...`}

-
+
+ +

{`${context.texts.SearchingOpponent} ${context.texts.PleaseWait}...`}

+
+
+
); diff --git a/mobile/src/const/channel_names.ts b/mobile/src/const/channel_names.ts index d088b0b..af41238 100644 --- a/mobile/src/const/channel_names.ts +++ b/mobile/src/const/channel_names.ts @@ -1,4 +1,5 @@ export const channel_new_game = "new_game" +export const channel_cancel_new_game = "cancel_new_game" export const channel_on_game_start = "on_game_start" export const channel_game_move = "game_move" export const channel_on_game_update = "on_game_update" diff --git a/mobile/src/screens/LobyScreen.tsx b/mobile/src/screens/LobyScreen.tsx index 031cf62..844f073 100644 --- a/mobile/src/screens/LobyScreen.tsx +++ b/mobile/src/screens/LobyScreen.tsx @@ -1,10 +1,11 @@ import * as React from 'react'; -import { View, Text, Button } from 'react-native'; +import { View, Text, Button, Pressable } from 'react-native'; import { useTranslation } from 'react-i18next'; import { LobyScreenProps } from '../types'; import { useEffect } from 'react'; import { CommonMancalaGame } from 'mancala.js'; -import { channel_on_game_start } from '../const/channel_names'; +import { channel_cancel_new_game, channel_on_game_start } from '../const/channel_names'; +import CircularPanel from '../components/CircularPanel'; export default function LobyScreen({ navigation, route }: LobyScreenProps) { @@ -17,6 +18,14 @@ export default function LobyScreen({ navigation, route }: LobyScreenProps) { navigation.replace("Game", { context, gameId: newGame.id, userKey }) } + + const onCancelNewGameClick = () => { + if (context.rtmt.connectionState === "connected") { + context.rtmt.sendMessage(channel_cancel_new_game, ""); + } + navigation.replace("Home", { context }) + } + useEffect(() => { context.rtmt.addMessageListener(channel_on_game_start, onGameStart); context.rtmt.sendMessage("new_game", {}); @@ -26,8 +35,12 @@ export default function LobyScreen({ navigation, route }: LobyScreenProps) { }, []); return ( - - {t('SearchingOpponent')} + + {t('SearchingOpponent') + " " + t('PleaseWait') + "..."}} /> + + + {t('Cancel')}} /> + ); } \ No newline at end of file