limit construction/training queues to 2
[browser-rts.git] / src / repository / training-queue.ts
1 import { v4 as uuid } from 'uuid';
2 import { Repository } from './base';
3
4 export enum Unit {
5     SOLDIERS = 'SOLDIERS',
6     ATTACKERS = 'ATTACKERS',
7     DEFENDERS = 'DEFENDERS',
8     SPECIAL_ATTACKERS = 'SPECIAL_ATTACKERS',
9     SPECIAL_DEFENDERS = 'SPECIAL_DEFENDERS'
10 }
11
12 export type UnitTrainingQueue = {
13     id: string;
14     owner: string;
15     unit_type: string;
16     amount: number;
17     created: number;
18     due: number;
19 };
20
21 export type UnitTrainingQueueWithName = {
22   display: string;
23 } & UnitTrainingQueue;
24
25 export class UnitTrainingQueueRepository extends Repository<UnitTrainingQueue> {
26     constructor() {
27         super('unit_training_queue');
28     }
29
30     async create(owner: string, due: number, type: string, amount: number): Promise<UnitTrainingQueue> {
31         const data: UnitTrainingQueue = {
32             id: uuid(),
33             unit_type: type,
34             created: Date.now(),
35             amount,
36             due,
37             owner
38         }
39
40         await this.Insert(data);
41         return data;
42     }
43
44     list(owner: string): Promise<UnitTrainingQueueWithName[]> {
45       return this.db.raw(`select q.*, u.display from unit_training_queue q join units u on u.slug = q.unit_type where owner = ? order by due`, owner);
46     }
47 }