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