diff --git a/card.go b/card.go index 08efd3d..c3d5f30 100644 --- a/card.go +++ b/card.go @@ -19,6 +19,7 @@ type Card struct { Effects []*Effect `json:"effects"` } +//go:generate enumer -type=CardType -json type CardType int const ( @@ -50,12 +51,6 @@ func NewEmpty(p int) *Card { Effects: []*Effect{}, } } -func (c CardType) String() string { - if c == -1 { - return " " - } - return []string{"V", "A", "2", "3", "4", "5", "6", "7", "8"}[c] -} func (c *Card) Empty() bool { return c.Type == EmptyValue diff --git a/cardtype_enumer.go b/cardtype_enumer.go new file mode 100644 index 0000000..503d64d --- /dev/null +++ b/cardtype_enumer.go @@ -0,0 +1,77 @@ +// Code generated by "enumer -type=CardType -json"; DO NOT EDIT. + +// +package tome_lib + +import ( + "encoding/json" + "fmt" +) + +const _CardTypeName = "EmptyValueValkAceTwoThreeFourFiveSixSevenEight" + +var _CardTypeIndex = [...]uint8{0, 10, 14, 17, 20, 25, 29, 33, 36, 41, 46} + +func (i CardType) String() string { + i -= -1 + if i < 0 || i >= CardType(len(_CardTypeIndex)-1) { + return fmt.Sprintf("CardType(%d)", i+-1) + } + return _CardTypeName[_CardTypeIndex[i]:_CardTypeIndex[i+1]] +} + +var _CardTypeValues = []CardType{-1, 0, 1, 2, 3, 4, 5, 6, 7, 8} + +var _CardTypeNameToValueMap = map[string]CardType{ + _CardTypeName[0:10]: -1, + _CardTypeName[10:14]: 0, + _CardTypeName[14:17]: 1, + _CardTypeName[17:20]: 2, + _CardTypeName[20:25]: 3, + _CardTypeName[25:29]: 4, + _CardTypeName[29:33]: 5, + _CardTypeName[33:36]: 6, + _CardTypeName[36:41]: 7, + _CardTypeName[41:46]: 8, +} + +// CardTypeString retrieves an enum value from the enum constants string name. +// Throws an error if the param is not part of the enum. +func CardTypeString(s string) (CardType, error) { + if val, ok := _CardTypeNameToValueMap[s]; ok { + return val, nil + } + return 0, fmt.Errorf("%s does not belong to CardType values", s) +} + +// CardTypeValues returns all values of the enum +func CardTypeValues() []CardType { + return _CardTypeValues +} + +// IsACardType returns "true" if the value is listed in the enum definition. "false" otherwise +func (i CardType) IsACardType() bool { + for _, v := range _CardTypeValues { + if i == v { + return true + } + } + return false +} + +// MarshalJSON implements the json.Marshaler interface for CardType +func (i CardType) MarshalJSON() ([]byte, error) { + return json.Marshal(i.String()) +} + +// UnmarshalJSON implements the json.Unmarshaler interface for CardType +func (i *CardType) UnmarshalJSON(data []byte) error { + var s string + if err := json.Unmarshal(data, &s); err != nil { + return fmt.Errorf("CardType should be a string, got %s", data) + } + + var err error + *i, err = CardTypeString(s) + return err +} diff --git a/go.mod b/go.mod index 5fc3f2b..c11df13 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module git.saintnet.tech/tomecraft/tome_lib go 1.16 -require github.com/google/uuid v1.3.0 +require ( + github.com/alvaroloes/enumer v1.1.2 // indirect + github.com/google/uuid v1.3.0 +) diff --git a/go.sum b/go.sum index 3dfe1c9..44d833d 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,13 @@ +github.com/alvaroloes/enumer v1.1.2 h1:5khqHB33TZy1GWCO/lZwcroBFh7u+0j40T83VUbfAMY= +github.com/alvaroloes/enumer v1.1.2/go.mod h1:FxrjvuXoDAx9isTJrv4c+T410zFi0DtXIT0m65DJ+Wo= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/pascaldekloe/name v0.0.0-20180628100202-0fd16699aae1 h1:/I3lTljEEDNYLho3/FUB7iD/oc2cEFgVmbHzV+O0PtU= +github.com/pascaldekloe/name v0.0.0-20180628100202-0fd16699aae1/go.mod h1:eD5JxqMiuNYyFNmyY9rkJ/slN8y59oEu4Ei7F8OoKWQ= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20190524210228-3d17549cdc6b h1:iEAPfYPbYbxG/2lNN4cMOHkmgKNsCuUwkxlDCK46UlU= +golang.org/x/tools v0.0.0-20190524210228-3d17549cdc6b/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= diff --git a/session.go b/session.go index 24b5869..52715f5 100644 --- a/session.go +++ b/session.go @@ -10,12 +10,13 @@ type SessionCmd string type SessionResp string const ( - SessionCmdQuery SessionCmd = "query" - SessionCmdJoin = "join" - SessionCmdLeave = "leave" - SessionCmdPlay = "play" - SessionCmdPoll = "poll" - SessionCmdReady = "ready" + SessionCmdQuery SessionCmd = "query" + SessionCmdJoin = "join" + SessionCmdLeave = "leave" + SessionCmdPlay = "play" + SessionCmdPoll = "poll" + SessionCmdReady = "ready" + SessionCmdLoadDeck = "load_deck" ) const ( @@ -26,7 +27,9 @@ const ( SessionRespJoinError = "join error" SessionRespLeft = "left" SessionRespPlayed = "played" + SessionRespDeckLoaded = "deck loaded" SessionRespError = "generic error" + SessionRespLoadDeckError = "load deck error" SessionRespBroadcastSenTurn = "Sentinal turn" SessionRespBroadcastScoTrun = "Scourge turn" SessionRespBroadcastSenWin = "Sentinal wins" @@ -46,10 +49,11 @@ type SessionCommand struct { MatchID uuid.UUID `json:"match_id"` Command SessionCmd `json:"command"` GameCommand *Command `json:"game_command,omitempty"` + Data string `json:"data,omitempty"` } func (s *SessionCommand) String() string { - return fmt.Sprintf("%v %v %v\n", s.ID, s.Command, s.GameCommand) + return fmt.Sprintf("%v %v %v %v\n", s.ID, s.Command, s.GameCommand, s.Data) } type SessionCommandResult struct {