This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mover.go
305 lines (254 loc) · 6.34 KB
/
mover.go
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package splenda
import "errors"
// A mover is a utility that holds shared code across different types of moves.
type mover struct {
gameID string
userID string
db *DB
game *Game
players []string
index int
}
// A movefunc implements the actual business logic of a move.
type movefunc func(*TX) (string, string, error)
// Move executes the overall workflow of a move transaction, calling out to
// a movefunc that implements the actual business logic.
func (m *mover) Move(move movefunc) (string, error) {
tx, err := m.db.NewTX(m.gameID)
if err != nil {
return "", err
}
defer tx.Close()
// Pre-move work.
if err := m.premove(tx); err != nil {
return "", err
}
// Run the actual move.
newstate, newplayer, err := move(tx)
if err != nil {
return "", err
}
// Clean up with post-move work.
return m.postmove(tx, newstate, newplayer)
}
// Premove does the common work to set up for a game move.
func (m *mover) premove(tx *TX) error {
game, err := tx.GetGameBasics() // TODO: FOR UPDATE to lock?
if err != nil {
return err
}
m.game = game
// Read the set of players for this game, find the index of
// the calling player, and confirm that it's their turn to move.
players, err := tx.GetPlayers()
if err != nil {
return err
}
m.players = players
index := find(m.userID, players)
if index == -1 {
return errors.New("no such game")
}
m.index = index
if m.userID != game.Current {
return errors.New("not your turn")
}
return nil
}
func find(needle string, haystack []string) int {
for i, hay := range haystack {
if hay == needle {
return i
}
}
return -1
}
// Postmove does the common work to finish up after a move.
func (m *mover) postmove(tx *TX, newstate string, newplayer string) (string, error) {
ts, err := tx.UpdateGame(m.game.TS, newstate, newplayer)
if err != nil {
return "", err
}
if err := tx.Commit(); err != nil {
return "", err
}
return ts, nil
}
// State returns the current state of the game.
func (m *mover) State() string {
return m.game.State
}
// IsRoundOver returns true if this is the last turn for the round.
func (m *mover) IsRoundOver() bool {
return m.index == len(m.players)-1
}
// GetCardID gets the ID of the card at a given index.
func (m *mover) GetCardID(tx *TX, tier int, index int) (string, error) {
if tier == 0 {
return m.GetReservedCardID(tx, index)
}
cards, err := tx.GetCards(tier)
if err != nil {
return "", err
}
card := cards[index]
if card == "" {
return "", errors.New("no card there")
}
return card, nil
}
func (m *mover) GetReservedCardID(tx *TX, index int) (string, error) {
_, reserved, err := tx.GetPlayerCards(m.userID)
if err != nil {
return "", err
}
if index < 0 || index >= len(reserved) {
return "", errors.New("no card there")
}
return reserved[index], nil
}
// GetCardCounts gets the number of cards of each color that the player has.
func (m *mover) GetCardCounts(tx *TX) (map[string]int, error) {
cards, _, err := tx.GetPlayerCards(m.userID)
if err != nil {
return nil, err
}
ret := map[string]int{}
for _, id := range cards {
card, ok := cardFromID(id)
if !ok {
return nil, errors.New("bogus card ID")
}
ret[card.color]++
}
return ret, nil
}
// EarnCoins transfers coins from the bank to the player if possible.
func (m *mover) EarnCoins(tx *TX, limits, coins map[string]int) error {
bank, err := tx.GetCoins()
if err != nil {
return err
}
purse, err := tx.GetPlayerCoins(m.userID)
if err != nil {
return err
}
for color, limit := range limits {
if bank[color] < limit {
return ErrInsufficientCoins
}
}
newbank := map[string]int{}
newpurse := map[string]int{}
for color, count := range coins {
newbank[color] = bank[color] - count
newpurse[color] = purse[color] + count
}
if err := tx.UpdateCoins(newbank); err != nil {
return err
}
if err := tx.UpdatePlayerCoins(m.userID, newpurse); err != nil {
return err
}
// TODO: If player has more than 10 coins now, make then give some back.
return nil
}
// PayCost attempts to pay the cost for a given card.
func (m *mover) PayCost(tx *TX, cards, cost map[string]int) error {
bank, err := tx.GetCoins()
if err != nil {
return err
}
purse, err := tx.GetPlayerCoins(m.userID)
if err != nil {
return err
}
newbank := map[string]int{}
newpurse := map[string]int{}
wildsneeded := 0
for color, coins := range cost {
coincost, wildcost := calculateActualCost(cards[color], purse[color], coins)
if coincost > 0 {
newbank[color] = bank[color] + coincost
newpurse[color] = purse[color] - coincost
}
wildsneeded += wildcost
}
if wildsneeded > 0 {
if wildsneeded > purse[wild] {
return ErrInsufficientCoins
}
newbank[wild] = bank[wild] + wildsneeded
newpurse[wild] = purse[wild] - wildsneeded
}
if err := tx.UpdateCoins(newbank); err != nil {
return err
}
if err := tx.UpdatePlayerCoins(m.userID, newpurse); err != nil {
return err
}
return nil
}
func calculateActualCost(cards, purse, cost int) (int, int) {
cost -= cards
if cost <= 0 {
// We have enough cards to totally cancel the cost.
return 0, 0
}
// We can directly afford it, pay the cost in regular coins.
if cost <= purse {
return cost, 0
}
// If we can't we need to spend some wilds.
return purse, cost - purse
}
// DealCard deals a card from the deck onto the board.
func (m *mover) DealCard(tx *TX, tier int, index int) error {
newcard, err := tx.GetTopCard(tier)
if err != nil {
return err
}
if newcard != "" {
err = tx.TransferCard(tier, index, newcard)
} else {
err = tx.DeleteCard(tier, index)
}
if err != nil {
return err
}
return nil
}
// CanAffordNoble checks if the player can now afford a noble.
func (m *mover) CanAffordNoble(tx *TX, cards map[string]int) (bool, error) {
nobles, err := tx.GetNobles()
if err != nil {
return false, err
}
for _, id := range nobles {
noble, ok := nobleFromID(id)
if !ok {
return false, errors.New("invalid noble ID")
}
if canAfford(cards, noble.cost) {
return true, nil
}
}
return false, nil
}
func canAfford(cards, cost map[string]int) bool {
for color, num := range cost {
if cards[color] < num {
return false
}
}
return true
}
// NextPlayer returns a triple that causes postmove to move to the next
// player's turn.
func (m *mover) NextPlayer() (string, string, error) {
next := m.index + 1
if next >= len(m.players) {
next = 0
}
return play, m.players[next], nil
}