MoZhuMiddleLogic.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. end
  132. function WL_MOZHU_QUERY(fd, msg)
  133. local human = ObjHuman.onlineUuid[msg.uuid]
  134. if human == nil then
  135. return
  136. end
  137. MoZhuLogic.WLQuery(human,msg)
  138. end
  139. function LW_MOZHU_FIGHT_END(fd, msg)
  140. -- 检测活动是否已经结束
  141. local combatInfo = msg.combatInfo
  142. local atkHurt = 0
  143. for pos = 1, CombatDefine.COMBAT_HERO_CNT do
  144. local obj = combatInfo.objList and combatInfo.objList[pos]
  145. if obj then
  146. atkHurt = atkHurt + obj.result[1]
  147. end
  148. end
  149. -- 魔兽 造成伤害
  150. for _,pos in ipairs(CombatDefine.SIDE2HELPPOS[CombatDefine.ATTACK_SIDE]) do
  151. local pet = combatInfo.helpList and combatInfo.helpList[pos]
  152. if pet and pet.isPet then
  153. atkHurt = atkHurt + pet.result[1]
  154. end
  155. end
  156. if atkHurt <= 0 then
  157. atkHurt = 0
  158. end
  159. local startTime, endTime = MoZhuLogic.getActTime()
  160. local now = os.time()
  161. local msgInner = InnerMsg.wl.WL_MOZHU_FIGHT_END
  162. if now < startTime or now > endTime then
  163. local rank, maxHurt = MoZhuDB.getRoleRank(msg.uuid)
  164. local unRank = MoZhuDB.getUnionRank(msg.unionUuid)
  165. msgInner.backType = 0
  166. msgInner.uuid = msg.uuid
  167. msgInner.oldRank= rank
  168. msgInner.rank = rank
  169. msgInner.unionRank = unRank
  170. msgInner.unionRanked = unRank
  171. msgInner.thisHurt = atkHurt
  172. msgInner.maxHurt = maxHurt
  173. msgInner.combatInfo = msg.combatInfo
  174. -- Msg.trace(msgInner)
  175. InnerMsg.sendMsg(fd, msgInner)
  176. return
  177. end
  178. local oldRank = MoZhuDB.getRoleRank(msg.uuid)
  179. local oldUnRank = MoZhuDB.getUnionRank(msg.unionUuid)
  180. MoZhuDB.updateHurt(msg.uuid, msg.roleBase, msg.maxHurt)
  181. MoZhuDB.updateUnion(msg.unionUuid, msg.unionBase)
  182. local newRank, maxHurt = MoZhuDB.getRoleRank(msg.uuid)
  183. local newUnRank = MoZhuDB.getUnionRank(msg.unionUuid)
  184. -- Util.printTable(MoZhuDB.getMoZhu())
  185. msgInner.backType = 1
  186. msgInner.uuid = msg.uuid
  187. msgInner.oldRank= oldRank
  188. msgInner.rank = newRank
  189. msgInner.unionRank = newUnRank
  190. msgInner.unionRanked = oldUnRank
  191. msgInner.thisHurt = atkHurt
  192. msgInner.maxHurt = maxHurt
  193. msgInner.combatInfo = msg.combatInfo
  194. -- Msg.trace(msgInner)
  195. InnerMsg.sendMsg(fd, msgInner)
  196. MoZhuDB.saveMoZhu()
  197. end
  198. function WL_MOZHU_FIGHT_END(fd, msg)
  199. local human = ObjHuman.onlineUuid[msg.uuid]
  200. if human == nil then
  201. return
  202. end
  203. MoZhuLogic.onfightEndWL(human,msg)
  204. end
  205. function LW_MOZHU_RANK_QUERY(fd, msg)
  206. local mozhuDB = MoZhuDB.getMoZhu()
  207. local msgInner = InnerMsg.wl.WL_MOZHU_RANK_QUERY
  208. msgInner.uuid = msg.uuid
  209. msgInner.type = msg.type
  210. msgInner.roleRank = {}
  211. msgInner.unionRank = {}
  212. if msg.type == 1 then
  213. fontRoleRank(mozhuDB, msgInner)
  214. else
  215. fontUnionRank(mozhuDB, msgInner)
  216. end
  217. -- Util.printTable(msgInner)
  218. InnerMsg.sendMsg(fd, msgInner)
  219. end
  220. function WL_MOZHU_RANK_QUERY(fd, msg)
  221. local human = ObjHuman.onlineUuid[msg.uuid]
  222. if human == nil then
  223. return
  224. end
  225. MoZhuLogic.rankQueryWL(human, msg)
  226. end
  227. function LW_MOZHU_UNION_GET(fd, msg)
  228. local mozhuDB = MoZhuDB.getMoZhu()
  229. local union = mozhuDB.union[msg.unionUuid]
  230. local rank = MoZhuDB.getUnionRank(msg.unionUuid)
  231. local ret = 0
  232. if rank > 0 and union then
  233. local config = MoZhuExcel.hurt[msg.id]
  234. if config.unionHurt <= union.hurt then
  235. ret = 1
  236. end
  237. end
  238. local msgInner = InnerMsg.wl.WL_MOZHU_UNION_GET
  239. msgInner.uuid = msg.uuid
  240. msgInner.ret = ret
  241. msgInner.id = msg.id
  242. InnerMsg.sendMsg(fd, msgInner)
  243. end
  244. function WL_MOZHU_UNION_GET(fd, msg)
  245. local human = ObjHuman.onlineUuid[msg.uuid]
  246. if human == nil then
  247. return
  248. end
  249. MoZhuLogic.unionGetWL(human, msg)
  250. end
  251. function LW_ROLE_UNION_OP(fd, msg)
  252. if msg.op == 1 then
  253. roleLeaveUnion(msg.uuid, msg.unionUuid)
  254. elseif msg.op == 2 then
  255. joinUnion(msg.uuid, msg.unionUuid)
  256. elseif msg.op == 3 then
  257. dismissUnion(msg.uuid, msg.unionUuid)
  258. end
  259. end
  260. function roleLeaveUnion(uuid, unionUuid)
  261. local mozhuDB = MoZhuDB.getMoZhu()
  262. local rank = MoZhuDB.getRoleRank(uuid)
  263. local role = mozhuDB.role[uuid]
  264. local union = mozhuDB.union[unionUuid]
  265. if rank > 0 and role then
  266. MoZhuDB.chageRoleUnionUuid(uuid, "")
  267. if union then
  268. MoZhuDB.refreshUnion(unionUuid)
  269. end
  270. MoZhuDB.saveMoZhu()
  271. end
  272. end
  273. function joinUnion(uuid, unionUuid)
  274. local mozhuDB = MoZhuDB.getMoZhu()
  275. local rank = MoZhuDB.getRoleRank(uuid)
  276. local role = mozhuDB.role[uuid]
  277. local union = mozhuDB.union[unionUuid]
  278. if rank > 0 and role then
  279. MoZhuDB.chageRoleUnionUuid(uuid, unionUuid)
  280. if union then
  281. MoZhuDB.refreshUnion(unionUuid)
  282. end
  283. MoZhuDB.saveMoZhu()
  284. end
  285. end
  286. function dismissUnion(uuid, unionUuid)
  287. local mozhuDB = MoZhuDB.getMoZhu()
  288. local rank = MoZhuDB.getRoleRank(uuid)
  289. local role = mozhuDB.role[uuid]
  290. if rank > 0 and role then
  291. MoZhuDB.chageRoleUnionUuid(uuid, unionUuid)
  292. end
  293. local union = mozhuDB.union[unionUuid]
  294. if union then
  295. MoZhuDB.dismissUnion(unionUuid)
  296. end
  297. end
  298. -------------------------------- Act End --------------------------
  299. function actOverUnion()
  300. local mozhuDB = MoZhuDB.getMoZhu()
  301. local svrList = {}
  302. for rank, unionUuid in ipairs(mozhuDB.unionRank) do
  303. local union = mozhuDB.union[unionUuid]
  304. if unionUuid and union and union.unionBase and union.unionBase.svrIndex and union.hurt > 0 then
  305. local svrIndex = union.unionBase.svrIndex
  306. svrList[svrIndex] = svrList[svrIndex] or {}
  307. svrList[svrIndex][unionUuid] = rank
  308. end
  309. end
  310. for svrIndex, list in pairs(svrList) do
  311. local msgInner = InnerMsg.wl.WL_MOZHU_RANK_OVER
  312. msgInner.type = 2
  313. msgInner.list = msgInner.list or {}
  314. Util.cleanTable(msgInner.list)
  315. msgInner.list = list
  316. local fd = MiddleManager.getFDBySvrIndex(svrIndex)
  317. InnerMsg.sendMsg(fd, msgInner)
  318. end
  319. end
  320. function actOverRole()
  321. local mozhuDB = MoZhuDB.getMoZhu()
  322. local svrList = {}
  323. for rank, uuid in ipairs(mozhuDB.roleRank) do
  324. local role = mozhuDB.role[uuid]
  325. if uuid and role and role.roleBase and role.roleBase.svrIndex and role.hurt > 0 then
  326. local svrIndex = role.roleBase.svrIndex
  327. svrList[svrIndex] = svrList[svrIndex] or {}
  328. svrList[svrIndex][uuid] = rank
  329. end
  330. end
  331. for svrIndex, list in pairs(svrList) do
  332. local msgInner = InnerMsg.wl.WL_MOZHU_RANK_OVER
  333. msgInner.type = 1
  334. msgInner.list = msgInner.list or {}
  335. Util.cleanTable(msgInner.list)
  336. msgInner.list = list
  337. local fd = MiddleManager.getFDBySvrIndex(svrIndex)
  338. InnerMsg.sendMsg(fd, msgInner)
  339. end
  340. end
  341. function initAfterHotClean()
  342. if _G.is_middle == true then
  343. MoZhuDB.refreshAllUnion()
  344. end
  345. --[[
  346. local uuidList = {}
  347. uuidList[1] = "61512a8f639f62538000641a"
  348. uuidList[2] = "616a5468d98e7c2e9b000ad8"
  349. uuidList[3] = "616a2005cdd131276d0003cc"
  350. uuidList[4] = "6160766fcdd1317fc1000065"
  351. if _G.is_middle ~= true then
  352. for _, uuid in ipairs(uuidList) do
  353. local human = ObjHuman.onlineUuid[uuid]
  354. if human then
  355. human.db.mozhu = {}
  356. human.db.mozhu.time = os.time()
  357. human.db.mozhu.hurtMax = 0
  358. human.db.mozhu.hurt = 0
  359. human.db.mozhu.tzCnt = 0
  360. human.db.mozhu.restCnt = 0
  361. else
  362. local RoleDBLogic = require("role.RoleDBLogic")
  363. local db = RoleDBLogic.getDb(uuid)
  364. if db then
  365. human = {}
  366. human.db = db
  367. human.db.mozhu = {}
  368. human.db.mozhu.time = os.time()
  369. human.db.mozhu.hurtMax = 0
  370. human.db.mozhu.hurt = 0
  371. human.db.mozhu.tzCnt = 0
  372. human.db.mozhu.restCnt = 0
  373. ObjHuman.save(human)
  374. end
  375. end
  376. end
  377. else
  378. for _, uuid in ipairs(uuidList) do
  379. MoZhuDB.delRole(uuid)
  380. end
  381. end
  382. ]]
  383. end