-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interaction.ts
75 lines (60 loc) · 1.67 KB
/
Interaction.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import {
DataObject,
IDataObject,
} from '@civ-clone/core-data-object/DataObject';
import {
RuleRegistry,
instance as ruleRegistryInstance,
} from '@civ-clone/core-rule/RuleRegistry';
import Player from '@civ-clone/core-player/Player';
import Created from './Rules/Interaction/Created';
import {
Turn,
instance as turnInstance,
} from '@civ-clone/core-turn-based-game/Turn';
export interface IInteraction extends IDataObject {
isBetween(...players: Player[]): boolean;
players(): Player[];
when(): number;
}
export class Interaction extends DataObject implements IInteraction {
#players: Set<Player> = new Set();
#ruleRegistry: RuleRegistry = ruleRegistryInstance;
#turn: Turn = turnInstance;
#when: number;
constructor(...args: (Player | RuleRegistry | Turn)[]) {
super();
args.forEach((arg) => {
if (arg instanceof Player) {
this.#players.add(arg);
}
if (arg instanceof RuleRegistry) {
this.#ruleRegistry = arg;
}
if (arg instanceof Turn) {
this.#turn = arg;
}
});
this.#when = this.#turn.value();
this.addKey('players', 'when');
this.#ruleRegistry.process(Created, this as Interaction);
}
isBetween(...players: Player[]): boolean {
const uniquePlayers = Array.from(new Set(players));
return (
uniquePlayers.every((player: Player): boolean =>
this.#players.has(player)
) && uniquePlayers.length === this.#players.size
);
}
players(): Player[] {
return Array.from(this.#players);
}
protected ruleRegistry(): RuleRegistry {
return this.#ruleRegistry;
}
when(): number {
return this.#when;
}
}
export default Interaction;