-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
78 lines (72 loc) · 1.66 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
package indigo
import (
"fmt"
"strconv"
"time"
qg "github.com/quibbble/quibbble-controller/pkg/game"
qgn "github.com/quibbble/quibbble-controller/pkg/gamenotation"
)
const (
Key = "indigo"
Min = 2
Max = 4
)
type Builder struct{}
func (b Builder) Create(snapshot *qgn.Snapshot) (qg.Game, error) {
if key := snapshot.Tags[qgn.KeyTag]; key != Key {
return nil, fmt.Errorf("%s is not a valid key", key)
}
teams, err := snapshot.Tags.Teams()
if err != nil {
return nil, err
}
variant, ok := snapshot.Tags[qgn.VariantTag]
if !ok {
variant = ClassicVariant
}
seed, err := snapshot.Tags.Seed()
if err != nil {
seed = time.Now().Unix()
}
game, err := NewIndigo(variant, seed, teams)
if err != nil {
return nil, err
}
for _, action := range snapshot.Actions {
if action.Index < 0 || action.Index >= len(teams) {
return nil, fmt.Errorf("invalid action %d", action.Index)
}
team := teams[action.Index]
switch action.Key {
case ActionToQGN[PlaceAction]:
if len(action.Details) != 3 {
return nil, fmt.Errorf("invalid action details %v", action.Details)
}
row, err := strconv.Atoi(action.Details[1])
if err != nil {
return nil, err
}
col, err := strconv.Atoi(action.Details[2])
if err != nil {
return nil, err
}
if err := game.Do(&qg.Action{
Team: team,
Type: QGNToAction[action.Key],
Details: PlaceDetails{action.Details[0], row, col},
}); err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("invalid action key %s", action.Key)
}
}
return game, nil
}
func (b Builder) GetInformation() *qg.Information {
return &qg.Information{
Key: Key,
Min: Min,
Max: Max,
}
}