GoldFirstKill.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. GoldFirstKill = {}
  2. local this = {}
  3. GoldFirstKill.Var = {
  4. GOLD_FIRST_KILL_TASK = "T$goldfirstkilltask", --个人首杀任务
  5. GOLD_FIRST_KILL_ENVELOPE = "T$firstkillenvelope", --全服首杀红包
  6. SERVER_FIRST_KILL_GOLD_BOSS = "R$serverfirstkillgoldboss" --全服首杀
  7. }
  8. ---任务状态
  9. GoldFirstKill.TASK_STATUS = {
  10. ---已接取
  11. ACCEPT = 1,
  12. ---已完成
  13. FINISH = 2,
  14. ---已提交
  15. SUBMIT = 3,
  16. }
  17. --请求领取首杀红包
  18. function GoldFirstKill.receiveFirstKillAward(actor,msgData)
  19. if not GoldFirstKill.tableOpen(actor) then
  20. tipinfo(actor, "活动未开启")
  21. return
  22. end
  23. local monsterId = msgData.monsterId
  24. local envelope = getplaydef(actor, GoldFirstKill.Var.GOLD_FIRST_KILL_ENVELOPE) or {}
  25. local isReceive = envelope[tostring(monsterId)]
  26. if isReceive then
  27. sendluamsg(actor, LuaMessageIdToClient.TIPS, "请勿重复领取!")
  28. return
  29. end
  30. if not isReceive then
  31. envelope[tostring(monsterId)] = true
  32. setplaydef(actor, GoldFirstKill.Var.GOLD_FIRST_KILL_ENVELOPE,envelope)
  33. --发奖励
  34. local config = ConfigDataManager.getTable("cfg_goldFirstKill","id",monsterId)
  35. if not config then
  36. return
  37. end
  38. local weight = string.split(config[1].redenvelopeweight,"#")
  39. local map = {}
  40. for k, v in pairs(weight) do
  41. map[k] = tonumber(v)
  42. end
  43. local index = math.weightedRandom(map)
  44. local redEnvelope = string.split(config[1].redenvelope,"|")[tonumber(index)]
  45. local reward = splitbyshuxianandjinghao2(redEnvelope)
  46. for itemCfgId, count in pairs(reward) do
  47. additemtobag(actor, itemCfgId, count, 0, 9999, '黄金首杀')
  48. end
  49. --奖励弹框
  50. sendluamsg(actor, LuaMessageIdToClient.COMMON_REWARD_PANEL, reward)
  51. --通知客户端
  52. sendluamsg(actor, LuaMessageIdToClient.RES_RECEIVE_FIRST_KILL_ENVELOPE, table.valueConvertToString(envelope))
  53. end
  54. end
  55. --根据权重进行随机
  56. function math.weightedRandom(weights)
  57. local total = 0
  58. for _, weight in pairs(weights) do
  59. total = total + weight
  60. end
  61. local rand = math.random() * total
  62. local cumulative = 0
  63. for item, weight in pairs(weights) do
  64. cumulative = cumulative + weight
  65. if cumulative >= rand then
  66. return item
  67. end
  68. end
  69. end
  70. --请求获取全服首杀数据和红包数据
  71. function GoldFirstKill.getFirstKillInfo(actor)
  72. if not GoldFirstKill.tableOpen(actor) then
  73. return
  74. end
  75. local allServerData = getsysvar(GoldFirstKill.Var.SERVER_FIRST_KILL_GOLD_BOSS) or {}
  76. local envelope = getplaydef(actor, GoldFirstKill.Var.GOLD_FIRST_KILL_ENVELOPE) or {}
  77. local taskInfo = getplaydef(actor, GoldFirstKill.Var.GOLD_FIRST_KILL_TASK) or {}
  78. local taskStatus = {}
  79. for mId,v in pairs(taskInfo) do
  80. taskStatus[mId] = v.status
  81. end
  82. local result = {}
  83. result.allServerData = allServerData
  84. result.envelope = envelope
  85. result.taskInfo = taskStatus
  86. sendluamsg(actor, LuaMessageIdToClient.RES_ALL_SERVER_FIRST_KILL_INFO, table.valueConvertToString(result))
  87. end
  88. --活动是否开启
  89. function GoldFirstKill.isActivityOpen(actor)
  90. local config = ConfigDataManager.getTable("sub_mainActivity","id",1)
  91. if config == nil then
  92. return false
  93. end
  94. return ConditionManager.Check(actor, config[1].actualcondition)
  95. end
  96. --页签是否开启
  97. function GoldFirstKill.tableOpen(actor)
  98. local config = ConfigDataManager.getTable("sub_mainActivity","id",1)
  99. if config == nil then
  100. return false
  101. end
  102. return ConditionManager.Check(actor, config[1].showcondition)
  103. end
  104. --登录时尝试接取任务
  105. function GoldFirstKill.login(actor)
  106. GoldFirstKill.AcceptTask(actor)
  107. end
  108. ---接取任务
  109. function GoldFirstKill.AcceptTask(actor)
  110. --活动是否开启
  111. if not GoldFirstKill.isActivityOpen(actor) then
  112. return
  113. end
  114. local taskInfo = getplaydef(actor, GoldFirstKill.Var.GOLD_FIRST_KILL_TASK)
  115. if taskInfo ~= nil then
  116. --通知客户端
  117. local result = {}
  118. for mId,v in pairs(taskInfo) do
  119. result[mId] = v.status
  120. end
  121. sendluamsg(actor, LuaMessageIdToClient.RES_GOAL_FIRST_KILL_CHANGE, table.valueConvertToString(result))
  122. return
  123. end
  124. taskInfo = {}
  125. local config = ConfigDataManager.getTable("cfg_goldFirstKill")
  126. if not config then
  127. return
  128. end
  129. --接取任务
  130. for i = 1, #config,1 do
  131. local cfg = config[i]
  132. local monsterId = cfg.id
  133. local taskId = cfg.taskid
  134. --local targetId = ConfigDataManager.getTableValue("cfg_task","taskTargetId","id",taskId)
  135. local targetCfg = ConfigDataManager.getTable("cfg_task_target","id",taskId)
  136. local task = {}
  137. task.taskGoalId = targetCfg[1].taskgoalparam
  138. task.goalCount = targetCfg[1].goalcount
  139. task.nowCount = 0
  140. task.status = GoldFirstKill.TASK_STATUS.ACCEPT
  141. taskInfo[monsterId] = task
  142. end
  143. setplaydef(actor, GoldFirstKill.Var.GOLD_FIRST_KILL_TASK, taskInfo)
  144. local result = {}
  145. for mId,v in pairs(taskInfo) do
  146. result[mId] = v.status
  147. end
  148. result = table.valueConvertToString(result)
  149. --通知客户端
  150. sendluamsg(actor, LuaMessageIdToClient.RES_GOAL_FIRST_KILL_CHANGE, result)
  151. end
  152. function GoldFirstKill.UpdateTaskProgress(actor, monsterId)
  153. local success, errorInfo = xpcall(this.UpdateTaskProgress, debug.traceback, actor, monsterId)
  154. gameDebug.assertPrint(success, "怪物死亡黄金任务更新进度异常:", actor, monsterId, errorInfo)
  155. end
  156. ---更新任务进度
  157. function this.UpdateTaskProgress(actor, monsterId)
  158. if actor == nil then
  159. return
  160. end
  161. --活动是否开启
  162. if not GoldFirstKill.isActivityOpen(actor) then
  163. return
  164. end
  165. local taskInfo = getplaydef(actor, GoldFirstKill.Var.GOLD_FIRST_KILL_TASK)
  166. if not taskInfo then
  167. return
  168. end
  169. local task = taskInfo[tostring(monsterId)]
  170. if not task then
  171. return
  172. end
  173. if task.status ~= GoldFirstKill.TASK_STATUS.ACCEPT then
  174. return
  175. end
  176. if tonumber(task.taskGoalId) ~= tonumber(monsterId) then
  177. return
  178. end
  179. task.nowCount = task.nowCount + 1
  180. if task.nowCount >= tonumber(task.goalCount) then
  181. task.status = GoldFirstKill.TASK_STATUS.FINISH
  182. end
  183. taskInfo[tostring(monsterId)] = task
  184. setplaydef(actor, GoldFirstKill.Var.GOLD_FIRST_KILL_TASK, taskInfo)
  185. --更新全服黄金首杀数据
  186. callonserial(actor, "update_gold_first_kill_data", monsterId)
  187. --通知客户端
  188. local result = {}
  189. for mId,v in pairs(taskInfo) do
  190. result[mId] = v.status
  191. end
  192. sendluamsg(actor, LuaMessageIdToClient.RES_GOAL_FIRST_KILL_CHANGE, table.valueConvertToString(result))
  193. end
  194. function update_gold_first_kill_data(actor,monsterId)
  195. --更新全服黄金首杀数据
  196. local allServerData = getsysvar(GoldFirstKill.Var.SERVER_FIRST_KILL_GOLD_BOSS)
  197. if allServerData == nil or allServerData == "" then
  198. allServerData = {}
  199. end
  200. local killInfo = allServerData[tostring(monsterId)]
  201. if not killInfo then
  202. killInfo = {}
  203. killInfo.killer = getbaseinfo(actor,"rid")
  204. killInfo.killerName = getbaseinfo(actor,"rolename")
  205. killInfo.killTime = getbaseinfo("now")
  206. allServerData[tostring(monsterId)] = killInfo
  207. setsysvar(GoldFirstKill.Var.SERVER_FIRST_KILL_GOLD_BOSS,allServerData)
  208. local config = ConfigDataManager.getTable("cfg_goldFirstKill","id",monsterId)
  209. if config ~= nil then
  210. local itemMap = string.toIntIntMap(config[1].serreward)
  211. local monsterCfg = ConfigDataManager.getTable("cfg_monster","id",monsterId)
  212. local name = monsterCfg[1].name
  213. sendconfigmailbyrid(actor, getbaseinfo(actor,"rid"), MailConfig.SERVER_FIRST_KILL_GOLD_BOSS, itemMap, name)
  214. noticeTip.noticeinfo(actor, StringIdConst.ALL_SERVER_FIRST_KILL, getbaseinfo(actor,"rolename"), name)
  215. end
  216. end
  217. end
  218. ---提交任务
  219. function GoldFirstKill.SubmitTask(actor, msgData)
  220. if not GoldFirstKill.tableOpen(actor) then
  221. tipinfo(actor, "活动未开启")
  222. return
  223. end
  224. local monsterId = msgData.monsterId
  225. local taskInfo = getplaydef(actor, GoldFirstKill.Var.GOLD_FIRST_KILL_TASK)
  226. if not taskInfo then
  227. tipinfo(actor, "任务未接取")
  228. return
  229. end
  230. local task = taskInfo[tostring(monsterId)]
  231. if not task then
  232. tipinfo(actor, "任务不存在")
  233. return
  234. end
  235. if tonumber(task.status) ~= GoldFirstKill.TASK_STATUS.FINISH then
  236. tipinfo(actor, "任务未完成")
  237. return
  238. end
  239. --修改任务状态
  240. task.status = GoldFirstKill.TASK_STATUS.SUBMIT
  241. taskInfo[tostring(monsterId)] = task
  242. setplaydef(actor, GoldFirstKill.Var.GOLD_FIRST_KILL_TASK, taskInfo)
  243. --发奖励
  244. local reward = GoldFirstKill.GetTaskReward(monsterId)
  245. for itemCfgId, count in pairs(reward) do
  246. additemtobag(actor, itemCfgId, count, 0, 9999, '黄金首杀')
  247. end
  248. --奖励弹框
  249. sendluamsg(actor, LuaMessageIdToClient.COMMON_REWARD_PANEL, reward)
  250. --通知客户端
  251. local ti = getplaydef(actor, GoldFirstKill.Var.GOLD_FIRST_KILL_TASK)
  252. local result = {}
  253. for mId,v in pairs(ti) do
  254. result[mId] = v.status
  255. end
  256. sendluamsg(actor, LuaMessageIdToClient.RES_RECEIVE_PERSONAL_FIRST_KILL_AWARD, table.valueConvertToString(result))
  257. end
  258. --获取任务奖励
  259. function GoldFirstKill.GetTaskReward(monsterId)
  260. local reward = {}
  261. local config = ConfigDataManager.getTable("cfg_goldFirstKill","id",monsterId)
  262. if not config then
  263. return reward
  264. end
  265. return string.toIntIntMap(config[1].perreward)
  266. end
  267. --全服奖励
  268. function GoldFirstKill.endTimeReward()
  269. local allData = getsysvar(GoldFirstKill.Var.SERVER_FIRST_KILL_GOLD_BOSS)
  270. if allData == nil or allData == "" then
  271. return
  272. end
  273. local allRoleInfos = getallrolesummaryinfos()
  274. if allRoleInfos == nil or next(allRoleInfos) == nil then
  275. return
  276. end
  277. for _, roleInfo in pairs(allRoleInfos) do
  278. local reward = {}
  279. local actor = roleInfo["actor"]
  280. if GoldFirstKill.tableOpen(actor) then
  281. return
  282. end
  283. --全服首杀红包奖励
  284. local envelope = getplaydef(actor, GoldFirstKill.Var.GOLD_FIRST_KILL_ENVELOPE) or {}
  285. for monsterId,v in pairs(envelope) do
  286. if not v then
  287. local config = ConfigDataManager.getTable("cfg_goldFirstKill","id",monsterId)
  288. if not config then
  289. return
  290. end
  291. local weight = string.split(config[1].redenvelopeweight,"#")
  292. local map = {}
  293. for k, value in pairs(weight) do
  294. map[k] = tonumber(value)
  295. end
  296. local index = math.weightedRandom(map)
  297. local redEnvelope = string.split(config[1].redenvelope,"|")[tonumber(index)]
  298. string.putIntIntMap(reward,redEnvelope)
  299. end
  300. end
  301. --怪物个人首杀奖励
  302. local taskInfo = getplaydef(actor, GoldFirstKill.Var.GOLD_FIRST_KILL_TASK)
  303. if taskInfo == nil or taskInfo == "" then
  304. taskInfo = {}
  305. end
  306. for monsterId,v in pairs(taskInfo) do
  307. if v.status == GoldFirstKill.TASK_STATUS.FINISH then
  308. table.mergeAdd(reward,GoldFirstKill.GetTaskReward(monsterId))
  309. end
  310. end
  311. --发邮件
  312. if table.count(reward) > 0 then
  313. sendconfigmailbyrid(actor,getbaseinfo(actor,"rid"), MailConfig.END_TIME_GOLD_BOSS_REWARD, reward, "")
  314. end
  315. setplaydef(actor, GoldFirstKill.Var.GOLD_FIRST_KILL_ENVELOPE,nil)
  316. setplaydef(actor, GoldFirstKill.Var.GOLD_FIRST_KILL_TASK,nil)
  317. end
  318. setsysvar(GoldFirstKill.Var.SERVER_FIRST_KILL_GOLD_BOSS,nil)
  319. end