srvBattle.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. --[[
  2. Author: neo
  3. Date: 2021-05-31 14:40:54
  4. LastEditTime: 2021-06-28 20:56:43
  5. LastEditors: Please set LastEditors
  6. Description: battle agent
  7. --]]
  8. local skynet = require "skynet"
  9. local battle = require "battle"
  10. local baseService = require("baseService")
  11. local battleCode = require "battle.battleCode"
  12. local root = {}
  13. local mapBattleObj = {} -- string.format("%s:%s:%s", matchNode, matchId, battleId) -> battleObj
  14. -- 开始战斗
  15. function root.start_battle(matchInfo, playerList, viewerList, battleParams, settleNodeInfo)
  16. local objBattle = battle.new(matchInfo, battleParams, settleNodeInfo)
  17. mapBattleObj[matchInfo.battleId] = objBattle
  18. objBattle:startBattle(playerList, viewerList)
  19. root.start_tick_timer()
  20. -- 通知 battleMgr
  21. skynet.call(".BattleMgr", "lua", "battleEnter", matchInfo.battleId, skynet.self())
  22. return true
  23. end
  24. -- 玩家是否正在战斗
  25. function root.is_user_in_battle(uid, battleId)
  26. if uid == nil or battleId == nil then
  27. return false
  28. end
  29. local objBattle = mapBattleObj[battleId]
  30. if objBattle == nil then
  31. return false
  32. end
  33. if objBattle:is_user_in_battle(uid) then
  34. return true
  35. end
  36. return false
  37. end
  38. -- 开始定时器
  39. function root.start_tick_timer()
  40. if root.timer then
  41. return
  42. end
  43. root.timer =
  44. create_timeout(
  45. 100,
  46. function()
  47. root.tick_time_out()
  48. end
  49. )
  50. end
  51. function root.tick_time_out()
  52. if root.timer then
  53. root.timer.delete()
  54. root.timer = nil
  55. end
  56. local unactiveList = nil
  57. for k, v in pairs(mapBattleObj) do
  58. if v.isActive then
  59. v.core:do_tick()
  60. else
  61. if unactiveList == nil then
  62. unactiveList = {}
  63. end
  64. table.insert(unactiveList, k)
  65. end
  66. end
  67. -- 清除非活跃战斗
  68. if unactiveList then
  69. for _, v in ipairs(unactiveList) do
  70. log.info("tick_time_out 释放战斗[%s]", tostring(v))
  71. mapBattleObj[v] = nil
  72. end
  73. end
  74. root.start_tick_timer()
  75. end
  76. -- 处理game转发过来的信息
  77. function root.forward(battleId, cmd, ...)
  78. if battleId == nil or is_empty(cmd) then
  79. log.error("battle error: battleId[%s] cmd[%s]", tostring(battleId), tostring(cmd))
  80. error("battle session error")
  81. end
  82. local objBattle = mapBattleObj[battleId]
  83. -- 已销毁/非活跃战斗
  84. if not objBattle or not objBattle.isActive then
  85. log.error("battle obj not exists, battleId = %s, cmd = %s", battleId, tostring(cmd))
  86. -- error("battle obj not exists")
  87. return {code = battleCode.BATTLE_NOT_EXIST}
  88. end
  89. local func = battle[cmd]
  90. if not func then
  91. error("battle func error")
  92. end
  93. return func(objBattle, ...)
  94. end
  95. function root.onStart()
  96. math.randomseed(tostring(os.time()) .. skynet.self())
  97. end
  98. function root.recycle()
  99. skynet.exit()
  100. end
  101. baseService.start(root)