in prog
This commit is contained in:
52
src/game/rule/operation/suit.go
Normal file
52
src/game/rule/operation/suit.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package operation
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"local/sandbox/cards/src/entity"
|
||||
)
|
||||
|
||||
type Suit int
|
||||
|
||||
const (
|
||||
_ Suit = iota
|
||||
Heart Suit = iota
|
||||
Spade Suit = iota
|
||||
Diamond Suit = iota
|
||||
Club Suit = iota
|
||||
)
|
||||
|
||||
var suitStrings = map[string]Suit{
|
||||
"heart": Heart,
|
||||
"spade": Spade,
|
||||
"diamond": Diamond,
|
||||
"club": Club,
|
||||
}
|
||||
|
||||
func (suit Suit) Is(card entity.Card) bool {
|
||||
return card.Suit == int(suit)
|
||||
}
|
||||
|
||||
func (suit Suit) MarshalJSON() ([]byte, error) {
|
||||
for k, v := range suitStrings {
|
||||
if suit == v {
|
||||
return json.Marshal(k)
|
||||
}
|
||||
}
|
||||
return json.Marshal("?")
|
||||
}
|
||||
|
||||
func (suit *Suit) UnmarshalJSON(b []byte) error {
|
||||
var s string
|
||||
err := json.Unmarshal(b, &s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for k, v := range suitStrings {
|
||||
if k == s {
|
||||
*suit = v
|
||||
return nil
|
||||
}
|
||||
}
|
||||
*suit = 0
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user