Updates and more

This commit is contained in:
2023-11-05 22:27:20 +01:00
parent e97e86f9ac
commit 7450ecdabb
48 changed files with 565 additions and 111 deletions

View File

@@ -0,0 +1,53 @@
import type {Player, SchematicType, Server} from "../types/data.js";
import {PlayerSchema, ServerSchema} from "../types/data.js";
import {cached, cachedFamily} from "./cached.js";
import type {Team} from "../types/team.js";
import {TeamSchema} from "../types/team.js";
import {derived, get, readable, writable} from "svelte/store";
import {dataRepo, fetchWithToken, pageRepo, tokenStore} from "../repo/repo.js";
import {z} from "zod";
export const schemTypes = cached<SchematicType[]>([], () =>
fetchWithToken(get(tokenStore), `/data/schematicTypes`)
.then(res => res.json()))
export const players = cached<Player[]>([], async () => {
const res = await fetchWithToken(get(tokenStore), `/data/users`);
return z.array(PlayerSchema).parse(await res.json());
})
export const gamemodes = cached<string[]>([], async () => {
const res = await fetchWithToken(get(tokenStore), `/data/gamemodes`);
return z.array(z.string()).parse(await res.json());
})
export const maps = cachedFamily<string, string[]>([], async (gamemode) => {
if (get(gamemodes).every(value => value !== gamemode)) return [];
const res = await fetchWithToken(get(tokenStore), `/data/gamemodes/${gamemode}/maps`);
if (!res.ok) {
return [];
} else {
return res.json();
}
})
export const groups = cached<string[]>([], async () => {
const res = await fetchWithToken(get(tokenStore), `/data/groups`);
return z.array(z.string()).parse(await res.json());
})
export const teams = cached<Team[]>([], async () => {
const res = await fetchWithToken(get(tokenStore), `/team`);
return z.array(TeamSchema).parse(await res.json());
})
export const branches = cached<string[]>([], async () => {
const res = await get(pageRepo).getBranches();
return z.array(z.string()).parse(res);
})
export const server = readable(new Promise((resolve) => {}), (set) => set(get(dataRepo).getServer()));
export const isWide = writable(window.innerWidth >= 640);
window.addEventListener("resize", () => isWide.set(window.innerWidth >= 640));