maplist when generating matches

This commit is contained in:
Kalle (Sendou) 2021-01-26 00:24:10 +02:00
parent c6f4574c69
commit e65d08a30e
3 changed files with 54 additions and 15 deletions

6
lib/shuffleArray.ts Normal file
View File

@ -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);
};

View File

@ -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;

View File

@ -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<RankedMode, string[]>, [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.";
}