-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom_manager.go
103 lines (77 loc) · 2.11 KB
/
room_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
package goreal
import (
"log"
"time"
"github.com/kutase/go-gameloop"
)
type RoomManager struct {
Path string
RoomEvents RoomEvents //room
Clients map[*Client]bool
Config *RoomConfig
}
type RoomConfig struct {
MaxUser int
SimulationTick int
}
// new room manager
func newRoomManager(path string, roomEvents RoomEvents) *RoomManager {
config := &RoomConfig{MaxUser: 1000, SimulationTick: 10}
return &RoomManager{RoomEvents: roomEvents, Path: path, Config: config, Clients: make(map[*Client]bool)}
}
func (rm *RoomManager) OnInit(gs *GameServer) {
log.Println("room init", rm.Path)
rm.RoomEvents.Init(gs, rm.Clients, rm.Config, rm)
}
func (rm *RoomManager) CanJoinTheRoom(client *Client) bool {
// todo check if client already join
_, ok := rm.Clients[client]
if ok {
log.Println("Client is already join.")
return true
}
canJoin := rm.RoomEvents.OnJoinRequest(client)
return canJoin
}
func (rm *RoomManager) AddClientToRoom(client *Client) {
rm.Clients[client] = true
// listen client's message
client.ListenMessage(rm)
// send join information to room
rm.RoomEvents.OnClientJoin(client)
}
func (rm *RoomManager) RemoveClientFromRoom(client *Client) {
_, ok := rm.Clients[client]
if !ok {
log.Println("client is not in room!")
return
}
rm.RoomEvents.OnLeave(client)
client.RemoveListener(rm)
delete(rm.Clients, client)
}
func (rm *RoomManager) ReceiveMessage(client *Client, message []byte) {
rm.RoomEvents.OnMessage(client, message)
}
func (rm *RoomManager) run() {
log.Printf("start %s room manager.", rm.Path)
//rm.RoomEvents.OnInit()
duration := rm.Config.SimulationTick
gl := gameLoop.New(time.Duration(duration), func(delta float64) {
defer func() {
if error := recover(); error != nil {
log.Printf("Runtime Error FROM Room: %s, Err: %v", rm.Path, error)
}
}()
rm.RoomEvents.OnUpdate(delta)
})
gl.Start()
}
// used inside of the room instance
func (rm *RoomManager) Kick(client *Client) {
rm.RemoveClientFromRoom(client)
}
func (rm *RoomManager) DisconnectClient(client *Client) {
rm.RoomEvents.OnDisconnect(client)
rm.RemoveClientFromRoom(client)
}