import ms from "pretty-ms"; import { useCallback, useState } from "react"; import { createGame } from "../../../domain/apiMethods"; import { RANDOM_STREET_VIEW, URBAN } from "../../../domain/genMethods"; import { FROZEN, NORMAL, TIME_BANK, RACE, COUNTRY_RACE, } from "../../../domain/ruleSets"; import useCountryLookup from "../../../hooks/useCountryLookup"; import Loading from "../Loading"; import { Dropdown, DropdownGroup, Item, CountryDropdown } from "./Dropdown"; import ErrorModal from "./ErrorModal"; import styles from "./GameCreationForm.module.css"; const DEFAULTS = { timer: 300, rounds: 5, countryLock: null, genMethod: RANDOM_STREET_VIEW, ruleSet: NORMAL, }; const PRESETS = { URBAN_AMERICA: { ...DEFAULTS, genMethod: URBAN, countryLock: "us", }, URBAN_GLOBAL: { ...DEFAULTS, genMethod: URBAN, }, FAST_FROZEN: { ...DEFAULTS, timer: 30, rounds: 3, genMethod: RANDOM_STREET_VIEW, ruleSet: FROZEN, }, }; const GameCreationForm = ({ afterCreate }) => { const [loading, setLoading] = useState(false); const [creationError, setCreationError] = useState(false); const [timer, setTimer] = useState(DEFAULTS.timer); const [rounds, setRounds] = useState(DEFAULTS.rounds); const [countryLock, setCountryLock] = useState(DEFAULTS.countryLock); const [genMethod, setGenMethod] = useState(DEFAULTS.genMethod); const [ruleSet, setRuleSet] = useState(DEFAULTS.ruleSet); const countryLookup = useCountryLookup(genMethod); const setPreset = useCallback( ({ timer: newTimer, rounds: newRounds, countryLock: newCountryLock, genMethod: newGenMethod, ruleSet: newRuleSet, }) => { setTimer(newTimer); setRounds(newRounds); setCountryLock(newCountryLock); setGenMethod(newGenMethod); setRuleSet(newRuleSet); }, [] ); if (loading || countryLookup === null) { return ; } const onCreateGame = async () => { setLoading(true); let gameId; try { gameId = await createGame(timer, rounds, countryLock, genMethod, ruleSet); } catch (e) { setCreationError(true); setLoading(false); return; } if (afterCreate) { afterCreate(gameId); } }; return (
setCreationError(false)} />
Default Urban America Urban Global Fast Frozen 30 Seconds 2 Minutes 5 Minutes 1 Hour 1 Round 3 Rounds 5 Rounds 10 Rounds Random Street View Urban Centers Normal Time Bank Frozen Race Country Race
); }; export default GameCreationForm;