-
Notifications
You must be signed in to change notification settings - Fork 0
/
StrategyAIClient.ts
106 lines (89 loc) · 2.98 KB
/
StrategyAIClient.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { AIClient, IAIClient } from '@civ-clone/core-ai-client/AIClient';
import {
ChoiceMeta,
DataForChoiceMeta,
} from '@civ-clone/core-client/ChoiceMeta';
import {
StrategyRegistry,
instance as strategyRegistryInstance,
} from '@civ-clone/core-strategy/StrategyRegistry';
import ChooseFromList from './PlayerActions/ChooseFromList';
import Data from './PlayerActions/ChooseFromList/Data';
import DataObject from '@civ-clone/core-data-object/DataObject';
import Player from '@civ-clone/core-player/Player';
import PlayerAction from '@civ-clone/core-player/PlayerAction';
import UnhandledAction from './UnhandledAction';
const MAX_ACTIONS_PER_TURN = 10_000;
export interface IStrategyAIClient extends IAIClient {
attempt(action: PlayerAction): boolean;
}
export class StrategyAIClient extends AIClient implements IStrategyAIClient {
#strategyRegistry: StrategyRegistry;
constructor(
player: Player,
strategyRegistry: StrategyRegistry = strategyRegistryInstance,
randomNumberGenerator: () => number = () => Math.random()
) {
super(player, randomNumberGenerator);
this.#strategyRegistry = strategyRegistry;
}
attempt(action: PlayerAction): boolean {
return this.#strategyRegistry.attempt(action);
}
async chooseFromList<Name extends keyof ChoiceMetaDataMap>(
meta: ChoiceMeta<Name>
): Promise<DataForChoiceMeta<ChoiceMeta<Name>>> {
const data = new Data<Name>(meta);
try {
if (this.attempt(new ChooseFromList(this.player(), data))) {
return data.value()!;
}
} catch (e) {
if (!(e instanceof UnhandledAction)) {
throw e;
}
console.warn(e);
}
return super.chooseFromList(meta);
}
async takeTurn(): Promise<void> {
// TODO: check if we need this _and_ the `handled` check.
let loopCheck = 0;
while (this.player().hasMandatoryActions()) {
const action = this.player().mandatoryAction();
if (loopCheck++ > MAX_ACTIONS_PER_TURN) {
throw new UnhandledAction(
`Loop detected on '${
action instanceof DataObject ? action.id() : action
}' (${
action.value() instanceof DataObject
? action.value().id()
: action.value()
})`
);
}
if (!(await this.attempt(action))) {
throw new UnhandledAction(
`No handler succeeded for '${
action instanceof DataObject ? action.id() : action
}' (${
action.value() instanceof DataObject
? action.value().id()
: action.value()
})`
);
}
}
// We don't need to worry about unhandled actions here as they won't be mandatory...
await Promise.all(
this.player()
.actions()
.map((action) => this.attempt(action))
);
// ...but they could result in some mandatory `Action`s, so lets check and re-run...
if (this.player().hasMandatoryActions()) {
await this.takeTurn();
}
}
}
export default StrategyAIClient;