-
Notifications
You must be signed in to change notification settings - Fork 1
/
builder.go
101 lines (92 loc) · 2.35 KB
/
builder.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
package go_carcassonne
import (
"fmt"
"strconv"
"strings"
bg "github.com/quibbble/go-boardgame"
"github.com/quibbble/go-boardgame/pkg/bgn"
)
const key = "Carcassonne"
type Builder struct{}
func (b *Builder) Create(options *bg.BoardGameOptions) (bg.BoardGame, error) {
return NewCarcassonne(options)
}
func (b *Builder) CreateWithBGN(options *bg.BoardGameOptions) (bg.BoardGameWithBGN, error) {
return NewCarcassonne(options)
}
func (b *Builder) Load(game *bgn.Game) (bg.BoardGameWithBGN, error) {
if game.Tags["Game"] != key {
return nil, loadFailure(fmt.Errorf("game tag does not match game key"))
}
teamsStr, ok := game.Tags["Teams"]
if !ok {
return nil, loadFailure(fmt.Errorf("missing teams tag"))
}
teams := strings.Split(teamsStr, ", ")
seedStr, ok := game.Tags["Seed"]
if !ok {
return nil, loadFailure(fmt.Errorf("missing seed tag"))
}
seed, err := strconv.Atoi(seedStr)
if err != nil {
return nil, loadFailure(err)
}
g, err := b.CreateWithBGN(&bg.BoardGameOptions{
Teams: teams,
MoreOptions: CarcassonneMoreOptions{
Seed: int64(seed),
},
})
if err != nil {
return nil, err
}
for _, action := range game.Actions {
if action.TeamIndex >= len(teams) {
return nil, loadFailure(fmt.Errorf("team index %d out of range", action.TeamIndex))
}
team := teams[action.TeamIndex]
actionType := notationToAction[string(action.ActionKey)]
if actionType == "" {
return nil, loadFailure(fmt.Errorf("invalid action key %s", string(action.ActionKey)))
}
var details interface{}
switch actionType {
case ActionPlaceTile:
result, err := decodePlaceTileActionDetailsBGN(action.Details)
if err != nil {
return nil, err
}
details = result
case ActionPlaceToken:
result, err := decodePlaceTokenActionDetailsBGN(action.Details)
if err != nil {
return nil, err
}
details = result
case bg.ActionSetWinners:
result, err := bg.DecodeSetWinnersActionDetailsBGN(action.Details, teams)
if err != nil {
return nil, err
}
details = result
}
if err := g.Do(&bg.BoardGameAction{
Team: team,
ActionType: actionType,
MoreDetails: details,
}); err != nil {
return nil, err
}
}
return g, nil
}
func (b *Builder) Info() *bg.BoardGameInfo {
return &bg.BoardGameInfo{
GameKey: b.Key(),
MinTeams: minTeams,
MaxTeams: maxTeams,
}
}
func (b *Builder) Key() string {
return key
}