BarTaskLogic.lua 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. local Lang = require("common.Lang")
  2. -- local Util = require("common.Util")
  3. local Msg = require("core.Msg")
  4. local ObjHuman = require("core.ObjHuman")
  5. local BarTaskExcel = require("excel.barTask")
  6. -- local ItemExcel = require("excel.item")
  7. local HeroExcel = require("excel.hero")
  8. local Grid = require("bag.Grid")
  9. local BagLogic = require("bag.BagLogic")
  10. local Broadcast = require("broadcast.Broadcast")
  11. local ItemDefine = require("bag.ItemDefine")
  12. -- local HeroDefine = require("hero.HeroDefine")
  13. local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
  14. local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
  15. local ChengjiuLogic = require("chengjiu.ChengjiuLogic")
  16. local ChengjiuDefine = require("chengjiu.ChengjiuDefine")
  17. local DailyTaskLogic = require("dailyTask.DailyTaskLogic")
  18. local RoleExcel = require("excel.role")
  19. local Util = require("common.Util")
  20. -- local MoshouLogic = require("moshou.MoshouLogic")
  21. -- local TequanShopLogic = require("present.TequanShopLogic")
  22. local GuideLogic = require("guide.GuideLogic")
  23. local HeroLogLogic = require("absAct.HeroLogLogic")
  24. local HeroGrowUp = require("absAct.HeroGrowUp")
  25. local MengxinLogic = require("present.MengxinLogic")
  26. local YunYingLogic = require("yunying.YunYingLogic")
  27. local TalismanLogic = require("talisman.TalismanLogic")
  28. local TriggerLogic = require("trigger.TriggerLogic")
  29. local TriggerDefine = require("trigger.TriggerDefine")
  30. DEFAULT_BASE_TASK_CNT = 6 -- 默认基本任务数
  31. DEFAULT_REFRESH_ZUANSHI_CNT = 30 -- 默认刷新所需钻石数量
  32. DEFAULT_MAX_TASK_CNT = 200 -- 最大的任务数
  33. DEFAULT_MAX_DOUBLE_CNT = 10 -- 双倍领取次数
  34. BARTASK_OP_1 = 1 -- 放弃任务
  35. BARTASK_OP_2 = 2 -- 刷新任务
  36. BARTASK_OP_3 = 3 -- 开始任务
  37. BARTASK_OP_4 = 4 -- 完成/加速任务
  38. BARTASK_DAY_FREE = 2 -- 玩家每日免费刷新次数
  39. BARTASK_QUICK_FINISH_TIME = 60 * 60 -- 玩家免费完成时间
  40. BARTASK_TASK_NEED_ISR = 2200 -- 接取任务所需要的情报
  41. local PURPLE_TASK_STAR = 4 -- 紫色任务的star
  42. GUIDE_DEFAUT_STAR = {[1] = 4}
  43. GUIDE_DEFAUT_ITEM = {[1]= {ItemDefine.ITEM_DRAWCARD_GAOCHOU_ID, 1}}
  44. -- 是否拥有特权 (首充后会获得一定特权)
  45. local function isHavePrivilege(human)
  46. if not human.db.topupAcount or human.db.topupAcount <= 0 then
  47. return false
  48. end
  49. return true
  50. end
  51. -- 领取任务所需消耗
  52. local function getTaskCost(human)
  53. local needIsr = BARTASK_TASK_NEED_ISR
  54. local cjPrivilege = ChengjiuLogic.checkPrivilege(human, ChengjiuDefine.PRIVILEGE_TYPE_5)
  55. if cjPrivilege then
  56. local rate =(100 - cjPrivilege) / 100
  57. needIsr = needIsr * rate
  58. end
  59. return needIsr
  60. end
  61. -- 自动领取紫色以上任务
  62. local function autoGetSpeTasks(human)
  63. if not human.db.bar.autoGetTask or human.db.bar.autoGetTask ~= 1 then
  64. return
  65. end
  66. local isGet = false
  67. local itemCnt = getTaskCost(human)
  68. for key, value in pairs(human.db.bar) do
  69. if type(key) == "number" and type(value) == "table" then
  70. if human.db.bar.qingbao < itemCnt then
  71. break
  72. end
  73. if value.star >= PURPLE_TASK_STAR and not value.isLock then
  74. getDo(human, key, nil, nil, true)
  75. isGet = true
  76. end
  77. end
  78. end
  79. return isGet
  80. end
  81. function setBarTaskData(msgData, taskID, human, dbData, now)
  82. if type(taskID) ~= "number" then
  83. return
  84. end
  85. if now == nil then
  86. now = os.time()
  87. end
  88. msgData.taskID = taskID
  89. local heroConfig = HeroExcel.hero[dbData.nameID]
  90. local name = heroConfig.name
  91. local tempConfig = BarTaskExcel.taskName[dbData.star]
  92. local action = nil
  93. if dbData.star <= 3 then
  94. action = tempConfig.actionName[dbData.actionID]
  95. msgData.name = Util.format(Lang.BAR_NAME_FORMAT, action, name)
  96. else
  97. action = tempConfig.taskName[dbData.actionID]
  98. msgData.name = Util.format(Lang.BAR_NAME_FORMAT, name, action)
  99. end
  100. msgData.star = dbData.star
  101. -- 存储的时间是:领取任务时间 - 1
  102. if dbData.ts then
  103. msgData.leftTime = BarTaskExcel.barTask[dbData.star].useTime -(now - dbData.ts)
  104. if msgData.leftTime < 0 then
  105. msgData.leftTime = 0
  106. end
  107. else
  108. msgData.leftTime = BarTaskExcel.barTask[dbData.star].useTime
  109. end
  110. msgData.needTime = BarTaskExcel.barTask[dbData.star].useTime
  111. -- local itemID = dbData.itemID
  112. -- local itemCnt = dbData.itemCnt
  113. -- Grid.makeItem(msgData.item, itemID, itemCnt)
  114. local need = getQuickNeed(msgData.leftTime)
  115. msgData.zuanshiNeed = need
  116. -- BarTaskExcel.barTask[dbData.star].zuanshiNeed
  117. msgData.heroIndex[0] = 0
  118. if dbData.hero then
  119. msgData.heroIndex[0] = #dbData.hero
  120. for i = 1, #dbData.hero do
  121. msgData.heroIndex[i] = dbData.hero[i]
  122. end
  123. end
  124. end
  125. function rewardQuery(human, star)
  126. local barTaskConfig = BarTaskExcel.barTask[star]
  127. local msgRet = Msg.gc.GC_BARTASK_REWARD_QUERY
  128. msgRet.star = star
  129. for k, v in ipairs(barTaskConfig.reward) do
  130. Grid.makeItem(msgRet.item[k], v[1], v[2][1])
  131. end
  132. msgRet.item[0] = #barTaskConfig.reward
  133. Msg.send(msgRet, human.fd)
  134. end
  135. -- 查询酒馆任务
  136. function query(human)
  137. if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_1104, true) then
  138. return
  139. end
  140. ObjHuman.updateDaily(human)
  141. initDB(human)
  142. guideBar(human)
  143. local isGet = autoGetSpeTasks(human)
  144. local needIsr = BARTASK_TASK_NEED_ISR
  145. local cjPrivilege = ChengjiuLogic.checkPrivilege(human, ChengjiuDefine.PRIVILEGE_TYPE_5)
  146. if cjPrivilege then
  147. local rate =(100 - cjPrivilege) / 100
  148. needIsr = needIsr * rate
  149. end
  150. local msgRet = Msg.gc.GC_BARTASK_QUERY
  151. local now = os.time()
  152. local ts1 = Util.getDayStartTime(now)
  153. local double = RoleSystemLogic.isDouble(human, RoleSystemDefine.ROLE_SYS_ID_1104)
  154. msgRet.refreshTime = 24 * 60 * 60 -(now - ts1)
  155. msgRet.refreshNeed = DEFAULT_REFRESH_ZUANSHI_CNT
  156. msgRet.qingbaoNeed = needIsr
  157. msgRet.nowQingBao = human.db.bar and human.db.bar.qingbao or 0
  158. msgRet.qingBaoMax = RoleExcel.exp[human.db.lv].qingBaoMax
  159. msgRet.tequan = human.db.bar and human.db.bar.vip or 0
  160. msgRet.tequanSuper = human.db.bar and human.db.bar.superVip or 0
  161. msgRet.freeCnt = BARTASK_DAY_FREE - human.db.bar.refresCnt
  162. msgRet.freeCnt = msgRet.freeCnt > 0 and msgRet.freeCnt or 0
  163. msgRet.doubleCnt = 0
  164. msgRet.hasCharge = isHavePrivilege(human) and 1 or 0
  165. msgRet.isAutoTask = human.db.bar.autoGetTask or 0
  166. msgRet.AutoTaskTips = isGet and 1 or 0
  167. --秘宝对悬赏情报上限加成
  168. local qingBaoAdd = TalismanLogic.getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.XUANSHANG_QB_MAX) or 0
  169. msgRet.qingBaoMax = msgRet.qingBaoMax + qingBaoAdd
  170. Grid.makeItem(msgRet.refresItem, ItemDefine.ITEM_BASE_JUANZHOU_ID, 1)
  171. if double and human.db.bar.doubleCnt <= DEFAULT_MAX_DOUBLE_CNT then
  172. msgRet.doubleCnt = DEFAULT_MAX_DOUBLE_CNT - human.db.bar.doubleCnt
  173. end
  174. local cnt = 0
  175. for key, value in pairs(human.db.bar) do
  176. if type(key) == "number" then
  177. cnt = cnt + 1
  178. setBarTaskData(msgRet.list[cnt], key, human, value, now)
  179. if cnt >= 200 then
  180. break
  181. end
  182. end
  183. end
  184. msgRet.list[0] = cnt
  185. -- Msg.trace(msgRet)
  186. Msg.send(msgRet, human.fd)
  187. end
  188. -- 刷新酒吧任务
  189. function refresh(human)
  190. ObjHuman.updateDaily(human)
  191. initDB(human)
  192. local allTaskCnt = getAllTaskCnt(human)
  193. if allTaskCnt >= DEFAULT_MAX_TASK_CNT then
  194. return Broadcast.sendErr(human, Lang.BAR_TASK_ALL_MAX_ERR)
  195. end
  196. local isFree = false
  197. if human.db.bar.refresCnt < BARTASK_DAY_FREE then
  198. isFree = true
  199. end
  200. -- 判断消耗
  201. if not isFree then
  202. -- 检测 是否拥有道具
  203. if BagLogic.getItemCnt(human, ItemDefine.ITEM_BASE_JUANZHOU_ID) > 0 then
  204. BagLogic.delItem(human, ItemDefine.ITEM_BASE_JUANZHOU_ID, 1, "bar_refresh")
  205. else
  206. local needZuanshi = DEFAULT_REFRESH_ZUANSHI_CNT
  207. if not ObjHuman.checkRMB(human, needZuanshi) then
  208. return
  209. end
  210. -- 扣消耗
  211. ObjHuman.decZuanshi(human, - needZuanshi, "bar_refresh")
  212. end
  213. end
  214. human.db.bar.refresCnt = human.db.bar.refresCnt + 1
  215. -- 改db
  216. for taskID, taskData in pairs(human.db.bar) do
  217. if type(taskID) == "number" then
  218. if taskData.isLock == nil then
  219. human.db.bar[taskID] = nil
  220. end
  221. end
  222. end
  223. addRandomTask(human, DEFAULT_BASE_TASK_CNT)
  224. query(human)
  225. TriggerLogic.PublishEvent(TriggerDefine.BARTASK_REFRESH, human.db._id, 1)
  226. end
  227. -- 查看具体任务信息
  228. function getQuery(human, taskID)
  229. ObjHuman.updateDaily(human)
  230. initDB(human)
  231. local taskData = human.db.bar[taskID]
  232. if taskData == nil then
  233. return
  234. end
  235. local msgRet = Msg.gc.GC_BARTASK_GET_QUERY
  236. local barTaskConfig = BarTaskExcel.barTask[taskData.star]
  237. msgRet.star = barTaskConfig.starNeed[1] or 0
  238. msgRet.camp[0] = taskData.camp and #taskData.camp or 0
  239. for i = 1, #taskData.camp do
  240. msgRet.camp[i] = taskData.camp[i]
  241. end
  242. -- Msg.trace(msgRet)
  243. Msg.send(msgRet, human.fd)
  244. end
  245. -- 新手引导下默认高抽
  246. function guideBar(human)
  247. if not human.db.bar or not human.db.bar[1] then return end
  248. local flag = GuideLogic.getGuideSkip(human, GuideLogic.SKIPTYPE_JUMP_BARTASK)
  249. if flag then return end
  250. human.db.bar[1].star = GUIDE_DEFAUT_STAR[1]
  251. -- human.db.bar[1].itemID = GUIDE_DEFAUT_ITEM[1][1]
  252. -- human.db.bar[1].itemCnt = GUIDE_DEFAUT_ITEM[1][2]
  253. GuideLogic.setDoSpecialGuide(human, GuideLogic.SKIPTYPE_JUMP_BARTASK)
  254. end
  255. -- 领取任务
  256. function getDo(human, taskID, heroIndexList, heroIDList, noSend)
  257. if not RoleSystemLogic.isOpen(human, RoleSystemDefine.ROLE_SYS_ID_1104, true) then
  258. return
  259. end
  260. ObjHuman.updateDaily(human)
  261. initDB(human)
  262. guideBar(human)
  263. local taskData = human.db.bar[taskID]
  264. if taskData == nil then
  265. return
  266. end
  267. local isNeedCkeck = true
  268. if isHavePrivilege(human) then
  269. isNeedCkeck = false
  270. end
  271. local barTaskConfig = BarTaskExcel.barTask[taskData.star]
  272. -- 判断数量
  273. -- if heroIDList[0] ~= heroIndexList[0] then
  274. -- return
  275. -- end
  276. if isNeedCkeck and heroIDList[0] ~= heroIndexList[0] then
  277. return
  278. end
  279. local needIsr = BARTASK_TASK_NEED_ISR
  280. local cjPrivilege = ChengjiuLogic.checkPrivilege(human, ChengjiuDefine.PRIVILEGE_TYPE_5)
  281. if cjPrivilege then
  282. local rate =(100 - cjPrivilege) / 100
  283. needIsr = needIsr * rate
  284. end
  285. -- 判定情报值
  286. if human.db.bar.qingbao < needIsr then
  287. return Broadcast.sendErr(human, Lang.BAR_TASK_GET_ERR_ISR)
  288. end
  289. -- 判断英雄
  290. -- local indexTable = { }
  291. -- for i = 1, heroIDList[0] do
  292. -- local tempIndex = heroIndexList[i]
  293. -- local heroGird = human.db.heroBag[tempIndex]
  294. -- if type(heroGird) ~= "table" then
  295. -- -- 没有这个英雄
  296. -- return
  297. -- end
  298. -- if heroGird.id ~= heroIDList[i] then
  299. -- -- 没对应上
  300. -- return
  301. -- end
  302. -- if indexTable[tempIndex] then
  303. -- -- 发了重复id
  304. -- return
  305. -- end
  306. -- if isSelectForBarTask(human, tempIndex) then
  307. -- -- 已经在其它悬赏中选择了
  308. -- return
  309. -- end
  310. -- indexTable[tempIndex] = true
  311. -- local heroConfig = HeroExcel.hero[heroGird.id]
  312. -- if heroGird.star >= needStar then
  313. -- nowStarCnt = nowStarCnt + 1
  314. -- end
  315. -- camp2Cnt[heroConfig.camp] =(camp2Cnt[heroConfig.camp] or 0) + 1
  316. -- end
  317. -- -- 判断条件
  318. -- if nowStarCnt < needStarCnt then
  319. -- return Broadcast.sendErr(human, Util.format(Lang.BAR_TASK_GET_ERR_STAR, needStar, needStarCnt))
  320. -- end
  321. -- local campLen = taskData.camp and #taskData.camp or 0
  322. -- for i = 1, campLen do
  323. -- local needCamp = taskData.camp[i]
  324. -- local nowCampCnt = camp2Cnt[needCamp] or 0
  325. -- if nowCampCnt < 1 then
  326. -- return Broadcast.sendErr(human, Lang.BAR_TASK_GET_ERR_CAMP)
  327. -- end
  328. -- camp2Cnt[needCamp] = nowCampCnt - 1
  329. -- end
  330. -- -- 扣除情报点
  331. -- human.db.bar.qingbao = human.db.bar.qingbao - needIsr
  332. -- -- 改db
  333. -- human.db.bar[taskID].isLock = true
  334. -- -- 因为:领取任务后,马上就去query查询,间隔太短
  335. -- -- 导致:时间间隔为0,此时剩下的时间和需要花费的时间相等,导致客户端无法判断
  336. -- -- 所以:领取时间要减去1秒,用来区分
  337. -- -- 链接至:query(human)查看
  338. -- human.db.bar[taskID].ts = os.time() -1
  339. -- human.db.bar[taskID].hero = { }
  340. -- for i = 1, heroIDList[0] do
  341. -- human.db.bar[taskID].hero[i] = heroIndexList[i]
  342. -- end
  343. if isNeedCkeck then
  344. local needStar = barTaskConfig.starNeed[1]
  345. local needStarCnt = barTaskConfig.starNeed[2]
  346. local nowStarCnt = 0
  347. local camp2Cnt = { }
  348. local indexTable = { }
  349. for i = 1, heroIDList[0] do
  350. local tempIndex = heroIndexList[i]
  351. local heroGird = human.db.heroBag[tempIndex]
  352. if type(heroGird) ~= "table" then
  353. -- 没有这个英雄
  354. return
  355. end
  356. if heroGird.id ~= heroIDList[i] then
  357. -- 没对应上
  358. return
  359. end
  360. if indexTable[tempIndex] then
  361. -- 发了重复id
  362. return
  363. end
  364. if isSelectForBarTask(human, tempIndex) then
  365. -- 已经在其它悬赏中选择了
  366. return
  367. end
  368. indexTable[tempIndex] = true
  369. local heroConfig = HeroExcel.hero[heroGird.id]
  370. if heroGird.star >= needStar then
  371. nowStarCnt = nowStarCnt + 1
  372. end
  373. camp2Cnt[heroConfig.camp] =(camp2Cnt[heroConfig.camp] or 0) + 1
  374. end
  375. -- 判断条件
  376. if nowStarCnt < needStarCnt then
  377. return Broadcast.sendErr(human, Util.format(Lang.BAR_TASK_GET_ERR_STAR, needStar, needStarCnt))
  378. end
  379. local campLen = taskData.camp and #taskData.camp or 0
  380. for i = 1, campLen do
  381. local needCamp = taskData.camp[i]
  382. local nowCampCnt = camp2Cnt[needCamp] or 0
  383. if nowCampCnt < 1 then
  384. return Broadcast.sendErr(human, Lang.BAR_TASK_GET_ERR_CAMP)
  385. end
  386. camp2Cnt[needCamp] = nowCampCnt - 1
  387. end
  388. human.db.bar[taskID].hero = { }
  389. for i = 1, heroIDList[0] do
  390. human.db.bar[taskID].hero[i] = heroIndexList[i]
  391. end
  392. end
  393. -- 扣除情报点
  394. human.db.bar.qingbao = human.db.bar.qingbao - needIsr
  395. -- 改db
  396. human.db.bar[taskID].isLock = true
  397. -- 因为:领取任务后,马上就去query查询,间隔太短
  398. -- 导致:时间间隔为0,此时剩下的时间和需要花费的时间相等,导致客户端无法判断
  399. -- 所以:领取时间要减去1秒,用来区分
  400. -- 链接至:query(human)查看
  401. human.db.bar[taskID].ts = os.time() -1
  402. -- 通知客户端
  403. if not noSend then
  404. sendBarTaskUpdate(human, taskID, BARTASK_OP_3)
  405. end
  406. DailyTaskLogic.recordDailyTaskFinishCnt(human, DailyTaskLogic.DAILY_TASK_ID_9, 1)
  407. ChengjiuLogic.onCallback(human, ChengjiuDefine.CJ_TASK_TYPE_7, 1)
  408. MengxinLogic.onCallBack(human,MengxinLogic.MX_TASK_TYPE_4,1)
  409. if taskData.star == 6 then
  410. ChengjiuLogic.onCallback(human, ChengjiuDefine.CJ_TASK_TYPE_18, 1)
  411. HeroGrowUp.onCallback(human, HeroGrowUp.TASKTYPE17, 1)
  412. end
  413. if taskData.star >= 5 then
  414. HeroLogLogic.finishTaskCB(human,HeroLogLogic.HERO_LOG_TYPE_4,1)
  415. end
  416. YunYingLogic.onCallBack(human, "onBarTaskgetDo",1)
  417. TriggerLogic.PublishEvent(TriggerDefine.BARTASK_GET, human.db._id, 1, taskData.star)
  418. end
  419. function getQuickNeed(surTime)
  420. for k, config in ipairs(BarTaskExcel.accTime) do
  421. if surTime < config.maxTime then
  422. return config.zuanshiNeed
  423. end
  424. end
  425. return BarTaskExcel.accTime[#BarTaskExcel.accTime].zuanshiNeed
  426. end
  427. -- 加速完成任务
  428. function quickFinish(human, taskID)
  429. ObjHuman.updateDaily(human)
  430. initDB(human)
  431. local taskData = human.db.bar[taskID]
  432. if taskData == nil then
  433. return
  434. end
  435. -- 条件判断
  436. if taskData.isLock == nil or taskData.ts == nil then
  437. return
  438. end
  439. -- 消耗判断
  440. local barTaskConfig = BarTaskExcel.barTask[taskData.star]
  441. if not barTaskConfig then return end
  442. local surTime = barTaskConfig.useTime -(os.time() - taskData.ts)
  443. local needZuanshi = getQuickNeed(surTime)
  444. if needZuanshi > 0 then
  445. if not ObjHuman.checkRMB(human, needZuanshi) then
  446. return
  447. end
  448. -- 扣消耗
  449. ObjHuman.decZuanshi(human, - needZuanshi, "bar_quick_finish")
  450. end
  451. -- 改db
  452. human.db.bar[taskID].ts = 0
  453. finishTask(human, taskID)
  454. end
  455. function randomTak(human, taskID)
  456. local taskData = human.db.bar[taskID]
  457. if taskData.random then return end
  458. local barTaskConfig = BarTaskExcel.barTask[taskData.star]
  459. if not barTaskConfig then return end
  460. local totalWeight = 0
  461. for i = 1, #barTaskConfig.reward do
  462. totalWeight = totalWeight + barTaskConfig.reward[i][3]
  463. end
  464. local getList = {}
  465. local getRare = 0
  466. for i = 1 , 3 do
  467. local random = math.random(1, totalWeight - getRare)
  468. for k, v in pairs(barTaskConfig.reward) do
  469. if not getList[k] and random <= v[3] then
  470. getList[k] = i
  471. getRare = getRare + v[3]
  472. break
  473. elseif not getList[k] then
  474. -- 防止循环最后一个是 已经随机过的 导致 随机不出来
  475. random = random - v[3]
  476. end
  477. end
  478. end
  479. taskData.random = {}
  480. for k, v in pairs(getList) do
  481. local config = barTaskConfig.reward[k]
  482. taskData.random[v] = {}
  483. taskData.random[v].itemID = config[1]
  484. taskData.random[v].itemCnt = math.random(config[2][1], config[2][2] )
  485. end
  486. end
  487. -- 提交任务
  488. function finishTask(human, taskID)
  489. ObjHuman.updateDaily(human)
  490. initDB(human)
  491. local taskData = human.db.bar[taskID]
  492. if taskData == nil then
  493. return
  494. end
  495. -- 判断条件
  496. local star = taskData.star
  497. local needTime = BarTaskExcel.barTask[star].useTime
  498. local now = os.time()
  499. if now - taskData.ts < needTime then
  500. return
  501. end
  502. if not human.db.bar[taskID].random then
  503. randomTak(human, taskID)
  504. end
  505. -- 双倍参数
  506. --[[
  507. local itemID = taskData.itemID
  508. local itemCnt = taskData.itemCnt
  509. local double = RoleSystemLogic.isDouble(human, RoleSystemDefine.ROLE_SYS_ID_1104)
  510. local isDouble = false
  511. if star == 4 and double and human.db.bar.doubleCnt < DEFAULT_MAX_DOUBLE_CNT then
  512. itemCnt = itemCnt * 2
  513. human.db.bar.doubleCnt = human.db.bar.doubleCnt + 1
  514. isDouble = true
  515. end
  516. ]]
  517. -- 改db
  518. -- human.db.bar[taskID] = nil
  519. -- 给奖励
  520. -- BagLogic.addItem(human, itemID, itemCnt, "bar_reward")
  521. local double = RoleSystemLogic.isDouble(human, RoleSystemDefine.ROLE_SYS_ID_1104)
  522. local isDouble = false
  523. local beishu = 1
  524. if star == 4 and double and human.db.bar.doubleCnt < DEFAULT_MAX_DOUBLE_CNT then
  525. isDouble = true
  526. beishu = 2
  527. end
  528. -- 通知客户端
  529. local msgRet = Msg.gc.GC_BARTASK_FINISH_TASK
  530. msgRet.taskID = taskID
  531. msgRet.double = isDouble and 1 or 0
  532. for k, v in ipairs(human.db.bar[taskID].random) do
  533. Grid.makeItem(msgRet.item[k], v.itemID, v.itemCnt * beishu)
  534. end
  535. msgRet.item[0] = #human.db.bar[taskID].random
  536. Msg.send(msgRet, human.fd)
  537. -- MoshouLogic.onCallBack(human, MoshouLogic.MOSHOU_TASKTYPE_XUANSHANG, 1)
  538. HeroGrowUp.onCallback(human, HeroGrowUp.TASKTYPE15, 1)
  539. end
  540. function chooseReward(human, taskID, index)
  541. ObjHuman.updateDaily(human)
  542. initDB(human)
  543. local taskData = human.db.bar[taskID]
  544. if taskData == nil then
  545. return
  546. end
  547. if not human.db.bar[taskID].random or not human.db.bar[taskID].random[index] then
  548. return
  549. end
  550. local itemID = human.db.bar[taskID].random[index].itemID
  551. local itemCnt = human.db.bar[taskID].random[index].itemCnt
  552. local double = RoleSystemLogic.isDouble(human, RoleSystemDefine.ROLE_SYS_ID_1104)
  553. local isDouble = false
  554. if taskData.star == 4 and double and human.db.bar.doubleCnt < DEFAULT_MAX_DOUBLE_CNT then
  555. itemCnt = itemCnt * 2
  556. human.db.bar.doubleCnt = human.db.bar.doubleCnt + 1
  557. isDouble = true
  558. end
  559. -- 改db
  560. human.db.bar[taskID] = nil
  561. -- 给奖励
  562. BagLogic.addItem(human, itemID, itemCnt, "bar_reward")
  563. local msgRet = Msg.gc.GC_BARTASK_CHOOSE_REWARD
  564. msgRet.taskID = taskID
  565. msgRet.double = isDouble and 1 or 0
  566. Grid.makeItem(msgRet.item, itemID, itemCnt)
  567. Msg.send(msgRet, human.fd)
  568. end
  569. -- 一键领取
  570. function CG_BARTASK_FINISH_ALL(human)
  571. do return end
  572. if not human.db.bar then return end
  573. -- 双倍参数 -- 清空次数
  574. local double = RoleSystemLogic.isDouble(human, RoleSystemDefine.ROLE_SYS_ID_1104)
  575. local isDouble
  576. local msgRet = Msg.gc.GC_BARTASK_FINISH_ALL
  577. local list = nil
  578. local cnt = 0
  579. for taskID, taskData in pairs(human.db.bar) do
  580. if type(taskID) == "number" and taskData.ts then
  581. local star = taskData.star
  582. local needTime = BarTaskExcel.barTask[star].useTime
  583. local now = os.time()
  584. if now - taskData.ts >= needTime then
  585. local itemID = taskData.itemID
  586. local itemCnt = taskData.itemCnt
  587. -- 计算双倍
  588. if star == 4 and double and human.db.bar.doubleCnt < DEFAULT_MAX_DOUBLE_CNT then
  589. itemCnt = itemCnt * 2
  590. human.db.bar.doubleCnt = human.db.bar.doubleCnt + 1
  591. isDouble = true
  592. end
  593. list = list or { }
  594. list[itemID] =(list[itemID] or 0) + itemCnt
  595. human.db.bar[taskID] = nil
  596. cnt = cnt + 1
  597. BagLogic.addItem(human, itemID, itemCnt, "bar_reward")
  598. end
  599. end
  600. end
  601. if not list then return end
  602. msgRet.double = isDouble and 1 or 0
  603. msgRet.items[0] = 0
  604. for itemID, itemCnt in pairs(list) do
  605. msgRet.items[0] = msgRet.items[0] + 1
  606. if not msgRet.items[msgRet.items[0]] then
  607. msgRet.items[msgRet.items[0]] = Util.copyTable(msgRet.items[msgRet.items[0] -1])
  608. end
  609. Grid.makeItem(msgRet.items[msgRet.items[0]], itemID, itemCnt)
  610. end
  611. -- Msg.trace(msgRet)
  612. Msg.send(msgRet, human.fd)
  613. query(human)
  614. end
  615. function heroIndexByDelForPos(human, index)
  616. if not human.db.bar then return end
  617. for taskID, v in pairs(human.db.bar) do
  618. if type(taskID) == "number" then
  619. local heroLen = v.hero and #v.hero or 0
  620. for i = 1, heroLen do
  621. if v.hero[i] == index then
  622. table.remove(v.hero, i)
  623. break
  624. end
  625. end
  626. end
  627. end
  628. end
  629. -- 是否已上阵
  630. function isSelectForBarTask(human, index)
  631. if not human.db.bar then return end
  632. for taskID, v in pairs(human.db.bar) do
  633. if type(taskID) == "number" then
  634. local heroLen = v.hero and #v.hero or 0
  635. for i = 1, heroLen do
  636. if v.hero[i] == index then
  637. return true
  638. end
  639. end
  640. end
  641. end
  642. end
  643. -- 激活酒馆悬赏 特权
  644. function activateBarVip(human, type)
  645. initDB(human)
  646. if type == 1 then
  647. human.db.bar.vip = 1
  648. elseif type == 2 then
  649. human.db.bar.superVip = 1
  650. end
  651. end
  652. ------------------------------功能函数-------------------------------------------
  653. function getEmptyIndex(human)
  654. for index = 1, 1000 do
  655. if human.db.bar[index] == nil then
  656. return index
  657. end
  658. end
  659. end
  660. -- 初始化酒馆任务数据
  661. function initDB(human)
  662. if human.db.bar ~= nil then
  663. return
  664. end
  665. human.db.bar = { }
  666. human.db.bar.refresCnt = 0
  667. human.db.bar.qingbao = 0
  668. human.db.bar.doubleCnt = 0 -- 双倍次数
  669. local newAddCnt = getBaseTaskCnt(human)
  670. addRandomTask(human, newAddCnt, {4}, true)
  671. end
  672. -- 跨天刷新酒馆任务
  673. function refreshDailyTask(human)
  674. if human.db.bar == nil then
  675. return
  676. end
  677. human.db.bar.refresCnt = 0
  678. human.db.bar.doubleCnt = 0 -- 双倍次数
  679. end
  680. -- 获取现在的任务总数
  681. function getAllTaskCnt(human)
  682. if human.db.bar == nil then
  683. return 0
  684. end
  685. local allTaskCnt = 0
  686. for key, value in pairs(human.db.bar) do
  687. if value ~= nil then
  688. allTaskCnt = allTaskCnt + 1
  689. end
  690. end
  691. return allTaskCnt
  692. end
  693. -- 获取玩家基础任务数
  694. function getBaseTaskCnt(human)
  695. return DEFAULT_BASE_TASK_CNT
  696. end
  697. -- 随机 悬赏任务 starList[第几个] = 星级 itemList[第几个] = {itemID, itemCnt}
  698. function addRandomTask(human, cnt, starList, isInit)
  699. if cnt < 1 then return end
  700. -- 检查是否 已经达到上限
  701. local allTaskCnt = getAllTaskCnt(human)
  702. if allTaskCnt >= 200 then
  703. return
  704. end
  705. local star = nil
  706. local camp = nil
  707. local nameID = nil
  708. local actionID = nil
  709. local vipChange = 0
  710. if (human.db.bar.refresCnt == BARTASK_DAY_FREE + 1 and human.db.bar.vip) or(human.db.bar.refresCnt == BARTASK_DAY_FREE + 3 and human.db.bar.superVip) then
  711. vipChange = math.random(1, cnt)
  712. end
  713. for i = 1, cnt do
  714. star = getRandomStar(human)
  715. if vipChange > 0 and i == vipChange then
  716. if human.db.bar.refresCnt == BARTASK_DAY_FREE + 1 then
  717. star = 4
  718. else
  719. star = 5
  720. end
  721. end
  722. if starList and starList[i] then
  723. star = starList[i]
  724. end
  725. itemID, itemCnt = getRandomItem(human, star)
  726. nameID, actionID = getRandomName(human, star)
  727. camp = getRandomCond(star)
  728. if isInit then
  729. camp[1] = 1
  730. camp[2] = 2
  731. end
  732. local emptyIndex = getEmptyIndex(human)
  733. if emptyIndex then
  734. human.db.bar[emptyIndex] = { }
  735. human.db.bar[emptyIndex].star = star
  736. human.db.bar[emptyIndex].nameID = nameID
  737. human.db.bar[emptyIndex].actionID = actionID
  738. human.db.bar[emptyIndex].camp = camp
  739. human.db.bar[emptyIndex].isLock = nil
  740. human.db.bar[emptyIndex].ts = nil
  741. human.db.bar[emptyIndex].hero = nil
  742. end
  743. end
  744. end
  745. -- 添加情报值
  746. function addBarQingBao(human, cnt)
  747. initDB(human)
  748. if human.db.bar == nil then
  749. return
  750. end
  751. local max = RoleExcel.exp[human.db.lv].qingBaoMax
  752. local qingBaoAdd = TalismanLogic.getTalismanAdd(human, TalismanLogic.OTHER_EFFECT_TBL.XUANSHANG_QB_MAX) or 0
  753. max = max + qingBaoAdd
  754. if human.db.bar.qingbao + cnt < max then
  755. human.db.bar.qingbao = human.db.bar.qingbao + cnt
  756. else
  757. human.db.bar.qingbao = max
  758. end
  759. end
  760. function getRandomStar(human)
  761. local barTaskConfig = BarTaskExcel.barTask
  762. local totalWeight = 0
  763. for i = 1, #barTaskConfig do
  764. totalWeight = totalWeight + barTaskConfig[i].chance
  765. end
  766. local randNum = math.random(1, totalWeight)
  767. local star = 0
  768. for j = 1, #barTaskConfig do
  769. local tempWeight = barTaskConfig[j].chance
  770. if randNum <= tempWeight then
  771. star = j
  772. break
  773. end
  774. randNum = randNum - tempWeight
  775. end
  776. return star
  777. end
  778. function getRandomItem(human, star)
  779. local barTaskConfig = BarTaskExcel.barTask[star]
  780. if not barTaskConfig then return end
  781. local totalWeight = 0
  782. for i = 1, #barTaskConfig.reward do
  783. totalWeight = totalWeight + barTaskConfig.reward[i][3]
  784. end
  785. local randNum = math.random(1, totalWeight)
  786. local itemID = 0
  787. local itemCnt = 0
  788. for j = 1, #barTaskConfig.reward do
  789. local tempWeight = barTaskConfig.reward[j][3]
  790. if randNum <= tempWeight then
  791. itemID = barTaskConfig.reward[j][1]
  792. local minCnt = barTaskConfig.reward[j][2][1]
  793. local maxCnt = barTaskConfig.reward[j][2][2]
  794. itemCnt = math.random(minCnt, maxCnt)
  795. break
  796. end
  797. randNum = randNum - tempWeight
  798. end
  799. return itemID, itemCnt
  800. end
  801. function getRandomName(human, star)
  802. local barTaskConfig = BarTaskExcel.taskName[star]
  803. if not barTaskConfig then return end
  804. local randNum = math.random(1, #barTaskConfig.heroName)
  805. local nameID = barTaskConfig.heroName[randNum]
  806. local actionID = nil
  807. if star <= 3 then
  808. actionID = math.random(1, #barTaskConfig.actionName)
  809. else
  810. actionID = math.random(1, #barTaskConfig.taskName)
  811. end
  812. return nameID, actionID
  813. end
  814. function getRandomCond(star)
  815. local barTaskConfig = BarTaskExcel.barTask[star]
  816. if not barTaskConfig then return end
  817. local heroCnt = barTaskConfig.heroCnt
  818. local camp = { }
  819. local c = 0
  820. local j = 0
  821. for i = 1, 5 do camp[i] = i end
  822. for i = 1, heroCnt do
  823. c = c + 1
  824. local r = math.random(c, #camp)
  825. camp[c], camp[r] = camp[r], camp[c]
  826. end
  827. for i = #camp, c + 1, -1 do
  828. camp[i] = nil
  829. end
  830. return camp
  831. end
  832. -- 下发酒馆任务更新
  833. function sendBarTaskUpdate(human, taskID, op)
  834. local msgRet = Msg.gc.GC_BARTASK_UPDATE
  835. msgRet.op = op
  836. setBarTaskData(msgRet.task, taskID, human, human.db.bar[taskID])
  837. msgRet.nowQingBao = human.db.bar and human.db.bar.qingbao or 0
  838. Msg.send(msgRet, human.fd)
  839. end
  840. -- 是否有红点
  841. function isDot(human)
  842. if human.db.bar == nil then
  843. return false
  844. end
  845. local now = os.time()
  846. for k, v in pairs(human.db.bar) do
  847. if type(k) == "number" then
  848. if v.ts == nil then
  849. return false
  850. end
  851. if v.isLock and v.ts then
  852. local needTime = BarTaskExcel.barTask[v.star].useTime
  853. if now - v.ts > needTime then
  854. return true
  855. end
  856. end
  857. end
  858. end
  859. return false
  860. end
  861. -- 更新勾选 "自动领取紫色以上任务"
  862. function UpdateOP(human, state)
  863. if not human.db.bar then
  864. return
  865. end
  866. if state ~= 0 and state ~= 1 then
  867. return Broadcast.sendErr(human, Lang.COMMON_ARGUMENT_ERROR)
  868. end
  869. human.db.bar.autoGetTask = state
  870. SendOPState(human)
  871. end
  872. function SendOPState(human)
  873. local msgRet = Msg.gc.GC_BARTASK_SET_OP
  874. msgRet.op = human.db.bar.autoGetTask or 0
  875. Msg.send(msgRet, human.fd)
  876. end