--[[ Author: zkj Date: 2021-05-31 14:40:54 LastEditTime: 2021-06-28 20:56:43 LastEditors: Please set LastEditors Description: battle agent --]] local skynet = require "skynet" local battle = require "battle" local baseService = require("baseService") local battleCode = require "battle.battleCode" local root = {} local mapBattleObj = {} -- string.format("%s:%s:%s", matchNode, matchId, battleId) -> battleObj -- 开始战斗 function root.start_battle(matchInfo, playerList, viewerList, battleParams, settleNodeInfo) local objBattle = battle.new(matchInfo, battleParams, settleNodeInfo) mapBattleObj[matchInfo.battleId] = objBattle objBattle:startBattle(playerList, viewerList) root.start_tick_timer() -- 通知 battleMgr skynet.call(".BattleMgr", "lua", "battleEnter", matchInfo.battleId, skynet.self()) return true end -- 玩家是否正在战斗 function root.is_user_in_battle(uid, battleId) if uid == nil or battleId == nil then return false end local objBattle = mapBattleObj[battleId] if objBattle == nil then return false end if objBattle:is_user_in_battle(uid) then return true end return false end -- 开始定时器 function root.start_tick_timer() if root.timer then return end root.timer = create_timeout( 100, function() root.tick_time_out() end ) end function root.tick_time_out() if root.timer then root.timer.delete() root.timer = nil end local unactiveList = nil for k, v in pairs(mapBattleObj) do if v.isActive then v.core:do_tick() else if unactiveList == nil then unactiveList = {} end table.insert(unactiveList, k) end end -- 清除非活跃战斗 if unactiveList then for _, v in ipairs(unactiveList) do log.info("tick_time_out 释放战斗[%s]", tostring(v)) mapBattleObj[v] = nil end end root.start_tick_timer() end -- 处理game转发过来的信息 function root.forward(battleId, cmd, ...) if battleId == nil or is_empty(cmd) then log.error("battle error: battleId[%s] cmd[%s]", tostring(battleId), tostring(cmd)) error("battle session error") end local objBattle = mapBattleObj[battleId] -- 已销毁/非活跃战斗 if not objBattle or not objBattle.isActive then log.error("battle obj not exists, battleId = %s, cmd = %s", battleId, tostring(cmd)) -- error("battle obj not exists") return {code = battleCode.BATTLE_NOT_EXIST} end local func = battle[cmd] if not func then error("battle func error") end return func(objBattle, ...) end function root.onStart() math.randomseed(tostring(os.time()) .. skynet.self()) end function root.recycle() skynet.exit() end baseService.start(root)