This repository has been archived by the owner on Apr 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
endOfRound.js
226 lines (202 loc) · 5.65 KB
/
endOfRound.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import { compose } from 'redux'
import {
addGoodsToPlayer,
countAnimals,
curriedAddGoodsToPlayer,
inventoryAdd,
inventoryAddToPlayer,
} from './common/player'
import { size } from './destinations/index'
import { spendInventory, remove } from './common/index'
import { playerBarnVehicles } from './common/barn'
import { flip, isInventoryItemButNotPeat, isDestination } from './common/tokens'
// 1: One
const emptyVehicleIfExists = vehicle => {
if (vehicle === null) return null
return {
...vehicle,
contents: vehicle.contents.fill(null),
}
}
const emptyBarn = player => ({
...player,
barn: Object.keys(player.barn).reduce(
(accum, key) => ({
...accum,
[key]: emptyVehicleIfExists(player.barn[key]),
}),
{}
),
})
const accrueTravelExperience = destinations => player => ({
...player,
travelExperience: destinations.reduce(
(sum, destination) => sum + size[destination],
player.travelExperience
),
})
const emptyVehicles = player => {
// if (!player) return null
const tokens = playerBarnVehicles(player)
.filter(v => v !== null)
.map(v => v.contents)
.flat()
.filter(i => i !== null)
.map(t => flip(t))
return compose(
emptyBarn,
accrueTravelExperience(tokens.filter(isDestination)),
inventoryAddToPlayer(...tokens.filter(isInventoryItemButNotPeat))
)(player)
}
// 2: Two
const milking = player => {
// 1/2/3 Food for 2/5/8 Sheep, 1/2/3 Food for 1/3/5 Cattle
const { sheep, cattle } = countAnimals(player)
const addedFood = [
// for each of these that's true, add 1 food
// TODO: make a test for this with 30 sheep, to
// ensure it isn't refactored to add one an divide by 3
// the rules are really explicit about a max 6.
sheep >= 2,
sheep >= 5,
sheep >= 8,
cattle >= 1,
cattle >= 3,
cattle >= 5,
].reduce((total, meetsThreshold) => total + (meetsThreshold ? 1 : 0), 0)
return addGoodsToPlayer({ player, good: 'food', amount: addedFood })
}
const babyAnimals = player => {
// TODO
if (!player) return null
return player
}
// 3: Three
const curriedAddInventoryToPlayer = (type, amount) => player => ({
...player,
inventory: [...player.inventory, ...new Array(amount).fill(type)],
})
const harvest = player => {
const types = player.land.flat().map(cell => cell.type)
return compose(
curriedAddGoodsToPlayer(
'grain',
types.filter(type => type === 'grain').length
),
curriedAddGoodsToPlayer(
'flax',
types.filter(type => type === 'flax').length
),
curriedAddInventoryToPlayer(
'wood',
types.filter(type => type === 'forest').length
)
)(player)
}
const sheering = player => {
// TODO
if (!player) return null
return player
}
// 4: Four
const sustenanceFood = player => {
if (player.goods.food >= 3) {
return addGoodsToPlayer({ player, good: 'food', amount: -3 })
} else if (player.goods.food + player.goods.grain >= 3)
return {
...player,
goods: {
...player.goods,
food: 0,
grain: 3 - player.goods.food,
},
}
else
return {
...player,
goods: {
...player.goods,
food: 0,
grain: 0,
// TODO: this could trigger asking the player which animals they want to eat.
// If the player ends up having to eat an animal, they need to do the "slaughter"
// action. Most of the time this will never be necesary, so it can be punted.
foodDeficit: 3 - player.goods.food - player.goods.grain,
},
}
}
const consumeInventory = (state, consumed) => {
if (state.inventory.includes(consumed)) {
return {
inventory: remove(consumed)(state.inventory),
demand: state.demand,
}
} else {
return {
inventory: state.inventory,
demand: [...state.demand, consumed],
}
}
}
const sustenanceFuel = player => {
const state0 = {
inventory: player.inventory,
demand: new Array(2).fill('peat'),
}
// for each element in demand, consume it in inventory (or stick it back in demand)
const state1 = state0.demand.reduce(consumeInventory, {
inventory: state0.inventory,
demand: [],
})
// for each element in demand, turn it into a wood and do the same
const state2 = state1.demand.fill('wood').reduce(consumeInventory, {
inventory: state1.inventory,
demand: [],
})
// for each element in demand, turn it into a timber and do the same again
const state3 = state2.demand.fill('timber').reduce(consumeInventory, {
inventory: state2.inventory,
demand: [],
})
// finally, replace inventory with inventory, and increase supply bottlenecks by size of demand
return {
...player,
inventory: state3.inventory,
supplyBottlenecks: player.supplyBottlenecks + state3.demand.length,
}
}
const onNovemberBeginForPlayer = compose(
// these are evaluated right to left, order is important for sustenance
sustenanceFood,
sustenanceFuel,
harvest,
milking,
emptyVehicles
)
const onMayBeginForPlayer = compose(
// these are evaluated right to left, order is important for sustenance
sustenanceFood,
sheering,
babyAnimals,
emptyVehicles
)
const forAllPlayersDo = f => G => ({
...G,
players: {
0: f(G.players['0']),
1: f(G.players['1']),
},
})
export const onNovemberBegin = forAllPlayersDo(onNovemberBeginForPlayer)
export const onMayBegin = forAllPlayersDo(onMayBeginForPlayer)
const lighthouseReset = lighthouse => ({
used: false,
// if lighthouse was not used, swap the owner
owner: lighthouse.used ? lighthouse.owner : -(lighthouse.owner - 1),
})
export const onRoundEnd = (G, ctx) => ({
...G,
halfYear: G.halfYear + 1,
lighthouse: lighthouseReset(G.lighthouse),
})