Battler-Generator/party.ts

84 lines
1.9 KiB
TypeScript
Raw Normal View History

2024-04-10 21:47:46 +00:00
// Making a whole new file for this, that's how complex it looks like it'll be.
import { Canvas, createCanvas, loadImage, GlobalFonts, SKRSContext2D } from "@napi-rs/canvas";
import {
GenerateBattlerOptions,
GenerateBattleImageOptions,
PlayerActionOptions,
Colour,
Opponents,
PlayerActions,
CustomActionOptions
} from "./types";
import {
generateBattler, drawText, applyText,
} from "./utils";
type Rank = "Beginner" | "Casual" | "Pro" | "Master";
type PartyOptions = {
ready: string;
player1: string;
username1: string;
rank1: Rank;
player2?: string;
username2?: string;
rank2?: Rank;
player3?: string;
username3?: string;
rank3?: Rank;
player4?: string;
username4?: string;
rank4?: Rank;
player5?: string;
username5?: string;
rank5?: Rank;
player6?: string;
username6?: string;
rank6?: Rank;
player7?: string;
username7?: string;
rank7?: Rank;
player8?: string;
username8?: string;
rank8?: Rank;
}
function generateMatchType(opts: PartyOptions): string {
const keys: (keyof PartyOptions)[] = [
'player1', 'player2', 'player3', 'player4', 'player5', 'player6', 'player7', 'player8'
];
let playerCount = keys.reduce((count, key) => opts[key] ? count + 1 : count, 0);
// If there's an odd number of players, round up to the next even number
if (playerCount % 2 !== 0) {
playerCount++;
}
// Divide by 2 to get the number for each team, since it's evenly matched (e.g. "1v1", "2v2")
const teamSize = playerCount / 2;
return `${teamSize}v${teamSize}`;
}
export default async function party(request: Request, url: import("url").URL): Promise<Response> {
const opts = Object.fromEntries(
Array.from(url.searchParams.entries())
.map(([key, value]) => [key, value.replaceAll('+', ' ')]),
) as PartyOptions;
if (!opts.player1 && !opts.username1 && !opts.rank1) {
return Response.json({
error: "At least one player must be in the party"
})
}
}