-
Notifications
You must be signed in to change notification settings - Fork 0
/
bgn.go
45 lines (39 loc) · 1012 Bytes
/
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
package go_tsuro
import (
"fmt"
"strconv"
bg "github.com/quibbble/go-boardgame"
"github.com/quibbble/go-boardgame/pkg/bgerr"
)
var (
actionToNotation = map[string]string{ActionPlaceTile: "p", bg.ActionSetWinners: "w"}
notationToAction = reverseMap(actionToNotation)
)
func (p *PlaceTileActionDetails) encodeBGN() []string {
return []string{strconv.Itoa(p.Row), strconv.Itoa(p.Column), p.Tile}
}
func decodePlaceTileActionDetailsBGN(notation []string) (*PlaceTileActionDetails, error) {
if len(notation) != 3 {
return nil, loadFailure(fmt.Errorf("invalid place tile notation"))
}
row, err := strconv.Atoi(notation[0])
if err != nil {
return nil, loadFailure(err)
}
column, err := strconv.Atoi(notation[1])
if err != nil {
return nil, loadFailure(err)
}
tile := notation[2]
return &PlaceTileActionDetails{
Row: row,
Column: column,
Tile: tile,
}, nil
}
func loadFailure(err error) error {
return &bgerr.Error{
Err: err,
Status: bgerr.StatusBGNDecodingFailure,
}
}