mancala/src/components/InfoPanel.tsx

117 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-06-04 14:49:34 +03:00
import { MancalaGame } from "mancala.js";
import * as React from "react";
import { FunctionComponent } from "react";
2022-07-14 13:38:00 +03:00
import { Context } from "../context/context";
import { getColorByBrightness } from "../util/ColorUtil";
2021-07-04 00:45:00 +03:00
2022-06-04 14:49:34 +03:00
function getInfoPanelTextByGameState(params: {
context: Context;
2022-07-14 13:23:15 +03:00
game?: MancalaGame;
crashMessage?: string;
userKey?: string;
userKeyWhoLeave?: string;
2022-06-04 14:49:34 +03:00
searchingOpponent: boolean;
2022-07-14 09:05:24 +03:00
}): string | undefined {
2022-06-04 14:49:34 +03:00
const {
context,
game,
crashMessage,
userKey,
userKeyWhoLeave,
searchingOpponent,
} = params;
if (searchingOpponent) {
return context.texts.SearchingOpponent + " " + context.texts.PleaseWait;
} else if (crashMessage) {
return context.texts.GameCrashed + " " + crashMessage;
} else if (userKeyWhoLeave) {
let message = context.texts.OpponentLeavesTheGame;
if (userKeyWhoLeave == userKey) {
message = context.texts.YouLeftTheGame;
}
return message;
} else if (game?.state == "ended") {
const wonPlayer = game.getWonPlayerId();
let whoWon =
game.getWonPlayerId() === userKey
? context.texts.YouWon
: context.texts.YouLost;
if (!wonPlayer) {
whoWon = context.texts.GameDraw;
}
return context.texts.GameEnded + " " + whoWon;
} else {
2022-07-09 13:12:22 +03:00
if (game) {
2022-07-14 13:23:15 +03:00
return userKey ? game.checkIsPlayerTurn(userKey)
2022-07-09 13:12:22 +03:00
? context.texts.YourTurn
2022-07-14 13:23:15 +03:00
: context.texts.OpponentTurn : undefined;
2022-07-09 13:12:22 +03:00
}
2022-06-04 14:49:34 +03:00
}
return undefined;
}
2021-07-04 00:45:00 +03:00
2022-06-04 14:49:34 +03:00
const InfoPanelContainer: FunctionComponent<{
context: Context;
color: string;
}> = (props) => {
return (
<div
style={{
background: props.color,
padding: "1vw 2vw",
marginTop: "1vw",
borderRadius: "10vw",
}}
>
{props.children}
</div>
);
};
2021-07-04 01:23:10 +03:00
2022-06-04 14:49:34 +03:00
const InfoPanel: FunctionComponent<{
context: Context;
2022-07-14 13:23:15 +03:00
game?: MancalaGame;
crashMessage?: string;
userKey?: string;
userKeyWhoLeave?: string;
2022-06-04 14:49:34 +03:00
searchingOpponent: boolean;
}> = ({
context,
game,
crashMessage,
userKey,
userKeyWhoLeave,
searchingOpponent,
}) => {
2022-07-14 13:23:15 +03:00
const isUserTurn = userKey ? game?.checkIsPlayerTurn(userKey) : false;
2022-07-09 13:12:22 +03:00
const containerColor = isUserTurn
? context.themeManager.theme.playerTurnColor
: context.themeManager.theme.boardColor;
const textColor = getColorByBrightness(
2022-07-09 13:12:22 +03:00
containerColor,
2022-07-13 15:21:30 +03:00
context.themeManager.theme.textColor,
context.themeManager.theme.textLightColor
2022-07-09 13:12:22 +03:00
);
const text = getInfoPanelTextByGameState({
context,
game,
crashMessage,
userKey,
userKeyWhoLeave,
searchingOpponent,
});
if (text) {
return (
<InfoPanelContainer context={context} color={containerColor}>
<h4 style={{ margin: "0", color: textColor }}>
{text}
</h4>
</InfoPanelContainer>
);
} else {
return (<div></div>)
}
};
2021-07-04 00:45:00 +03:00
2022-06-04 14:49:34 +03:00
export default InfoPanel;