Blame view

src/actions/HeroAction.lua 26.7 KB
0a07bdd9   zhouahaihai   角色升级 。gm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  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
be9c9ca6   zhouahaihai   角色评论
15
  local table_unpack = table.unpack
0a07bdd9   zhouahaihai   角色升级 。gm
16
17
  
  local _M = {}
058a0cbb   zhouhaihai   抽卡
18
  
0a07bdd9   zhouahaihai   角色升级 。gm
19
20
21
  function _M.levelUpRpc( agent, data )
  	local role = agent.role
  	local msg = MsgPack.unpack(data)
36482c8b   zhouhaihai   回收养成
22
  
0a07bdd9   zhouahaihai   角色升级 。gm
23
  	local hero = role.heros[msg.id]
1c35c4cf   gaofengduan   fix hero awake
24
  	if not hero then return 1 end
0a07bdd9   zhouahaihai   角色升级 。gm
25
  
1c35c4cf   gaofengduan   fix hero awake
26
  	if hero:getProperty("level") >= hero:getMaxLevel() then return 2 end
8c74292c   zhouahaihai   增加item 以及 角色突破
27
  	local curData = csvdb["unit_expCsv"][hero:getProperty("level")]
997cbdfe   zhouahaihai   技能养成
28
  	local cost = {[ItemId.Exp] = curData.exp, [ItemId.Gold] = curData.gold}
1c35c4cf   gaofengduan   fix hero awake
29
  	if not role:checkItemEnough(cost) then return 3 end
3133cb76   zhouhaihai   日志
30
  	role:costItems(cost, {log = {desc = "heroLevelUp", int1 = msg.id, int2 = hero:getProperty("type")}})
0a07bdd9   zhouahaihai   角色升级 。gm
31
  	hero:updateProperty({field = "level", delta = 1})
1c35c4cf   gaofengduan   fix hero awake
32
  
3133cb76   zhouhaihai   日志
33
34
  	hero:log({desc = "levelUp", int1 = hero:getProperty("level")})
  
53e8037e   zhouhaihai   任务
35
  	role:checkTaskEnter("HeroLevelUp", {level = hero:getProperty("level")})
0a07bdd9   zhouahaihai   角色升级 。gm
36
37
38
39
  	SendPacket(actionCodes.Hero_levelUpRpc, '')
  	return true
  end
  
8c74292c   zhouahaihai   增加item 以及 角色突破
40
41
42
43
  function _M.breakRpc( agent, data )
  	local role = agent.role
  	local msg = MsgPack.unpack(data)
  	local hero = role.heros[msg.id]
1c35c4cf   gaofengduan   fix hero awake
44
  	if not hero then return 1 end
8c74292c   zhouahaihai   增加item 以及 角色突破
45
  
1c35c4cf   gaofengduan   fix hero awake
46
47
  	if hero:getProperty("level") < hero:getMaxLevel() then return 2 end
  	if hero:getProperty("breakL") >= #csvdb["unit_breakCsv"] then return 3 end
8c74292c   zhouahaihai   增加item 以及 角色突破
48
  	local curData = csvdb["unit_breakCsv"][hero:getProperty("breakL")]
997cbdfe   zhouahaihai   技能养成
49
  	local cost = {[ItemId.BreakCost] = curData.cost, [ItemId.Gold] = curData.gold}
1c35c4cf   gaofengduan   fix hero awake
50
  	if not role:checkItemEnough(cost) then return 4 end
3133cb76   zhouhaihai   日志
51
  	role:costItems(cost, {log = {desc = "heroBreak", int1 = msg.id, int2 = hero:getProperty("type")}})
8c74292c   zhouahaihai   增加item 以及 角色突破
52
  	hero:updateProperty({field = "breakL", delta = 1})
1c35c4cf   gaofengduan   fix hero awake
53
  
3133cb76   zhouhaihai   日志
54
55
  	hero:log({desc = "break", int1 = hero:getProperty("breakL")})
  
8c74292c   zhouahaihai   增加item 以及 角色突破
56
57
58
  	SendPacket(actionCodes.Hero_breakRpc, '')
  	return true
  end
0a07bdd9   zhouahaihai   角色升级 。gm
59
  
997cbdfe   zhouahaihai   技能养成
60
61
62
63
  function _M.wakeRpc(agent, data)
  	local role = agent.role
  	local msg = MsgPack.unpack(data)
  	local hero = role.heros[msg.id]
1c35c4cf   gaofengduan   fix hero awake
64
65
66
67
68
  	if not hero then return 1 end
  	if hero:getProperty("wakeL") >= #csvdb["unit_wakeCsv"] then return 2 end
  	local typ = hero:getProperty("type")
  	local wakeData = csvdb["unit_wakeCsv"][hero:getProperty("wakeL")]
  	if not wakeData then return 3 end
15cba0bf   zhouhaihai   修改天赋升级消耗
69
70
  	local costMaterial = wakeData.costMaterial:toArray(true,"=")
  	local cost = {[typ] = wakeData.costFigment,[globalCsv.unit_wake_cost[hero:getCamp()][costMaterial[1]]] = costMaterial[2]}
1c35c4cf   gaofengduan   fix hero awake
71
72
  	if not role:checkItemEnough(cost) then
  		return 4
997cbdfe   zhouahaihai   技能养成
73
  	end
1c35c4cf   gaofengduan   fix hero awake
74
  
3133cb76   zhouhaihai   日志
75
  	role:costItems(cost, {log = {desc = "heroWake", int1 = msg.id, int2 = hero:getProperty("type")}})
0889982f   zhouhaihai   优化角色技能等级
76
  
997cbdfe   zhouahaihai   技能养成
77
78
  	hero:updateProperty({field = "wakeL", delta = 1})
  
53e8037e   zhouhaihai   任务
79
  	local curLevel = hero:getProperty("wakeL")
f6a9215f   zhouhaihai   觉醒 误删了 任务
80
  	role:checkTaskEnter("Wake", {heroType = typ, wakeL = curLevel})
53e8037e   zhouhaihai   任务
81
82
83
  	if curLevel == 4 then -- 解锁cg
  		role:checkTaskEnter("WakeCG", {heroType = typ})
  	end
3133cb76   zhouhaihai   日志
84
85
  	hero:log({desc = "wake", int1 = hero:getProperty("wakeL")})
  
53e8037e   zhouhaihai   任务
86
  	
997cbdfe   zhouahaihai   技能养成
87
88
89
90
  	SendPacket(actionCodes.Hero_wakeRpc, '')
  	return true
  end
  
997cbdfe   zhouahaihai   技能养成
91
92
93
94
95
  
  function _M.talentRpc(agent, data)
  	local role = agent.role
  	local msg = MsgPack.unpack(data)
  	local hero = role.heros[msg.id]
b57f0bae   gaofengduan   fix hero talent
96
  	if not hero then return 1 end
997cbdfe   zhouahaihai   技能养成
97
98
99
  
  	local index = msg.index -- 第几个天赋
  	local need = {[1] = 1, [2] = 1, [3] = 1, [4] = 1}
b57f0bae   gaofengduan   fix hero talent
100
  	if not need[index] then return 2 end
997cbdfe   zhouahaihai   技能养成
101
102
103
  
  	local talent = hero:getProperty("talent")
  	local curStage = talent:getv(0, 1)
b57f0bae   gaofengduan   fix hero talent
104
  	if curStage > csvdb["unit_breakCsv"][hero:getProperty("breakL")].talent then return 3 end
1c35c4cf   gaofengduan   fix hero awake
105
106
  
  	local curData = csvdb["unit_talentCsv"][curStage]
b57f0bae   gaofengduan   fix hero talent
107
  	if not curData then return 4 end
997cbdfe   zhouahaihai   技能养成
108
109
  
  	local level = talent:getv(index, 0)
b57f0bae   gaofengduan   fix hero talent
110
  	if level >= #curData then return 5 end
997cbdfe   zhouahaihai   技能养成
111
112
113
  
  	local talentData = curData[level]
  	if not talentData then return end
b57f0bae   gaofengduan   fix hero talent
114
115
116
117
  	local cost = talentData.money:toNumMap()
  	local cost2 = talentData.cost:toNumMap()
  	for k,v in pairs(cost2) do
  		cost[globalCsv.unit_talent_cost[csvdb["unitCsv"][hero:getProperty("type")].camp][k]] = v
4b7c7c96   zhouahaihai   增加 清空 挂机 冒险gm 角色经验
118
  	end
b57f0bae   gaofengduan   fix hero talent
119
  	if not role:checkItemEnough(cost) then return 6 end
3133cb76   zhouhaihai   日志
120
  	role:costItems(cost, {log = {desc = "heroTalent", int1 = msg.id, int2 = hero:getProperty("type")}})
997cbdfe   zhouahaihai   技能养成
121
122
123
124
125
126
127
128
129
130
131
  	talent = talent:incrv(index, 1)
  
  	--是否进阶
  	local max = true
  	for i = 1, 4 do
  		if talent:getv(i, 0) < #curData then
  			max = false
  			break
  		end
  	end
  	if max then
b9bd5cb6   zhouahaihai   天赋阶段 bug
132
  		talent = talent:setv(0, curStage + 1)
997cbdfe   zhouahaihai   技能养成
133
134
135
136
137
  		for i = 1, 4 do
  			talent = talent:setv(i, 0)
  		end
  	end
  	hero:updateProperty({field = "talent", value = talent})
53e8037e   zhouhaihai   任务
138
139
140
141
142
143
144
145
  	local aheadLevel = 0
  	for i = 1, talent:getv(0, 1) - 1 do
  		aheadLevel = aheadLevel + #csvdb["unit_talentCsv"][i]
  	end
  	aheadLevel = aheadLevel + talent:getv(index, 0)
  
  	role:checkTaskEnter("HeroTalent", {heroType = hero:getProperty("type"), alv = aheadLevel})
  
3133cb76   zhouhaihai   日志
146
147
  	hero:log({desc = "talent", int1 = index, int2 = talent:getv(index, 0)})
  
997cbdfe   zhouahaihai   技能养成
148
149
150
151
  	SendPacket(actionCodes.Hero_talentRpc, '')
  	return true
  end
  
be9c9ca6   zhouahaihai   角色评论
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
  -- 暂时没有这个功能
  function _M.likeHeroRpc(agent, data)
  	local role = agent.role
  	local msg = MsgPack.unpack(data)
  	local heroType = msg.type
  	local result = {status = 0}
  	local isLike = false
  	local hadLike = role:getProperty("likeHero"):toArray(true, "=")
  	for _, v in pairs(hadLike) do
  		if v == heroType then
  			isLike = true
  			break
  		end
  	end
  	if isLike then
  		result.status = 1
  	else
  		redisproxy:hincrby("hero:like", "hero:"..heroType, 1)
  		table.insert(hadLike, heroType)
  		role:setProperty("likeHero", table.concat(hadLike, "="))
  	end
  
  	SendPacket(actionCodes.Hero_likeHeroRpc, MsgPack.pack(result))
  	return true
  end
  local RankLikeNum = 5 --热度显示几个
  local TimeLikeNum = 95 -- 时间显示几个
  local function getCommentKey(heroType)
  	return {
  		commentListKey = string.format("list:%d:herocomments", heroType),
  		commentRankKey = string.format("rank:%d:herocomments", heroType),
  		commentKey = string.format("hero:%d:comments", heroType),
  	}
  end
  
  local function trimComment(heroType, commentId) -- 剪裁 CommentList
  	local commentKey = getCommentKey(heroType)
  	local redret = redisproxy:pipelining(function (red)
  		red:lpush(commentKey.commentListKey, commentId)
  		red:lrange(commentKey.commentListKey, TimeLikeNum,-1)
  		red:ltrim(commentKey.commentListKey, 0, TimeLikeNum - 1)
  		red:zrevrange(commentKey.commentRankKey, 0, RankLikeNum - 1)
  	end)
  	local hots = {}
  	for _, hot in pairs(redret[4]) do
  		hots[hot] = 1
  	end
  	redisproxy:pipelining(function (red)
  		local needDel = {}
  		for _, tempId in pairs(redret[2]) do
  			if not hots[tempId] then
  				table.insert(needDel, tempId)
  			end
  		end
  		if #needDel > 0 then
  			red:zrem(commentKey.commentRankKey, table_unpack(needDel))
  			red:hdel(commentKey.commentKey, table_unpack(needDel))
  		end
  	end)
  end
  
  
  function _M.commentHeroRpc(agent, data)
  	local role = agent.role
  	local msg = MsgPack.unpack(data)
  	local heroType = msg.type
  	local content = msg.content
  
  	local result = {status = 0}  -- status 0 成功  1 已经评论过了
  	local curStutus = role.dailyData:getProperty("commentHero")
  	if curStutus:getv(heroType, 0) ~= 0 then
  		result.status = 1
  	else
  		local commentKey = getCommentKey(heroType)
da898074   zhouhaihai   pvp 高级领奖
226
  		local SERV = string.format(".NAMED%d", math.random(1, 5))
be9c9ca6   zhouahaihai   角色评论
227
228
229
230
231
232
  		local legal, mod = skynet.call(SERV, "lua", "check", content)
  		if not legal then
  			content = mod or ""
  		end
  		local commentId = tostring(redisproxy:hincrby("hero:comment:autoincr", "hero:" .. heroType, 1))
  		local comment = {
1c35c4cf   gaofengduan   fix hero awake
233
  			commentId = commentId,
be9c9ca6   zhouahaihai   角色评论
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
  			content = content,
  			roleId = role:getProperty("id"),
  			name = role:getProperty("name"),
  			-- time = skynet.timex()
  		}
  		redisproxy:hset(commentKey.commentKey, commentId, json.encode(comment))
  		trimComment(heroType, commentId)
  
  		comment.like = 0
  		result.comment = comment
  		role.dailyData:setProperty("commentHero", curStutus:setv(heroType, 1))
  	end
  	SendPacket(actionCodes.Hero_commentHeroRpc, MsgPack.pack(result))
  	return true
  end
  
  function _M.getCommentsRpc(agent, data)
  	local role = agent.role
  	local msg = MsgPack.unpack(data)
  	local heroType = msg.type
  	local list = {}  -- 评论列表
  	local commentKey = getCommentKey(heroType)
  	local commentRoleKey = string.format("comment:%d:like", role:getProperty("id"))
  	local redret = redisproxy:pipelining(function (red)
  		red:lrange(commentKey.commentListKey, 0,TimeLikeNum - 1)
  		red:zrevrange(commentKey.commentRankKey, 0, -1, "WITHSCORES") --热门
  		red:hget("hero:like", "hero:"..heroType)
  		red:lrange(commentRoleKey, 0, 999)
  	end)
  
  	local likeMap = {}
  	local idList = {}
  	local liked = {}
  	for i = 1, #redret[2], 2 do
  		likeMap[redret[2][i]] = redret[2][i + 1]
  		if i < RankLikeNum * 2 then
  			table.insert(idList, redret[2][i])
  		end
  	end
  	for i = 1, #redret[1] do
  		table.insert(idList, redret[1][i])
  	end
  	for i = 1, #redret[4] do
  		liked[redret[4][i]] = 1
  	end
  
  	local commentData = redisproxy:pipelining(function (red)
  		for _, commentId in ipairs(idList) do
  			red:hget(commentKey.commentKey, commentId)
  		end
  	end)
  	for _, commentS in ipairs(commentData or {}) do
  		local comment = json.decode(commentS)
  		comment.like = likeMap[tostring(comment.commentId)] or 0
  		comment.liked = liked[heroType .. ":" .. comment.commentId] or 0
  		table.insert(list, comment)
  	end
  	SendPacket(actionCodes.Hero_getCommentsRpc, MsgPack.pack({list = list, like = tonumber(redret[3] or 0)}))
  	return true
  end
  
  function _M.likeCommentRpc(agent, data)
  	local role = agent.role
  	local msg = MsgPack.unpack(data)
  	local actType = msg.actType  -- 1 顶 2 踩
  	local heroType = msg.type
  	local commentId = msg.commentId  --评论id
  	local commentKey = getCommentKey(heroType)
  	local add = 0
  	if actType == 1 then
  		add = 1
  	elseif actType == 2 then
  		add = -1
  	else
1c35c4cf   gaofengduan   fix hero awake
308
  		return
be9c9ca6   zhouahaihai   角色评论
309
310
  	end
  
1c35c4cf   gaofengduan   fix hero awake
311
312
  	local result = {status = 0}
  	local commentIndex = heroType .. ":" .. commentId
be9c9ca6   zhouahaihai   角色评论
313
314
  	local commentRoleKey = string.format("comment:%d:like", role:getProperty("id"))
  	local redret = redisproxy:pipelining(function (red)
1c35c4cf   gaofengduan   fix hero awake
315
  		red:hexists(commentKey.commentKey, commentId)
be9c9ca6   zhouahaihai   角色评论
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
  		red:lrem(commentRoleKey, 1, commentIndex)
  		red:lpush(commentRoleKey, commentIndex)
  		red:ltrim(commentRoleKey, 0, 999)
  	end)
  	if (tonumber(redret[2]) or 0) > 0 then
  		result.status = 1
  	else
  		if redret[1] == 1 then-- 查不到也返回ture
  			local redret2 = redisproxy:pipelining(function (red)
  				red:zrevrange(commentKey.commentRankKey, 0, RankLikeNum - 1) --热门
  				red:zincrby(commentKey.commentRankKey, add, commentId)
  				red:zrevrange(commentKey.commentRankKey, 0, RankLikeNum - 1) --热门
  			end)
  			local out = {}
  			for _, v in pairs(redret2[1]) do
  				out[v] = 1
  			end
  			local new = {}
  			for _, v in pairs(redret2[3]) do
  				if out[v] then
  					out[v] = nil
  				else
  					new[v] = 1
  				end
  			end
  			for tempId, _ in pairs(out) do
  				trimComment(heroType, tempId)
  			end
  			redisproxy:pipelining(function (red)
  				for tempId, _ in pairs(new) do
  					red:lrem(commentKey.commentListKey, 0, tempId)
  				end
  			end)
  		end
  	end
  
  	SendPacket(actionCodes.Hero_likeCommentRpc, MsgPack.pack(result))
  	return true
  end
  
14f1591b   zhouhaihai   删除好感度相关
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
  -- function _M.loveItemRpc(agent, data)
  -- 	local role = agent.role
  -- 	local msg = MsgPack.unpack(data)
  -- 	local hero = role.heros[msg.heroId]
  -- 	if not hero then
  -- 		return
  -- 	end
  -- 	local curL = hero:getProperty("loveL")
  -- 	local curExp = hero:getProperty("loveExp")
  -- 	local curType = hero:getProperty("type")
  -- 	local curPlus = csvdb["unit_love_plusCsv"][curType]
  -- 	if not curPlus then
  -- 		return
  -- 	end
  -- 	if curL >= curPlus.limit then
  -- 		SendPacket(actionCodes.Hero_loveItemRpc, MsgPack.pack({errMsg = 1}))	--已满级
  -- 		return true
  -- 	end
  -- 	local curEffect = csvdb["unit_love_effectCsv"][curL]
  -- 	if not curEffect then
  -- 		return
  -- 	end
  -- 	if curExp >= curEffect.loveValue and not msg.bBreak then
  -- 		SendPacket(actionCodes.Hero_loveItemRpc, MsgPack.pack({errMsg = 2}))	--当前等级经验已满
  -- 		return true
  -- 	end
  
  -- 	if msg.bBreak then
  -- 		local cost = curEffect.cost:toArray(true, "=")
  -- 		if not role:checkItemEnough({[cost[1]] = cost[2]}) then
  -- 			SendPacket(actionCodes.Hero_loveItemRpc, MsgPack.pack({errMsg = 3, itemId = cost[1]}))	--物品不足
  -- 			return true
  -- 		end
  -- 		role:costItems({[cost[1]] = cost[2]})
  -- 		local newLevel = curL + 1
  -- 		hero:updateProperty({field = "loveL", value = newLevel})
  -- 		hero:updateProperty({field = "loveExp", value = 0})
  
  -- 		if role:getProperty("loveStatus"):getv(curType, 0) < newLevel then
  -- 			role:changeUpdates({{type = "loveStatus", field = curType, value = newLevel}}) -- 总的
  -- 		end
  
  -- 		role:checkTaskEnter("LoveBreak", {heroType = curType, loveL = newLevel})
  
  -- 	else
  -- 		local delta = globalCsv.unit_love_presentValue[msg.itemId]
  -- 		if not delta then
  -- 			return
  -- 		end
  -- 		if not role:checkItemEnough({[msg.itemId] = 1}) then
  -- 			SendPacket(actionCodes.Hero_loveItemRpc, MsgPack.pack({errMsg = 3, itemId = msg.itemId}))
  -- 			return true
  -- 		end
  -- 		local newExp = curExp + delta
  -- 		if newExp > curEffect.loveValue then
  -- 			newExp = curEffect.loveValue
  -- 		end
  -- 		role:costItems({[msg.itemId] = 1})
  -- 		hero:updateProperty({field = "loveExp", value = newExp})
  -- 	end
  -- 	SendPacket(actionCodes.Hero_loveItemRpc, "")
  -- 	return true
  -- end
  
  -- function _M.loveTaskRpc(agent, data)
  -- 	local role = agent.role
  -- 	local msg = MsgPack.unpack(data)
  -- 	local hero = role.heros[msg.id]
  -- 	if not hero then return end
  
  -- 	local curL = hero:getProperty("loveL")
  -- 	local curExp = hero:getProperty("loveExp")
  -- 	local curType = hero:getProperty("type")
  -- 	local curPlus = csvdb["unit_love_plusCsv"][curType]
  -- 	if not curPlus or curL >= curPlus.limit then return end
  
  --  	local curEffect = csvdb["unit_love_effectCsv"][curL]
  -- 	if not curEffect or curExp < curEffect.loveValue then return end
  
  -- 	local lastEffect = csvdb["unit_love_effectCsv"][curL + 1]
  -- 	local newExp = curExp - curEffect.loveValue
  -- 	if lastEffect and curL + 1 < curPlus.limit then
  -- 		if newExp >= lastEffect.loveValue then
  -- 			-- todo  发任务
  -- 		end
  -- 	else
  -- 		newExp = 0
  -- 	end
  -- 	local newLevel = curL + 1
  -- 	hero:updateProperty({field = "loveExp", value = newExp})
  -- 	hero:updateProperty({field = "loveL", value = newLevel})
  
  -- 	if role:getProperty("loveStatus"):getv(curType, 0) < newLevel then
  -- 		role:changeUpdates({{type = "loveStatus", field = curType, value = newLevel}}) -- 总的
  -- 	end
  
  -- 	role:checkTaskEnter("LoveBreak", {heroType = curType, loveL = newLevel})
  
  -- 	SendPacket(actionCodes.Hero_loveTaskRpc, "")
  -- 	return true
  -- end
6947e382   zhouahaihai   好感度, 皮肤
457
  
312b9db5   zhouahaihai   背包
458
459
460
461
462
463
464
465
466
467
468
469
470
471
  function _M.createHeroRpc(agent, data)
  	local role = agent.role
  	local msg = MsgPack.unpack(data)
  	local heroType = msg.heroType
  	local unitData = csvdb["unitCsv"][heroType]
  	if not unitData then return end
  	local cost = globalCsv.unit_fragment_cost[unitData["rare"]]
  	if not cost then return end
  	if role:getItemCount(heroType) < cost then return end
  
  	for _, hero in pairs(role.heros) do
  		if hero:getProperty("type") == heroType then return end
  	end
  
3133cb76   zhouhaihai   日志
472
473
  	role:costItems({[heroType] = cost}, {log = {desc = "createHero"}})
  	role:award({[heroType + ItemStartId.Hero] = 1}, {log = {desc = "createHero"}})
312b9db5   zhouahaihai   背包
474
475
476
477
478
  
  	SendPacket(actionCodes.Hero_createHeroRpc, "")
  	return true
  end
  
43cc5f51   gaofengduan   调整 equip 数据结构
479
  -- typ 位置,level等级对应唯一装备,level为0时为移除,不为0时无则装备,有则替换
24d77701   gaofengduan   fix equip
480
  function _M.referEquipsRpc(agent, data)
43cc5f51   gaofengduan   调整 equip 数据结构
481
482
483
484
485
486
487
  	local role = agent.role
  	local msg = MsgPack.unpack(data)
  	local hero = role.heros[msg.id]
  	if not hero then return 10 end
  	local equips = msg.equips
  	if not equips or not next(equips) then return 11 end
  
056c01a0   zhouhaihai   简化装备
488
489
490
491
492
  	for typ = 1, 4 do -- 4件装备
  		if equips[typ] and equips[typ] ~= 0 then
  			if role:getEquipCount(typ, equips[typ]) <= 0 then
  				return
  			end
43cc5f51   gaofengduan   调整 equip 数据结构
493
  		end
43cc5f51   gaofengduan   调整 equip 数据结构
494
  	end
056c01a0   zhouhaihai   简化装备
495
496
497
498
  	local curEquip = hero:getProperty("equip")
  	for typ = 1, 4 do -- 4件装备
  		if equips[typ] then
  			local cur = curEquip:getv(typ, 0)
ee999bde   zhouhaihai   零件优化
499
500
  			if cur ~= equips[typ] then
  				if equips[typ] == 0 then
056c01a0   zhouhaihai   简化装备
501
  					curEquip = curEquip:delk(typ)
ee999bde   zhouhaihai   零件优化
502
  				else
3133cb76   zhouhaihai   日志
503
  					role:addEquip(typ, equips[typ], -1, {log = {desc = "refer"}}) -- 穿上
056c01a0   zhouhaihai   简化装备
504
505
  					curEquip = curEquip:setv(typ, equips[typ])
  				end
ee999bde   zhouhaihai   零件优化
506
507
  
  				if cur ~= 0 then
3133cb76   zhouhaihai   日志
508
  					role:addEquip(typ, cur, 1, {log = {desc = "refer"}}) -- 脱掉
ee999bde   zhouhaihai   零件优化
509
  				end
43cc5f51   gaofengduan   调整 equip 数据结构
510
511
  			end
  		end
43cc5f51   gaofengduan   调整 equip 数据结构
512
  	end
056c01a0   zhouhaihai   简化装备
513
514
515
  	-- 更新角色
  	hero:updateProperty({field = "equip", value = curEquip})
  
43cc5f51   gaofengduan   调整 equip 数据结构
516
517
518
519
  	SendPacket(actionCodes.Hero_referEquipsRpc, "")
  	return true
  end
  
ee3ac0b5   gaofengduan   fix magic
520
521
  -- typ 位置,uid对应唯一符文,uid为0时为移除,不为0时无则装备,有则替换
  function _M.referRunesRpc(agent, data)
43cc5f51   gaofengduan   调整 equip 数据结构
522
523
524
525
526
527
  	local role = agent.role
  	local msg = MsgPack.unpack(data)
  	local hero = role.heros[msg.id]
  	if not hero then return 10 end
  	local runes = msg.runes
  	if not runes or not next(runes) then return 11 end
ee3ac0b5   gaofengduan   fix magic
528
  
ee999bde   zhouhaihai   零件优化
529
530
531
532
533
  	for typ = 1, 6 do
  		if runes[typ] and runes[typ] ~= 0 then
  			local ownRune = role.runeBag[runes[typ]]
  			if not ownRune then return end
  			if ownRune:getProperty("refer") ~= 0 then return end
ee3ac0b5   gaofengduan   fix magic
534
535
  		end
  	end
ee999bde   zhouhaihai   零件优化
536
537
538
539
540
541
542
543
544
545
  	local curRune = hero:getProperty("rune")
  	for typ = 1, 6 do
  		if runes[typ] then
  			local cur = curRune:getv(typ, 0)
  			if cur ~= runes[typ] then
  				if runes[typ] == 0 then
  					curRune = curRune:delk(typ)
  				else
  					local newRune = role.runeBag[runes[typ]]
  					newRune:updateProperty({field = "refer",value = hero:getProperty("id")})
4ea1b5ac   zhouhaihai   穿戴零件
546
  					curRune = curRune:setv(typ, runes[typ])
ee999bde   zhouhaihai   零件优化
547
  				end
ee3ac0b5   gaofengduan   fix magic
548
  
ee999bde   zhouhaihai   零件优化
549
550
551
552
553
554
  				if cur ~= 0 then
  					local oldR = role.runeBag[cur]
  					if oldR then
  						oldR:updateProperty({field = "refer",value = 0})
  					end
  				end
ee3ac0b5   gaofengduan   fix magic
555
  			end
ee3ac0b5   gaofengduan   fix magic
556
  		end
ee3ac0b5   gaofengduan   fix magic
557
  	end
4ea1b5ac   zhouhaihai   穿戴零件
558
  	hero:updateProperty({field = "rune", value = curRune})
ee3ac0b5   gaofengduan   fix magic
559
  	SendPacket(actionCodes.Hero_referRunesRpc, "")
43cc5f51   gaofengduan   调整 equip 数据结构
560
561
562
  	return true
  end
  
3b069d52   zhouhaihai   增加获取 food 后台
563
564
565
566
567
568
569
570
571
572
573
574
  function _M.createHeroRandomRpc(agent, data)
  	local role = agent.role
  	local msg = MsgPack.unpack(data)
  	local itemId = msg.itemId
  	local itemData = csvdb["itemCsv"][itemId]
  	if not itemData or itemData.type ~= ItemType.HeroFCommon then return end
  	local cost = globalCsv.unit_fragment_cost[itemData.quality]
  	if not cost or role:getItemCount(itemId) < cost then return end
  
  	local randomData = csvdb["item_randomCsv"][tonumber(itemData.use_effect)]
  	if not randomData then return end
  
007af97e   zhouhaihai   item_random 结构更改
575
  	local temp = randomData.gift1:randWeight(true)
3b069d52   zhouhaihai   增加获取 food 后台
576
577
578
579
580
581
582
  	if not temp or not next(temp) then return end
  	local reward = {}
  	if role:isHaveHero(temp[1]) then
  		reward = {[temp[1]] = cost}
  	else
  		reward = {[temp[1] + ItemStartId.Hero] = 1}
  	end
3133cb76   zhouhaihai   日志
583
584
  	role:costItems({[itemId] = cost}, {log = {desc = "createHeroRandom"}})
  	reward = role:award(reward, {log = {desc = "createHeroRandom"}})
3b069d52   zhouhaihai   增加获取 food 后台
585
586
587
588
  	SendPacket(actionCodes.Hero_createHeroRandomRpc, MsgPack.pack({reward = reward}))
  	return true
  end
  
36482c8b   zhouhaihai   回收养成
589
590
591
  function _M.getResetRewardRpc(agent, data)
  	local role = agent.role
  	local msg = MsgPack.unpack(data)
36482c8b   zhouhaihai   回收养成
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
  	
  	local hero = role.heros[msg.id]
  	if not hero then return end
  
  	local level = hero:getProperty("level")
  	local breakL = hero:getProperty("breakL")
  	local talent = hero:getProperty("talent")
  
  	if level <= 1 and talent == "" then return end
  
  	local reward = {}
  	while level > 1 do
  		local curData = csvdb["unit_expCsv"][level - 1]
  		reward[ItemId.Exp] = (reward[ItemId.Exp] or 0) + curData.exp
  		reward[ItemId.Gold] = (reward[ItemId.Gold] or 0) + curData.gold
  		level = level - 1
  	end
  
  	while breakL > 0 do
  		local curData = csvdb["unit_breakCsv"][breakL - 1]
  		reward[ItemId.BreakCost] = (reward[ItemId.BreakCost] or 0) + curData.cost
  		reward[ItemId.Gold] = (reward[ItemId.Gold] or 0) + curData.gold
  		breakL = breakL - 1
  	end
  
  	local stage = talent:getv(0, 1)
  	local tlevel = {talent:getv(1, 0), talent:getv(2, 0), talent:getv(3, 0), talent:getv(4, 0)}
  
  	local talentCostIds = globalCsv.unit_talent_cost[csvdb["unitCsv"][hero:getProperty("type")].camp]
  	while stage > 0 do
  		local curData = csvdb["unit_talentCsv"][stage]
  		for level = math.max(table.unpack(tlevel)), 1, -1 do
  			local add = 0
  			for i = 1, 4 do
  				if tlevel[i] == level then
  					add = add + 1
  					tlevel[i] = tlevel[i] - 1
  				end
  			end
  			local talentData = curData[level - 1]
  			for itemId, count in pairs(talentData.money:toNumMap()) do
  				reward[itemId] = (reward[itemId] or 0) + count * add
  			end
  			for idx , count in pairs(talentData.cost:toNumMap()) do
  				reward[talentCostIds[idx]] = (reward[talentCostIds[idx]] or 0) + count * add
  			end
  		end
  		stage = stage - 1
  		curData = csvdb["unit_talentCsv"][stage]
  		if curData then
  			tlevel = {#curData, #curData, #curData, #curData}
  		end
  	end
  
  	hero:updateProperty({field = "level", value = level})
  	hero:updateProperty({field = "breakL", value = breakL})
  	hero:updateProperty({field = "talent", value = ""})
3133cb76   zhouhaihai   日志
649
  	hero:log({desc = "resetHero"})
36482c8b   zhouhaihai   回收养成
650
651
652
653
  
  	for itemId, count in pairs(reward) do
  		reward[itemId] = math.floor(count * globalCsv.unit_back_discount)
  	end
3133cb76   zhouhaihai   日志
654
  	reward = role:award(reward, {log = {desc = "resetHero", int1 = msg.id, int2 = hero:getProperty("type")}})
36482c8b   zhouhaihai   回收养成
655
656
657
658
659
  
  	SendPacket(actionCodes.Hero_getResetRewardRpc, MsgPack.pack({reward = reward}))
  	return true
  end
  
058a0cbb   zhouhaihai   抽卡
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
  
  local function randomDrawCondition(pool, condition)
  	local value = {}
  	for idx, field in ipairs(condition) do
  		local lpool = {}
  		local curIdx = 1
  		while pool[field .. "_" .. curIdx] do
  			table.insert(lpool, {pool[field .. "_" .. curIdx]})
  			curIdx = curIdx + 1
  		end
  		if next(lpool) then
  			value[idx] =  math.randWeight(lpool, 1)
  		end
  	end
  	return value
  end
  
  
  local function fillDrawPool(curPool, resultPool, isNeedFunc)
  	for itemId, oneData in pairs(csvdb["build_poolCsv"]) do
  		if oneData["pool_" .. curPool] and oneData["pool_" .. curPool] ~= "" then
  			local itemData = csvdb["itemCsv"][itemId]
  			if itemData and isNeedFunc(itemData) then
b9659961   zhouhaihai   pool bug
683
  				for _, one in ipairs(oneData["pool_" .. curPool]:toTableArray(true)) do
058a0cbb   zhouhaihai   抽卡
684
685
686
687
688
689
690
691
692
693
694
  					table.insert(resultPool, {itemId, one[1], one[2]})  -- itemId, count, 概率
  				end
  			end
  		end
  	end
  end
  
  function _M.drawHeroRpc(agent, data)
  	local role = agent.role
  	local msg = MsgPack.unpack(data)
  
d232676a   zhouhaihai   功能解锁 冒险返回
695
  	if not role:isFuncUnlock(FuncUnlock.GetHero) then return end
058a0cbb   zhouhaihai   抽卡
696
697
698
699
  	local pool = msg.pool	-- 1 2 3
  	local drawType = msg.type -- 1 单抽 2 十连
  	
  	local buildTypeData = csvdb["build_typeCsv"][pool]
1976004f   zhouhaihai   测试
700
  	if not buildTypeData then return 1 end
058a0cbb   zhouhaihai   抽卡
701
702
703
704
705
  
  	local costs = {{"draw_card", "draw_coin"}, {"draw10_card", "draw10_coin"}}  -- 抽取消耗
  	local drawCount = {1, 10} -- 抽取次数
  
  	local costT = costs[drawType]
1976004f   zhouhaihai   测试
706
  	if not costT then return 2 end
058a0cbb   zhouhaihai   抽卡
707
708
709
710
  	local cost = buildTypeData[costT[1]]:toNumMap()
  	if not role:checkItemEnough(cost) then
  		cost = buildTypeData[costT[2]]:toNumMap()
  		if not role:checkItemEnough(cost) then
1976004f   zhouhaihai   测试
711
  			return 3
058a0cbb   zhouhaihai   抽卡
712
713
714
715
716
717
718
719
720
721
  		end
  	end
  
  	-- 开始抽
  	local rateTypes = {"unitRate", "fragmentRate", "itemRate"}
  
  	local typePool = {}
  	for _, rateType in ipairs(rateTypes) do
  		table.insert(typePool, {buildTypeData[rateType]})
  	end
058a0cbb   zhouhaihai   抽卡
722
723
724
725
  
  	local resultPool = {}
  
  	local fillPoolFunc = {
a35233c6   zhouhaihai   保底和回馈
726
  		unitRate = function(fixRare, fixCamp, fixJob)
058a0cbb   zhouhaihai   抽卡
727
728
  			local condition = {"rare", "camp", "job"}
  			local values = randomDrawCondition(csvdb["build_unitCsv"][pool], condition)
a35233c6   zhouhaihai   保底和回馈
729
730
731
  			values[1] = fixRare or values[1]
  			values[2] = fixCamp or values[2]
  			values[3] = fixJob or values[3]
058a0cbb   zhouhaihai   抽卡
732
733
734
735
736
737
738
739
740
741
  			fillDrawPool(pool, resultPool, function(itemData)
  				if itemData.type ~= ItemType.Hero then return end
  				local heroData = csvdb["unitCsv"][itemData.id - ItemStartId.Hero]
  				if not heroData then return end
  				for idx, field in ipairs(condition) do
  					if heroData[field] ~= values[idx] then return end
  				end
  				return true
  			end)
  		end,
a35233c6   zhouhaihai   保底和回馈
742
  		fragmentRate = function(fixRare, fixCamp, fixJob)
058a0cbb   zhouhaihai   抽卡
743
744
  			local condition = {"rare", "camp", "job"}
  			local values = randomDrawCondition(csvdb["build_fragmentCsv"][pool], condition)
a35233c6   zhouhaihai   保底和回馈
745
746
747
  			values[1] = fixRare or values[1]
  			values[2] = fixCamp or values[2]
  			values[3] = fixJob or values[3]
058a0cbb   zhouhaihai   抽卡
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
  			fillDrawPool(pool, resultPool, function(itemData)
  				if itemData.type ~= ItemType.HeroFragment then return end
  				local heroData = csvdb["unitCsv"][itemData.id]
  				if not heroData then return end
  				for idx, field in ipairs(condition) do
  					if heroData[field] ~= values[idx] then return end
  				end
  				return true
  			end)
  		end,	
  		itemRate = function()
  			fillDrawPool(pool, resultPool, function(itemData)
  				if itemData.type == ItemType.HeroFragment or itemData.type == ItemType.Hero then return end
  				return true
  			end)
  		end,
  	}
  
3133cb76   zhouhaihai   日志
766
  	role:costItems(cost, {log = {desc = "drawHero", short1 = pool}})
a35233c6   zhouhaihai   保底和回馈
767
  	local floorHeroCount = role:getProperty("floorHero")[pool] or 0
058a0cbb   zhouhaihai   抽卡
768
  
53e8037e   zhouhaihai   任务
769
  	local ssrCount = 0
058a0cbb   zhouhaihai   抽卡
770
771
  	local reward = {}
  	for i = 1, drawCount[drawType] do
a35233c6   zhouhaihai   保底和回馈
772
  		floorHeroCount = floorHeroCount + 1
216fb30d   zhouhaihai   连抽 bug
773
774
  
  		resultPool = {}
a35233c6   zhouhaihai   保底和回馈
775
  		local isFloorBack = globalCsv.draw_floor_back_counts[pool] and floorHeroCount >= globalCsv.draw_floor_back_counts[pool]
db8e475e   zhouhaihai   抽奖
776
  		while not next(resultPool) do
a35233c6   zhouhaihai   保底和回馈
777
778
779
780
781
782
783
  			local rateType
  
  			if isFloorBack then
  				rateType = 1 --保底英雄
  			else
  				rateType = math.randWeight(typePool, 1)
  			end
db8e475e   zhouhaihai   抽奖
784
  			if not fillPoolFunc[rateTypes[rateType]] then return 4 end
a35233c6   zhouhaihai   保底和回馈
785
  			if isFloorBack then
3df938f9   zhouhaihai   明确保底 ssr
786
  				fillPoolFunc[rateTypes[rateType]](3) -- 保底 sr 【郑斌】明确
a35233c6   zhouhaihai   保底和回馈
787
788
789
  			else
  				fillPoolFunc[rateTypes[rateType]]()
  			end
db8e475e   zhouhaihai   抽奖
790
791
792
793
794
  		end
  
  		local idx = math.randWeight(resultPool, 3)
  		local temp = resultPool[idx]
  		local itemData = csvdb["itemCsv"][temp[1]]
53e8037e   zhouhaihai   任务
795
  
3df938f9   zhouhaihai   明确保底 ssr
796
797
798
799
800
801
  		if itemData.type == ItemType.Hero then
  			if itemData.quality == 4 then
  				ssrCount = ssrCount + 1
  			elseif itemData.quality == 3 then
  				floorHeroCount = 0
  			end
53e8037e   zhouhaihai   任务
802
803
  		end
  
db8e475e   zhouhaihai   抽奖
804
805
806
807
  		if itemData.type == ItemType.Hero and role:isHaveHero(itemData.id - ItemStartId.Hero) then
  			local fragId = itemData.id - ItemStartId.Hero
  			local heroData = csvdb["unitCsv"][fragId]
  			local count = globalCsv.draw_unit_tofragment[heroData.rare] * temp[2]
3133cb76   zhouhaihai   日志
808
  			role:award({[fragId] = count}, {log = {desc = "drawHero", short1 = pool}})
db8e475e   zhouhaihai   抽奖
809
810
  			table.insert(reward, {id = fragId, count = count, from = temp[1], fcount = temp[2]})
  		else
3133cb76   zhouhaihai   日志
811
  			role:award({[temp[1]] = temp[2]}, {log = {desc = "drawHero", short1 = pool}})
db8e475e   zhouhaihai   抽奖
812
  			table.insert(reward, {id = temp[1], count = temp[2]})
058a0cbb   zhouhaihai   抽卡
813
814
815
  		end
  	end
  
a35233c6   zhouhaihai   保底和回馈
816
817
818
819
820
821
  	if globalCsv.draw_floor_back_counts[pool] then
  		local floorHero = role:getProperty("floorHero")
  		floorHero[pool] = floorHeroCount
  		role:updateProperty({field = "floorHero", value = floorHero})
  	end
  
aef8ca87   zhouhaihai   两个bug
822
823
824
825
826
  	if pool == 1 then
  		local repayHero = role:getProperty("repayHero")
  		repayHero = math.min(globalCsv.draw_super_repay_count, repayHero + drawCount[drawType])
  		role:updateProperty({field = "repayHero", value = repayHero})
  	end
a35233c6   zhouhaihai   保底和回馈
827
  
53e8037e   zhouhaihai   任务
828
829
830
831
  	role:checkTaskEnter("DrawHero", {pool = pool, count = drawCount[drawType]})
  	if ssrCount > 0 then
  		role:checkTaskEnter("DrawSSR", {count = ssrCount})
  	end
3133cb76   zhouhaihai   日志
832
  	role:log("hero_action", {desc = "drawHero", short1 = pool, int1 = drawCount[drawType]})
058a0cbb   zhouhaihai   抽卡
833
834
835
836
  	SendPacket(actionCodes.Hero_drawHeroRpc, MsgPack.pack({reward = reward})) -- 这个 reward 是数组
  	return true
  end
  
a35233c6   zhouhaihai   保底和回馈
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
  function _M.repayHeroRpc(agent, data)
  	local role = agent.role
  
  	local repayHero = role:getProperty("repayHero")
  	if repayHero < globalCsv.draw_super_repay_count then
  		return
  	end
  
  	role:updateProperty({field = "repayHero", value = 0})
  	local id = math.randWeight(csvdb["build_giftCsv"], "pool_1")
  
  	local reward = {}
  	local itemData = csvdb["itemCsv"][id]
  	if itemData.type == ItemType.Hero and role:isHaveHero(itemData.id - ItemStartId.Hero) then
  		local fragId = itemData.id - ItemStartId.Hero
  		local heroData = csvdb["unitCsv"][fragId]
  		local count = globalCsv.draw_unit_tofragment[heroData.rare]
3133cb76   zhouhaihai   日志
854
  		role:award({[fragId] = count}, {log = {desc = "heroRepay"}})
a35233c6   zhouhaihai   保底和回馈
855
856
  		reward = {id = fragId, count = count, from = id, fcount = 1}
  	else
3133cb76   zhouhaihai   日志
857
  		role:award({[id] = 1}, {log = {desc = "heroRepay"}})
a35233c6   zhouhaihai   保底和回馈
858
859
  		reward = {id = id, count = 1}
  	end
3133cb76   zhouhaihai   日志
860
  	role:log("hero_action", {desc = "heroRepay"})
a35233c6   zhouhaihai   保底和回馈
861
862
863
864
  	SendPacket(actionCodes.Hero_repayHeroRpc, MsgPack.pack({reward = reward}))
  	return true
  end
  
0a07bdd9   zhouahaihai   角色升级 。gm
865
  return _M