GoldFirstKill.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. local allServerData = getsysvar(GoldFirstKill.Var.SERVER_FIRST_KILL_GOLD_BOSS)
  187. if allServerData == nil or allServerData == "" then
  188. allServerData = {}
  189. end
  190. local killInfo = allServerData[tostring(monsterId)]
  191. if not killInfo then
  192. killInfo = {}
  193. killInfo.killer = getbaseinfo(actor,"rid")
  194. killInfo.killerName = getbaseinfo(actor,"rolename")
  195. killInfo.killTime = getbaseinfo("now")
  196. allServerData[tostring(monsterId)] = killInfo
  197. setsysvar(GoldFirstKill.Var.SERVER_FIRST_KILL_GOLD_BOSS,allServerData)
  198. local config = ConfigDataManager.getTable("cfg_goldFirstKill","id",monsterId)
  199. if config ~= nil then
  200. local itemMap = string.toIntIntMap(config[1].serreward)
  201. local monsterCfg = ConfigDataManager.getTable("cfg_monster","id",monsterId)
  202. local name = monsterCfg[1].name
  203. sendconfigmailbyrid(actor, getbaseinfo(actor,"rid"), MailConfig.SERVER_FIRST_KILL_GOLD_BOSS, itemMap, name)
  204. noticeTip.noticeinfo(actor, StringIdConst.ALL_SERVER_FIRST_KILL, getbaseinfo(actor,"rolename"), name)
  205. end
  206. end
  207. --通知客户端
  208. local result = {}
  209. for mId,v in pairs(taskInfo) do
  210. result[mId] = v.status
  211. end
  212. sendluamsg(actor, LuaMessageIdToClient.RES_GOAL_FIRST_KILL_CHANGE, table.valueConvertToString(result))
  213. end
  214. ---提交任务
  215. function GoldFirstKill.SubmitTask(actor, msgData)
  216. if not GoldFirstKill.tableOpen(actor) then
  217. tipinfo(actor, "活动未开启")
  218. return
  219. end
  220. local monsterId = msgData.monsterId
  221. local taskInfo = getplaydef(actor, GoldFirstKill.Var.GOLD_FIRST_KILL_TASK)
  222. if not taskInfo then
  223. tipinfo(actor, "任务未接取")
  224. return
  225. end
  226. local task = taskInfo[tostring(monsterId)]
  227. if not task then
  228. tipinfo(actor, "任务不存在")
  229. return
  230. end
  231. if tonumber(task.status) ~= GoldFirstKill.TASK_STATUS.FINISH then
  232. tipinfo(actor, "任务未完成")
  233. return
  234. end
  235. --修改任务状态
  236. task.status = GoldFirstKill.TASK_STATUS.SUBMIT
  237. taskInfo[tostring(monsterId)] = task
  238. setplaydef(actor, GoldFirstKill.Var.GOLD_FIRST_KILL_TASK, taskInfo)
  239. --发奖励
  240. local reward = GoldFirstKill.GetTaskReward(monsterId)
  241. for itemCfgId, count in pairs(reward) do
  242. additemtobag(actor, itemCfgId, count, 0, 9999, '黄金首杀')
  243. end
  244. --奖励弹框
  245. sendluamsg(actor, LuaMessageIdToClient.COMMON_REWARD_PANEL, reward)
  246. --通知客户端
  247. local ti = getplaydef(actor, GoldFirstKill.Var.GOLD_FIRST_KILL_TASK)
  248. local result = {}
  249. for mId,v in pairs(ti) do
  250. result[mId] = v.status
  251. end
  252. sendluamsg(actor, LuaMessageIdToClient.RES_RECEIVE_PERSONAL_FIRST_KILL_AWARD, table.valueConvertToString(result))
  253. end
  254. --获取任务奖励
  255. function GoldFirstKill.GetTaskReward(monsterId)
  256. local reward = {}
  257. local config = ConfigDataManager.getTable("cfg_goldFirstKill","id",monsterId)
  258. if not config then
  259. return reward
  260. end
  261. return string.toIntIntMap(config[1].perreward)
  262. end
  263. --全服奖励
  264. function GoldFirstKill.endTimeReward()
  265. local allData = getsysvar(GoldFirstKill.Var.SERVER_FIRST_KILL_GOLD_BOSS)
  266. if allData == nil or allData == "" then
  267. return
  268. end
  269. local allRoleInfos = getallrolesummaryinfos()
  270. if allRoleInfos == nil or next(allRoleInfos) == nil then
  271. return
  272. end
  273. for _, roleInfo in pairs(allRoleInfos) do
  274. local reward = {}
  275. local actor = roleInfo["actor"]
  276. if GoldFirstKill.tableOpen(actor) then
  277. return
  278. end
  279. --全服首杀红包奖励
  280. local envelope = getplaydef(actor, GoldFirstKill.Var.GOLD_FIRST_KILL_ENVELOPE) or {}
  281. for monsterId,v in pairs(envelope) do
  282. if not v then
  283. local config = ConfigDataManager.getTable("cfg_goldFirstKill","id",monsterId)
  284. if not config then
  285. return
  286. end
  287. local weight = string.split(config[1].redenvelopeweight,"#")
  288. local map = {}
  289. for k, value in pairs(weight) do
  290. map[k] = tonumber(value)
  291. end
  292. local index = math.weightedRandom(map)
  293. local redEnvelope = string.split(config[1].redenvelope,"|")[tonumber(index)]
  294. string.putIntIntMap(reward,redEnvelope)
  295. end
  296. end
  297. --怪物个人首杀奖励
  298. local taskInfo = getplaydef(actor, GoldFirstKill.Var.GOLD_FIRST_KILL_TASK)
  299. if taskInfo == nil or taskInfo == "" then
  300. taskInfo = {}
  301. end
  302. for monsterId,v in pairs(taskInfo) do
  303. if v.status == GoldFirstKill.TASK_STATUS.FINISH then
  304. table.mergeAdd(reward,GoldFirstKill.GetTaskReward(monsterId))
  305. end
  306. end
  307. --发邮件
  308. if table.count(reward) > 0 then
  309. sendconfigmailbyrid(actor,getbaseinfo(actor,"rid"), MailConfig.END_TIME_GOLD_BOSS_REWARD, reward, "")
  310. end
  311. setplaydef(actor, GoldFirstKill.Var.GOLD_FIRST_KILL_ENVELOPE,nil)
  312. setplaydef(actor, GoldFirstKill.Var.GOLD_FIRST_KILL_TASK,nil)
  313. end
  314. setsysvar(GoldFirstKill.Var.SERVER_FIRST_KILL_GOLD_BOSS,nil)
  315. end