capsuled.lua
7.58 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
require "ProtocolCode"
require "shared.init"
require "GlobalVar"
require "RedisKeys"
require "skynet.manager"
skynet = require "skynet"
csvdb = require "shared.csvdata"
redisproxy = require "shared.redisproxy"
datacenter = require "skynet.datacenter"
local capsules = {}
local CMD = {}
NotifyChangeType = {
JOIN = 1,
EXIT = 2,
DRAW = 3,
SPECIAL = 4,
}
skynet.register_protocol {
name = "role",
id = 101,
pack = skynet.pack,
unpack = skynet.unpack,
}
local function rpcRole(roleId, funcName, ...)
local fields = ...
local agent = datacenter.get("agent", roleId)
if agent and agent.serv then
if funcName == "getProperties" then
return true, skynet.call(agent.serv, "role", funcName, fields)
else
return true, skynet.call(agent.serv, "role", funcName, ...)
end
else
local roleCross = require("models.RoleCross")
if funcName == "getProperties" then
return false, roleCross.handle(funcName, roleId, fields)
else
return false, roleCross.handle(funcName, roleId, ...)
end
end
end
function getNameByRoleId(roleId)
local status, name = rpcRole(roleId, "getProperty", "name")
return name
end
function broadCastCapsule(roleId, capsuleId, broadInfo)
local capsule = capsules[capsuleId]
if not capsule then print("not capsule :" .. capsuleId) return end
local register = capsule:getProperty("register") or {}
if next(capsule) then
for id, _ in pairs(register) do
if id ~= roleId then
rpcRole(id, "SendPacket", actionCodes.Capsule_notifyChange, MsgPack.pack(broadInfo)) -- 通知对方
end
end
end
end
function broadCastSpecial(roleId, capsuleId, broadInfo)
if not broadInfo or not next(broadInfo) then return end
local capsule = capsules[capsuleId]
if not capsule then skynet.error("not capsule: " .. capsuleId) return end
local register = capsule:getProperty("register") or {}
for id, _ in pairs(register) do
if id ~= roleId then
if broadInfo[id] then
rpcRole(id, "paySpecialReward", id, broadInfo[id])
end
end
end
end
local function add(roleId, capsuleId)
local capsule = capsules[capsuleId]
if not capsule then skynet.error("not capsule: " .. capsuleId) return end
if next(capsule) then
capsule:join(roleId)
broadCastCapsule(roleId, capsuleId, {notifyType= NotifyChangeType.JOIN, roleId = roleId})
return capsule:data(roleId)
end
print("id 不存在: ".. capsuleId)
return nil
end
local function capsuleRefreshing()
for _, v in pairs(capsules) do
if v:refreshing() then
v:init()
v:create()
end
end
end
--扭蛋机刷新
local function check_capsules()
pcall(capsuleRefreshing)
skynet.timeout(60, check_capsules)
end
function CMD.reset()
local now = skynet.timex()
local res = redisproxy:smembers(CAPSULE_INFO) or {}
for _, key in pairs(res) do
redisproxy:del(CAPSULE_PUBLIC:format(key))
redisproxy:srem(CAPSULE_INFO, key)
end
for _, data in pairs(csvdb["ichibankuji_mainCsv"]) do
for _, val in ipairs(data) do
if val.type == 1 then
local key = val.id..val.room
if not capsules[key] then
local capsule = require("models.Capsule").new({ key = CAPSULE_PUBLIC:format(key), id= val.id, room = val.room, typ = 1, name=val.name})
capsule:init()
capsule:create()
capsules[key] = capsule
redisproxy:sadd(CAPSULE_INFO, key)
end
end
end
end
end
function CMD.start()
local now = skynet.timex()
local res = redisproxy:smembers(CAPSULE_INFO) or {}
for _, key in pairs(res) do
local capsule = require("models.Capsule").new({ key = CAPSULE_PUBLIC:format(key)})
capsule:load()
if capsule:isShow() then
if capsule:refreshing(now) then
capsule:init()
capsule:create()
end
capsules[key] = capsule
else
redisproxy:del(CAPSULE_PUBLIC:format(key))
redisproxy:srem(CAPSULE_INFO, key)
end
end
for _, data in pairs(csvdb["ichibankuji_mainCsv"]) do
for _, val in ipairs(data) do
if val.type == 1 then
local key = val.id..val.room
if not capsules[key] then
local capsule = require("models.Capsule").new({ key = CAPSULE_PUBLIC:format(key), id= val.id, room = val.room, typ = 1, name=val.name})
capsule:init()
capsule:create()
capsules[key] = capsule
redisproxy:sadd(CAPSULE_INFO, key)
end
end
end
end
check_capsules()
end
function CMD.list(coin)
local tmpCapsules = {}
for k, v in pairs(capsules) do
if v:getProperty("coin") == coin then
local onlineCount= v:getOnlineCount()
tmpCapsules[k] = {id=v:getProperty("id"), room=v:getProperty("room"), typ=v:getProperty("typ"), people=onlineCount[2], coin= v:getProperty("coin")}
end
end
return tmpCapsules
end
function CMD.join(roleId, capsuleId)
if capsuleId then
return add(roleId, capsuleId)
end
return nil
end
function CMD.exit(roleId, capsuleId)
if capsuleId then
local capsule = capsules[capsuleId]
if not capsule then skynet.error("not capsule: " .. capsuleId) return end
capsule:exit(roleId)
broadCastCapsule(roleId, capsuleId, {notifyType = NotifyChangeType.EXIT, roleId = roleId})
else
for _, capsule in pairs(capsules) do
if next(capsule) then
capsule:exit(roleId)
broadCastCapsule(roleId, capsuleId, {notifyType = NotifyChangeType.EXIT, roleId = roleId})
end
end
end
end
function CMD.draw_capsule(roleId, capsuleId, full, drawsNum, cares)
local capsule = capsules[capsuleId]
if not capsule then skynet.error("not capsule: " .. capsuleId) return 2 end
local ret, drawReward = capsule:draw(roleId, full, drawsNum, cares)
if ret > 5 then
--broadCastCapsule(roleId, capsuleId, {notifyType = NotifyChangeType.DRAW, notify = notify})
broadCastSpecial(roleId, capsuleId, drawReward["specials"])
end
drawReward["specials"] = nil
return ret, drawReward, capsule:data(roleId)
end
function CMD.register(roleId, capsuleId)
local capsule = capsules[capsuleId]
if not capsule then skynet.error("not capsule: " .. capsuleId) return nil end
return capsule:register(roleId)
end
function CMD.goods_stock(capsuleId)
local capsule = capsules[capsuleId]
if not capsule then skynet.error("not capsule: " .. capsuleId) return 0 end
return capsule:getGoodsAmount(), capsule:getProperty("token")
end
function CMD.capsule_data(roleId, capsuleId)
local capsule = capsules[capsuleId]
if not capsule then skynet.error("not capsule: " .. capsuleId) return nil end
return capsule:data(roleId)
end
function CMD.page_record(capsuleId, up, idx)
local capsule = capsules[capsuleId]
if not capsule then skynet.error("not capsule: " .. capsuleId) return nil end
return capsule:pageRecord(up, idx)
end
skynet.start(function()
skynet.dispatch("lua", function(session, address, cmd, ...)
local f = CMD[string.lower(cmd)]
skynet.ret(skynet.pack(f(...)))
end)
skynet.register("capsuled")
globalCsv = csvdb["GlobalDefineCsv"]
end)