-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
228 lines (213 loc) · 7.29 KB
/
handler.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package grouplay
import (
"encoding/json"
"fmt"
"gopkg.in/igm/sockjs-go.v2/sockjs"
"net/http"
"strings"
)
func NewHandler(prefix string) http.Handler {
return sockjs.NewHandler(prefix, sockjs.DefaultOptions, quoridorHandler)
}
func quoridorHandler(session sockjs.Session) {
for {
if msg, err := session.Recv(); err == nil {
handleMsg(session, msg)
continue
}
break
}
}
func handleMsg(session sockjs.Session, msg string) {
fmt.Println("Message :", msg)
decoder := json.NewDecoder(strings.NewReader(msg))
message := new(Message)
decoder.Decode(message)
if message.Confirm {
fmt.Println("Received confirm message : " + msg + " , from " + session.ID())
return
}
switch message.Cmd {
case CmdCreateGroup:
if player, ok := FindPlayer(session.ID()); ok {
decoder := json.NewDecoder(strings.NewReader(message.Msg))
createInfo := new(CreateGroupMesssage)
decoder.Decode(createInfo)
game := GetGame(createInfo.Game)
if ok, err := player.CreateGroup(game, createInfo.Max, createInfo.AllowSpectator); ok {
SendStructMessage(session, message.Cmd, struct {
ID string `json:"groupId"`
Hoster bool `json:"isHoster"`
OK bool `json:"ok"`
}{ID: player.GroupHosted.ID, Hoster: true, OK: true}, true)
NotifyGroupListToAll()
} else {
SendErrorMessage(session, message.Cmd, err.Error(), false, true)
}
} else {
err := NewError("No user found with id " + session.ID())
SendErrorMessage(session, message.Cmd, err.Error(), false, true)
}
case CmdJoinGroup:
if player, ok := FindPlayer(session.ID()); ok {
decoder := json.NewDecoder(strings.NewReader(message.Msg))
joinInfo := new(JoinGroupMessage)
decoder.Decode(joinInfo)
if ok, err := player.JoinGroup(joinInfo.GroupId); ok {
SendStructMessage(session, message.Cmd, struct {
ID string `json:"groupId"`
OK bool `json:"ok"`
}{ID: player.GroupJoined.ID, OK: true}, true)
NotifyGroupListToAll()
} else {
SendErrorMessage(session, message.Cmd, err.Error(), false, true)
}
} else {
err := NewError("No user found with id " + session.ID())
SendErrorMessage(session, message.Cmd, err.Error(), false, true)
}
case CmdExitGroup:
if player, ok := FindPlayer(session.ID()); ok {
decoder := json.NewDecoder(strings.NewReader(message.Msg))
exitInfo := new(ExitGroupMessage)
decoder.Decode(exitInfo)
if ok, err := player.ExitGroup(exitInfo.GroupId); ok {
SendStructMessage(session, message.Cmd, struct {
OK bool `json:"ok"`
}{OK: true}, true)
NotifyGroupListToAll()
} else {
SendErrorMessage(session, message.Cmd, err.Error(), false, true)
}
} else {
err := NewError("No user found with id " + session.ID())
SendErrorMessage(session, message.Cmd, err.Error(), false, true)
}
case CmdRegister:
decoder := json.NewDecoder(strings.NewReader(message.Msg))
registerInfo := new(RegisterMessage)
decoder.Decode(registerInfo)
if err := Register(session, registerInfo.ID, registerInfo.Name); err == nil {
SendStructMessage(session, message.Cmd, struct {
ID string `json:"id"`
Name string `json:"name"`
OK bool `json:"ok"`
}{session.ID(), registerInfo.Name, true}, true)
NotifyGroupListToAll()
} else {
SendErrorMessage(session, message.Cmd, err.Error(), false, true)
}
CheckPlayingGame(registerInfo.ID, session.ID())
case CmdStartGame:
if player, ok := FindPlayer(session.ID()); ok {
decoder := json.NewDecoder(strings.NewReader(message.Msg))
startGameMessage := new(StartGameMessage)
decoder.Decode(startGameMessage)
group := player.GroupHosted
if group == nil {
err := NewError("You haven't hosted a group.")
SendErrorMessage(session, message.Cmd, err.Error(), false, true)
}
if err := StartGame(group, startGameMessage.GroupId); err == nil {
//Notify all player to action
group.NotifyPlayer(message.Cmd, struct {
OK bool `json:"ok"`
}{true})
} else {
SendErrorMessage(session, message.Cmd, err.Error(), false, true)
}
} else {
err := NewError("No user found with id " + session.ID())
SendErrorMessage(session, message.Cmd, err.Error(), false, true)
}
case CmdGetData:
if player, ok := FindPlayer(session.ID()); ok {
if err := GetDataForPlayer(player); err != nil {
SendErrorMessage(session, message.Cmd, err.Error(), false, true)
}
} else {
err := NewError("No user found with id " + session.ID())
SendErrorMessage(session, message.Cmd, err.Error(), false, true)
}
case CmdPlayerAction:
if player, ok := FindPlayer(session.ID()); ok {
decoder := json.NewDecoder(strings.NewReader(message.Msg))
dataUpdateMessage := new(DataUpdateMessage)
decoder.Decode(dataUpdateMessage)
if player.GroupJoined != nil {
if err := UpdateData(player, player.GroupJoined, dataUpdateMessage.Action, dataUpdateMessage.Data); err == nil {
SendStructMessage(session, message.Cmd, struct {
OK bool `json:"ok"`
}{true}, true)
} else {
SendErrorMessage(session, message.Cmd, err.Error(), false, true)
}
} else {
err := NewError("You are not playing in the group")
SendErrorMessage(session, message.Cmd, err.Error(), false, true)
}
} else {
err := NewError("No user found with id " + session.ID())
SendErrorMessage(session, message.Cmd, err.Error(), false, true)
}
case CmdQuitGame:
if player, ok := FindPlayer(session.ID()); ok {
NotifyGroupListToOne(player)
}
case CmdStopGame:
if player, ok := FindPlayer(session.ID()); ok {
if player.GroupHosted != nil {
if player.GroupHosted.Playing {
player.GroupHosted.Playing = false
player.InGame = false
for _, p := range player.GroupHosted.Players {
p.InGame = false
}
player.GroupHosted.NotifyAllExcept(CmdHostStop, "", player)
}
}
NotifyGroupListToOne(player)
}
case CmdGetGameList:
SendStructMessage(session, message.Cmd, GetGameList(), true)
case CmdSpectateGame:
if player, ok := FindPlayer(session.ID()); ok {
decoder := json.NewDecoder(strings.NewReader(message.Msg))
spectInfo := new(SpectateGroupMessage)
decoder.Decode(spectInfo)
if ok, err := player.SpectateGame(spectInfo.GroupId); ok {
player.Index = 5
NotifyGroupListToSpectator(player)
SendStructMessage(session, message.Cmd, struct {
ID string `json:"groupId"`
OK bool `json:"ok"`
}{ID: player.GroupSpectating.ID, OK: true}, true)
} else {
SendErrorMessage(session, message.Cmd, err.Error(), false, true)
}
} else {
err := NewError("No user found with id " + session.ID())
SendErrorMessage(session, message.Cmd, err.Error(), false, true)
}
case CmdStopSpectating:
if player, ok := FindPlayer(session.ID()); ok {
if player.GroupSpectating != nil {
for i, spectator := range player.GroupSpectating.Spectators {
if player == spectator {
player.GroupSpectating.Spectators = append(player.GroupSpectating.Spectators[:i], player.GroupSpectating.Spectators[i+1:]...)
player.GroupSpectating = nil
player.InGame = false
player.Index = 0
NotifyGroupListToSpectator(player)
return
}
}
err := NewError("Not found the spectator ")
SendErrorMessage(session, message.Cmd, err.Error(), false, true)
}
} else {
err := NewError("No user found with id " + session.ID())
SendErrorMessage(session, message.Cmd, err.Error(), false, true)
}
}
}