From 72afd68009a08323624d3918106d50cbc6527778 Mon Sep 17 00:00:00 2001 From: taylor Date: Sun, 27 Oct 2024 19:31:11 -0600 Subject: [PATCH] strategy configs --- client/models/FIRST/strategy-config.ts | 20 +++++ client/models/FIRST/strategy.ts | 89 --------------------- client/views/pages/strategy/Strategy.svelte | 29 +------ 3 files changed, 21 insertions(+), 117 deletions(-) diff --git a/client/models/FIRST/strategy-config.ts b/client/models/FIRST/strategy-config.ts index 48e7986cd..460cb8ef1 100644 --- a/client/models/FIRST/strategy-config.ts +++ b/client/models/FIRST/strategy-config.ts @@ -1,6 +1,8 @@ import { Cache } from "../cache"; import { StrategyConfigs as SC } from "../../../server/utilities/tables"; import { EventEmitter } from "../../../shared/event-emitter"; +import { attemptAsync } from "../../../shared/check"; +import { ServerRequest } from "../../utilities/requests"; type StrategyConfigEvents = { update: undefined; @@ -34,4 +36,22 @@ export class StrategyConfig extends Cache { this.type = config.type; this.value = config.value; } + + delete() { + return attemptAsync(async () => { + await ServerRequest.post('/api/strategy/delete-config', { + id: this.id + }); + }); + } + + update(data: Partial>) { + const { team, type, value } = data; + return ServerRequest.post('/api/strategy/update-config', { + id: this.id, + team, + type, + value + }); + } } \ No newline at end of file diff --git a/client/models/FIRST/strategy.ts b/client/models/FIRST/strategy.ts index 1dfdfc6b7..e5de98c4d 100644 --- a/client/models/FIRST/strategy.ts +++ b/client/models/FIRST/strategy.ts @@ -166,11 +166,6 @@ export class Strategy extends Cache { }); } - getChecks() { - return attemptAsync(async () => { - return (await Check.from(this)).unwrap(); - }); - } select() { Strategy.current = this; Strategy.emit('select', this); @@ -181,90 +176,6 @@ export class Strategy extends Cache { } } -type CheckEvents = { - 'new-check': string; - 'remove-check': string; -}; - -export class Check extends EventEmitter { - public static readonly checks = [ - 'Start: Amp', - 'Start: Center', - 'Start: Source', - 'Auto: 3 Close Note', - 'Auto: 1 Close, 3 centerline', - 'Defense', - 'Finisher', - 'Lobber', - 'Amper', - 'Trap' - ]; - - public static from(strategy: Strategy) { - return attemptAsync(async () => { - const data = strategy.checks; - const teams = (await strategy.getTeams()) - .unwrap() - .filter(Boolean) - .map(t => t.number); - const checks = data.map(d => d.split(':') as [string, string]); - - return teams.map(t => { - const c = checks.filter(c => +c[0] === t).map(c => c.slice(1).join(':')); - return new Check(t, c, strategy); - }) as [Check, Check, Check, Check, Check, Check]; - }); - } - - constructor( - public readonly team: number, - public checks: string[], - public readonly strategy: Strategy - ) { - super(); - } - - private change() { - return attemptAsync(async () => { - const data = this.serialize(); - await this.strategy.update({ - checks: [...data, ...this.strategy.checks] - // Remove duplicates - .filter((c, i, a) => a.indexOf(c) === i) - }); - }); - } - - serialize(): string[] { - // ['2122:check'] - return this.checks.map(c => `${this.team}:${c}`); - } - - add(check: string) { - this.checks.push(check); - this.emit('new-check', check); - return this.change(); - } - - remove(check: string) { - this.checks = this.checks.filter(c => c !== check); - this.strategy.checks = this.strategy.checks.filter(c => c !== `${this.team}:${check}`); - this.emit('remove-check', check); - return this.change(); - } - - toggle(check: string) { - if (this.has(check)) { - return this.remove(check); - } - return this.add(check); - } - - has(check: string) { - return this.checks.includes(check); - } -} - socket.on('strategy:new', async (data: S) => { const s = Strategy.retrieve(data); Strategy.emit('new', s); diff --git a/client/views/pages/strategy/Strategy.svelte b/client/views/pages/strategy/Strategy.svelte index 18b90d42d..8afe81e93 100644 --- a/client/views/pages/strategy/Strategy.svelte +++ b/client/views/pages/strategy/Strategy.svelte @@ -1,6 +1,6 @@