protocol.go

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