UnionLivenessLogic.lua 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. local Msg = require("core.Msg")
  2. local UnionDBLogic = require("union.UnionDBLogic")
  3. local UnionExcel = require("excel.union")
  4. local Grid = require("bag.Grid")
  5. local BagLogic = require("bag.BagLogic")
  6. local Util = require("common.Util")
  7. local RoleAttr = require("role.RoleAttr")
  8. local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
  9. local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
  10. DAY_UPDATE_TYPE = 1
  11. WEEK_UPDATE_TYPE = 2
  12. local function initUnionLiveDB(human)
  13. if human.db.unionLive ~= nil then
  14. return
  15. end
  16. human.db.unionLive = {}
  17. human.db.unionLive.lv = 0
  18. human.db.unionLive.liveness = 0
  19. human.db.unionLive.todayLiveness = 0
  20. human.db.unionLive.weekLiveness = 0
  21. human.db.unionLive.task = {}
  22. human.db.unionLive.reward = {}
  23. local liveConfig = UnionExcel.liveness
  24. local len = #liveConfig
  25. for i = 1,len do
  26. local v = liveConfig[i]
  27. human.db.unionLive.task[i] = {}
  28. human.db.unionLive.task[i].id = i
  29. human.db.unionLive.task[i].type = v.type
  30. human.db.unionLive.task[i].reach = 0
  31. human.db.unionLive.task[i].cnt = 0
  32. human.db.unionLive.task[i].status = 0
  33. human.db.unionLive.task[i].ts = os.time()
  34. end
  35. end
  36. function livenessQuery(human)
  37. -- 判断玩家是否有工会
  38. -- 判断玩家公会是否存在
  39. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  40. if union == nil then
  41. return
  42. end
  43. -- 初始化数据
  44. initUnionLiveDB(human)
  45. local nowLv = human.db.unionLive.lv
  46. local nextLv = nowLv + 1
  47. local maxLv = #UnionExcel.liveLv
  48. local msgRet = Msg.gc.GC_UNION_LIVENESS_QUERY
  49. msgRet.lv = human.db.unionLive.lv
  50. msgRet.nowLiveness = human.db.unionLive.liveness
  51. msgRet.maxLiveness = UnionExcel.liveLv[nextLv] and UnionExcel.liveLv[nextLv].needLiveness or 0
  52. msgRet.todayLiveness = human.db.unionLive.todayLiveness
  53. msgRet.weekLiveness = human.db.unionLive.weekLiveness
  54. -- 等级奖励
  55. local liveLvConfig = nil
  56. local len = 0
  57. msgRet.reward.status = 0
  58. msgRet.reward.lv = nowLv
  59. for i = 1,nowLv do
  60. -- 有任务奖励未领
  61. if human.db.unionLive.reward[i] == nil then
  62. liveLvConfig = UnionExcel.liveLv[i]
  63. msgRet.reward.status = 1
  64. msgRet.reward.lv = i
  65. len = #liveLvConfig.reward
  66. break
  67. end
  68. end
  69. -- 没有奖励未领,且下一级存在
  70. if len == 0 and nextLv <= maxLv then
  71. liveLvConfig = UnionExcel.liveLv[nextLv]
  72. msgRet.reward.status = 0
  73. msgRet.reward.lv = nextLv
  74. len = #liveLvConfig.reward
  75. end
  76. for i = 1,len do
  77. local v = liveLvConfig.reward[i]
  78. Grid.makeItem(msgRet.reward.reward[i],v[1],v[2])
  79. end
  80. msgRet.reward.reward[0] = len
  81. -- 当前等级属性
  82. if nowLv == 0 then
  83. msgRet.attr[0] = 0
  84. else
  85. len = #UnionExcel.liveLv[nowLv].attr
  86. for i = 1,len do
  87. local v = UnionExcel.liveLv[nowLv].attr[i]
  88. msgRet.attr[i].key = v[1]
  89. msgRet.attr[i].value = v[2]
  90. end
  91. msgRet.attr[0] = len
  92. end
  93. -- 下级等级属性
  94. if UnionExcel.liveLv[nextLv] == nil then
  95. msgRet.nextAttr[0] = 0
  96. else
  97. len = #UnionExcel.liveLv[nextLv].attr
  98. for i = 1,len do
  99. local v = UnionExcel.liveLv[nextLv].attr[i]
  100. msgRet.nextAttr[i].key = v[1]
  101. msgRet.nextAttr[i].value = v[2]
  102. end
  103. msgRet.nextAttr[0] = len
  104. end
  105. -- 活跃度任务
  106. len = #human.db.unionLive.task
  107. for i = 1,len do
  108. local net = msgRet.unionLiveness[i]
  109. local v = human.db.unionLive.task[i]
  110. local config = UnionExcel.liveness[v.id]
  111. net.taskID = v.id
  112. net.type = v.type
  113. net.nowReach = v.reach
  114. net.need = config.need
  115. net.nowCnt = v.cnt
  116. net.maxCnt = config.maxCnt
  117. net.liveness = config.liveness
  118. net.pageID = config.pageID
  119. net.status = v.cnt < config.maxCnt and 0 or 1
  120. net.name = config.name
  121. net.desc = config.desc
  122. end
  123. msgRet.unionLiveness[0] = len
  124. Msg.send(msgRet,human.fd)
  125. end
  126. function livenessRewardQuery(human)
  127. -- 判断玩家是否有工会
  128. -- 判断玩家公会是否存在
  129. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  130. if union == nil then
  131. return
  132. end
  133. local config = UnionExcel.liveLv
  134. local msgRet = Msg.gc.GC_UNION_LIVENESS_REWARD_QUERY
  135. local len = #config
  136. for i = 1,len do
  137. local lenReward = #config[i].reward
  138. for j = 1,lenReward do
  139. local net = msgRet.reward[i].reward[j]
  140. local v = config[i].reward[j]
  141. Grid.makeItem(net,v[1],v[2])
  142. end
  143. msgRet.reward[i].reward[0] = lenReward
  144. msgRet.reward[i].status = 0
  145. msgRet.reward[i].lv = i
  146. end
  147. msgRet.reward[0] = len
  148. Msg.send(msgRet,human.fd)
  149. end
  150. function touchLiveness(human,id,cnt)
  151. -- 判断玩家是否有工会
  152. -- 判断玩家公会是否存在
  153. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  154. if union == nil then
  155. return
  156. end
  157. -- 初始化数据
  158. initUnionLiveDB(human)
  159. local config = UnionExcel.liveness[id]
  160. -- 验证是否有此任务
  161. if human.db.unionLive.task[id] == nil then
  162. return
  163. end
  164. -- 验证此任务是否已经完成
  165. if human.db.unionLive.task[id].cnt >= config.maxCnt then
  166. return
  167. end
  168. -- 增加次数
  169. human.db.unionLive.task[id].reach = human.db.unionLive.task[id].reach + cnt
  170. while true do
  171. if human.db.unionLive.task[id].cnt >= config.maxCnt then
  172. break
  173. end
  174. if human.db.unionLive.task[id].reach < config.need then
  175. break
  176. end
  177. human.db.unionLive.task[id].reach = human.db.unionLive.task[id].reach - config.need
  178. human.db.unionLive.task[id].cnt = human.db.unionLive.task[id].cnt + 1
  179. -- 增加经验
  180. touchLv(human,config.liveness)
  181. end
  182. end
  183. function touchLv(human,liveness)
  184. -- 判断玩家是否有工会
  185. -- 判断玩家公会是否存在
  186. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  187. if union == nil then
  188. return
  189. end
  190. -- 初始化数据
  191. initUnionLiveDB(human)
  192. -- 判断是否达到最大等级
  193. local maxLv = #UnionExcel.liveLv
  194. local nextLv = human.db.unionLive.lv + 1
  195. if nextLv > maxLv then
  196. return
  197. end
  198. local config = UnionExcel.liveLv[nextLv]
  199. if config == nil then
  200. return
  201. end
  202. human.db.unionLive.liveness = human.db.unionLive.liveness + liveness
  203. human.db.unionLive.todayLiveness = human.db.unionLive.todayLiveness + liveness
  204. human.db.unionLive.weekLiveness = human.db.unionLive.weekLiveness + liveness
  205. local roundCnt = 0
  206. while true do
  207. if config ~= nil and human.db.unionLive.liveness < config.needLiveness then
  208. break
  209. end
  210. -- 死循环保护
  211. roundCnt = roundCnt + 1
  212. if roundCnt > maxLv then
  213. break
  214. end
  215. human.db.unionLive.liveness = human.db.unionLive.liveness - config.needLiveness
  216. human.db.unionLive.lv = human.db.unionLive.lv + 1
  217. RoleAttr.cleanHeroAttrCache(human)
  218. -- 达到最大等级后,清除活跃度,并跳出
  219. if human.db.unionLive.lv >= maxLv then
  220. human.db.unionLive.liveness = 0
  221. break
  222. end
  223. nextLv = human.db.unionLive.lv + 1
  224. config = UnionExcel.liveLv[nextLv]
  225. end
  226. end
  227. function getReward(human,lv)
  228. -- 判断玩家是否有工会
  229. -- 判断玩家公会是否存在
  230. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  231. if union == nil then
  232. return
  233. end
  234. -- 初始化数据
  235. initUnionLiveDB(human)
  236. -- 校验等级是否达成
  237. if lv == nil or lv <= 0 then
  238. return
  239. end
  240. if lv > human.db.unionLive.lv then
  241. return
  242. end
  243. -- 校验等级奖励是否已领
  244. if human.db.unionLive.reward[lv] ~= nil then
  245. return
  246. end
  247. -- 改dB
  248. human.db.unionLive.reward[lv] = 1
  249. -- 发奖励
  250. BagLogic.addItemList(human, UnionExcel.liveLv[lv].reward, "unionLive_reward")
  251. RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_1004)
  252. RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_1001)
  253. livenessQuery(human)
  254. end
  255. -- 每日更新
  256. function updateDaily(human)
  257. -- 判断玩家是否有工会
  258. -- 判断玩家公会是否存在
  259. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  260. if union == nil then
  261. return
  262. end
  263. -- 初始化数据
  264. initUnionLiveDB(human)
  265. human.db.unionLive.todayLiveness = 0
  266. local now = os.time()
  267. local len = #human.db.unionLive.task
  268. for i = 1,len do
  269. local v = human.db.unionLive.task[i]
  270. local clean = nil
  271. if v.type == DAY_UPDATE_TYPE then
  272. clean = Util.isSameDayByTimes(now,v.ts)
  273. elseif v.type == WEEK_UPDATE_TYPE then
  274. clean = Util.isSameWeek(now,v.ts)
  275. end
  276. if clean ~= true then
  277. v.reach = 0
  278. v.cnt = 0
  279. end
  280. end
  281. end
  282. function doCalcHero(human,attrs)
  283. -- 判断玩家是否有工会
  284. -- 判断玩家公会是否存在
  285. if not human then return end
  286. local union = UnionDBLogic.getUnion(human.db.unionUuid)
  287. if union == nil then
  288. return
  289. end
  290. -- 初始化数据
  291. initUnionLiveDB(human)
  292. if human.db.unionLive.lv <= 0 then
  293. return
  294. end
  295. local config = UnionExcel.liveLv[human.db.unionLive.lv]
  296. if config == nil then
  297. return
  298. end
  299. for k, v in ipairs(config.attr) do
  300. RoleAttr.updateValue(v[1], v[2], attrs)
  301. end
  302. end
  303. function isDot(human)
  304. initUnionLiveDB(human)
  305. local nowLv = human.db.unionLive.lv
  306. for i = 1,nowLv do
  307. -- 有任务奖励未领
  308. if human.db.unionLive.reward[i] == nil then
  309. return true
  310. end
  311. end
  312. return
  313. end