Blame view

src/actions/HangAction.lua 9.69 KB
384bb077   zhouahaihai   挂机
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  local ipairs = ipairs
  local table = table
  local math = math
  local next = next
  local string = string
  local redisproxy = redisproxy
  local MsgPack = MsgPack
  local getRandomName = getRandomName
  local mcast_util = mcast_util
  local string_format = string.format
  local tonumber = tonumber
  local require = require
  local table_insert = table.insert
  local tconcat = table.concat
  local table_unpack = table.unpack
  
  local _M = {}
  
  local function checkReward(role)
  	local hangInfo = role:getProperty("hangInfo")
  	if not hangInfo.carbonId or not hangInfo.coinTime or not hangInfo.itemTime then
  		return
  	end
  	local carbonData = csvdb["idle_battleCsv"][hangInfo.carbonId]
bcf5bfbb   zhouahaihai   挂机new
25
26
  	local nowCoinTime = math.min(skynet.timex(), hangInfo.endCoinTime or 0)
  	local nowItemTime = math.min(skynet.timex(), hangInfo.endItemTime or 0)
384bb077   zhouahaihai   挂机
27
  
bcf5bfbb   zhouahaihai   挂机new
28
  	local coinCount = math.max(0, math.floor((nowCoinTime - hangInfo.coinTime) / globalCsv.idle_money_produce_cd))
9962b061   zhouahaihai   补充 挂机
29
  	hangInfo.coinTime = hangInfo.coinTime + coinCount * globalCsv.idle_money_produce_cd
384bb077   zhouahaihai   挂机
30
  
bcf5bfbb   zhouahaihai   挂机new
31
  	local itemCount = math.max(0, math.floor((nowItemTime - hangInfo.itemTime) / globalCsv.idle_item_produce_cd))
9962b061   zhouahaihai   补充 挂机
32
  	hangInfo.itemTime = hangInfo.itemTime + itemCount * globalCsv.idle_item_produce_cd
384bb077   zhouahaihai   挂机
33
34
35
36
  
  	local items = role:getProperty("hangBag")
  	items[ItemId.Gold] = (items[ItemId.Gold] or 0) + coinCount * carbonData.money
  	items[ItemId.Exp] = (items[ItemId.Exp] or 0) + coinCount * carbonData.exp
4b7c7c96   zhouahaihai   增加 清空 挂机 冒险gm 角色经验
37
  	items[ItemId.PlayerExp] = (items[ItemId.PlayerExp] or 0) + coinCount * carbonData.playerExp
384bb077   zhouahaihai   挂机
38
39
40
41
42
  
  	local pool = {}
  	for _, temp in pairs(carbonData.item:toArray()) do
  		table.insert(pool, temp:toArray(true, "="))
  	end
9962b061   zhouahaihai   补充 挂机
43
44
  	local curTypeCount = 0
  	for id, _ in pairs(items) do
bcf5bfbb   zhouahaihai   挂机new
45
  		if id ~= ItemId.Gold and id ~= ItemId.Exp and id ~= ItemId.PlayerExp then
9962b061   zhouahaihai   补充 挂机
46
47
48
  			curTypeCount = curTypeCount + 1
  		end
  	end
384bb077   zhouahaihai   挂机
49
50
  	for i = 1, itemCount do
  		local cur = pool[math.randWeight(pool, 3)]
9962b061   zhouahaihai   补充 挂机
51
52
53
54
55
56
  		if items[cur[1]] or curTypeCount < role:getProperty("hangBagLimit") then
  			if not items[cur[1]] then
  				curTypeCount = curTypeCount + 1
  			end
  			items[cur[1]] = (items[cur[1]] or 0) + cur[2]
  		end
384bb077   zhouahaihai   挂机
57
58
59
  	end
  
  	if coinCount > 0 or itemCount > 0 then
5af8b4aa   zhouahaihai   挂机down
60
  		return true
384bb077   zhouahaihai   挂机
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  	end
  end
  
  
  
  --开始一个新的关卡
  function _M.startRpc( agent, data )
  	local role = agent.role
  	local msg = MsgPack.unpack(data)
  	local carbonId = msg.carbonId
  	local carbonData = csvdb["idle_battleCsv"][carbonId]
  	if not carbonData then return end
  
  	local hangPass = role:getProperty("hangPass")
5d80aca6   zhouahaihai   挂机优化
75
76
77
78
  
  	for _, preCarbonId in ipairs(carbonData.prepose:toArray(true, "=")) do
  		if not hangPass[preCarbonId] then return end
  	end
384bb077   zhouahaihai   挂机
79
  
5af8b4aa   zhouahaihai   挂机down
80
81
82
  	if checkReward(role) then
  		role:updateProperty({field = "hangBag", value = role:getProperty("hangBag")})
  	end
384bb077   zhouahaihai   挂机
83
84
  
  	local hangInfo = role:getProperty("hangInfo")
5af8b4aa   zhouahaihai   挂机down
85
  	local isNew = not hangInfo.carbonId
384bb077   zhouahaihai   挂机
86
87
88
89
  	hangInfo.carbonId = carbonId
  	local nowTime = skynet.timex()
  	hangInfo.coinTime = nowTime
  	hangInfo.itemTime = nowTime
5af8b4aa   zhouahaihai   挂机down
90
  	if isNew then
bcf5bfbb   zhouahaihai   挂机new
91
92
  		hangInfo.endCoinTime = nowTime + globalCsv.idle_producetime_max
  		hangInfo.endItemTime = nowTime + globalCsv.idle_producetime_max
5af8b4aa   zhouahaihai   挂机down
93
  	end
384bb077   zhouahaihai   挂机
94
  	if not hangPass[carbonId] then
5af8b4aa   zhouahaihai   挂机down
95
96
97
  		hangInfo.bossTime = nowTime + carbonData.idle_time
  	else
  		hangInfo.bossTime = nil
384bb077   zhouahaihai   挂机
98
99
100
101
102
103
104
105
106
107
108
  	end
  	role:updateProperty({field = "hangInfo", value = hangInfo})
  
  	SendPacket(actionCodes.Hang_startRpc, '')
  	return true
  end
  
  -- 每隔1分钟检查一次
  function _M.checkRpc(agent, data)
  	local role = agent.role
  	-- local msg = MsgPack.unpack(data)
5af8b4aa   zhouahaihai   挂机down
109
110
111
112
  	if checkReward(role) then
  		role:updateProperty({field = "hangBag", value = role:getProperty("hangBag")})
  		role:updateProperty({field = "hangInfo", value = role:getProperty("hangInfo")})
  	end
384bb077   zhouahaihai   挂机
113
114
115
116
117
118
119
  	SendPacket(actionCodes.Hang_checkRpc, MsgPack.pack({}))
  	return true
  end
  
  function _M.startBattleRpc(agent, data)
  	local role = agent.role
  	local msg = MsgPack.unpack(data)
bcf5bfbb   zhouahaihai   挂机new
120
121
  	local carbonId = msg.carbonId
  	local curData = csvdb["idle_battleCsv"][carbonId]
a1e08c9c   zhengshouren   宝箱关战斗异常
122
123
124
  	if not curData then 
  		return 1
  	end
bcf5bfbb   zhouahaihai   挂机new
125
  
384bb077   zhouahaihai   挂机
126
  	local hangInfo = role:getProperty("hangInfo")
bcf5bfbb   zhouahaihai   挂机new
127
  	if curData.main ~= 1 then
a1e08c9c   zhengshouren   宝箱关战斗异常
128
129
130
  		if carbonId ~= hangInfo.carbonId then 
  			return 2
  		end
bcf5bfbb   zhouahaihai   挂机new
131
132
  	end
  
384bb077   zhouahaihai   挂机
133
  	local hangPass = role:getProperty("hangPass")
a1e08c9c   zhengshouren   宝箱关战斗异常
134
135
136
  	if hangPass[carbonId] then 
  		return 3
  	end
bcf5bfbb   zhouahaihai   挂机new
137
  
384bb077   zhouahaihai   挂机
138
139
140
  	local key = tostring(math.random())
  	hangInfo.key = key
  	local nowTime = skynet.timex()
384bb077   zhouahaihai   挂机
141
142
143
144
145
146
147
148
149
  	role:updateProperty({field = "hangInfo", value = hangInfo})
  	SendPacket(actionCodes.Hang_startBattleRpc, MsgPack.pack({key = key}))
  	return true
  end
  
  function _M.endBattleRpc(agent, data)
  	local role = agent.role
  	local msg = MsgPack.unpack(data)
  	local hangInfo = role:getProperty("hangInfo")
4906ebd2   zhengshouren   宝箱关副本结算异常
150
151
152
  	if not msg.key or msg.key ~= hangInfo.key then 
  		return 1
  	end
bcf5bfbb   zhouahaihai   挂机new
153
154
  	local carbonId = msg.carbonId
  	local carbonData = csvdb["idle_battleCsv"][carbonId]
4906ebd2   zhengshouren   宝箱关副本结算异常
155
156
157
  	if not carbonData then
  		return 2
  	end
bcf5bfbb   zhouahaihai   挂机new
158
  	if carbonData.main ~= 1 then
4906ebd2   zhengshouren   宝箱关副本结算异常
159
160
161
  		if carbonId ~= hangInfo.carbonId then 
  			return 3
  		end
bcf5bfbb   zhouahaihai   挂机new
162
  	end
384bb077   zhouahaihai   挂机
163
  	local hangPass = role:getProperty("hangPass")
4906ebd2   zhengshouren   宝箱关副本结算异常
164
165
166
  	if hangPass[carbonId] then 
  		return 4
  	end
384bb077   zhouahaihai   挂机
167
168
  	local reward
  	if msg.starNum and msg.starNum > 0 then --win
bcf5bfbb   zhouahaihai   挂机new
169
  		hangPass[carbonId] = 1
384bb077   zhouahaihai   挂机
170
  		role:updateProperty({field = "hangPass", value = hangPass})
bcf5bfbb   zhouahaihai   挂机new
171
172
173
  		if carbonData.main ~= 1 then
  			hangInfo.bossTime = nil
  		end
384bb077   zhouahaihai   挂机
174
175
  		-- reward
  		reward = {}
384bb077   zhouahaihai   挂机
176
177
  		reward[ItemId.Gold] = carbonData.money_clear
  		reward[ItemId.Exp] = carbonData.exp_clear
00115a7a   zhouahaihai   奖励发放
178
  		reward[ItemId.PlayerExp] = carbonData.playerExp_clear
384bb077   zhouahaihai   挂机
179
  		for itemId, count in pairs(carbonData.item_clear:toNumMap()) do
384bb077   zhouahaihai   挂机
180
181
  			reward[itemId] = count
  		end
8c199cec   zhengshouren   简化物品奖励接口,设置默认参数
182
  		reward = role:award(reward)
00e663bd   zhouhaihai   剧情相关
183
184
  
  		role:checkTaskEnter(role.TaskType.HangPass, {id = carbonId})
384bb077   zhouahaihai   挂机
185
186
187
188
189
190
191
192
193
194
195
196
197
  	end
  	hangInfo.key = nil
  	role:updateProperty({field = "hangInfo", value = hangInfo})
  	SendPacket(actionCodes.Hang_endBattleRpc, MsgPack.pack({
  		starNum = msg.starNum, 
  		reward = reward,
  		}))
  	return true
  end
  
  function _M.roleFormatRpc(agent , data)
  	local role = agent.role
  	local msg = MsgPack.unpack(data)
384bb077   zhouahaihai   挂机
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
  	local hangTeam = role:getProperty("hangTeam")
  	for slot, heroId in pairs(msg.heros) do
  		if not role.heros[heroId] then
  			return
  		end
  	end
  	table.clear(hangTeam)
  	hangTeam.heros = {}
  	for slot, heroId in pairs(msg.heros) do
  		hangTeam.heros[slot] = heroId
  	end
  	hangTeam.leader = msg.leader
  
  	role:updateProperty({field = "hangTeam", value = hangTeam}) 
  	SendPacket(actionCodes.Hang_roleFormatRpc, '')
  	return true
  end
  
5af8b4aa   zhouahaihai   挂机down
216
217
218
219
220
  function _M.getRewardRpc(agent , data)
  	local role = agent.role
  	checkReward(role)
  	local items = role:getProperty("hangBag")
  	if not next(items) then return end
8c199cec   zhengshouren   简化物品奖励接口,设置默认参数
221
  	local reward = role:award(items)
5af8b4aa   zhouahaihai   挂机down
222
223
224
  	table.clear(items)
  	local hangInfo = role:getProperty("hangInfo")
  	local nowTime = skynet.timex()
bcf5bfbb   zhouahaihai   挂机new
225
226
  	hangInfo.endItemTime = nowTime + globalCsv.idle_producetime_max
  	hangInfo.endCoinTime = nowTime + globalCsv.idle_producetime_max
5af8b4aa   zhouahaihai   挂机down
227
228
229
230
231
232
233
234
235
236
237
  	hangInfo.coinTime = nowTime
  	hangInfo.itemTime = nowTime
  	role:updateProperty({field = "hangBag", value = items})
  	role:updateProperty({field = "hangInfo", value = hangInfo})
  
  	SendPacket(actionCodes.Hang_getRewardRpc, MsgPack.pack({
  		reward = reward
  	}))
  	return true
  end
  
bcf5bfbb   zhouahaihai   挂机new
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
  function _M.getRewardItemRpc(agent , data)
  	local role = agent.role
  	checkReward(role)
  	local items = role:getProperty("hangBag")
  	if not next(items) then return end
  
  	local notNeed = {[ItemId.Gold] = 1, [ItemId.Exp] = 1, [ItemId.PlayerExp] = 1}
  	local reward = {}
  	for itemId , count in pairs(items) do
  		if not notNeed[itemId] then
  			reward[itemId] = count
  			items[itemId] = nil
  		end
  	end
  	if not next(reward) then return end
8c199cec   zhengshouren   简化物品奖励接口,设置默认参数
253
  	local reward = role:award(reward)
bcf5bfbb   zhouahaihai   挂机new
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
  
  	local hangInfo = role:getProperty("hangInfo")
  	local nowTime = skynet.timex()
  	hangInfo.endItemTime = nowTime + globalCsv.idle_producetime_max
  	hangInfo.itemTime = nowTime
  	role:updateProperty({field = "hangBag", value = items})
  	role:updateProperty({field = "hangInfo", value = hangInfo})
  
  	SendPacket(actionCodes.Hang_getRewardItemRpc, MsgPack.pack({
  		reward = reward
  	}))
  	return true
  end
  
  function _M.getRewardCoinRpc(agent , data)
  	local role = agent.role
  	checkReward(role)
  	local items = role:getProperty("hangBag")
  	if not next(items) then return end
  
  	local need = {[ItemId.Gold] = 1, [ItemId.Exp] = 1, [ItemId.PlayerExp] = 1}
  	local reward = {}
  	for itemId , count in pairs(items) do
  		if need[itemId] then
  			reward[itemId] = count
  			items[itemId] = nil
  		end
  	end
8c199cec   zhengshouren   简化物品奖励接口,设置默认参数
282
  	local reward = role:award(reward)
bcf5bfbb   zhouahaihai   挂机new
283
284
285
286
287
288
289
290
291
292
293
294
295
  	
  	local hangInfo = role:getProperty("hangInfo")
  	local nowTime = skynet.timex()
  	hangInfo.endCoinTime = nowTime + globalCsv.idle_producetime_max
  	hangInfo.coinTime = nowTime
  	role:updateProperty({field = "hangBag", value = items})
  	role:updateProperty({field = "hangInfo", value = hangInfo})
  
  	SendPacket(actionCodes.Hang_getRewardCoinRpc, MsgPack.pack({
  		reward = reward
  	}))
  	return true
  end
9962b061   zhouahaihai   补充 挂机
296
297
298
299
300
301
302
303
304
305
306
307
308
  
  function _M.quickRpc(agent , data)
  	local role = agent.role
  
  	local hangInfo = role:getProperty("hangInfo")
  	if not hangInfo.carbonId then return end
  	local carbonData = csvdb["idle_battleCsv"][hangInfo.carbonId]
  
  	local curCount = role.dailyData:getProperty("hangQC") + 1
  	local costs = globalCsv.idle_quickproduce_cost:toArray(true, "=")
  	if not costs[curCount] then return end
  	if costs[curCount] > 0 then
  		if not role:checkItemEnough({[ItemId.Diamond] = costs[curCount]}) then return end
36204e3c   zhengshouren   贩卖逻辑
309
  		role:costItems({[ItemId.Diamond] = costs[curCount]})
9962b061   zhouahaihai   补充 挂机
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
  	end
  
  	role.dailyData:updateProperty({field = "hangQC", value = curCount})
  
  	local time = globalCsv.idle_quickproduce_time
  	local reward = {}
  
  	local coinCount = math.floor(time / globalCsv.idle_money_produce_cd)
  	local itemCount = math.floor(time / globalCsv.idle_item_produce_cd)
  	reward[ItemId.Gold] = (reward[ItemId.Gold] or 0) + coinCount * carbonData.money
  	reward[ItemId.Exp] = (reward[ItemId.Exp] or 0) + coinCount * carbonData.exp
  
  	local pool = {}
  	for _, temp in pairs(carbonData.item:toArray()) do
  		table.insert(pool, temp:toArray(true, "="))
  	end
  	for i = 1, itemCount do
  		local cur = pool[math.randWeight(pool, 3)]
  		reward[cur[1]] = (reward[cur[1]] or 0) + cur[2]
  	end
  
8c199cec   zhengshouren   简化物品奖励接口,设置默认参数
331
  	reward = role:award(reward)
9962b061   zhouahaihai   补充 挂机
332
333
334
335
336
337
338
  
  	SendPacket(actionCodes.Hang_quickRpc, MsgPack.pack({
  		reward = reward
  	}))
  	return true
  end
  
384bb077   zhouahaihai   挂机
339
  return _M