-
Notifications
You must be signed in to change notification settings - Fork 1
/
bgn.go
135 lines (122 loc) · 4.65 KB
/
bgn.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
package go_carcassonne
import (
"fmt"
"strconv"
bg "github.com/quibbble/go-boardgame"
"github.com/quibbble/go-boardgame/pkg/bgerr"
)
var (
actionToNotation = map[string]string{ActionPlaceTile: "i", ActionPlaceToken: "o", ActionRotateTileRight: "r", ActionRotateTileLeft: "l", bg.ActionSetWinners: "w"}
notationToAction = reverseMap(actionToNotation)
sideToNotation = map[string]string{SideTop: "t", SideRight: "r", SideBottom: "b", SideLeft: "l"}
notationToSide = reverseMap(sideToNotation)
farmSideToNotation = map[string]string{FarmSideTopA: "ta", FarmSideTopB: "tb", FarmSideRightA: "ra", FarmSideRightB: "rb", FarmSideBottomA: "ba", FarmSideBottomB: "bb", FarmSideLeftA: "la", FarmSideLeftB: "lb"}
notationToFarmSide = reverseMap(farmSideToNotation)
structureToNotation = map[string]string{Road: "r", Farm: "f", City: "c", Cloister: "m", NilStructure: "n"}
notationToStructure = reverseMap(structureToNotation)
tokenToNotation = map[string]string{Farmer: "f", Knight: "k", Thief: "t", Monk: "m"}
notationToToken = reverseMap(tokenToNotation)
boolToNotation = map[bool]string{true: "t", false: "f"}
notationToBool = map[string]bool{"t": true, "f": false}
)
func (p *PlaceTileActionDetails) encodeBGN() []string {
return []string{
strconv.Itoa(p.X), strconv.Itoa(p.Y),
structureToNotation[p.Tile.Top], structureToNotation[p.Tile.Right], structureToNotation[p.Tile.Bottom], structureToNotation[p.Tile.Left],
structureToNotation[p.Tile.Center],
boolToNotation[p.Tile.ConnectedCitySides], boolToNotation[p.Tile.Banner],
}
}
func decodePlaceTileActionDetailsBGN(notation []string) (*PlaceTileActionDetails, error) {
if len(notation) != 9 {
return nil, loadFailure(fmt.Errorf("got %d but wanted %d fields in when decoding %s details", len(notation), 2, ActionPlaceTile))
}
x, err := strconv.Atoi(notation[0])
if err != nil {
return nil, loadFailure(err)
}
y, err := strconv.Atoi(notation[1])
if err != nil {
return nil, loadFailure(err)
}
top, ok := notationToStructure[notation[2]]
if !ok {
return nil, loadFailure(fmt.Errorf("failed to get top of tile"))
}
right, ok := notationToStructure[notation[3]]
if !ok {
return nil, loadFailure(fmt.Errorf("failed to get right of tile"))
}
bottom, ok := notationToStructure[notation[4]]
if !ok {
return nil, loadFailure(fmt.Errorf("failed to get bottom of tile"))
}
left, ok := notationToStructure[notation[5]]
if !ok {
return nil, loadFailure(fmt.Errorf("failed to get left of tile"))
}
center, ok := notationToStructure[notation[6]]
if !ok {
return nil, loadFailure(fmt.Errorf("failed to get center of tile"))
}
connectedCitySides, ok := notationToBool[notation[7]]
if !ok {
return nil, loadFailure(fmt.Errorf("failed to get connectedCitySides"))
}
banner, ok := notationToBool[notation[8]]
if !ok {
return nil, loadFailure(fmt.Errorf("failed to get banner"))
}
return &PlaceTileActionDetails{
X: x,
Y: y,
Tile: TileActionDetails{
top, right, bottom, left, center, connectedCitySides, banner,
},
}, nil
}
func (p *PlaceTokenActionDetails) encodeBGN() []string {
if p.Pass {
return []string{boolToNotation[p.Pass]}
} else if p.Type == Monk {
return []string{boolToNotation[p.Pass], strconv.Itoa(p.X), strconv.Itoa(p.Y), tokenToNotation[p.Type]}
} else if p.Type == Farmer {
return []string{boolToNotation[p.Pass], strconv.Itoa(p.X), strconv.Itoa(p.Y), tokenToNotation[p.Type], farmSideToNotation[p.Side]}
}
return []string{boolToNotation[p.Pass], strconv.Itoa(p.X), strconv.Itoa(p.Y), tokenToNotation[p.Type], sideToNotation[p.Side]}
}
func decodePlaceTokenActionDetailsBGN(notation []string) (*PlaceTokenActionDetails, error) {
if len(notation) < 1 || len(notation) > 5 {
return nil, loadFailure(fmt.Errorf("got %d but wanted %d to %d fields in when decoding %s details", len(notation), 1, 5, ActionPlaceToken))
}
pass, ok := notationToBool[notation[0]]
if !ok {
return nil, loadFailure(fmt.Errorf("got %s but wanted be 0 or 1 for for Pass when decoding %s details", notation[0], ActionPlaceToken))
}
if pass {
return &PlaceTokenActionDetails{Pass: pass}, nil
}
x, err := strconv.Atoi(notation[1])
if err != nil {
return nil, loadFailure(err)
}
y, err := strconv.Atoi(notation[2])
if err != nil {
return nil, loadFailure(err)
}
token := notationToToken[notation[3]]
if len(notation) == 4 {
return &PlaceTokenActionDetails{Pass: pass, X: x, Y: y, Type: token}, nil
}
side := notationToSide[notation[4]]
if token == Farmer {
side = notationToFarmSide[notation[4]]
}
return &PlaceTokenActionDetails{Pass: pass, X: x, Y: y, Type: token, Side: side}, nil
}
func loadFailure(err error) error {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusBGNDecodingFailure,
}
}