mancala/apps/mobile/src/screens/HomeScreen.tsx
Halit Aksoy d1918fed10 feat(mobile): offer games against the bot
The lobby offers the bot levels after 10 seconds without an opponent, the
home screen has Play with bot, the bot is shown with its level, and a new
game after a bot game is one at the same level.
2026-10-16 14:23:22 +03:00

79 lines
3.1 KiB
TypeScript

import * as React from 'react';
import { View, Button, Pressable, Text } from 'react-native';
import { useTranslation } from 'react-i18next';
import { HomeScreenProps } from '../types';
import CircularPanel from '../components/CircularPanel';
import BotLevelPicker from '../components/BotLevelPicker';
import { SvgXml } from 'react-native-svg';
import helpSvg from '../svg/help';
import { getColorByBrightness } from "@mancala/core";
export function HomeScreen({ navigation, route }: HomeScreenProps) {
const { context } = route.params;
const { t } = useTranslation();
const [lastGameId, setLastGameId] = React.useState<string | undefined>(undefined);
const [choosingBotLevel, setChoosingBotLevel] = React.useState(false);
const textColorOnAppBar = getColorByBrightness(
context.themeManager.theme.appBarBgColor,
context.themeManager.theme.textColor,
context.themeManager.theme.textLightColor
);
const updateHeaderButton = () => {
navigation.setOptions({
headerRight: () => (
<Pressable onPress={() => {
navigation.push("Help", { context })
}}>
<SvgXml xml={helpSvg.replaceAll("customColor", textColorOnAppBar)} width="24" height="24" />
</Pressable>
),
});
}
React.useEffect(() => {
updateHeaderButton();
const savedGameId = context.storage.getString('lastGameId');
if (savedGameId) {
setLastGameId(savedGameId);
}
}, []);
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center', backgroundColor: context.themeManager.theme?.background }}>
<Pressable onPress={() => navigation.replace('Loby', { context })}>
<CircularPanel color={context.themeManager.theme?.boardColor} children={<Text style={{ color: context.themeManager.theme.textColor }}>{t('NewGame')}</Text>} />
</Pressable>
<View style={{ height: 20 }} />
{choosingBotLevel ? (
<View style={{ alignItems: 'center', gap: 10, paddingHorizontal: 16 }}>
<Text style={{ color: context.themeManager.theme.textColor }}>{t('ChooseBotLevel')}</Text>
<BotLevelPicker context={context} onChoose={(botLevel) => navigation.replace('Loby', { context, botLevel })} />
</View>
) : (
<Pressable onPress={() => setChoosingBotLevel(true)}>
<CircularPanel color={context.themeManager.theme?.boardColor} children={<Text style={{ color: context.themeManager.theme.textColor }}>{t('PlayWithBot')}</Text>} />
</Pressable>
)}
{lastGameId && (
<>
<View style={{ height: 20 }} />
<Pressable onPress={async () => {
try {
const userKey = await context.userKeyStore.getUserKey();
navigation.replace('Game', { context, gameId: lastGameId, userKey });
} catch (error) {
context.storage.deleteString('lastGameId');
setLastGameId(undefined);
}
}}>
<CircularPanel color={context.themeManager.theme?.boardColor} children={<Text style={{ color: context.themeManager.theme.textColor }}>{t('RejoinGame')}</Text>} />
</Pressable>
</>
)}
</View>
);
}