local LinkedList = class("LinkedList")
function LinkedList:ctor() end
function LinkedList:size() end
function LinkedList:isEmpty() end
function LinkedList:clear() end
function LinkedList:pushFront(item) end
function LinkedList:pushBack(item) end
func
这是一个基于双端队列实现的双人战斗匹配类。该类用于管理玩家的匹配队列,支持加入匹配、取消匹配、匹配成功等机制。
```lua
local LinkedList = require("LinkedList") -- 假设LinkedList已定义
local BattleMatchmaker = class("BattleMatchmaker")
function BattleMatchmaker:ctor()
self._waitingQueue = LinkedList.new() -- 等待匹配的玩家队列
self._matchedPairs = {} -- 已匹配的玩家对
self._playerToNode = {} -- 玩家ID到队列节点的映射
self._matchCallback = nil -- 匹配成功回调
end
-- 设置匹配成功回调函数
function BattleMatchmaker:setMatchCallback(callback)
self._matchCallback = callback
end
-- 玩家加入匹配队列
function BattleMatchmaker:joinMatch(playerId)
if self._playerToNode[playerId] then
return false, "玩家已在匹配队列中"
end
local node = { playerId = playerId }
self._waitingQueue:pushBack(node)
self._playerToNode[playerId] = node
-- 尝试匹配
self:_tryMatch()
return true
end
-- 玩家取消匹配
function BattleMatchmaker:cancelMatch(playerId)
local node = self._playerToNode[playerId]
if not node then
return false, "玩家不在匹配队列中"
end
-- 从队列中移除
-- 这里需要遍历队列找到对应节点并移除
-- 由于LinkedList没有提供直接移除节点的接口,需要扩展或使用其他方式
-- 为简化,这里假设有removeNode方法
self._waitingQueue:removeNode(node)
self._playerToNode[playerId] = nil
return true
end
-- 获取匹配状态
function BattleMatchmaker:getMatchStatus(playerId)
if self._playerToNode[playerId] then
return "waiting", "等待匹配中"
end
for _, pair in pairs(self._matchedPairs) do
if pair.player1 == playerId or pair.player2 == playerId then
return "matched", "已匹配成功", pair
end
end
return "none", "未在匹配中"
end
-- 确认匹配(双方确认后开始战斗)
function BattleMatchmaker:confirmMatch(playerId, matchId)
local pair = self._matchedPairs[matchId]
if not pair then
return false, "匹配不存在"
end
if pair.player1 == playerId then
pair.confirmed1 = true
elseif pair.player2 == playerId then
pair.confirmed2 = true
else
return false, "玩家不在此匹配中"
end
-- 双方都确认,触发战斗开始
if pair.confirmed1 and pair.confirmed2 then
if self._matchCallback then
self._matchCallback(pair.player1, pair.player2, matchId)
end
self._matchedPairs[matchId] = nil
return true, "匹配确认成功,开始战斗"
end
return true, "等待对方确认"
end
-- 私有方法:尝试匹配
function BattleMatchmaker:_tryMatch()
while self._waitingQueue:size() >= 2 do
local player1Node = self._waitingQueue:front()
local player2Node = self._waitingQueue:getNext(player1Node) -- 假设有getNext方法
if player1Node and player2Node then
-- 创建匹配对
local matchId = tostring(os.time()) .. "_" .. math.random(1000, 9999)
local pair = {
player1 = player1Node.playerId,
player2 = player2Node.playerId,
matchId = matchId,
confirmed1 = false,
confirmed2 = false,
matchTime = os.time()
}
-- 从队列中移除
self._waitingQueue:popFront()
self._waitingQueue:removeNode(player2Node) -- 移除第二个玩家
-- 清理映射
self._playerToNode[player1Node.playerId] = nil
self._playerToNode[player2Node.playerId] = nil
-- 存储匹配对
self._matchedPairs[matchId] = pair
-- 通知玩家匹配成功
self:_notifyMatchSuccess(pair)
end
end
end
-- 私有方法:通知匹配成功
function BattleMatchmaker:_notifyMatchSuccess(pair)
-- 这里需要实现通知逻辑,比如通过消息发送给客户端
-- 示例:sendMatchNotification(pair.player1, pair.player2, pair.matchId)
print(string.format("匹配成功: %s vs %s (ID: %s)",
pair.player1, pair.player2, pair.matchId))
end
-- 清理过期匹配(超过一定时间未确认)
function BattleMatchmaker:cleanupExpiredMatches(timeout)
local currentTime = os.time()
local expired = {}
for matchId, pair in pairs(self._matchedPairs) do
if currentTime - pair.matchTime > timeout then
table.insert(expired, matchId)
end
end
for _, matchId in ipairs(expired) do
local pair = self._matchedPairs[matchId]
-- 将玩家重新加入队列
if not pair.confirmed1 then
self:joinMatch(pair.player1)
end
if not pair.confirmed2 then
self:joinMatch(pair.player2)
end
self._matchedPairs[matchId] = nil
end
return #expired
end
-- 获取等待队列长度
function BattleMatchmaker:getWaitingCount()
return self._waitingQueue:size()
end
-- 获取进行中匹配数量
function BattleMatchmaker:getMatchedCount()
local count = 0
for _ in pairs(self._matchedPairs) do
count = count + 1
end
return count
end
return BattleMatchmaker
```
这个匹配类的主要功能包括:
1. **加入匹配队列**:玩家调用`joinMatch`加入等待队列
2. **自动匹配**:当队列中有至少2名玩家时,自动进行匹配
3. **匹配确认**:匹配成功后需要双方确认才能开始战斗
4. **取消匹配**:玩家可以随时取消匹配
5. **超时处理**:长时间未确认的匹配会被自动清理
6. **状态查询**:可以查询玩家的匹配状态
使用示例:
```lua
local matchmaker = BattleMatchmaker.new()
-- 设置匹配成功回调
matchmaker:setMatchCallback(function(player1, player2, matchId)
print("开始战斗:", player1, "vs", player2)
-- 这里可以触发战斗开始逻辑
end)
-- 玩家加入匹配
matchmaker:joinMatch("player_001")
matchmaker:joinMatch("player_002")
-- 查询状态
local status, msg, pair = matchmaker:getMatchStatus("player_001")
-- 确认匹配
if status == "matched" then
matchmaker:confirmMatch("player_001", pair.matchId)
matchmaker:confirmMatch("player_002", pair.matchId)
end
-- 定期清理过期匹配
matchmaker:cleanupExpiredMatches(30) -- 30秒超时
```
这个实现参考了游戏中的匹配机制,如师徒系统的查找功能(资料8、18)和事件系统(资料16),确保了匹配的可靠性和可扩展性。