MoZhuMiddleLogic.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. ---- 次元魔珠 中心服
  2. local Util = require("common.Util")
  3. local ObjHuman = require("core.ObjHuman")
  4. local MoZhuDB = require("mozhu.MoZhuDB")
  5. local MoZhuExcel = require("excel.mozhu")
  6. local InnerMsg = require("core.InnerMsg")
  7. local MoZhuLogic = require("mozhu.MoZhuLogic")
  8. local CombatDefine = require("combat.CombatDefine")
  9. local Msg = require("core.Msg")
  10. local MiddleManager = require("middle.MiddleManager")
  11. function sendReward(mozhuData)
  12. local unionList = {}
  13. for rank, unionUuid in ipairs(mozhuData.unionRank) do
  14. unionList[unionUuid] = rank
  15. end
  16. MoZhuLogic.actOverRank({
  17. type = 2,
  18. list = unionList
  19. })
  20. local roleList = {}
  21. for rank, uuid in ipairs(mozhuData.roleRank) do
  22. roleList[uuid] = rank
  23. end
  24. MoZhuLogic.actOverRank({
  25. type = 1,
  26. list = roleList
  27. })
  28. end
  29. function onHour(hour)
  30. if _G.is_middle ~= true then
  31. -- 目前没有跨服,暂时注释
  32. --return
  33. end
  34. local nowWeek = Util.getWeekDay()
  35. -- mozhu 暂未开放
  36. if not MoZhuLogic.MOZHU_OPEN_DAY[nowWeek] then
  37. --是否需要重置mozhu数据
  38. return
  39. end
  40. local mozhuData = MoZhuDB.getMoZhu()
  41. if not next(mozhuData) or not Util.isSameDay(mozhuData.time) then
  42. MoZhuDB.initMoZhu()
  43. mozhuData = MoZhuDB.getMoZhu()
  44. end
  45. -- 是否发奖励
  46. local start,finish = MoZhuLogic.getActTime()
  47. local nowTime = os.time()
  48. if nowTime >= finish and nowTime < (start + 86400) and (not mozhuData.isReward or not Util.isSameDay(mozhuData.isReward))then
  49. mozhuData.isReward = nowTime
  50. -- 延后处理 随机十秒
  51. local r = math.random(10)
  52. require("core.Timer").addLater(r,function()
  53. sendReward(mozhuData)
  54. MoZhuDB.saveMoZhu()
  55. end)
  56. -- 发放上一日 排名奖励
  57. --actOverUnion()
  58. --actOverRole()
  59. MoZhuDB.saveMoZhu()
  60. end
  61. end
  62. function getCiTiao()
  63. local mozhuDB = MoZhuDB.getMoZhu()
  64. return mozhuDB.citiao
  65. end
  66. function fontRoleRank(mozhuDB, msgInner, topRank)
  67. local maxRank = topRank and topRank or #mozhuDB.roleRank
  68. maxRank = maxRank <= #mozhuDB.roleRank and maxRank or #mozhuDB.roleRank
  69. maxRank = maxRank <= 100 and maxRank or 100
  70. for i = 1, maxRank do
  71. local uuid = mozhuDB.roleRank[i]
  72. local role = mozhuDB.role[uuid]
  73. if uuid and role then
  74. -- local net = msgInner.roleRank[i]
  75. msgInner.roleRank[i] = {}
  76. msgInner.roleRank[i].uuid = uuid
  77. msgInner.roleRank[i].roleBase = role.roleBase
  78. msgInner.roleRank[i].hurt = role.hurt
  79. end
  80. end
  81. end
  82. function fontUnionRank(mozhuDB, msgInner, topRank)
  83. local maxRank = topRank and topRank or #mozhuDB.unionRank
  84. maxRank = maxRank <= #mozhuDB.unionRank and maxRank or #mozhuDB.unionRank
  85. maxRank = maxRank <= 50 and maxRank or 50
  86. for i = 1, maxRank do
  87. local unionUuid = mozhuDB.unionRank[i]
  88. local union = mozhuDB.union[unionUuid]
  89. if unionUuid and union then
  90. -- local net = msgInner.unionRank[i]
  91. msgInner.unionRank[i] = {}
  92. msgInner.unionRank[i].unionUuid = unionUuid
  93. msgInner.unionRank[i].unionBase = union.unionBase
  94. msgInner.unionRank[i].hurt = union.hurt
  95. end
  96. end
  97. end
  98. function fontMyRank(mozhuDB, uuid, msgInner)
  99. local rank = MoZhuDB.getRoleRank(uuid)
  100. local role = mozhuDB.role[uuid]
  101. if rank > 0 and role then
  102. msgInner.info.myRank.uuid = uuid
  103. msgInner.info.myRank.rank = rank
  104. msgInner.info.myRank.roleBase = role.roleBase
  105. msgInner.info.myRank.hurt = role.hurt
  106. end
  107. end
  108. function fontMyUnion(mozhuDB, unionUuid, msgInner)
  109. local union = mozhuDB.union[unionUuid]
  110. local rank = MoZhuDB.getUnionRank(unionUuid)
  111. if rank > 0 and union then
  112. msgInner.info.myUnion.unionUuid = unionUuid
  113. msgInner.info.myUnion.rank = rank
  114. msgInner.info.myUnion.unionBase = union.unionBase
  115. msgInner.info.myUnion.hurt = union.hurt
  116. end
  117. end
  118. function LW_MOZHU_QUERY(fd, msg)
  119. MoZhuDB.checkUnion()
  120. local mozhuDB = MoZhuDB.getMoZhu()
  121. if not mozhuDB or not mozhuDB.time then
  122. -- MoZhuDB.initMoZhu()
  123. -- mozhuDB = MoZhuDB.getMoZhu()
  124. end
  125. -- Util.printTable(mozhuDB)
  126. local msgInner = {} --InnerMsg.wl.WL_MOZHU_QUERY
  127. msgInner.uuid = msg.uuid
  128. msgInner.myRank = 0
  129. msgInner.roleRank = {}
  130. msgInner.unionRank = {}
  131. msgInner.info = {}
  132. msgInner.info.citiao = {}
  133. msgInner.info.myRank = {}
  134. msgInner.info.myUnion = {}
  135. msgInner.info.citiao = mozhuDB.citiao
  136. msgInner.info.citiaoTime = mozhuDB.citiaoTime
  137. fontUnionRank(mozhuDB, msgInner, 10)
  138. fontRoleRank(mozhuDB, msgInner, 10)
  139. fontMyRank(mozhuDB, msg.uuid, msgInner)
  140. fontMyUnion(mozhuDB, msg.unionUuid, msgInner)
  141. --InnerMsg.sendMsg(fd, msgInner)
  142. WL_MOZHU_QUERY(fd,msgInner)
  143. end
  144. function WL_MOZHU_QUERY(fd, msg)
  145. local human = ObjHuman.onlineUuid[msg.uuid]
  146. if human == nil then
  147. return
  148. end
  149. MoZhuLogic.WLQuery(human,msg)
  150. end
  151. function LW_MOZHU_FIGHT_END(fd, msg)
  152. -- 检测活动是否已经结束
  153. local combatInfo = msg.combatInfo
  154. local atkHurt = 0
  155. for pos = 1, CombatDefine.COMBAT_HERO_CNT do
  156. local obj = combatInfo.objList and combatInfo.objList[pos]
  157. if obj then
  158. atkHurt = atkHurt + obj.result[1]
  159. end
  160. end
  161. -- 魔兽 造成伤害
  162. for _,pos in ipairs(CombatDefine.SIDE2HELPPOS[CombatDefine.ATTACK_SIDE]) do
  163. local pet = combatInfo.helpList and combatInfo.helpList[pos]
  164. if pet and pet.isPet then
  165. atkHurt = atkHurt + pet.result[1]
  166. end
  167. end
  168. if atkHurt <= 0 then
  169. atkHurt = 0
  170. end
  171. local startTime, endTime = MoZhuLogic.getActTime()
  172. local now = os.time()
  173. local msgInner = {} --InnerMsg.wl.WL_MOZHU_FIGHT_END
  174. if now < startTime or now > endTime then
  175. local rank, maxHurt = MoZhuDB.getRoleRank(msg.uuid)
  176. local unRank = MoZhuDB.getUnionRank(msg.unionUuid)
  177. msgInner.backType = 0
  178. msgInner.uuid = msg.uuid
  179. msgInner.oldRank= rank
  180. msgInner.rank = rank
  181. msgInner.unionRank = unRank
  182. msgInner.unionRanked = unRank
  183. msgInner.thisHurt = atkHurt
  184. msgInner.maxHurt = maxHurt
  185. msgInner.combatInfo = msg.combatInfo
  186. -- Msg.trace(msgInner)
  187. --InnerMsg.sendMsg(fd, msgInner)
  188. WL_MOZHU_FIGHT_END(fd,msgInner)
  189. return
  190. end
  191. local oldRank = MoZhuDB.getRoleRank(msg.uuid)
  192. local oldUnRank = MoZhuDB.getUnionRank(msg.unionUuid)
  193. MoZhuDB.updateHurt(msg.uuid, msg.roleBase, msg.maxHurt)
  194. MoZhuDB.updateUnion(msg.unionUuid, msg.unionBase)
  195. local newRank, maxHurt = MoZhuDB.getRoleRank(msg.uuid)
  196. local newUnRank = MoZhuDB.getUnionRank(msg.unionUuid)
  197. -- Util.printTable(MoZhuDB.getMoZhu())
  198. msgInner.backType = 1
  199. msgInner.uuid = msg.uuid
  200. msgInner.oldRank= oldRank
  201. msgInner.rank = newRank
  202. msgInner.unionRank = newUnRank
  203. msgInner.unionRanked = oldUnRank
  204. msgInner.thisHurt = atkHurt
  205. msgInner.maxHurt = maxHurt
  206. msgInner.combatInfo = msg.combatInfo
  207. -- Msg.trace(msgInner)
  208. --InnerMsg.sendMsg(fd, msgInner)
  209. WL_MOZHU_FIGHT_END(fd,msgInner)
  210. MoZhuDB.saveMoZhu()
  211. end
  212. function WL_MOZHU_FIGHT_END(fd, msg)
  213. local human = ObjHuman.onlineUuid[msg.uuid]
  214. if human == nil then
  215. return
  216. end
  217. MoZhuLogic.onfightEndWL(human,msg)
  218. end
  219. function LW_MOZHU_RANK_QUERY(fd, msg)
  220. local mozhuDB = MoZhuDB.getMoZhu()
  221. local msgInner = {}--InnerMsg.wl.WL_MOZHU_RANK_QUERY
  222. msgInner.uuid = msg.uuid
  223. msgInner.type = msg.type
  224. msgInner.roleRank = {}
  225. msgInner.unionRank = {}
  226. if msg.type == 1 then
  227. fontRoleRank(mozhuDB, msgInner)
  228. else
  229. fontUnionRank(mozhuDB, msgInner)
  230. end
  231. --InnerMsg.sendMsg(fd, msgInner)
  232. WL_MOZHU_RANK_QUERY(fd,msgInner)
  233. end
  234. function WL_MOZHU_RANK_QUERY(fd, msg)
  235. local human = ObjHuman.onlineUuid[msg.uuid]
  236. if human == nil then
  237. return
  238. end
  239. MoZhuLogic.rankQueryWL(human, msg)
  240. end
  241. function LW_MOZHU_UNION_GET(fd, msg)
  242. local mozhuDB = MoZhuDB.getMoZhu()
  243. local union = mozhuDB.union[msg.unionUuid]
  244. local rank = MoZhuDB.getUnionRank(msg.unionUuid)
  245. local ret = 0
  246. if rank > 0 and union then
  247. local config = MoZhuExcel.hurt[msg.id]
  248. if config.unionHurt <= union.hurt then
  249. ret = 1
  250. end
  251. end
  252. local msgInner = {} --InnerMsg.wl.WL_MOZHU_UNION_GET
  253. msgInner.uuid = msg.uuid
  254. msgInner.ret = ret
  255. msgInner.id = msg.id
  256. --InnerMsg.sendMsg(fd, msgInner)
  257. WL_MOZHU_UNION_GET(fd,msgInner)
  258. end
  259. function WL_MOZHU_UNION_GET(fd, msg)
  260. local human = ObjHuman.onlineUuid[msg.uuid]
  261. if human == nil then
  262. return
  263. end
  264. MoZhuLogic.unionGetWL(human, msg)
  265. end
  266. function LW_ROLE_UNION_OP(fd, msg)
  267. if msg.op == 1 then
  268. roleLeaveUnion(msg.uuid, msg.unionUuid)
  269. elseif msg.op == 2 then
  270. joinUnion(msg.uuid, msg.unionUuid)
  271. elseif msg.op == 3 then
  272. dismissUnion(msg.uuid, msg.unionUuid)
  273. end
  274. end
  275. function roleLeaveUnion(uuid, unionUuid)
  276. local mozhuDB = MoZhuDB.getMoZhu()
  277. local rank = MoZhuDB.getRoleRank(uuid)
  278. local role = mozhuDB.role[uuid]
  279. local union = mozhuDB.union[unionUuid]
  280. if rank > 0 and role then
  281. MoZhuDB.chageRoleUnionUuid(uuid, "")
  282. if union then
  283. MoZhuDB.refreshUnion(unionUuid)
  284. end
  285. MoZhuDB.saveMoZhu()
  286. end
  287. end
  288. function joinUnion(uuid, unionUuid)
  289. local mozhuDB = MoZhuDB.getMoZhu()
  290. local rank = MoZhuDB.getRoleRank(uuid)
  291. local role = mozhuDB.role[uuid]
  292. local union = mozhuDB.union[unionUuid]
  293. if rank > 0 and role then
  294. MoZhuDB.chageRoleUnionUuid(uuid, unionUuid)
  295. if union then
  296. MoZhuDB.refreshUnion(unionUuid)
  297. end
  298. MoZhuDB.saveMoZhu()
  299. end
  300. end
  301. function dismissUnion(uuid, unionUuid)
  302. local mozhuDB = MoZhuDB.getMoZhu()
  303. local rank = MoZhuDB.getRoleRank(uuid)
  304. local role = mozhuDB.role[uuid]
  305. if rank > 0 and role then
  306. MoZhuDB.chageRoleUnionUuid(uuid, unionUuid)
  307. end
  308. local union = mozhuDB.union[unionUuid]
  309. if union then
  310. MoZhuDB.dismissUnion(unionUuid)
  311. end
  312. end
  313. -------------------------------- Act End --------------------------
  314. function actOverUnion()
  315. local mozhuDB = MoZhuDB.getMoZhu()
  316. local svrList = {}
  317. for rank, unionUuid in ipairs(mozhuDB.unionRank) do
  318. local union = mozhuDB.union[unionUuid]
  319. if unionUuid and union and union.unionBase and union.unionBase.svrIndex and union.hurt > 0 then
  320. local svrIndex = union.unionBase.svrIndex
  321. svrList[svrIndex] = svrList[svrIndex] or {}
  322. svrList[svrIndex][unionUuid] = rank
  323. end
  324. end
  325. for svrIndex, list in pairs(svrList) do
  326. local msgInner = InnerMsg.wl.WL_MOZHU_RANK_OVER
  327. msgInner.type = 2
  328. msgInner.list = msgInner.list or {}
  329. Util.cleanTable(msgInner.list)
  330. msgInner.list = list
  331. local fd = MiddleManager.getFDBySvrIndex(svrIndex)
  332. InnerMsg.sendMsg(fd, msgInner)
  333. end
  334. end
  335. function actOverRole()
  336. local mozhuDB = MoZhuDB.getMoZhu()
  337. local svrList = {}
  338. for rank, uuid in ipairs(mozhuDB.roleRank) do
  339. local role = mozhuDB.role[uuid]
  340. if uuid and role and role.roleBase and role.roleBase.svrIndex and role.hurt > 0 then
  341. local svrIndex = role.roleBase.svrIndex
  342. svrList[svrIndex] = svrList[svrIndex] or {}
  343. svrList[svrIndex][uuid] = rank
  344. end
  345. end
  346. for svrIndex, list in pairs(svrList) do
  347. local msgInner = InnerMsg.wl.WL_MOZHU_RANK_OVER
  348. msgInner.type = 1
  349. msgInner.list = msgInner.list or {}
  350. Util.cleanTable(msgInner.list)
  351. msgInner.list = list
  352. local fd = MiddleManager.getFDBySvrIndex(svrIndex)
  353. InnerMsg.sendMsg(fd, msgInner)
  354. end
  355. end
  356. function initAfterHotClean()
  357. if _G.is_middle == true then
  358. MoZhuDB.refreshAllUnion()
  359. end
  360. --[[
  361. local uuidList = {}
  362. uuidList[1] = "61512a8f639f62538000641a"
  363. uuidList[2] = "616a5468d98e7c2e9b000ad8"
  364. uuidList[3] = "616a2005cdd131276d0003cc"
  365. uuidList[4] = "6160766fcdd1317fc1000065"
  366. if _G.is_middle ~= true then
  367. for _, uuid in ipairs(uuidList) do
  368. local human = ObjHuman.onlineUuid[uuid]
  369. if human then
  370. human.db.mozhu = {}
  371. human.db.mozhu.time = os.time()
  372. human.db.mozhu.hurtMax = 0
  373. human.db.mozhu.hurt = 0
  374. human.db.mozhu.tzCnt = 0
  375. human.db.mozhu.restCnt = 0
  376. else
  377. local RoleDBLogic = require("role.RoleDBLogic")
  378. local db = RoleDBLogic.getDb(uuid)
  379. if db then
  380. human = {}
  381. human.db = db
  382. human.db.mozhu = {}
  383. human.db.mozhu.time = os.time()
  384. human.db.mozhu.hurtMax = 0
  385. human.db.mozhu.hurt = 0
  386. human.db.mozhu.tzCnt = 0
  387. human.db.mozhu.restCnt = 0
  388. ObjHuman.save(human)
  389. end
  390. end
  391. end
  392. else
  393. for _, uuid in ipairs(uuidList) do
  394. MoZhuDB.delRole(uuid)
  395. end
  396. end
  397. ]]
  398. end