-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.go
77 lines (62 loc) · 1.33 KB
/
player.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
package wvss
import (
"golang.org/x/net/websocket"
)
// Define player team
const (
TeamWalrus int = iota
TeamSlime
)
// Define player
type Player struct {
Conn *websocket.Conn
Team int
Matched bool
Room *Room
}
// Define room
type Room struct {
Player1 *Player
Player2 *Player
}
func NewRoom(player1 *Player, player2 *Player) *Room {
room := &Room{player1, player2}
player1.Matched = true
player1.Room = room
player2.Matched = true
player2.Room = room
return room
}
func (r *Room) Broadcast(packet *Packet) (err error) {
Msgpack.Send(r.Player1.Conn, packet)
Msgpack.Send(r.Player2.Conn, packet)
return
}
// Player List
type PlayerList []*Player
func (pl *PlayerList) UnmatchPlayer(team int) (list []*Player) {
// Initialize empty list
list = make([]*Player, 0)
for _, v := range *pl {
if v.Team == -1 { // Skip no register team's player
continue
}
if v.Team != team && v.Matched == false {
list = append(list, v)
}
}
return
}
func (pl *PlayerList) Add(conn *websocket.Conn) *Player {
player := Player{Conn: conn, Matched: false, Team: -1}
*pl = append(*pl, &player)
return &player
}
func (pl *PlayerList) Remove(player *Player) {
for k, v := range *pl {
if player == v {
player.Room.Broadcast(&Packet{StatusPacket, &Status{"Exit", 1}})
*pl = append((*pl)[:k], (*pl)[k+1:]...)
}
}
}