MoZhuMiddleLogic.lua 13 KB

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