Skip to content

Commit

Permalink
strategy config
Browse files Browse the repository at this point in the history
  • Loading branch information
tsaxking committed Sep 16, 2024
1 parent 04210ad commit a88a016
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 6 deletions.
37 changes: 37 additions & 0 deletions client/models/FIRST/strategy-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Cache } from "../cache";
import { StrategyConfigs as SC } from "../../../server/utilities/tables";
import { EventEmitter } from "../../../shared/event-emitter";

type StrategyConfigEvents = {
update: undefined;
};

type GlobalStrategyConfigEvents = {
new: StrategyConfig;
};

export class StrategyConfig extends Cache<StrategyConfigEvents> {
public static readonly emitter = new EventEmitter<GlobalStrategyConfigEvents>();

public static on = StrategyConfig.emitter.on.bind(StrategyConfig.emitter);
public static off = StrategyConfig.emitter.off.bind(StrategyConfig.emitter);
public static once = StrategyConfig.emitter.once.bind(StrategyConfig.emitter);
public static emit = StrategyConfig.emitter.emit.bind(StrategyConfig.emitter);



public readonly id: string;
public readonly strategyId: string;
public team: number;
public type: string;
public value: string;

constructor(config: SC) {
super();
this.id = config.id;
this.strategyId = config.strategyId;
this.team = config.team;
this.type = config.type;
this.value = config.value;
}
}
72 changes: 72 additions & 0 deletions server/routes/api/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Route } from '../../structure/app/app';
import { validate } from '../../middleware/data-type';
import { Strategy } from '../../structure/cache/strategy';
import { CompLevel } from '../../../shared/db-types-extended';
import { StrategyConfig } from '../../structure/cache/strategy-configs';

export const router = new Route();

Expand Down Expand Up @@ -128,3 +129,74 @@ router.post<{
req.io.emit('strategy:update', s);
}
);

router.post<{
strategyId: string;
team: number;
type: string;
value: string;
}>('/new-config', validate({
strategyId: 'string',
team: 'number',
type: 'string',
value: 'string'
}), async (req, res) => {
const { strategyId, team, type, value } = req.body;
const strategy = (await Strategy.fromId(strategyId)).unwrap();
if (!strategy) return res.sendStatus('strategy:not-found');

const config = (await strategy.newConfig({ team, type, value })).unwrap();

res.sendStatus('strategy-config:new');
req.io.emit('strategy-config:new', config);
});

router.post<{
id: string;
team: number;
type: string;
value: string;
}>('/update-config', validate({
id: 'string',
team: 'number',
type: 'string',
value: 'string'
}), async (req, res) => {
const { id, team, type, value } = req.body;
const config = (await StrategyConfig.fromId(id)).unwrap();
if (!config) return res.sendStatus('strategy-config:not-found');

await config.update({ team, type, value });

res.sendStatus('strategy-config:updated');
req.io.emit('strategy-config:update', config);
});

router.post<{
id: string;
}>('/delete-config', validate({
id: 'string',
}), async (req, res) => {
const { id } = req.body;
const config = (await StrategyConfig.fromId(id)).unwrap();
if (!config) return res.sendStatus('strategy-config:not-found');

await config.delete();

res.sendStatus('strategy-config:deleted');
req.io.emit('strategy-config:delete', config);
});

router.post<{
strategyId: string;
}>('/get-configs', validate({
strategyId: 'string',
}), async (req, res) => {
const { strategyId } = req.body;
const strategy = (await Strategy.fromId(strategyId)).unwrap();
if (!strategy) return res.sendStatus('strategy:not-found');

const configs = (await strategy.getConfigs()).unwrap();

res.json(configs);
});
8 changes: 4 additions & 4 deletions server/structure/cache/strategy-configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { DB } from '../../utilities/databases';
import { Strategy } from './strategy';


export class StrategyConfigs extends Cache {
export class StrategyConfig extends Cache {
public static new(config: Omit<SC, 'id'>) {
return attemptAsync(async () => {
const id = uuid();
Expand All @@ -15,7 +15,7 @@ export class StrategyConfigs extends Cache {
id
})).unwrap();

return new StrategyConfigs({
return new StrategyConfig({
id,
...config
});
Expand All @@ -25,14 +25,14 @@ export class StrategyConfigs extends Cache {
public static fromId(id: string) {
return attemptAsync(async () => {
const config = (await DB.run('strategy-configs/from-id', { id })).unwrap();
return new StrategyConfigs(config);
return new StrategyConfig(config);
});
}

public static fromStrategyId(strategyId: string) {
return attemptAsync(async () => {
const configs = (await DB.all('strategy-configs/from-strategy', { strategyId })).unwrap();
return configs.map(config => new StrategyConfigs(config));
return configs.map(config => new StrategyConfig(config));
});
}

Expand Down
10 changes: 8 additions & 2 deletions server/structure/cache/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { attemptAsync } from '../../../shared/check';
import { CompLevel } from '../../../shared/db-types-extended';
import { uuid } from '../../utilities/uuid';
import { FIRSTWhiteboard } from './whiteboards';
import { StrategyConfigs } from './strategy-configs';
import { StrategyConfig } from './strategy-configs';
import { StrategyConfigs as SC } from '../../utilities/tables';

export class Strategy extends Cache {
public static fromId(id: string) {
Expand Down Expand Up @@ -85,6 +86,11 @@ export class Strategy extends Cache {
}

getConfigs() {
return StrategyConfigs.fromStrategyId(this.id);
return StrategyConfig.fromStrategyId(this.id);
}


newConfig(data: Omit<SC, 'id' | 'strategyId'>) {
return StrategyConfig.new({ ...data, strategyId: this.id });
}
}

0 comments on commit a88a016

Please sign in to comment.