Skip to content

Commit

Permalink
strategy configs
Browse files Browse the repository at this point in the history
  • Loading branch information
tsaxking committed Oct 28, 2024
1 parent a025650 commit 72afd68
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 117 deletions.
20 changes: 20 additions & 0 deletions client/models/FIRST/strategy-config.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -34,4 +36,22 @@ export class StrategyConfig extends Cache<StrategyConfigEvents> {
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<Omit<SC, 'id' | 'strategyId'>>) {
const { team, type, value } = data;
return ServerRequest.post('/api/strategy/update-config', {
id: this.id,
team,
type,
value
});
}
}
89 changes: 0 additions & 89 deletions client/models/FIRST/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,6 @@ export class Strategy extends Cache<StrategyUpdateData> {
});
}

getChecks() {
return attemptAsync(async () => {
return (await Check.from(this)).unwrap();
});
}
select() {
Strategy.current = this;
Strategy.emit('select', this);
Expand All @@ -181,90 +176,6 @@ export class Strategy extends Cache<StrategyUpdateData> {
}
}

type CheckEvents = {
'new-check': string;
'remove-check': string;
};

export class Check extends EventEmitter<CheckEvents> {
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);
Expand Down
29 changes: 1 addition & 28 deletions client/views/pages/strategy/Strategy.svelte
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<script lang="ts">
import { FIRSTMatch } from '../../../models/FIRST/match';
import { Check, Strategy } from '../../../models/FIRST/strategy';
import { Strategy } from '../../../models/FIRST/strategy';
import { onMount } from 'svelte';
import { alert, prompt, select } from '../../../utilities/notifications';
import { FIRSTAlliance } from '../../../models/FIRST/alliance';
import Alliance from '../../components/strategy/2024Alliance.svelte';
import { type MatchInterface } from '../../../models/FIRST/interfaces/match';
// import { dateString } from '../../../../shared/clock';
// import { Loop } from '../../../../shared/loop';
import Checks from '../../components/strategy/Checks.svelte';
import Comment from '../../components/strategy/Comment.svelte';
import MatchSelect from '../../components/main/MatchSelect.svelte';
import StrategySelect from '../../components/strategy/StrategySelect.svelte';
Expand All @@ -28,7 +27,6 @@
let red: FIRSTAlliance | undefined;
let blue: FIRSTAlliance | undefined;
let match: MatchInterface | undefined;
let checks: Check[] = [];
// let currentTime = date();
Expand Down Expand Up @@ -84,14 +82,6 @@
const selectStrategy = async (s: Strategy) => {
if (!match) return console.error('Strategy: match not selected');
strategy = s;
// s.select();
const c = await s.getChecks();
if (c.isErr()) {
return console.error(c.error);
}
checks = c.value;
};
onMount(() => {
Expand Down Expand Up @@ -157,23 +147,6 @@
<div class="row mb-3">
<Comment bind:strategy />
</div>
<div class="row mb-3">
<!-- Checks -->
<div class="col-md-6">
<Checks checks="{checks[0]}" />
<hr />
<Checks checks="{checks[1]}" />
<hr />
<Checks checks="{checks[2]}" />
</div>
<div class="col-md-6">
<Checks checks="{checks[3]}" />
<hr />
<Checks checks="{checks[4]}" />
<hr />
<Checks checks="{checks[5]}" />
</div>
</div>
{:else}
<p>No strategy selected</p>
{/if}
Expand Down

0 comments on commit 72afd68

Please sign in to comment.