From e65d08a30e2d4973b4afced8056151bcf4f33b46 Mon Sep 17 00:00:00 2001 From: "Kalle (Sendou)" <38327916+Sendouc@users.noreply.github.com> Date: Tue, 26 Jan 2021 00:24:10 +0200 Subject: [PATCH] maplist when generating matches --- lib/shuffleArray.ts | 6 ++++++ pages/api/play/matches.ts | 41 ++++++++++++++++++++++++++++++++++++++- pages/maps.tsx | 22 ++++++++------------- 3 files changed, 54 insertions(+), 15 deletions(-) create mode 100644 lib/shuffleArray.ts diff --git a/lib/shuffleArray.ts b/lib/shuffleArray.ts new file mode 100644 index 000000000..93dda2a1d --- /dev/null +++ b/lib/shuffleArray.ts @@ -0,0 +1,6 @@ +export const shuffleArray = (array: string[]) => { + return array + .map((a) => ({ sort: Math.random(), value: a })) + .sort((a, b) => a.sort - b.sort) + .map((a) => a.value); +}; diff --git a/pages/api/play/matches.ts b/pages/api/play/matches.ts index 8b6a4d6b1..88995ac4b 100644 --- a/pages/api/play/matches.ts +++ b/pages/api/play/matches.ts @@ -1,5 +1,6 @@ import { getMySession } from "lib/getMySession"; import { getLadderRounds } from "lib/playFunctions"; +import { shuffleArray } from "lib/shuffleArray"; import { NextApiRequest, NextApiResponse } from "next"; import { getAllLadderRegisteredTeamsForMatches } from "prisma/queries/getAllLadderRegisteredTeamsForMatches"; @@ -50,12 +51,15 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) { // ) // ); + const eighteenMaps = getMaplist(); + res.status(200).json( matches.flatMap((round, i) => round.map((match) => ({ data: { date: "", - maplist: {}, + maplist: + i === 0 ? eighteenMaps.slice(0, 9) : eighteenMaps.slice(9, 18), order: i + 1, players: { create: match.flatMap((team, teamI) => @@ -71,4 +75,39 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) { ); } +function getMaplist() { + const modes = shuffleArray(["SZ", "TC", "RM", "CB"]); + const stages = shuffleArray([ + "The Reef", + "Musselforge Fitness", + "Starfish Mainstage", + "Humpback Pump Track", + "Inkblot Art Academy", + "Sturgeon Shipyard", + "Manta Maria", + "Snapper Canal", + "Blackbelly Skatepark", + "MakoMart", + "Shellendorf Institute", + "Goby Arena", + "Piranha Pit", + "Camp Triggerfish", + "Wahoo World", + "New Albacore Hotel", + "Ancho-V Games", + "Skipper Pavilion", + //"Moray Towers", + //"Port Mackerel", + //"Walleye Warehouse", + //"Arowana Mall", + //"Kelp Dome" + ]); + + return stages.map((stage) => { + const mode = modes.pop(); + modes.unshift(mode!); + return { stage, mode }; + }); +} + export default matchesHandler; diff --git a/pages/maps.tsx b/pages/maps.tsx index 8abb6e1c0..628078cb5 100644 --- a/pages/maps.tsx +++ b/pages/maps.tsx @@ -28,6 +28,7 @@ import MyContainer from "components/common/MyContainer"; import SubText from "components/common/SubText"; import { stages } from "lib/lists/stages"; import { setManySearchParams } from "lib/setSearchParams"; +import { shuffleArray } from "lib/shuffleArray"; import { useRouter } from "next/router"; import { ChangeEvent, Fragment, useEffect, useState } from "react"; import { FiCheck, FiFilter, FiRotateCw } from "react-icons/fi"; @@ -108,13 +109,6 @@ const MapsGeneratorPage = () => { setManySearchParams(poolForUrl(newStagesSelected)); }; - const shuffled = (array: string[]) => { - return array - .map((a) => ({ sort: Math.random(), value: a })) - .sort((a, b) => a.sort - b.sort) - .map((a) => a.value); - }; - const generateMaps = () => { let modeStages = Object.entries(stagesSelected).reduce( (acc: Record, [stage, modes]) => { @@ -125,10 +119,10 @@ const MapsGeneratorPage = () => { ); modeStages = { - SZ: shuffled(modeStages.SZ), - TC: shuffled(modeStages.TC), - RM: shuffled(modeStages.RM), - CB: shuffled(modeStages.CB), + SZ: shuffleArray(modeStages.SZ), + TC: shuffleArray(modeStages.TC), + RM: shuffleArray(modeStages.RM), + CB: shuffleArray(modeStages.CB), }; const modesFromGenerationMode = @@ -136,9 +130,9 @@ const MapsGeneratorPage = () => { ? ["TC", "RM", "CB"] : ["SZ", "TC", "RM", "CB"]; - const modes = (shuffled(modesFromGenerationMode) as RankedMode[]).filter( - (mode) => modeStages[mode].length > 0 - ); + const modes = (shuffleArray( + modesFromGenerationMode + ) as RankedMode[]).filter((mode) => modeStages[mode].length > 0); if (modes.length === 0) { return "I can't generate a maplist without any maps in it you know."; }