github.com/loomnetwork/gamechain@v0.0.0-20200406110549-36c47eb97a92/battleground/proto/CardKey.go (about) 1 package battleground_proto 2 3 import ( 4 "bytes" 5 "fmt" 6 "github.com/gogo/protobuf/jsonpb" 7 "github.com/gogo/protobuf/proto" 8 "github.com/loomnetwork/gamechain/types/zb/zb_custombase" 9 "github.com/loomnetwork/gamechain/types/zb/zb_enums" 10 ) 11 12 type CardKey struct { 13 MouldId int64 14 Variant zb_enums.CardVariant_Enum 15 } 16 17 func (value *CardKey) Size() int { 18 return proto.Size(value.protoType()) 19 } 20 21 func (value CardKey) Marshal() ([]byte, error) { 22 return proto.Marshal(value.protoType()) 23 } 24 25 func (value *CardKey) Unmarshal(data []byte) error { 26 protoValue := &zb_custombase.CardKey{} 27 err := proto.Unmarshal(data, protoValue) 28 if err != nil { 29 return err 30 } 31 32 value.MouldId = protoValue.MouldId 33 value.Variant = protoValue.Variant 34 return nil 35 } 36 37 func (value CardKey) MarshalJSON() ([]byte, error) { 38 m := jsonpb.Marshaler{ 39 OrigName: false, 40 Indent: "", 41 EmitDefaults: true, 42 } 43 44 jsonString, err := m.MarshalToString(value.protoType()) 45 if err != nil { 46 return nil, err 47 } 48 49 return []byte(jsonString), nil 50 } 51 52 func (value *CardKey) UnmarshalJSON(data []byte) error { 53 var raw zb_custombase.CardKey 54 err := jsonpb.Unmarshal(bytes.NewReader(data), &raw) 55 if err != nil { 56 return err 57 } 58 *value = CardKey{ 59 MouldId: raw.MouldId, 60 Variant: raw.Variant, 61 } 62 return nil 63 } 64 65 func (value *CardKey) String() string { 66 out := fmt.Sprintf("MouldId: %d", value.MouldId) 67 if value.Variant != zb_enums.CardVariant_Standard { 68 out += fmt.Sprintf(", Variant: %s", zb_enums.CardVariant_Enum_name[int32(value.Variant)]) 69 } 70 71 return out 72 } 73 74 func (value *CardKey) Compare(other *CardKey) int { 75 if value.MouldId > other.MouldId { 76 return 1 77 } else if value.MouldId < other.MouldId { 78 return -1 79 } else { 80 if value.Variant > other.Variant { 81 return 1 82 } else if value.Variant < other.Variant { 83 return -1 84 } else { 85 return 0 86 } 87 } 88 } 89 90 func (value *CardKey) protoType() *zb_custombase.CardKey { 91 return &zb_custombase.CardKey { 92 MouldId: value.MouldId, 93 Variant: value.Variant, 94 } 95 }