Blame view

src/actions/PvpAction.lua 10 KB
440aa055   zhouhaihai   聊天
1
2
3
4
5
  local ipairs = ipairs
  local table = table
  local math = math
  local redisproxy = redisproxy
  local MsgPack = MsgPack
4cf74232   zhouhaihai   pvp
6
7
  local httpc = require("http.httpc")
  local remoteUrl = skynet.getenv("codeurl")
440aa055   zhouhaihai   聊天
8
9
10
  
  
  local _M = {}
4cf74232   zhouhaihai   pvp
11
12
  local _pvpBattleInfoCacheC = {}  --查询列表 缓存pvp战斗相关数据缓存
  local _pvpStartBattleCache = nil -- 
440aa055   zhouhaihai   聊天
13
  
4cf74232   zhouhaihai   pvp
14
15
  local _pvpRecordInfoCache = {} -- 记录缓存
  local _pvpRecordBattleInfoCache = {} -- 记录战斗数据缓存
f317b790   zhouhaihai   pvp 遗留
16
17
  local _revengeRecord = {} -- 复仇对单人1分钟间隔
  local RevengeWaitTime = 60
440aa055   zhouhaihai   聊天
18
19
20
  
  function _M.formatCommonRpc(agent , data)
  	local role = agent.role
fa565e0c   zhouhaihai   优化结构
21
  	local roleId = role:getProperty("id")
440aa055   zhouhaihai   聊天
22
  	local msg = MsgPack.unpack(data)
fa565e0c   zhouhaihai   优化结构
23
  	local pvpTC = role:getProperty("pvpTC")
3dbbc9f3   zhouhaihai   加上新的任务
24
  	for slot, heroId in pairs(msg.heros or {}) do
440aa055   zhouhaihai   聊天
25
26
27
28
  		if not role.heros[heroId] then
  			return
  		end
  	end
3dbbc9f3   zhouhaihai   加上新的任务
29
  	if not msg.heros or not next(msg.heros) then
fa565e0c   zhouhaihai   优化结构
30
  		return
440aa055   zhouhaihai   聊天
31
  	end
f603a60f   zhouhaihai   支援技实装
32
33
34
35
36
37
38
  	local supports = {}
  	for slot, support in pairs(msg.supports) do
  		if slot ~= 1 and slot ~= 2 then return end
  		local level = role.dinerData:getProperty("dishTree"):getv(support, 0)
  		if level <= 0 then return end
  		supports[slot] = support
  	end
440aa055   zhouhaihai   聊天
39
  
fa565e0c   zhouhaihai   优化结构
40
41
42
43
44
45
  	table.clear(pvpTC)
  	pvpTC.heros = {}
  	for slot, heroId in pairs(msg.heros) do
  		pvpTC.heros[slot] = heroId
  	end
  	pvpTC.leader = msg.leader
f603a60f   zhouhaihai   支援技实装
46
  	pvpTC.supports = supports
fa565e0c   zhouhaihai   优化结构
47
48
  	
  	role:savePvpCTeam(pvpTC)
440aa055   zhouhaihai   聊天
49
50
51
52
  	SendPacket(actionCodes.Pvp_formatCommonRpc, '')
  	return true
  end
  
4cf74232   zhouhaihai   pvp
53
  
3dbbc9f3   zhouhaihai   加上新的任务
54
  local function getMatchInfo(role, pvpList, battleCache)
4cf74232   zhouhaihai   pvp
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  	table.clear(battleCache)
  	local redret = redisproxy:pipelining(function(red)
  		for _, info in ipairs(pvpList) do
  			if info.t == 1 then
  				red:zscore(RANK_PVP_COMMON, info.id)
  			end
  		end
  	end)
  
  	local matches = {}
  	local curIdx = 1
  	for idx, info in ipairs(pvpList) do
  		local curInfo = {idx = idx}
  		if info.t == 1 then --玩家
  			curInfo.roleId = info.id
3dbbc9f3   zhouhaihai   加上新的任务
70
  			curInfo.score = role:unpackPvpScore(redret[curIdx] or 0)
4cf74232   zhouhaihai   pvp
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  			curIdx = curIdx + 1
  			-- name, level, headId, battleV, heros
  			local online, roleInfo = rpcRole(curInfo.roleId, "pvpCInfo")
  			for k , v in pairs(roleInfo) do
  				if k == "battleInfo" then
  					battleCache[curInfo.roleId] = v
  				else
  					curInfo[k] = v
  				end
  			end
  		elseif info.t == 2 then  	-- robot
  			curInfo.robot = info.id
  		end
  		table.insert(matches, curInfo)
  	end
  	return matches
  end
  
  -- 获取pvp信息
  function _M.infoRpc(agent, data)
440aa055   zhouhaihai   聊天
91
  	local role = agent.role
4cf74232   zhouhaihai   pvp
92
  	local roleId = role:getProperty("id")
440aa055   zhouhaihai   聊天
93
  	local msg = MsgPack.unpack(data)
4cf74232   zhouhaihai   pvp
94
  	local ptype = msg.ptype or 1
440aa055   zhouhaihai   聊天
95
  
4cf74232   zhouhaihai   pvp
96
97
98
99
100
101
  	local response = {}
  	if ptype == 1 then -- 普通pvp
  		local redret = redisproxy:pipelining(function(red)
  			red:zscore(RANK_PVP_COMMON, roleId)
  			red:zrevrank(RANK_PVP_COMMON, roleId)
  		end)
aaf6a9e6   zhouhaihai   分数默认1000
102
  		local score = role:unpackPvpScore(redret[1])
4cf74232   zhouhaihai   pvp
103
  		local rank = tonumber(redret[2] or -2) + 1  --排名 1 - ...  -1 未上榜 没打过pvp
aaf6a9e6   zhouhaihai   分数默认1000
104
  
4cf74232   zhouhaihai   pvp
105
106
107
108
109
110
111
  		local pvpMC = role:getProperty("pvpMC")
  		if not next(pvpMC) then --没有分配过对手
  			role:refreshPvpMatchC(score)
  			pvpMC = role:getProperty("pvpMC")
  		end
  		if not next(pvpMC) then return end
  		
4cf74232   zhouhaihai   pvp
112
  		response.rank = rank
b115474f   zhouhaihai   默认积分 1000
113
  		response.score = score
3dbbc9f3   zhouhaihai   加上新的任务
114
  		response.matches = getMatchInfo(role, pvpMC, _pvpBattleInfoCacheC)
440aa055   zhouhaihai   聊天
115
  
4cf74232   zhouhaihai   pvp
116
117
118
119
120
121
  	elseif ptype == 2 then -- 高级pvp
  		return
  	else
  		return
  	end
  	SendPacket(actionCodes.Pvp_infoRpc, MsgPack.pack(response))
440aa055   zhouhaihai   聊天
122
123
124
  	return true
  end
  
4cf74232   zhouhaihai   pvp
125
  function _M.refreshMatchCRpc(agent, data)
440aa055   zhouhaihai   聊天
126
127
  	local role = agent.role
  	local msg = MsgPack.unpack(data)
4cf74232   zhouhaihai   pvp
128
129
130
  	role:refreshPvpMatchC()
  
  	local pvpMC = role:getProperty("pvpMC")
3dbbc9f3   zhouhaihai   加上新的任务
131
  	local matches = getMatchInfo(role, pvpMC, _pvpBattleInfoCacheC)
440aa055   zhouhaihai   聊天
132
  
4cf74232   zhouhaihai   pvp
133
  	SendPacket(actionCodes.Pvp_refreshMatchCRpc, MsgPack.pack({matches = matches}))
440aa055   zhouhaihai   聊天
134
135
136
137
138
139
  	return true
  end
  
  function _M.buyCountRpc(agent, data)
  	local role = agent.role
  	local msg = MsgPack.unpack(data)
4cf74232   zhouhaihai   pvp
140
  	local count = msg.count
440aa055   zhouhaihai   聊天
141
  
4cf74232   zhouhaihai   pvp
142
143
144
145
146
147
148
149
  	if math.illegalNum(count, 1, math.huge) then
  		return 1
  	end
  
  	local cost = {[ItemId.Diamond] = globalCsv.pvp_buy_cost * count}
  	if not role:checkItemEnough(cost) then return 2 end
  	role:costItems(cost)
  	role:award({[ItemId.PvpKey] = count})
440aa055   zhouhaihai   聊天
150
151
152
153
154
155
  
  	SendPacket(actionCodes.Pvp_buyCountRpc, '')
  	return true
  end
  
  function _M.startBattleRpc(agent, data)
4cf74232   zhouhaihai   pvp
156
157
  	local role = agent.role
  	local msg = MsgPack.unpack(data)
440aa055   zhouhaihai   聊天
158
  
4cf74232   zhouhaihai   pvp
159
160
161
  	local idx = msg.idx
  	local revenge = msg.revenge
  
3dbbc9f3   zhouhaihai   加上新的任务
162
163
164
  	local pvpTC = role:getProperty("pvpTC")
  	if not pvpTC.heros or not next(pvpTC.heros) then return 1 end
  
85083dba   zhouhaihai   拉黑从申请列表移除
165
  	local matchInfo, result, key, wait
3dbbc9f3   zhouhaihai   加上新的任务
166
  
85083dba   zhouhaihai   拉黑从申请列表移除
167
  	local now = skynet.timex()
4cf74232   zhouhaihai   pvp
168
  	if revenge then  --复仇
85083dba   zhouhaihai   拉黑从申请列表移除
169
  		local temp = _pvpRecordInfoCache[idx]
3dbbc9f3   zhouhaihai   加上新的任务
170
  		if not temp then return 2 end
85083dba   zhouhaihai   拉黑从申请列表移除
171
  
f317b790   zhouhaihai   pvp 遗留
172
  		if not _revengeRecord[temp.id] or now >= _revengeRecord[temp.id] then
85083dba   zhouhaihai   拉黑从申请列表移除
173
174
175
176
177
178
179
  			if temp.t == 1 then
  				matchInfo = _pvpRecordBattleInfoCache[temp.id]
  			elseif temp.t == 2 then
  				matchInfo = {robot = temp.id}
  			end
  		else
  			result = 1
f317b790   zhouhaihai   pvp 遗留
180
  			wait = _revengeRecord[temp.id] - now
4cf74232   zhouhaihai   pvp
181
182
183
  		end
  	else  --打正常
  		local pvpMC = role:getProperty("pvpMC")
3dbbc9f3   zhouhaihai   加上新的任务
184
  		if not pvpMC[idx] then return 3 end
4cf74232   zhouhaihai   pvp
185
186
187
188
189
190
191
  		if pvpMC[idx].t == 1 then
  			matchInfo = _pvpBattleInfoCacheC[pvpMC[idx].id]
  		elseif pvpMC[idx].t == 2 then
  			matchInfo = {robot = pvpMC[idx].id}
  		end
  	end
  
3dbbc9f3   zhouhaihai   加上新的任务
192
  	if not result and not matchInfo then return 4 end
85083dba   zhouhaihai   拉黑从申请列表移除
193
194
195
196
197
198
  
  	if not result then
  		-- 次数扣一波
  		local pvpFree = role.dailyData:getProperty("pvpFree")
  		if pvpFree >= globalCsv.pvp_battle_free_count then
  			local cost = {[ItemId.PvpKey] = 1}
3dbbc9f3   zhouhaihai   加上新的任务
199
  			if not role:checkItemEnough(cost) then return 5 end
85083dba   zhouhaihai   拉黑从申请列表移除
200
201
202
203
  			role:costItems(cost)
  		else
  			role.dailyData:updateProperty({field = "pvpFree", delta = 1})
  		end
4cf74232   zhouhaihai   pvp
204
  
85083dba   zhouhaihai   拉黑从申请列表移除
205
206
  		key = tostring(math.random())
  		_pvpStartBattleCache = {idx = idx, key = key, revenge = revenge}
3dbbc9f3   zhouhaihai   加上新的任务
207
208
  
  		role:checkTaskEnter("PvpBattle")
4cf74232   zhouhaihai   pvp
209
210
  	end
  
85083dba   zhouhaihai   拉黑从申请列表移除
211
  	SendPacket(actionCodes.Pvp_startBattleRpc, MsgPack.pack({matchInfo = matchInfo, key = key, result = result, wait = wait}))
4cf74232   zhouhaihai   pvp
212
  	return true
440aa055   zhouhaihai   聊天
213
214
215
  end
  
  function _M.endBattleRpc(agent, data)
4cf74232   zhouhaihai   pvp
216
217
218
  	local role = agent.role
  	local roleId = role:getProperty("id")
  	local msg = MsgPack.unpack(data)
440aa055   zhouhaihai   聊天
219
  
4cf74232   zhouhaihai   pvp
220
221
222
  	if not msg.key or not _pvpStartBattleCache or msg.key ~= _pvpStartBattleCache.key then 
  		return 1
  	end
3dbbc9f3   zhouhaihai   加上新的任务
223
224
  
  	if not msg.idx or msg.idx ~= _pvpStartBattleCache.idx then
4cf74232   zhouhaihai   pvp
225
226
  		return 2
  	end
440aa055   zhouhaihai   聊天
227
  
4cf74232   zhouhaihai   pvp
228
  	local isWin = msg.starNum and msg.starNum > 0
85083dba   zhouhaihai   拉黑从申请列表移除
229
  	local now = skynet.timex()
4cf74232   zhouhaihai   pvp
230
231
232
233
234
  
  	local revenge = _pvpStartBattleCache.revenge
  	local match
  	if revenge then
  		match = _pvpRecordInfoCache[msg.idx]
f317b790   zhouhaihai   pvp 遗留
235
  		_revengeRecord[match.id] = now + RevengeWaitTime  -- 1分钟内不能再打
4cf74232   zhouhaihai   pvp
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
  	else
  		local pvpMC = role:getProperty("pvpMC")
  		match = pvpMC[msg.idx]
  	end
  	
  	if not match then return end
  
  	local temp = string.randWeight(csvdb["player_expCsv"][role:getProperty("level")].pvpBonus, true)
  	local reward = role:award({[temp[1]] = temp[2]})
  	local myScore, matchScore, oldmyScore, oldMatchScore, myRank, oldMyRank = role:changePvpScoreCommon(match.t == 1 and match.id or -1, isWin)
  
  	_pvpBattleInfoCacheC = {}  --重新发阵容了 没毛病
  	_pvpRecordInfoCache = {} -- 记录刷新了
  	_pvpRecordBattleInfoCache = {} -- 取新纪录的时候搞
  	_pvpStartBattleCache = nil
  
  	-- 请求上传录像
4cf74232   zhouhaihai   pvp
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
  	local params = {
  		["roleid"] = roleId,
  		["key"] = "zhaolugame20191016",
  		["time"] = now,
  	}
  	local status, body = httpc.get(remoteUrl, "/applyvideo?" .. httpGetFormatData(params), {}, {})
  	local video = nil
  	if tonumber(status) == 200 then
  		local result = json.decode(body)
  		video = result.name
  	else
  		skynet.error("applyvideo", "error", status, body, content)
  	end
  
  	-- 加入战斗记录
  	redisproxy:pipelining(function(red)
  		local dbKey = string.format(RECORD_PVP_COMMON, roleId)
  		red:lpush(dbKey, MsgPack.pack({
  			id = match.id,
  			t = match.t,
  			win = isWin,
  			time = now,
  			video = video,
  			sdelta = myScore - oldmyScore,
  		}))
8c20d812   zhouhaihai   pvp bUg
278
  		red:ltrim(dbKey, 0, 9)
4cf74232   zhouhaihai   pvp
279
280
281
282
283
284
285
286
287
288
289
  		-- 对方加入战斗记录
  		if match.t == 1 then
  			dbKey = string.format(RECORD_PVP_COMMON, match.id)
  			red:lpush(dbKey, MsgPack.pack({
  				id = roleId,
  				t = 1,
  				win = not isWin,
  				time = now,
  				video = video,
  				sdelta = matchScore - oldMatchScore,
  			}))
8c20d812   zhouhaihai   pvp bUg
290
  			red:ltrim(dbKey, 0, 9)
4cf74232   zhouhaihai   pvp
291
292
  		end
  	end)
3dbbc9f3   zhouhaihai   加上新的任务
293
294
295
296
  
  	if isWin then
  		role:checkTaskEnter("PvpWin", {score = myScore})
  	end
440aa055   zhouhaihai   聊天
297
  	
4cf74232   zhouhaihai   pvp
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
  	SendPacket(actionCodes.Pvp_endBattleRpc, MsgPack.pack({
  		reward = reward,
  		myScore = myScore,
  		matchScore = matchScore,
  		oldmyScore = oldmyScore,
  		oldMatchScore = oldMatchScore,
  		myRank = myRank,
  		oldMyRank = oldMyRank,
  		video = video,
  	}))
  	return true
  end
  
  function _M.rankListRpc(agent, data)
  	local role = agent.role
  	local roleId = role:getProperty("id")
f317b790   zhouhaihai   pvp 遗留
314
  	local msg = MsgPack.unpack(data)
4cf74232   zhouhaihai   pvp
315
316
317
318
319
320
321
322
323
  	local ptype = msg.ptype or 1
  
  	local response = {}
  	if ptype == 1 then -- 普通pvp
  		local redret = redisproxy:pipelining(function(red)
  			red:zscore(RANK_PVP_COMMON, roleId)
  			red:zrevrank(RANK_PVP_COMMON, roleId)
  			red:zrevrange(RANK_PVP_COMMON, 0, 99, "WITHSCORES")
  		end)
aaf6a9e6   zhouhaihai   分数默认1000
324
  		local score = role:unpackPvpScore(redret[1])
4cf74232   zhouhaihai   pvp
325
  		local rank = tonumber(redret[2] or -2) + 1  --排名 1 - ...  -1 未上榜 没打过pvp
4cf74232   zhouhaihai   pvp
326
327
  		local rankList = {}
  		for i = 1, #redret[3], 2 do
f317b790   zhouhaihai   pvp 遗留
328
329
  			local roleId = tonumber(redret[3][i])
  			local score = role:unpackPvpScore(redret[3][i + 1])
4cf74232   zhouhaihai   pvp
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
  			local online, curInfo = rpcRole(roleId, "friendSInfo")
  			curInfo.score = score
  			curInfo.roleId = roleId
  			table.insert(rankList, curInfo)
  		end
  		
  		response.score = score
  		response.rank = rank
  		response.rankList = rankList
  
  	elseif ptype == 2 then -- 高级pvp
  		return
  	else
  		return
  	end
  	SendPacket(actionCodes.Pvp_rankListRpc, MsgPack.pack(response))
  	return true
  end
  
  function _M.recordListRpc(agent, data)
  	local role = agent.role
  	local roleId = role:getProperty("id")
8c20d812   zhouhaihai   pvp bUg
352
  	local msg = MsgPack.unpack(data)
4cf74232   zhouhaihai   pvp
353
354
355
356
357
358
359
360
361
  	local ptype = msg.ptype or 1
  
  	local recordList = {}
  	local now = skynet.timex()
  	if ptype == 1 then -- 普通pvp
  		local rlist = redisproxy:lrange(string.format(RECORD_PVP_COMMON, roleId), 0 , 9)
  		local tempList = {}
  		for _, temp in ipairs(rlist) do
  			local one = MsgPack.unpack(temp)
85083dba   zhouhaihai   拉黑从申请列表移除
362
  			if now - one.time <= globalCsv.pvp_record_keep_time then -- 大于一天的弃之
4cf74232   zhouhaihai   pvp
363
364
365
366
367
  				table.insert(tempList, one)
  			end
  		end
  		_pvpRecordInfoCache = tempList
  
3dbbc9f3   zhouhaihai   加上新的任务
368
  		recordList = getMatchInfo(role, tempList, _pvpRecordBattleInfoCache)
4cf74232   zhouhaihai   pvp
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
  		for idx, info in ipairs(recordList) do
  			local temp = tempList[idx]
  			info.win = temp.win
  			info.time = temp.time
  			info.video = temp.video
  			info.sdelta = temp.sdelta
  		end
  		
  	elseif ptype == 2 then -- 高级pvp
  		return
  	else
  		return
  	end
  	SendPacket(actionCodes.Pvp_recordListRpc, MsgPack.pack({list = recordList}))
  	return true
440aa055   zhouhaihai   聊天
384
385
386
  end
  
  return _M