-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame-action.js
193 lines (155 loc) · 5.33 KB
/
game-action.js
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import _ from 'lodash';
import Move from './move';
const GameAction = () => {};
// updatePlayerPosition
const updatePlayerPosition = (args) => {
const {game, dist, playerIndex} = args;
// todo add errors for missing args
game.players[playerIndex].position += dist;
return game;
};
// applyNewResources
const applyNewResources = (args) => {
let game = args.game;
const {playerIndex, dist} = args;
// todo add errors for missing args
if (game.players[playerIndex].position >= 40) {
// the last tile index is 35
// we need to reset the position to an inbounds index
game = resetPlayerPosition(args);
}
const newResources = determineNewResources({game, playerIndex, dist});
Object.keys(newResources).forEach(k => {
game.players[playerIndex].resources[k] = newResources[k];
});
return game;
};
const resetPlayerPosition = (args) => {
// todo add errors for missing args
const {game, playerIndex} = args;
game.players[playerIndex].position -= 40;
return game;
}
const determineNewResources = (args) => {
const {game, playerIndex, dist} = args;
const tile = getBoardTile({game, playerIndex});
const type = tile.type;
const startResources = _.clone(game.players[playerIndex].resources);
let newResources;
if (type === 'robot') {
// robo-calibrate increases all resources to a minimum of 2
const minResources = robotTileEnforceMin({game, playerIndex});
// robo-calibrate decreases all high values to be 2 away from the minValue
const regResources = robotTileRegulatePeaks({game, playerIndex});
newResources = Object.assign(startResources, minResources, regResources);
} else {
// player gets resources
newResources = Object.assign({}, startResources);
newResources[type] += dist;
}
return newResources;
};
const getBoardTile = (args) => {
// todo add errors for missing args
const {game, playerIndex} = args;
const board = game.board.getBoard();
const position = game.players[playerIndex].position;
return board[position];
};
const robotTileEnforceMin = (args) => {
const {game, playerIndex} = args;
const resources = {};
Object.keys(game.players[playerIndex].resources).forEach(type => {
// increase any resource that is less than 2, up to 2
if (game.players[playerIndex].resources[type] < 2) {
resources[type] = 2;
}
});
return resources;
};
const robotTileRegulatePeaks = (args) => {
const resources = {};
const {game, playerIndex} = args;
// gather all the resource values into an array so we can do math
const values = Object.keys(game.players[playerIndex].resources).map(type => {
return game.players[playerIndex].resources[type];
});
// Math.min gets the smallest current value
// Math.max ensures that the smallest amount we will refer to is 2
const smallestAmount = Math.max(Math.min(...values), 2);
Object.keys(game.players[playerIndex].resources).forEach(type => {
// growth will be maximized on a turn where all resources are within 2
const currentAmount = game.players[playerIndex].resources[type];
if (currentAmount > smallestAmount + 2) {
resources[type] = smallestAmount + 2;
}
});
return resources;
};
// growPlant
const growPlant = (args) => {
let game = args.game;
const {playerIndex} = args;
const growthAmount = determinePlantGrowth({game, playerIndex});
game.players[playerIndex].growth += growthAmount;
// consume the resources
if (growthAmount > 0) {
game = GameAction.reduceResources({game, playerIndex});
}
if (game.players[playerIndex].growth >= 11) {
game.players[playerIndex].growth = 11; // maxHeight on plant is 11
game.hasWinner = true;
}
return game;
};
const determinePlantGrowth = (args) => {
const {game, playerIndex} = args;
const values = Object.keys(game.players[playerIndex].resources).map(key => {
return game.players[playerIndex].resources[key];
});
const smallestValue = Math.min(...values);
const largestValue = Math.max(...values);
const difference = largestValue - smallestValue;
if (smallestValue === 0) {
return 0;
}
if (difference <= 2) {
return 2;
}
return 1;
};
const recordMove = (args) => {
const {game, playerIndex} = args;
const growth = game.players[playerIndex].growth;
const playerResources = _.cloneDeep(game.players[playerIndex].resources);
const dist = args.dist;
const tile = _.cloneDeep(getBoardTile({game, playerIndex}));
const move = new Move({playerIndex, growth, playerResources, dist, tile});
game.moves.push(move);
return game;
};
const reduceResources = (args) => {
// eslint-disable-next-line
const {game, playerIndex} = args;
Object.keys(game.players[playerIndex].resources).forEach(key => {
if (game.players[playerIndex].resources[key] > 0) {
game.players[playerIndex].resources[key] -= 1;
} else {
game.players[playerIndex].resources[key] = 0;
}
});
return game;
};
GameAction.updatePlayerPosition = updatePlayerPosition;
GameAction.applyNewResources = applyNewResources;
GameAction.growPlant = growPlant;
GameAction.recordMove = recordMove;
GameAction.reduceResources = reduceResources;
// normally these would be private but we'll export them for testing
GameAction.getBoardTile = getBoardTile;
GameAction.robotTileEnforceMin = robotTileEnforceMin;
GameAction.robotTileRegulatePeaks = robotTileRegulatePeaks;
GameAction.determineNewResources = determineNewResources;
GameAction.resetPlayerPosition = resetPlayerPosition;
GameAction.determinePlantGrowth = determinePlantGrowth;
export default GameAction;