This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dto.go
150 lines (127 loc) · 3.12 KB
/
dto.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package splenda
import "fmt"
// UserList is a list of users.
type UserList struct {
Users []string `json:"users"`
}
// Login is a request to log in.
type Login struct {
ID string `json:"id"`
Password string `json:"pw"`
}
// SID is a session ID.
type SID struct {
SID string `json:"sid"`
}
// GameSummary lists the ID and players of a given game.
type GameSummary struct {
ID string `json:"id"`
Players []string `json:"players"`
}
// GameList lists the current games.
type GameList struct {
Games []*GameSummary `json:"games,omitempty"`
}
// Noble describes a noble tile.
type Noble struct {
ID string `json:"id"`
Points int `json:"points"`
Cost map[string]int `json:"cost"`
}
// ToNobles hydrates a list of Noble DTOs from their IDs.
func ToNobles(ids []string) ([]*Noble, error) {
ret := []*Noble{}
for _, id := range ids {
if id == "" {
ret = append(ret, nil)
continue
}
noble, ok := nobleFromID(id)
if !ok {
return nil, fmt.Errorf("bogus noble id: %v", id)
}
ret = append(ret, &Noble{
ID: id,
Points: 3, // All nobles are worth three points.
Cost: noble.cost,
})
}
return ret, nil
}
// Card describes a gem card.
type Card struct {
ID string `json:"id"`
Color string `json:"color"`
Points int `json:"points"`
Cost map[string]int `json:"cost"`
}
// ToCards hydrates a list of Card DTOs from their IDs.
func ToCards(ids []string) ([]*Card, error) {
ret := []*Card{}
for _, id := range ids {
card, err := ToCard(id)
if err != nil {
return nil, err
}
ret = append(ret, card)
}
return ret, nil
}
// ToCard hydrates a Card DTO from its ID.
func ToCard(id string) (*Card, error) {
if id == "" {
return nil, nil
}
card, ok := cardFromID(id)
if !ok {
return nil, fmt.Errorf("bogus card id: %v", id)
}
return &Card{
ID: id,
Color: card.color,
Points: card.points,
Cost: card.cost,
}, nil
}
// Table describes the state of the shared table.
type Table struct {
Coins map[string]int `json:"coins"`
Nobles []*Noble `json:"nobles"`
Cards [][]*Card `json:"cards"`
Decks []int `json:"decks"`
}
// Player describes the state of a player's hand.
type Player struct {
ID string `json:"id"`
Coins map[string]int `json:"coins"`
Nobles []*Noble `json:"nobles"`
Cards map[string][]*Card `json:"cards"`
Reserved []*Card `json:"reserved"`
Points int `json:"points"`
}
// Game describes the overall state of the game.
type Game struct {
ID string `json:"id"`
TS string `json:"ts"`
State string `json:"state"`
Current string `json:"current"`
Table *Table `json:"table"`
Players []*Player `json:"players"`
}
// Take3 is a request to take three coins.
type Take3 struct {
Colors []string `json:"colors"`
}
// Take2 is a request to take two coins.
type Take2 struct {
Color string `json:"color"`
}
// Buy is a request to buy a card.
type Buy struct {
Tier int `json:"tier"`
Index int `json:"index"`
}
// TS is a response containing an updated timestamp.
type TS struct {
TS string `json:"ts"`
}