-
Notifications
You must be signed in to change notification settings - Fork 0
/
stored-game.go
44 lines (38 loc) · 1.22 KB
/
stored-game.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
package checkers
import (
fmt "fmt"
"cosmossdk.io/errors"
"github.com/alice/checkers/rules"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func (storedGame StoredGame) GetBlackAddress() (black sdk.AccAddress, err error) {
black, errBlack := sdk.AccAddressFromBech32(storedGame.Black)
return black, errors.Wrapf(errBlack, ErrInvalidBlack.Error(), storedGame.Black)
}
func (storedGame StoredGame) GetRedAddress() (red sdk.AccAddress, err error) {
red, errRed := sdk.AccAddressFromBech32(storedGame.Red)
return red, errors.Wrapf(errRed, ErrInvalidRed.Error(), storedGame.Red)
}
func (storedGame StoredGame) ParseGame() (game *rules.Game, err error) {
board, errBoard := rules.Parse(storedGame.Board)
if errBoard != nil {
return nil, errors.Wrapf(errBoard, ErrGameNotParseable.Error())
}
board.Turn = rules.StringPieces[storedGame.Turn].Player
if board.Turn.Color == "" {
return nil, errors.Wrapf(fmt.Errorf("turn: %s", storedGame.Turn), ErrGameNotParseable.Error())
}
return board, nil
}
func (storedGame StoredGame) Validate() (err error) {
_, err = storedGame.GetBlackAddress()
if err != nil {
return err
}
_, err = storedGame.GetRedAddress()
if err != nil {
return err
}
_, err = storedGame.ParseGame()
return err
}