要在匹配器中加入分段匹配功能,需要修改数据结构以包含玩家分段信息,并在匹配逻辑中考虑分段差异。
首先,修改数据结构,在玩家加入匹配时记录其分段:
```lua
-- 玩家加入匹配队列,增加segment分段参数
function JiHuHuiWuMatch:joinMatch(playerId, segment)
if self._playerToNode[playerId] then
return false, "玩家已在匹配队列中"
end
local node = {
playerId = playerId,
segment = segment or 0 -- 默认分段为0
}
self._waitingQueue:pushBack(node)
self._playerToNode[playerId] = node
-- 尝试匹配
self:_tryMatch()
return true
end
```
修改匹配逻辑,考虑分段差异:
```lua
-- 私有方法:尝试匹配,考虑分段
function JiHuHuiWuMatch:_tryMatch()
if self._waitingQueue:size() < 2 then
return
end
-- 将队列转换为数组以便遍历
local players = {}
local node = self._waitingQueue:front()
while node do
table.insert(players, node)
node = self._waitingQueue:getNext(node)
end
-- 按分段排序(可选)
table.sort(players, function(a, b)
return a.segment < b.segment
end)
-- 尝试匹配
for i = 1, #players - 1 do
local player1 = players[i]
-- 寻找分段相近的对手
for j = i + 1, #players do
local player2 = players[j]
-- 分段差异阈值(可配置)
local segmentDiff = math.abs(player1.segment - player2.segment)
if segmentDiff <= self._maxSegmentDiff or self._maxSegmentDiff == nil then
-- 匹配成功
local matchId = tostring(os.time()) .. "_" .. math.random(1000, 9999)
local pair = {
player1 = player1.playerId,
player2 = player2.playerId,
segment1 = player1.segment,
segment2 = player2.segment,
matchId = matchId,
confirmed1 = false,
confirmed2 = false,
matchTime = os.time()
}
-- 从队列中移除
self._waitingQueue:removeNode(player1)
self._waitingQueue:removeNode(player2)
-- 清理映射
self._playerToNode[player1.playerId] = nil
self._playerToNode[player2.playerId] = nil
-- 存储匹配对
self._matchedPairs[matchId] = pair
-- 通知玩家匹配成功
self:_notifyMatchSuccess(pair)
-- 继续匹配其他玩家
break
end
end
end
end
```
添加分段相关配置和方法:
```lua
function JiHuHuiWuMatch:ctor()
self._waitingQueue = LinkedList.new()
self._matchedPairs = {}
self._playerToNode = {}
self._matchCallback = nil
self._maxSegmentDiff = 2 -- 最大分段差异,可配置
self._segmentConfig = {
{min = 0, max = 1000, name = "青铜"},
{min = 1001, max = 2000, name = "白银"},
{min = 2001, max = 3000, name = "黄金"},
-- 更多分段配置...
}
end
-- 设置最大分段差异
function JiHuHuiWuMatch:setMaxSegmentDiff(diff)
self._maxSegmentDiff = diff
end
-- 根据分数获取分段
function JiHuHuiWuMatch:getSegmentByScore(score)
for _, config in ipairs(self._segmentConfig) do
if score >= config.min and score <= config.max then
return config
end
end
return self._segmentConfig[1] -- 默认返回第一个分段
end
-- 获取玩家分段信息
function JiHuHuiWuMatch:getPlayerSegmentInfo(playerId)
local node = self._playerToNode[playerId]
if node then
return node.segment
end
for _, pair in pairs(self._matchedPairs) do
if pair.player1 == playerId then
return pair.segment1
elseif pair.player2 == playerId then
return pair.segment2
end
end
return nil
end
```
修改匹配状态查询,包含分段信息:
```lua
-- 获取匹配状态,返回分段信息
function JiHuHuiWuMatch:getMatchStatus(playerId)
if self._playerToNode[playerId] then
local node = self._playerToNode[playerId]
return "waiting", string.format("等待匹配中(分段:%d)", node.segment), {segment = node.segment}
end
for _, pair in pairs(self._matchedPairs) do
if pair.player1 == playerId or pair.player2 == playerId then
local opponent = pair.player1 == playerId and pair.player2 or pair.player1
local mySegment = pair.player1 == playerId and pair.segment1 or pair.segment2
local oppSegment = pair.player1 == playerId and pair.segment2 or pair.segment1
return "matched",
string.format("已匹配成功(我的分段:%d,对手分段:%d)", mySegment, oppSegment),
pair
end
end
return "none", "未在匹配中"
end
```
添加分段统计功能:
```lua
-- 获取各分段等待人数
function JiHuHuiWuMatch:getSegmentWaitingCounts()
local counts = {}
local node = self._waitingQueue:front()
while node do
local segment = node.segment
counts[segment] = (counts[segment] or 0) + 1
node = self._waitingQueue:getNext(node)
end
return counts
end
-- 获取匹配成功率统计(按分段)
function JiHuHuiWuMatch:getMatchSuccessRateBySegment()
-- 这里可以实现统计各分段的匹配成功率
-- 需要记录历史匹配数据
end
```
这样修改后,匹配器会根据玩家的分段进行匹配,优先匹配分段相近的玩家,确保对战的公平性。可以根据实际需求调整分段差异阈值和分段配置。