1package protocol
2
3import (
4 "time"
5
6 "github.com/gofrs/uuid"
7 "github.com/mailru/easyjson"
8 "github.com/zikaeroh/codies/internal/game"
9)
10
11// See protocol/index.ts.
12
13//go:generate go run github.com/mailru/easyjson/easyjson -disallow_unknown_fields protocol.go
14
15type ExistsQuery struct {
16 RoomID string `queryparam:"roomID"`
17}
18
19//easyjson:json
20type RoomRequest struct {
21 RoomName string `json:"roomName"`
22 RoomPass string `json:"roomPass"`
23 Create bool `json:"create"`
24}
25
26func (r *RoomRequest) Valid() (msg string, valid bool) {
27 if len(r.RoomName) == 0 {
28 return "Room name cannot be empty.", false
29 }
30
31 if len(r.RoomName) > 20 {
32 return "Room name too long.", false
33 }
34
35 if len(r.RoomPass) == 0 {
36 return "Room pass cannot be empty.", false
37 }
38
39 return "", true
40}
41
42//easyjson:json
43type RoomResponse struct {
44 ID *string `json:"id,omitempty"`
45 Error *string `json:"error,omitempty"`
46}
47
48//easyjson:json
49type TimeResponse struct {
50 Time time.Time `json:"time"`
51}
52
53//easyjson:json
54type StatsResponse struct {
55 Rooms int `json:"rooms"`
56 Clients int `json:"clients"`
57}
58
59type WSQuery struct {
60 RoomID string `queryparam:"roomID"`
61 PlayerID uuid.UUID `queryparam:"playerID"`
62 Nickname string `queryparam:"nickname"`
63}
64
65func (w *WSQuery) Valid() (msg string, valid bool) {
66 if w.RoomID == "" {
67 return "Room ID cannot be empty.", false
68 }
69
70 if w.PlayerID == uuid.Nil {
71 return "Player ID cannot be empty", false
72 }
73
74 if len(w.Nickname) == 0 {
75 return "Nickname cannot be empty.", false
76 }
77
78 if len(w.Nickname) > 16 {
79 return "Nickname too long.", false
80 }
81
82 return "", true
83}
84
85//easyjson:json
86type ClientNote struct {
87 Method ClientMethod `json:"method"`
88 Version int `json:"version"`
89 Params easyjson.RawMessage `json:"params"`
90}
91
92type ClientMethod string
93
94const NewGameMethod = ClientMethod("newGame")
95
96//easyjson:json
97type NewGameParams struct{}
98
99const EndTurnMethod = ClientMethod("endTurn")
100
101//easyjson:json
102type EndTurnParams struct{}
103
104const RandomizeTeamsMethod = ClientMethod("randomizeTeams")
105
106//easyjson:json
107type RandomizeTeamsParams struct{}
108
109const RevealMethod = ClientMethod("reveal")
110
111//easyjson:json
112type RevealParams struct {
113 Row int `json:"row"`
114 Col int `json:"col"`
115}
116
117const ChangeTeamMethod = ClientMethod("changeTeam")
118
119//easyjson:json
120type ChangeTeamParams struct {
121 Team game.Team `json:"team"`
122}
123
124const ChangeNicknameMethod = ClientMethod("changeNickname")
125
126//easyjson:json
127type ChangeNicknameParams struct {
128 Nickname string `json:"nickname"`
129}
130
131const ChangeRoleMethod = ClientMethod("changeRole")
132
133//easyjson:json
134type ChangeRoleParams struct {
135 Spymaster bool `json:"spymaster"`
136}
137
138const ChangePackMethod = ClientMethod("changePack")
139
140//easyjson:json
141type ChangePackParams struct {
142 Num int `json:"num"`
143 Enable bool `json:"enable"`
144}
145
146const ChangeTurnModeMethod = ClientMethod("changeTurnMode")
147
148//easyjson:json
149type ChangeTurnModeParams struct {
150 Timed bool `json:"timed"`
151}
152
153const ChangeTurnTimeMethod = ClientMethod("changeTurnTime")
154
155//easyjson:json
156type ChangeTurnTimeParams struct {
157 Seconds int `json:"seconds"`
158}
159
160const AddPacksMethod = ClientMethod("addPacks")
161
162//easyjson:json
163type AddPacksParams struct {
164 Packs []struct {
165 Name string `json:"name"`
166 Words []string `json:"words"`
167 } `json:"packs"`
168}
169
170const RemovePackMethod = ClientMethod("removePack")
171
172//easyjson:json
173type RemovePackParams struct {
174 Num int `json:"num"`
175}
176
177type ServerMethod string
178
179//easyjson:json
180type ServerNote struct {
181 Method ServerMethod `json:"method"`
182 Params interface{} `json:"params"`
183}
184
185const ChangeHideBombMethod = ClientMethod("changeHideBomb")
186
187//easyjson:json
188type ChangeHideBombParams struct {
189 HideBomb bool `json:"hideBomb"`
190}
191
192func StateNote(s *State) ServerNote {
193 return ServerNote{
194 Method: "state",
195 Params: s,
196 }
197}
198
199//easyjson:json
200type State struct {
201 Version int `json:"version"`
202 Teams [][]*StatePlayer `json:"teams"`
203 Turn game.Team `json:"turn"`
204 Winner *game.Team `json:"winner"`
205 Board [][]*StateTile `json:"board"`
206 WordsLeft []int `json:"wordsLeft"`
207 Lists []*StateWordList `json:"lists"`
208 Timer *StateTimer `json:"timer"`
209 HideBomb bool `json:"hideBomb"`
210}
211
212//easyjson:json
213type StatePlayer struct {
214 PlayerID game.PlayerID `json:"playerID"`
215 Nickname string `json:"nickname"`
216 Spymaster bool `json:"spymaster"`
217}
218
219//easyjson:json
220type StateTile struct {
221 Word string `json:"word"`
222 Revealed bool `json:"revealed"`
223 View *StateView `json:"view"`
224}
225
226//easyjson:json
227type StateView struct {
228 Team game.Team `json:"team"`
229 Neutral bool `json:"neutral"`
230 Bomb bool `json:"bomb"`
231}
232
233//easyjson:json
234type StateWordList struct {
235 Name string `json:"name"`
236 Count int `json:"count"`
237 Custom bool `json:"custom"`
238 Enabled bool `json:"enabled"`
239}
240
241//easyjson:json
242type StateTimer struct {
243 TurnTime int `json:"turnTime"`
244 TurnEnd time.Time `json:"turnEnd"`
245}