-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgroup_manager.go
263 lines (237 loc) · 6.65 KB
/
group_manager.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package grouplay
import (
"fmt"
)
const (
MaxGroupNum = 10
)
var groups map[string]*GameGroup
func init() {
groups = make(map[string]*GameGroup)
}
type GameGroup struct {
ID string `json:"host"`
Host *GamePlayer `json:"host"`
MaxPlayer int `json:"limit"`
Players []*GamePlayer `json:"players"`
Spectators []*GamePlayer `json:"spectators"`
AllowSpectator bool `json:"allowSpectator"`
Playing bool `json:"playing"`
Game *Game `json:"game"`
}
type SimpleGroup struct {
ID string `json:"id"`
Host *SimplePlayer `json:"host"`
MaxPlayer int `json:"limit"`
Players []*SimplePlayer `json:"players"`
Spectators []*SimplePlayer `json:"spectators"`
AllowSpectator bool `json:"allowSpectator"`
Playing bool `json:"playing"`
Game *Game `json:"game"`
}
type SimplePlayer struct {
Name string `json:"name"`
ID string `json:""`
}
type MyInfo struct {
Session string `json:"session"`
InGame bool `json:"ingame"`
Index int `json:"index"`
}
// Join : Let a player join into a group if the group doesn't reach the max size.
func (g *GameGroup) Join(p *GamePlayer) error {
if g.Playing {
return NewError("The game has been already started, try spectating!")
}
if length := len(g.Players); length == g.MaxPlayer {
return NewError("Player number has reached the max size.")
}
if g.Exist(p) {
return NewError("Already added into the group.")
}
g.Players = append(g.Players, p)
return nil
}
// Spectate : Let a player spectate a group game if it is playing.
func (g *GameGroup) Spectate(p *GamePlayer) error {
fmt.Println("GameGroup1==>", g)
if g.Playing {
if length := len(g.Players); length == g.MaxPlayer {
g.Spectators = append(g.Spectators, p)
} else {
return NewError("The player have left!")
}
} else {
return NewError("The game not start, try to join it!")
}
fmt.Println("GameGroup2==>", g)
return nil
}
func (g *GameGroup) Exit(p *GamePlayer) error {
if g.Playing {
return NewError("The game has been already started, can't exit!")
}
for i, player := range g.Players {
if player == p {
// Remove relation
player.GroupJoined = nil
g.Players = append(g.Players[:i], g.Players[i+1:]...)
// If is host of a group
if player.GroupHosted != nil {
player.GroupHosted.Host = nil
player.GroupHosted = nil
delete(groups, g.ID)
}
// Set new mapping
if len(g.Players) > 0 {
if g.Host == nil {
newHost := g.Players[0]
g.Host = newHost
g.ID = newHost.ID
newHost.GroupHosted = g
groups[g.ID] = g
}
}
return nil
}
}
return NewError("You are not in the specified group.")
}
func (g *GameGroup) Exist(p *GamePlayer) bool {
for _, player := range g.Players {
if player == p {
return true
}
}
return false
}
func FindGroup(id string) (group *GameGroup, ok bool) {
fmt.Println("Try to find a group with id", id)
if id == "" {
return group, false
}
group, ok = groups[id]
return group, ok
}
// CreateGroup : Create a group and name it with the player's name
func CreateGroup(game *Game, player *GamePlayer, max int, allowSpectate bool) (group *GameGroup) {
group = &GameGroup{
ID: player.ID,
Host: player,
MaxPlayer: max,
Players: make([]*GamePlayer, 0),
Spectators: make([]*GamePlayer, 0),
AllowSpectator: allowSpectate,
Playing: false,
Game: game,
}
// Add to group
groups[group.ID] = group
return group
}
func generateSimplePlayer(p *GamePlayer) *SimplePlayer {
simple := SimplePlayer{
ID: p.ID,
Name: p.Name,
}
return &simple
}
func generateSimplePlayerArray(list []*GamePlayer) []*SimplePlayer {
simple := make([]*SimplePlayer, len(list))
for i, p := range list {
simple[i] = generateSimplePlayer(p)
}
return simple
}
func generateSimpleGroup(g *GameGroup) *SimpleGroup {
simple := SimpleGroup{
ID: g.ID,
Host: generateSimplePlayer(g.Host),
MaxPlayer: g.MaxPlayer,
Players: generateSimplePlayerArray(g.Players),
Spectators: generateSimplePlayerArray(g.Spectators),
AllowSpectator: g.AllowSpectator,
Playing: g.Playing,
Game: g.Game,
}
return &simple
}
func BuildGroupList() GroupListMessage {
waiting := make([]*SimpleGroup, 0)
playing := make([]*SimpleGroup, 0)
for _, group := range groups {
simpleGroup := generateSimpleGroup(group)
if group.Playing {
fmt.Println("Add playing group")
playing = append(playing, simpleGroup)
} else {
fmt.Println("Add waiting group")
waiting = append(waiting, simpleGroup)
}
}
return GroupListMessage{nil, nil, waiting, playing}
}
func NotifyGroupListToSpectator(p *GamePlayer) {
groupList := BuildGroupList()
if p.GroupSpectating != nil {
groupList.Joined = generateSimpleGroup(p.GroupSpectating)
} else {
groupList.Joined = nil
}
// Create my info
groupList.Info = &MyInfo{p.ID, p.InGame, p.Index}
SendStructMessage(*p.Session, CmdGroupUpdate, groupList, true)
}
func NotifyGroupListToOne(p *GamePlayer) {
groupList := BuildGroupList()
if p.GroupJoined != nil {
groupList.Joined = generateSimpleGroup(p.GroupJoined)
} else {
groupList.Joined = nil
}
// Create my info
groupList.Info = &MyInfo{p.ID, p.InGame, 0}
SendStructMessage(*p.Session, CmdGroupUpdate, groupList, true)
}
func NotifyGroupListToAll() {
groupList := BuildGroupList()
for _, p := range players {
fmt.Println("player-->", p)
if p.GroupJoined != nil {
groupList.Joined = generateSimpleGroup(p.GroupJoined)
} else {
groupList.Joined = nil
}
// Create my info
groupList.Info = &MyInfo{p.ID, p.InGame, 0}
SendStructMessage(*p.Session, CmdGroupUpdate, groupList, true)
}
}
func (g *GameGroup) NotifyPlayerExcept(cmd string, msg interface{}, player *GamePlayer) {
for _, p := range g.Players {
if p != player {
SendStructMessage(*p.Session, cmd, msg, true)
}
}
}
func (g *GameGroup) NotifyPlayer(cmd string, msg interface{}) {
g.NotifyPlayerExcept(cmd, msg, nil)
}
func (g *GameGroup) NotifySpectatorExcept(cmd string, msg interface{}, player *GamePlayer) {
for _, p := range g.Spectators {
if p != player {
SendStructMessage(*p.Session, cmd, msg, true)
}
}
}
func (g *GameGroup) NotifySpectator(cmd string, msg interface{}) {
g.NotifySpectatorExcept(cmd, msg, nil)
}
func (g *GameGroup) NotifyAll(cmd string, msg interface{}) {
g.NotifyPlayerExcept(cmd, msg, nil)
g.NotifySpectatorExcept(cmd, msg, nil)
}
func (g *GameGroup) NotifyAllExcept(cmd string, msg interface{}, player *GamePlayer) {
g.NotifyPlayerExcept(cmd, msg, player)
g.NotifySpectatorExcept(cmd, msg, player)
}