Blame view

src/models/RolePvp.lua 6.21 KB
fa565e0c   zhouhaihai   优化结构
1
2
3
4
5
6
7
8
  
  local RolePvp = {}
  
  RolePvp.bind = function (Role)
  
  local PVP_RANK_TIME_SORT_STD = 1924876800 -- 2030-12-31 00:00:00
  local PVP_RANK_TIME_SORT_PLACE = 1000000 -- 时间戳占据 6位数
  local PVP_RANK_TIME_SORT_PRECISION = 360 -- 时间精度 每6分钟忽略差异
b115474f   zhouhaihai   默认积分 1000
9
  local PVP_RANK_ROBOT_SCORE = globalCsv.pvp_base_score -- 机器人积分
fa565e0c   zhouhaihai   优化结构
10
  
3dbbc9f3   zhouhaihai   加上新的任务
11
12
  
  function Role:unpackPvpScore(score)
fa565e0c   zhouhaihai   优化结构
13
14
15
16
  	score = tonumber(score or 0)
  	return math.floor(score / PVP_RANK_TIME_SORT_PLACE)
  end
  
3dbbc9f3   zhouhaihai   加上新的任务
17
  function Role:packPvpScore(score, now)
fa565e0c   zhouhaihai   优化结构
18
19
20
21
  	now = now or skynet.timex()
  	return math.floor(score * PVP_RANK_TIME_SORT_PLACE + (PVP_RANK_TIME_SORT_STD - now) / PVP_RANK_TIME_SORT_PRECISION)
  end
  
fa565e0c   zhouhaihai   优化结构
22
23
24
25
26
  function Role:changePvpScoreCommon(matchId, isWin)
  	local roleId = self:getProperty("id")
  	local isPlayer = matchId ~= -1
  	local redret = redisproxy:pipelining(function(red)
  		red:zscore(RANK_PVP_COMMON, roleId)
4cf74232   zhouhaihai   pvp
27
  		red:zrevrank(RANK_PVP_COMMON, roleId)
fa565e0c   zhouhaihai   优化结构
28
29
30
31
  		if isPlayer then
  			red:zscore(RANK_PVP_COMMON, matchId)
  		end
  	end)
3dbbc9f3   zhouhaihai   加上新的任务
32
  	local myScore = self:unpackPvpScore(redret[1])
4cf74232   zhouhaihai   pvp
33
  	local oldMyRank = tonumber(redret[2] or -2) + 1
fa565e0c   zhouhaihai   优化结构
34
35
  	local matchScore = PVP_RANK_ROBOT_SCORE
  	if isPlayer then
3dbbc9f3   zhouhaihai   加上新的任务
36
  		matchScore = self:unpackPvpScore(redret[3])
fa565e0c   zhouhaihai   优化结构
37
  	end
4cf74232   zhouhaihai   pvp
38
  	local oldmyScore, oldMatchScore = myScore, matchScore
fa565e0c   zhouhaihai   优化结构
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  
  	if isWin then
  		local scoreChange = math.ceil(60 / (1 + 10 ^ ((myScore - matchScore) / 400)))
  		myScore = myScore + scoreChange
  		matchScore = matchScore - scoreChange
  	else
  		local scoreChange = math.ceil(60 / (1 + 10 ^ ((matchScore - myScore) / 400)))
  		myScore = myScore - scoreChange
  		matchScore = matchScore + scoreChange
  	end
  
  	myScore = math.max(myScore, 0)
  	matchScore = math.max(matchScore, 0)
  
  	local now = skynet.timex()
  	redisproxy:pipelining(function(red)
3dbbc9f3   zhouhaihai   加上新的任务
55
  		red:zadd(RANK_PVP_COMMON, self:packPvpScore(myScore, now), roleId)
fa565e0c   zhouhaihai   优化结构
56
  		if isPlayer then
3dbbc9f3   zhouhaihai   加上新的任务
57
  			red:zadd(RANK_PVP_COMMON, self:packPvpScore(matchScore, now), matchId)
fa565e0c   zhouhaihai   优化结构
58
  		end
4cf74232   zhouhaihai   pvp
59
  		red:zrevrank(RANK_PVP_COMMON, roleId)
fa565e0c   zhouhaihai   优化结构
60
  	end)
4cf74232   zhouhaihai   pvp
61
62
63
64
65
66
67
68
  	local myRank
  	if isPlayer then
  		myRank = tonumber(redret[3] or -2) + 1
  	else
  		myRank = tonumber(redret[2] or -2) + 1
  	end
  	self:refreshPvpMatchC(myScore)
  	return myScore, matchScore, oldmyScore, oldMatchScore, myRank, oldMyRank
fa565e0c   zhouhaihai   优化结构
69
70
71
72
  end
  
  function Role:refreshPvpMatchC(score)
  	local roleId = self:getProperty("id")
b115474f   zhouhaihai   默认积分 1000
73
74
75
76
77
78
79
80
81
82
  	
  	if not score then
  		local tempScore = redisproxy:zscore(RANK_PVP_COMMON, roleId)
  		if not tempScore then
  			score = globalCsv.pvp_base_score
  		else
  			score = self:unpackPvpScore(tempScore)
  		end
  	end
  	
fa565e0c   zhouhaihai   优化结构
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
  	local function getPlayers(levels)
  		local redret = redisproxy:pipelining(function(red)
  			for _, level in ipairs(levels) do
  				local low, high = table.unpack(level)
  				red:zadd(RANK_PVP_COMMON, low * PVP_RANK_TIME_SORT_PLACE, "std_temp1")
  				red:zadd(RANK_PVP_COMMON, high * PVP_RANK_TIME_SORT_PLACE + PVP_RANK_TIME_SORT_PLACE - 1, "std_temp2")
  				red:ZREVRANK(RANK_PVP_COMMON, "std_temp1")
  				red:ZREVRANK(RANK_PVP_COMMON, "std_temp2")
  			end
  			red:zrem(RANK_PVP_COMMON, "std_temp1", "std_temp2")
  		end)
  
  		local PreGetCount = 7 
  		local redret = redisproxy:pipelining(function(red)
  			for idx, level in ipairs(levels) do
  				local rank1 = tonumber(redret[(idx - 1) * 4 + 3])
  				local rank2 = tonumber(redret[(idx - 1) * 4 + 4])
  				if rank1 - rank2 > PreGetCount then
  					rank2 = math.randomInt(rank2, rank1 - PreGetCount + 1)
  					rank1 = rank2 + PreGetCount - 1
  				end
  				red:ZREVRANGE(RANK_PVP_COMMON, rank2, rank1)
  			end
  		end)
  		return redret
  	end
  
  	local findIdx = #globalCsv.pvp_division
  	for idx, limit in ipairs(globalCsv.pvp_division) do
  		if score < limit then
  			findIdx = idx - 1
  			break
  		end
  	end
  	local levels = {
  		{}, {}, {}
  	}
  	if globalCsv.pvp_division[findIdx + 1] then
  		levels[1] = {globalCsv.pvp_division[findIdx + 1], (globalCsv.pvp_division[findIdx + 2] or 100000000) - 1}
  	end
  	levels[2] =  {globalCsv.pvp_division[findIdx], (globalCsv.pvp_division[findIdx + 1] or 100000000) - 1}
  	if globalCsv.pvp_division[findIdx - 1] then
  		levels[3] =  {globalCsv.pvp_division[findIdx - 1], globalCsv.pvp_division[findIdx] - 1}
  	end
  	local redirect = {}
  	for i = #levels , 1, -1 do
  		if not next(levels[i]) then
  			table.remove(levels, i)
  			redirect[i] = -1
  			for _, v in pairs(redirect) do
  				redirect[_] = v - 1
  			end
  		else
  			redirect[i] = i
  		end
  	end
  
  	local result = getPlayers(levels)
  	local matchC = self:getProperty("pvpMC")
  	local hadPlayer = {[roleId] = 1}
  	local hadRobot = {}
  	for _, one in pairs(matchC) do
  		if one.t == 1 then
  			hadPlayer[one.id] = 1
  		elseif one.t == 2 then
  			hadRobot[one.id] = 1
  		end
  	end
  
  	for _, temp in pairs(result) do
  		for i = #temp, 1, -1 do
  			local id = tonumber(temp[i])
  			if hadPlayer[id] then
  				table.remove(temp, i)
  			else
  				temp[i] = id
  				hadPlayer[id] = 1
  			end
  		end
  	end
  	-- 增加第几个
  	local function getPlayer(idx)
  		for i = idx, 3 do
  			if redirect[i] ~= -1 then
  				local curR = result[redirect[i]] or {}
  				if next(curR) then
  					local curIdx = math.randomInt(1, #curR)
  					local objId = curR[curIdx]
  					table.remove(curR, curIdx)
  					return objId
  				end
  			end
  		end
  	end
  
  	local tempMatchC = {}
  	local curCount = 0
  	for i = 1, 3 do
  		local objId = getPlayer(i)
  		if objId then
  			tempMatchC[i] = {t = 1, id = objId}
  			curCount = curCount + 1
  		end
  	end
  	
  	-- 正常的玩家不够了 低一档继续
  	if curCount < 3 then
  		local level = nil
  		if globalCsv.pvp_division[findIdx - 2] then
  			level =  {globalCsv.pvp_division[findIdx - 2], globalCsv.pvp_division[findIdx - 1] - 1}
  		end
  		if level then
  			local result = getPlayers({level})[1] or {}
  			for i = #result, 1, -1 do
  				local id = tonumber(result[i])
  				if hadPlayer[id] then
  					table.remove(result, i)
  				else
  					result[i] = id
  					hadPlayer[id] = 1
  				end
  			end
  
  			if next(result) then
  				for i = curCount + 1, 3 do
  					local curIdx = math.randomInt(1, #result)
  					local objId = result[curIdx]
  					table.remove(result, curIdx)
  					tempMatchC[i] = {t = 1, id = objId}
  					curCount = curCount + 1
  					if not next(result) then
  						break
  					end
  				end
  			end
  		end
  	end
  
  	-- 增加机器人
  	if curCount < 3 then
  		for i = curCount + 1, 3 do
  			while true do
  				local id = math.randomInt(1, #csvdb["pvp_robotCsv"])
  				if not hadRobot[id] then
  					hadRobot[id] = 1
  					tempMatchC[i] = {t = 2, id = id}
  					break
  				end
  			end
  		end
  	end
  
  	self:setProperty("pvpMC", tempMatchC)
  end
  
  
  end
  
  
  return RolePvp