-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathlogic.lua
86 lines (68 loc) · 1.45 KB
/
logic.lua
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
require "call"
require "role"
-- connect id
gconnid = 0
Logic = {
roles = {}
}
function Logic:onConnect(conn)
gconnid = gconnid + 1
local role = Role:new()
role.session = conn
conn:setUsernum(gconnid)
self.roles[gconnid] = role
return role
end
function Logic:onClose(conn)
local connid = conn:getUsernum()
local role = self.roles[connid]
role.session = nil
self.roles[connid] = nil
end
function Logic:onMsg(conn, callid, pack)
local connid = conn:getUsernum()
local role = self.roles[connid]
-- call
local func = getcallfu(callid)
func(role, pack)
end
-- role id
groleid = 1000;
function role_enter(role)
groleid = groleid + 1
role.roleid = groleid
-- server exec
print("role[".. groleid .. "] enter server")
local pack = NetPacket:new()
pack:pushString("sync role[".. groleid .. "] data")
role:call(sync_role, pack)
end
function sync_role(role, pack)
-- client exec
print(pack:getString())
local ret = NetPacket:new()
ret:pushString("hello server")
role:call(msg_one, ret)
end
function msg_one(role, pack)
-- server exec
print(pack:getString())
local ret = NetPacket:new()
ret:pushString("hello client")
role:call(msg_two, ret)
end
function msg_two(role, pack)
-- client exec
print(pack:getString())
local ret = NetPacket:new()
ret:pushString("bye bye!")
role:call(msg_bye, ret)
end
function msg_bye(role, pack)
-- server exec
print(pack:getString())
end
register(sync_role)
register(msg_one)
register(msg_two)
register(msg_bye)