MoZhuMiddleLogic.lua 14 KB

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