From 47f28d1ca8648618e4ff34f3f05128c402e8992f Mon Sep 17 00:00:00 2001 From: "Kalle (Sendou)" <38327916+Sendouc@users.noreply.github.com> Date: Thu, 31 Dec 2020 15:45:43 +0200 Subject: [PATCH] maps feature complete --- components/layout/IconNavBar.tsx | 15 +- pages/maps.tsx | 313 ++++++++++++++++++++++--------- 2 files changed, 235 insertions(+), 93 deletions(-) diff --git a/components/layout/IconNavBar.tsx b/components/layout/IconNavBar.tsx index fedb4b985..11cf17a3e 100644 --- a/components/layout/IconNavBar.tsx +++ b/components/layout/IconNavBar.tsx @@ -80,7 +80,20 @@ export const navIcons: { { code: "u", displayName: t`User Search`, menuItems: [] }, { code: "freeagents", displayName: t`Free Agents`, menuItems: [] }, // { code: "teams", displayName: t`Teams`, menuItems: [] }, - { code: "plans", displayName: t`Map Planner`, menuItems: [] }, + { + code: "plans", + displayName: t`Maps`, + menuItems: [ + { + code: "plans", + displayName: t`Map Planner`, + }, + { + code: "maps", + displayName: t`Maplist Generator`, + }, + ], + }, { code: "tournaments", displayName: t`Tournaments`, menuItems: [] }, { code: "plus", diff --git a/pages/maps.tsx b/pages/maps.tsx index 8eb5365b8..541efadc1 100644 --- a/pages/maps.tsx +++ b/pages/maps.tsx @@ -9,9 +9,12 @@ import { Input, InputGroup, InputRightElement, + Radio, + RadioGroup, + Stack, Textarea, } from "@chakra-ui/react"; -import { Trans } from "@lingui/macro"; +import { t, Trans } from "@lingui/macro"; import { useLingui } from "@lingui/react"; import { RankedMode } from "@prisma/client"; import Breadcrumbs from "components/common/Breadcrumbs"; @@ -21,18 +24,24 @@ import SubText from "components/common/SubText"; import { stages } from "lib/lists/stages"; import { useRouter } from "next/router"; import { ChangeEvent, Fragment, useState } from "react"; +import { FiFilter, FiRotateCw } from "react-icons/fi"; const MapsGeneratorPage = () => { const router = useRouter(); const { i18n } = useLingui(); + const [stagesSelected, setStagesSelected] = useState< Record >(getInitialStages()); + const [generationMode, setGenerationMode] = useState< + "EQUAL" | "SZ_EVERY_OTHER" + >("EQUAL"); const [maplist, setMaplist] = useState(""); const [count, setCount] = useState(9); + const [editing, setEditing] = useState(false); function getInitialStages() { - return Object.entries(router.query).reduce( + const filtersFromUrl = Object.entries(router.query).reduce( (acc: Record, cur) => { if (stages.includes(cur[0]) && typeof cur[1] === "string") { // @ts-ignore @@ -44,6 +53,13 @@ const MapsGeneratorPage = () => { }, {} ); + + return Object.keys(filtersFromUrl).length + ? filtersFromUrl + : stages.reduce((acc: Record, cur) => { + acc[cur] = ["SZ", "TC", "RM", "CB"]; + return acc; + }, {}); } const poolForUrl = (stagesSelectedForUrl: Record) => { @@ -99,7 +115,12 @@ const MapsGeneratorPage = () => { CB: shuffled(modeStages.CB), }; - const modes = (shuffled(["SZ", "TC", "RM", "CB"]) as RankedMode[]).filter( + const modesFromGenerationMode = + generationMode === "SZ_EVERY_OTHER" + ? ["TC", "RM", "CB"] + : ["SZ", "TC", "RM", "CB"]; + + const modes = (shuffled(modesFromGenerationMode) as RankedMode[]).filter( (mode) => modeStages[mode].length > 0 ); if (modes.length === 0) { @@ -108,17 +129,28 @@ const MapsGeneratorPage = () => { const stagesAlreadyPicked = new Set(); + const isSZFirst = Math.random() > 0.5; + return new Array(count) .fill(null) .map((_, i) => { - modes.push(modes.shift() as RankedMode); - const stageArray = modeStages[modes[0]]; + let modeOfRound: RankedMode = "SZ"; + + if ( + generationMode === "SZ_EVERY_OTHER" && + i % 2 === Number(isSZFirst) + ) { + modes.push(modes.shift() as RankedMode); + modeOfRound = modes[0]; + } + + const stageArray = modeStages[modeOfRound]; stageArray.push(stageArray.shift() as string); let shifted = 0; while ( - stagesAlreadyPicked.has(stageArray[0]) || + stagesAlreadyPicked.has(stageArray[0]) && shifted >= stageArray.length ) { stageArray.push(stageArray.shift() as string); @@ -127,103 +159,200 @@ const MapsGeneratorPage = () => { stagesAlreadyPicked.add(stageArray[0]); - return `${i + 1}) ${modes[0]} on ${stageArray[0]}`; + return `${i + 1}) ${modeOfRound} on ${stageArray[0]}`; }) .join("\n"); }; return ( - - - - - Pro tip: bookmark this page after making your map list to save it - - - - - - - - - - - - - - - - - - {stages.map((stage) => ( - - {i18n._(stage)} + + {editing ? ( + <> + + + + Pro tip: bookmark this page after making your map list to save it + + + + + + + + + + {stages.map((stage) => { + const buttonIsAdd = (stagesSelected[stage]?.length ?? 0) < 4; + return ( + + {i18n._(stage)} - - handleChange(e, "SZ", stage)} - /> - handleChange(e, "TC", stage)} - /> - handleChange(e, "RM", stage)} - /> - handleChange(e, "CB", stage)} - /> - - ))} - - - Share your map pool - - {window && ( - - - - - - + setStagesSelected(newStagesSelected); + + router.replace({ + pathname: "/maps", + query: poolForUrl(newStagesSelected), + }); + }} + > + {buttonIsAdd ? All : Clear} + + handleChange(e, "SZ", stage)} + /> + handleChange(e, "TC", stage)} + /> + handleChange(e, "RM", stage)} + /> + handleChange(e, "CB", stage)} + /> + + ); + })} + + + Share your map pool + + {window && ( + + + + + + + )} + + ) : ( + + + + + + + + + + + + + + + {Object.entries(stagesSelected).map(([stage, modes]) => + modes.includes("SZ") ? {stage} : null + )} + + + {Object.entries(stagesSelected).map(([stage, modes]) => + modes.includes("TC") ? {stage} : null + )} + + + {Object.entries(stagesSelected).map(([stage, modes]) => + modes.includes("RM") ? {stage} : null + )} + + + {Object.entries(stagesSelected).map(([stage, modes]) => + modes.includes("CB") ? {stage} : null + )} + + )} - + + + + setGenerationMode(value as "EQUAL" | "SZ_EVERY_OTHER") + } + value={generationMode} > - Generate maps - - {maplist &&