MoZhuMiddleLogic.lua 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. local rewardIdxList
  247. if rank > 0 and union then
  248. -- local config = MoZhuExcel.hurt[msg.id]
  249. -- if config.unionHurt <= union.hurt then
  250. -- ret = 1
  251. -- end
  252. for k,v in ipairs(MoZhuExcel.hurt) do
  253. if v.unionHurt <= union.hurt then
  254. ret = ret + 1
  255. rewardIdxList = rewardIdxList or {}
  256. rewardIdxList[ret] = k
  257. end
  258. end
  259. end
  260. local msgInner = {} --InnerMsg.wl.WL_MOZHU_UNION_GET
  261. msgInner.uuid = msg.uuid
  262. msgInner.ret = ret
  263. msgInner.id = msg.id
  264. msgInner.rewardIdxList = rewardIdxList
  265. --InnerMsg.sendMsg(fd, msgInner)
  266. WL_MOZHU_UNION_GET(fd,msgInner)
  267. end
  268. function WL_MOZHU_UNION_GET(fd, msg)
  269. local human = ObjHuman.onlineUuid[msg.uuid]
  270. if human == nil then
  271. return
  272. end
  273. MoZhuLogic.unionGetWL(human, msg)
  274. end
  275. function LW_ROLE_UNION_OP(fd, msg)
  276. if msg.op == 1 then
  277. roleLeaveUnion(msg.uuid, msg.unionUuid)
  278. elseif msg.op == 2 then
  279. joinUnion(msg.uuid, msg.unionUuid)
  280. elseif msg.op == 3 then
  281. dismissUnion(msg.uuid, msg.unionUuid)
  282. end
  283. end
  284. function roleLeaveUnion(uuid, unionUuid)
  285. local mozhuDB = MoZhuDB.getMoZhu()
  286. local rank = MoZhuDB.getRoleRank(uuid)
  287. local role = mozhuDB.role[uuid]
  288. local union = mozhuDB.union[unionUuid]
  289. if rank > 0 and role then
  290. MoZhuDB.chageRoleUnionUuid(uuid, "")
  291. if union then
  292. MoZhuDB.refreshUnion(unionUuid)
  293. end
  294. MoZhuDB.saveMoZhu()
  295. end
  296. end
  297. function joinUnion(uuid, unionUuid)
  298. local mozhuDB = MoZhuDB.getMoZhu()
  299. local rank = MoZhuDB.getRoleRank(uuid)
  300. local role = mozhuDB.role[uuid]
  301. local union = mozhuDB.union[unionUuid]
  302. if rank > 0 and role then
  303. MoZhuDB.chageRoleUnionUuid(uuid, unionUuid)
  304. if union then
  305. MoZhuDB.refreshUnion(unionUuid)
  306. end
  307. MoZhuDB.saveMoZhu()
  308. end
  309. end
  310. function dismissUnion(uuid, unionUuid)
  311. local mozhuDB = MoZhuDB.getMoZhu()
  312. local rank = MoZhuDB.getRoleRank(uuid)
  313. local role = mozhuDB.role[uuid]
  314. if rank > 0 and role then
  315. MoZhuDB.chageRoleUnionUuid(uuid, unionUuid)
  316. end
  317. local union = mozhuDB.union[unionUuid]
  318. if union then
  319. MoZhuDB.dismissUnion(unionUuid)
  320. end
  321. end
  322. -------------------------------- Act End --------------------------
  323. function actOverUnion()
  324. local mozhuDB = MoZhuDB.getMoZhu()
  325. local svrList = {}
  326. for rank, unionUuid in ipairs(mozhuDB.unionRank) do
  327. local union = mozhuDB.union[unionUuid]
  328. if unionUuid and union and union.unionBase and union.unionBase.svrIndex and union.hurt > 0 then
  329. local svrIndex = union.unionBase.svrIndex
  330. svrList[svrIndex] = svrList[svrIndex] or {}
  331. svrList[svrIndex][unionUuid] = rank
  332. end
  333. end
  334. for svrIndex, list in pairs(svrList) do
  335. local msgInner = InnerMsg.wl.WL_MOZHU_RANK_OVER
  336. msgInner.type = 2
  337. msgInner.list = msgInner.list or {}
  338. Util.cleanTable(msgInner.list)
  339. msgInner.list = list
  340. local fd = MiddleManager.getFDBySvrIndex(svrIndex)
  341. InnerMsg.sendMsg(fd, msgInner)
  342. end
  343. end
  344. function actOverRole()
  345. local mozhuDB = MoZhuDB.getMoZhu()
  346. local svrList = {}
  347. for rank, uuid in ipairs(mozhuDB.roleRank) do
  348. local role = mozhuDB.role[uuid]
  349. if uuid and role and role.roleBase and role.roleBase.svrIndex and role.hurt > 0 then
  350. local svrIndex = role.roleBase.svrIndex
  351. svrList[svrIndex] = svrList[svrIndex] or {}
  352. svrList[svrIndex][uuid] = rank
  353. end
  354. end
  355. for svrIndex, list in pairs(svrList) do
  356. local msgInner = InnerMsg.wl.WL_MOZHU_RANK_OVER
  357. msgInner.type = 1
  358. msgInner.list = msgInner.list or {}
  359. Util.cleanTable(msgInner.list)
  360. msgInner.list = list
  361. local fd = MiddleManager.getFDBySvrIndex(svrIndex)
  362. InnerMsg.sendMsg(fd, msgInner)
  363. end
  364. end
  365. function initAfterHotClean()
  366. if _G.is_middle == true then
  367. MoZhuDB.refreshAllUnion()
  368. end
  369. --[[
  370. local uuidList = {}
  371. uuidList[1] = "61512a8f639f62538000641a"
  372. uuidList[2] = "616a5468d98e7c2e9b000ad8"
  373. uuidList[3] = "616a2005cdd131276d0003cc"
  374. uuidList[4] = "6160766fcdd1317fc1000065"
  375. if _G.is_middle ~= true then
  376. for _, uuid in ipairs(uuidList) do
  377. local human = ObjHuman.onlineUuid[uuid]
  378. if human then
  379. human.db.mozhu = {}
  380. human.db.mozhu.time = os.time()
  381. human.db.mozhu.hurtMax = 0
  382. human.db.mozhu.hurt = 0
  383. human.db.mozhu.tzCnt = 0
  384. human.db.mozhu.restCnt = 0
  385. else
  386. local RoleDBLogic = require("role.RoleDBLogic")
  387. local db = RoleDBLogic.getDb(uuid)
  388. if db then
  389. human = {}
  390. human.db = db
  391. human.db.mozhu = {}
  392. human.db.mozhu.time = os.time()
  393. human.db.mozhu.hurtMax = 0
  394. human.db.mozhu.hurt = 0
  395. human.db.mozhu.tzCnt = 0
  396. human.db.mozhu.restCnt = 0
  397. ObjHuman.save(human)
  398. end
  399. end
  400. end
  401. else
  402. for _, uuid in ipairs(uuidList) do
  403. MoZhuDB.delRole(uuid)
  404. end
  405. end
  406. ]]
  407. end