src/functions/magazines.ts (view raw)
1import path from 'node:path';
2const { magazineSize } = require(path.join(process.cwd(), 'config.json'));
3
4const magazines = new Map<string, Magazine>();
5
6function sameDay(date1: Date, date2: Date): boolean {
7 return (
8 date1.getFullYear() === date2.getFullYear() &&
9 date1.getMonth() === date2.getMonth() &&
10 date1.getDate() === date2.getDate()
11 );
12 }
13
14export class Magazine {
15 #size: number;
16 #left: number;
17 #last: Date;
18
19 constructor(size: number) {
20 this.#size = size;
21 this.#left = size;
22 this.#last = new Date();
23 }
24
25 update() {
26 const now = new Date();
27 if (!sameDay(now, this.#last))
28 this.#left = this.#size;
29 }
30
31 get left() {
32 this.update();
33 return this.#left;
34 }
35
36 get size() {
37 return this.#size
38 }
39
40 shoot() {
41 this.update();
42 if (this.#left <= 0) {
43 return false;
44 }
45
46 this.#last = new Date();
47 this.#left--;
48 return true;
49 }
50}
51
52export function getMagazine(user: string): Magazine {
53 if (magazines.has(user))
54 return magazines.get(user);
55
56 const q = new Magazine(magazineSize ?? 6);
57 magazines.set(user, q);
58 return q;
59}