-
Notifications
You must be signed in to change notification settings - Fork 0
/
quill.go
383 lines (374 loc) · 10.5 KB
/
quill.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package go_quill
import (
"fmt"
"math/rand"
"strings"
"github.com/mitchellh/mapstructure"
bg "github.com/quibbble/go-boardgame"
"github.com/quibbble/go-boardgame/pkg/bgerr"
"github.com/quibbble/go-boardgame/pkg/bgn"
"github.com/quibbble/go-quill/internal/game"
st "github.com/quibbble/go-quill/internal/game/state"
cd "github.com/quibbble/go-quill/internal/game/state/card"
"github.com/quibbble/go-quill/pkg/uuid"
)
const (
minTeams = 2
maxTeams = 2
)
type Quill struct {
state *game.Game
actions []*bg.BoardGameAction
teams []string
teamToUUID map[string]uuid.UUID
uuidToTeam map[uuid.UUID]string
targets []uuid.UUID
options *QuillMoreOptions
}
func NewQuill(options *bg.BoardGameOptions) (*Quill, error) {
if len(options.Teams) < minTeams {
return nil, &bgerr.Error{
Err: fmt.Errorf("at least %d teams required to create a game of %s", minTeams, key),
Status: bgerr.StatusTooFewTeams,
}
} else if len(options.Teams) > maxTeams {
return nil, &bgerr.Error{
Err: fmt.Errorf("at most %d teams allowed to create a game of %s", maxTeams, key),
Status: bgerr.StatusTooManyTeams,
}
} else if duplicates(options.Teams) {
return nil, &bgerr.Error{
Err: fmt.Errorf("duplicate teams found"),
Status: bgerr.StatusInvalidOption,
}
}
var details QuillMoreOptions
if err := mapstructure.Decode(options.MoreOptions, &details); err != nil {
return nil, &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidOption,
}
}
teamToUUID := make(map[string]uuid.UUID)
uuidToTeam := make(map[uuid.UUID]string)
gen := uuid.NewGen(rand.New(rand.NewSource(details.Seed)))
for _, team := range options.Teams {
playerUUID := gen.New('P')
teamToUUID[team] = playerUUID
uuidToTeam[playerUUID] = team
}
state, err := game.NewGame(details.Seed, teamToUUID[options.Teams[0]], teamToUUID[options.Teams[1]], details.Decks[0], details.Decks[1])
if err != nil {
return nil, &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidOption,
}
}
targets, err := state.GetNextTargets(state.GetTurn())
if err != nil {
return nil, &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
return &Quill{
state: state,
actions: make([]*bg.BoardGameAction, 0),
targets: targets,
teams: options.Teams,
teamToUUID: teamToUUID,
uuidToTeam: uuidToTeam,
options: &details,
}, nil
}
func (q *Quill) Do(action *bg.BoardGameAction) error {
if q.state.Winner != nil {
return &bgerr.Error{
Err: fmt.Errorf("game already over"),
Status: bgerr.StatusGameOver,
}
}
team, ok := q.teamToUUID[action.Team]
if !ok {
return &bgerr.Error{
Err: fmt.Errorf("team not found"),
Status: bgerr.StatusInvalidActionDetails,
}
}
switch action.ActionType {
case ActionNextTargets:
var details NextTargetsActionDetails
if err := mapstructure.Decode(action.MoreDetails, &details); err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidActionDetails,
}
}
targets, err := q.state.GetNextTargets(team, details.Targets...)
if err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
q.targets = targets
case ActionPlayCard:
var details PlayCardActionDetails
if err := mapstructure.Decode(action.MoreDetails, &details); err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidActionDetails,
}
}
details.PlayCard = q.state.GetCard(details.Card)
action.MoreDetails = details
if err := q.state.PlayCard(team, details.Card, details.Targets...); err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
q.actions = append(q.actions, action)
targets, err := q.state.GetNextTargets(team)
if err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
q.targets = targets
case ActionSackCard:
var details SackCardActionDetails
if err := mapstructure.Decode(action.MoreDetails, &details); err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidActionDetails,
}
}
if err := q.state.SackCard(team, details.Card, details.Option); err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
q.actions = append(q.actions, action)
targets, err := q.state.GetNextTargets(team)
if err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
q.targets = targets
case ActionAttackUnit:
var details AttackUnitActionDetails
if err := mapstructure.Decode(action.MoreDetails, &details); err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidActionDetails,
}
}
details.AttackerCard = q.state.GetCard(details.Attacker)
details.DefenderCard = q.state.GetCard(details.Defender)
action.MoreDetails = details
if err := q.state.AttackUnit(team, details.Attacker, details.Defender); err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
q.actions = append(q.actions, action)
targets, err := q.state.GetNextTargets(team)
if err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
q.targets = targets
case ActionMoveUnit:
var details MoveUnitActionDetails
if err := mapstructure.Decode(action.MoreDetails, &details); err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidActionDetails,
}
}
details.UnitCard = q.state.GetCard(details.Unit)
x, y, _ := q.state.Board.GetTileXY(details.Tile)
details.TileXY = []int{x, y}
action.MoreDetails = details
if err := q.state.MoveUnit(team, details.Unit, details.Tile); err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
q.actions = append(q.actions, action)
targets, err := q.state.GetNextTargets(team)
if err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
q.targets = targets
case ActionEndTurn:
if err := q.state.EndTurn(q.teamToUUID[action.Team]); err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
q.actions = append(q.actions, action)
targets, err := q.state.GetNextTargets(q.state.GetTurn())
if err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidAction,
}
}
q.targets = targets
case bg.ActionSetWinners:
var details bg.SetWinnersActionDetails
if err := mapstructure.Decode(action.MoreDetails, &details); err != nil {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusInvalidActionDetails,
}
}
if len(details.Winners) != 1 {
return &bgerr.Error{
Err: fmt.Errorf("there can only be one winner"),
Status: bgerr.StatusInvalidActionDetails,
}
}
winner, ok := q.teamToUUID[details.Winners[0]]
if !ok {
return &bgerr.Error{
Err: fmt.Errorf("team not found"),
Status: bgerr.StatusInvalidActionDetails,
}
}
q.state.Winner = &winner
q.actions = append(q.actions, action)
q.targets = make([]uuid.UUID, 0)
default:
return &bgerr.Error{
Err: fmt.Errorf("cannot process action type %s", action.ActionType),
Status: bgerr.StatusUnknownActionType,
}
}
return nil
}
func (q *Quill) GetSnapshot(team ...string) (*bg.BoardGameSnapshot, error) {
if len(team) > 1 {
return nil, &bgerr.Error{
Err: fmt.Errorf("get snapshot requires zero or one team"),
Status: bgerr.StatusTooManyTeams,
}
}
winners := make([]string, 0)
if q.state.Winner != nil {
winners = append(winners, q.uuidToTeam[*q.state.Winner])
}
hand := make(map[string][]st.ICard)
for id, h := range q.state.Hand {
cards := h.GetItems()
if len(team) == 1 && q.uuidToTeam[id] != team[0] {
empty := make([]st.ICard, 0)
for i := 0; i < len(cards); i++ {
empty = append(empty, cd.NewEmptyCard(id))
}
cards = empty
}
hand[q.uuidToTeam[id]] = cards
}
playRange := make(map[string][]int)
for _, team := range q.teams {
min, max := q.state.Board.GetPlayableRowRange(q.teamToUUID[team])
playRange[team] = []int{min, max}
}
deck := make(map[string]int)
for id, d := range q.state.Deck {
deck[q.uuidToTeam[id]] = d.GetSize()
}
mana := make(map[string]*st.Mana)
for id, m := range q.state.Mana {
mana[q.uuidToTeam[id]] = m
}
sacked := make(map[string]bool)
for id, s := range q.state.Sacked {
sacked[q.uuidToTeam[id]] = s
}
targets := make([]uuid.UUID, 0)
if len(team) == 1 && q.state.GetTurn() == q.teamToUUID[team[0]] {
targets = q.targets
}
return &bg.BoardGameSnapshot{
Turn: q.uuidToTeam[q.state.GetTurn()],
Teams: q.teams,
Winners: winners,
MoreData: QuillSnapshotData{
Board: q.state.Board.XYs,
PlayRange: playRange,
UUIDToTeam: q.uuidToTeam,
Hand: hand,
Deck: deck,
Mana: mana,
Sacked: sacked,
},
Targets: targets,
Actions: q.actions,
Message: fmt.Sprintf("%s must complete their turn", q.uuidToTeam[q.state.GetTurn()]),
}, nil
}
func (q *Quill) GetBGN() *bgn.Game {
tags := map[string]string{
bgn.GameTag: key,
bgn.TeamsTag: strings.Join(q.teams, ", "),
bgn.SeedTag: fmt.Sprintf("%d", q.options.Seed),
DecksTag: strings.Join([]string{
strings.Join(q.options.Decks[0], ", "),
strings.Join(q.options.Decks[1], ", "),
}, " : "),
}
actions := make([]bgn.Action, 0)
for _, action := range q.actions {
bgnAction := bgn.Action{
TeamIndex: indexOf(q.teams, action.Team),
ActionKey: rune(actionToNotation[action.ActionType][0]),
}
switch action.ActionType {
case ActionPlayCard:
var details PlayCardActionDetails
_ = mapstructure.Decode(action.MoreDetails, &details)
bgnAction.Details = details.encodeBGN()
case ActionSackCard:
var details SackCardActionDetails
_ = mapstructure.Decode(action.MoreDetails, &details)
bgnAction.Details = details.encodeBGN()
case ActionAttackUnit:
var details AttackUnitActionDetails
_ = mapstructure.Decode(action.MoreDetails, &details)
bgnAction.Details = details.encodeBGN()
case ActionMoveUnit:
var details MoveUnitActionDetails
_ = mapstructure.Decode(action.MoreDetails, &details)
bgnAction.Details = details.encodeBGN()
case ActionEndTurn:
var details EndTurnActionDetails
_ = mapstructure.Decode(action.MoreDetails, &details)
bgnAction.Details = details.encodeBGN()
case bg.ActionSetWinners:
var details bg.SetWinnersActionDetails
_ = mapstructure.Decode(action.MoreDetails, &details)
bgnAction.Details, _ = details.EncodeBGN(q.teams)
}
actions = append(actions, bgnAction)
}
return &bgn.Game{
Tags: tags,
Actions: actions,
}
}