2022-07-30 14:17:16 +03:00
|
|
|
import { CommonMancalaGame, MancalaGame } from 'mancala.js';
|
|
|
|
|
import * as React from 'react';
|
|
|
|
|
import { FunctionComponent, useEffect } from 'react';
|
2022-07-31 00:58:45 +03:00
|
|
|
import { Link, useNavigate } from 'react-router-dom';
|
2022-07-30 14:17:16 +03:00
|
|
|
import Center from '../components/Center';
|
|
|
|
|
import CircularPanel from '../components/CircularPanel';
|
|
|
|
|
import HeaderBar from '../components/headerbar/HeaderBar';
|
|
|
|
|
import HeaderbarIcon from '../components/headerbar/HeaderbarIcon';
|
|
|
|
|
import HeaderbarTitle from '../components/headerbar/HeaderbarTitle';
|
|
|
|
|
import ThemeSwitchMenu from '../components/headerbar/ThemeSwitchMenu';
|
|
|
|
|
import PageContainer from '../components/PageContainer';
|
|
|
|
|
import Row from '../components/Row';
|
2024-06-16 23:31:33 +03:00
|
|
|
import { channel_cancel_new_game, channel_on_game_start } from '@mancala/core';
|
2022-07-30 14:17:16 +03:00
|
|
|
import { Context } from '../context/context';
|
|
|
|
|
import { Theme } from '../theme/Theme';
|
|
|
|
|
import { getColorByBrightness } from '../util/ColorUtil';
|
2024-03-31 17:54:07 +03:00
|
|
|
import Button from "../components/Button";
|
2022-07-30 14:17:16 +03:00
|
|
|
|
|
|
|
|
const LobyPage: FunctionComponent<{
|
|
|
|
|
context: Context,
|
|
|
|
|
userKey?: string,
|
|
|
|
|
theme: Theme
|
|
|
|
|
}> = ({ context, userKey, theme }) => {
|
|
|
|
|
|
|
|
|
|
let navigate = useNavigate();
|
|
|
|
|
|
2022-09-02 00:03:17 +03:00
|
|
|
const onGameStart = (message: Object) => {
|
|
|
|
|
const newGame: CommonMancalaGame = message as CommonMancalaGame;
|
|
|
|
|
navigate(`/game/${newGame.id}`)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-31 17:54:07 +03:00
|
|
|
const onCancelNewGameClick = () => {
|
|
|
|
|
if (context.rtmt.connectionState === "connected") {
|
|
|
|
|
context.rtmt.sendMessage(channel_cancel_new_game, "");
|
|
|
|
|
}
|
|
|
|
|
navigate("/")
|
|
|
|
|
}
|
2022-07-30 14:17:16 +03:00
|
|
|
useEffect(() => {
|
2022-09-02 00:03:17 +03:00
|
|
|
context.rtmt.addMessageListener(channel_on_game_start, onGameStart);
|
2022-07-30 14:17:16 +03:00
|
|
|
context.rtmt.sendMessage("new_game", {});
|
2022-09-02 00:03:17 +03:00
|
|
|
return () => {
|
|
|
|
|
context.rtmt.removeMessageListener(channel_on_game_start, onGameStart);
|
|
|
|
|
}
|
2022-07-30 14:17:16 +03:00
|
|
|
}, []);
|
2022-09-02 00:03:17 +03:00
|
|
|
|
2022-07-30 14:17:16 +03:00
|
|
|
const textColorOnAppBar = getColorByBrightness(
|
|
|
|
|
context.themeManager.theme.appBarBgColor,
|
|
|
|
|
context.themeManager.theme.textColor,
|
|
|
|
|
context.themeManager.theme.textLightColor
|
|
|
|
|
);
|
|
|
|
|
const textColorOnBoard = getColorByBrightness(
|
|
|
|
|
context.themeManager.theme.boardColor,
|
|
|
|
|
context.themeManager.theme.textColor,
|
|
|
|
|
context.themeManager.theme.textLightColor
|
|
|
|
|
);
|
|
|
|
|
return (
|
|
|
|
|
<PageContainer theme={theme!}>
|
|
|
|
|
<HeaderBar color={theme?.appBarBgColor}>
|
|
|
|
|
<Row>
|
2022-07-31 00:58:45 +03:00
|
|
|
<Link style={{ textDecoration: 'none' }} to={"/"}>
|
|
|
|
|
<HeaderbarIcon />
|
|
|
|
|
</Link>
|
|
|
|
|
<Link style={{ textDecoration: 'none' }} to={"/"}>
|
|
|
|
|
<HeaderbarTitle title={context.texts.Mancala} color={textColorOnAppBar} />
|
|
|
|
|
</Link>
|
2022-07-30 14:17:16 +03:00
|
|
|
</Row>
|
|
|
|
|
<Row>
|
|
|
|
|
<ThemeSwitchMenu context={context} textColor={textColorOnAppBar} />
|
|
|
|
|
</Row>
|
|
|
|
|
</HeaderBar>
|
|
|
|
|
<Center>
|
2024-03-31 17:54:07 +03:00
|
|
|
<div style={{
|
|
|
|
|
display: "flex",
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
alignContent: "center"
|
|
|
|
|
}}>
|
|
|
|
|
<CircularPanel color={context.themeManager.theme.boardColor}>
|
|
|
|
|
<h4 style={{ margin: "0", color: textColorOnBoard }}>{`${context.texts.SearchingOpponent} ${context.texts.PleaseWait}...`}</h4>
|
|
|
|
|
</CircularPanel>
|
|
|
|
|
<Button
|
|
|
|
|
context={context}
|
|
|
|
|
color={context.themeManager.theme.boardColor}
|
|
|
|
|
text={context.texts.Cancel}
|
|
|
|
|
onClick={onCancelNewGameClick} />
|
|
|
|
|
</div>
|
|
|
|
|
|
2022-07-30 14:17:16 +03:00
|
|
|
</Center>
|
|
|
|
|
</PageContainer>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default LobyPage;
|