forked from HimbeerserverDE/multiserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.go
65 lines (51 loc) · 1.39 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
package main
import "sync"
var onlinePlayers map[string]bool
var onlinePlayerMu sync.RWMutex
var onJoinPlayer []func(*Peer)
var onLeavePlayer []func(*Peer)
// RegisterOnJoinPlayer registers a callback function that is called
// when a TOSERVER_CLIENT_READY pkt is received from the Peer
func RegisterOnJoinPlayer(function func(*Peer)) {
onJoinPlayer = append(onJoinPlayer, function)
}
// RegisterOnLeavePlayer registers a callback function that is called
// when a client Peer disconnects
func RegisterOnLeavePlayer(function func(*Peer)) {
onLeavePlayer = append(onLeavePlayer, function)
}
func processJoin(p *Peer) {
onlinePlayerMu.Lock()
defer onlinePlayerMu.Unlock()
rpcSrvMu.Lock()
for srv := range rpcSrvs {
srv.doRpc("->JOIN "+string(p.username), "--")
}
rpcSrvMu.Unlock()
onlinePlayers[string(p.username)] = true
for i := range onJoinPlayer {
onJoinPlayer[i](p)
}
}
func processLeave(p *Peer) {
onlinePlayerMu.Lock()
defer onlinePlayerMu.Unlock()
rpcSrvMu.Lock()
for srv := range rpcSrvs {
srv.doRpc("->LEAVE "+string(p.username), "--")
}
rpcSrvMu.Unlock()
onlinePlayers[string(p.username)] = false
for i := range onLeavePlayer {
onLeavePlayer[i](p)
}
}
// IsOnline reports if a player is connected
func IsOnline(name string) bool {
onlinePlayerMu.RLock()
defer onlinePlayerMu.RUnlock()
return onlinePlayers[name]
}
func init() {
onlinePlayers = make(map[string]bool)
}