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
177func StateNote(s *State) ServerNote {
178	return ServerNote{
179		Method: "state",
180		Params: s,
181	}
182}
183
184//easyjson:json
185type State struct {
186	Version   int              `json:"version"`
187	Teams     [][]*StatePlayer `json:"teams"`
188	Turn      game.Team        `json:"turn"`
189	Winner    *game.Team       `json:"winner"`
190	Board     [][]*StateTile   `json:"board"`
191	WordsLeft []int            `json:"wordsLeft"`
192	Lists     []*StateWordList `json:"lists"`
193	Timer     *StateTimer      `json:"timer"`
194}
195
196//easyjson:json
197type StatePlayer struct {
198	PlayerID  game.PlayerID `json:"playerID"`
199	Nickname  string        `json:"nickname"`
200	Spymaster bool          `json:"spymaster"`
201}
202
203//easyjson:json
204type StateTile struct {
205	Word     string     `json:"word"`
206	Revealed bool       `json:"revealed"`
207	View     *StateView `json:"view"`
208}
209
210//easyjson:json
211type StateView struct {
212	Team    game.Team `json:"team"`
213	Neutral bool      `json:"neutral"`
214	Bomb    bool      `json:"bomb"`
215}
216
217//easyjson:json
218type StateWordList struct {
219	Name    string `json:"name"`
220	Count   int    `json:"count"`
221	Custom  bool   `json:"custom"`
222	Enabled bool   `json:"enabled"`
223}
224
225//easyjson:json
226type StateTimer struct {
227	TurnTime int       `json:"turnTime"`
228	TurnEnd  time.Time `json:"turnEnd"`
229}