robot.lua
5.1 KB
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
require "ProtocolCode"
require "shared.init"
require "utils.init"
skynet = require "skynet"
local socketdriver = require "skynet.socketdriver"
local netpack = require "skynet.netpack"
local xxtea = require "xxtea"
local httpc = require("http.httpc")
local config = require "robot_config"
local XXTEA_KEY = "699D448D6D24f7F941E9F6E99F823E18"
local CMD = {}
local client = {}
local eventListener = {}
local ignoreListener = {
["Role.updateProperty"] = function(data)
if not client.role then return end
local msg = MsgPack.unpack(data)
for _, one in pairs(msg) do
client.role[one.key] = one.newValue
end
end,
["Role.updateProperties"] = function(data)
local msg = MsgPack.unpack(data)
for field, value in pairs(msg) do
client.role[field] = value
end
end,
["Role.notifyNewEvent"] = true,
["Role.chat"] = true,
["Role.updateItems"] = true,
["Gm.receiveResponse"] = true,
["Role.changeUpdate"] = true,
["Role.loadRunes"] = true,
["Hero.loadInfos"] = true,
["Store.updateproperty"] = true,
["Sys.maintainNotice"] = true,
["Hero.drawHeroExtraRewardNtf"] = true,
["Sys.innerErrorMsg"] = function(data)
local msg = MsgPack.unpack(data)
log("innerErrorMsg: " .. msg.id)
end,
}
function addListener(actionCode, callback, isKeep)
callback = callback or function() end
local handlerName = actionHandlers[actionCode]
if string.sub(handlerName, -3, -1) == "Rpc" then
actionCode = actionCode + rpcResponseBegin
end
if eventListener[actionHandlers[actionCode]] then
log(handlerName .. " had listener")
end
local trueCall = callback
if not isKeep then
trueCall = function(data)
removeListener(actionCode)
callback(data)
end
end
eventListener[actionHandlers[actionCode]] = trueCall
end
function removeListener(actionCode)
local handlerName = actionHandlers[actionCode]
if string.sub(handlerName, -3, -1) == "Rpc" then
actionCode = actionCode + rpcResponseBegin
end
eventListener[actionHandlers[actionCode]] = nil
end
function triggerListener(listener, data)
if eventListener[listener] then
if #data > 0 then data = xxtea.decrypt(data, XXTEA_KEY) end
eventListener[listener](data)
elseif ignoreListener[listener] then
if type(ignoreListener[listener]) == "function" then
if #data > 0 then data = xxtea.decrypt(data, XXTEA_KEY) end
ignoreListener[listener](data)
end
else
-- log(listener .. " no handle!!!")
end
end
function sendServer(actionCode, bin)
local session = 0
local handlerName = actionHandlers[actionCode]
if actionCode == actionCodes.Role_queryLoginRpc or string.sub(handlerName, -3, -1) ~= "Rpc" then
session = nil
end
local data = string.pack("H", actionCode)
if session then
data = data .. string.pack("H", session)
end
if #bin > 0 then bin = xxtea.encrypt(bin, XXTEA_KEY) end
data = data .. bin
socketdriver.send(client.fd, netpack.pack(data))
end
function requestServer(actionCode, bin, callback, isKeep)
addListener(actionCode, callback, isKeep)
sendServer(actionCode, bin)
end
local function heartBeat()
sendServer(actionCodes.Sys_heartBeat, "")
skynet.timeout(500, heartBeat)
end
local function startUnit(unit)
if unit == "login" then
else
unit = math.randWeight(config.units, "weight")
end
local ok, unitTest = pcall(require, "unitTest." .. unit)
if not ok then
log("unitTest load error : " .. unitTest)
end
unitTest.new(client):startTest()
end
-- 登录成功开始任务
function CMD.task()
heartBeat()
-- 20 秒后开始执行任务 错开登录
skynet.sleep(math.randomInt(2000, 3000))
startUnit()
end
function randomName()
local str = "1234567890abcdefghijklmnopqrstuvwxyz"
local len = #str
local result = ""
for i=1,10 do
local idx = math.random(1, len)
result = result .. string.sub(str, idx, idx)
end
return result
end
-- 开始登录
function CMD.start(fd, id)
client.fd = fd
client.clientId = id
local uname = config.uname_prefix .. id
client.uname = uname
local status, body = httpc.get(config.http, "/login?" .. httpGetFormatData({token = uname, device = "test", channel = "develop"}))
if tonumber(status) ~= 200 then
log("http get error", uname, body)
return
end
local servInfo = json.decode(body)
client.uid = servInfo.userid
startUnit("login")
end
-- 退出
function CMD.exit()
sendServer(actionCodes.Gm_clientRequest, MsgPack.pack({cmd = "gmmsg", pm1 = "123"}))
skynet.sleep(50)
skynet.ret(skynet.pack())
skynet.exit()
end
skynet.register_protocol {
name = "client",
id = skynet.PTYPE_CLIENT,
unpack = function (msg, sz)
local data = skynet.tostring(msg, sz)
local cmd = string.unpack("H", string.sub(data, 1, 2))
return cmd, string.sub(data, 3)
end,
dispatch = function(session, address, cmd, data)
local actionName = actionHandlers[cmd]
if not actionName then
log("actionName not exist", actionName)
return
end
if cmd > rpcResponseBegin and actionName ~= "Role.queryLoginRpcResponse" then
-- 回复 有session
session = string.unpack("H", string.sub(data, 1, 2))
data = string.sub(data, 3)
end
triggerListener(actionName, data)
end,
}
skynet.start(function ()
skynet.dispatch("lua", function (_,_,cmd,...)
skynet.ret(skynet.pack(CMD[cmd](...)))
end)
end)