-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasicStateManager.ts
115 lines (101 loc) · 2.51 KB
/
basicStateManager.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
107
108
109
110
111
112
113
114
115
import { IncomingMessage } from "../zrp";
import { JsCard } from "./jsTypes";
/**
* A basic snapshot of the bot state
*/
export type BasicBotState = {
/**
* A list of all cards the bot current has
*
* @type {JsCard[]}
*/
deck: JsCard[];
/**
* The card that's on top of the stack currently
*
* @type {JsCard}
*/
topCard: JsCard;
/**
* Whether the bot is the active player or not
*
* @type {boolean}
*/
isActive: boolean;
};
/**
* a bot state manager managing only the bare minimum state need for the bot to make decisions
*/
export class BasicBotStateManger {
private _state: BasicBotState;
public constructor() {
this._state = {
deck: [],
topCard: new JsCard(),
isActive: false,
};
}
public get state(): BasicBotState {
return this._state;
}
public getState() {
return this.state;
}
public aggregateNotification(message: IncomingMessage) {
switch (message.Code) {
case ZRPCode.SendCards:
this.aggregateNewCards(message.Payload);
break;
case ZRPCode.StateUpdated:
this.aggregateStateUpdate(message.Payload);
break;
case ZRPCode.RemoveCards:
this.aggregateRemoveCard(message.Payload);
break;
case ZRPCode.SendHand:
this.aggregateSendHand(message.Payload);
break;
case ZRPCode.SendPileTop:
this.aggregatePileTop(message.Payload);
break;
case ZRPCode.StartTurn:
this._state.isActive = true;
break;
case ZRPCode.EndTurn:
this._state.isActive = false;
break;
}
}
public reset() {
this._state = {
deck: [],
topCard: new JsCard(),
isActive: false,
};
}
private aggregateNewCards(data: SendCardsNotification) {
for (const card of data.Cards) {
this._state.deck.push(new JsCard(card));
}
}
private aggregateRemoveCard(data: RemoveCardNotification) {
for (const card of data.Cards) {
const index = this._state.deck.findIndex((c) => c.equals(card));
if (index >= 0) {
this._state.deck.splice(index, 1);
}
}
}
private aggregateSendHand(data: SendDeckNotification) {
this._state.deck = [];
for (const card of data.Hand) {
this._state.deck.push(new JsCard(card));
}
}
private aggregatePileTop(data: SendPileTopNotification) {
this._state.topCard = new JsCard(data);
}
private aggregateStateUpdate(data: StateUpdateNotification) {
this._state.topCard = new JsCard(data.PileTop);
}
}