bag.lua 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  1. -- 资源
  2. local code = require "code"
  3. local gameConst = require("const.gameConst")
  4. local util_user = require("utils.util_user")
  5. local timeUtil = require "utils.timeUtil"
  6. local baseAdapt = require "base.baseAdapt"
  7. local resAdapt = require("adapt.resAdapt")
  8. local moduleData = require("data.module")
  9. local heroData = require("data.hero")
  10. local hookData = require("data.hook")
  11. local MODULE_NAME = "tb_res"
  12. -- 升序排.
  13. local function sortAsc(list, key)
  14. table.sort(
  15. list or {},
  16. function(a, b)
  17. return a[key] < b[key]
  18. end
  19. )
  20. end
  21. local root = {}
  22. -- 玩家资源初始化(新手物品初始化)
  23. function root:init(uid)
  24. local lastInitTime = self:_get_last_init_time(uid)
  25. -- 仅初始化1次
  26. if lastInitTime > 0 then
  27. return
  28. end
  29. local initConfig = resAdapt:get_init_conf()
  30. local initItems = {}
  31. for _, v in ipairs(initConfig) do
  32. table.arry_merge(initItems, v.items)
  33. end
  34. if #initItems > 0 then
  35. local reason = gameConst.RES_REASON_PRE.INIT
  36. self:update_items(uid, initItems, reason)
  37. end
  38. local curTime = timeUtil.now(uid)
  39. self:_set_last_init_time(uid, curTime)
  40. end
  41. -- 请求获取背包信息
  42. function root:get_info(uid)
  43. log.info("get_info uid:%s", uid)
  44. local itemInfo = moduleData:hget_json(uid, MODULE_NAME, "itemInfo") or {}
  45. local isUpdate = false
  46. local curTime = timeUtil.now(uid)
  47. for i = #(itemInfo or {}), 1, -1 do
  48. local item = itemInfo[i]
  49. if item.expTime and curTime > item.expTime then
  50. -- 已经过期
  51. isUpdate = true
  52. item.count = 0
  53. end
  54. if item.count == 0 then
  55. isUpdate = true
  56. table.remove(itemInfo, i)
  57. end
  58. end
  59. if isUpdate then
  60. -- 存储
  61. moduleData:hset(uid, MODULE_NAME, "itemInfo", itemInfo)
  62. end
  63. return itemInfo
  64. end
  65. --[[ 请求增减资源列表
  66. 返回的是 DataResUpdateItem 列表
  67. ]]
  68. function root:update_items(uid, items, reason)
  69. -- log.info("update_items uid:%s items:%s reason:%s", uid, tostring(items), tostring(reason))
  70. local itemInfo = moduleData:hget_json(uid, MODULE_NAME, "itemInfo") or {}
  71. local list = {}
  72. for _, v in ipairs(items) do
  73. local updateItems = self:_update_item(uid, itemInfo, v.id, v.count, v.bid)
  74. if updateItems then
  75. table.arry_merge(list, updateItems)
  76. end
  77. end
  78. -- 存储
  79. moduleData:hset(uid, MODULE_NAME, "itemInfo", itemInfo)
  80. -- 推送
  81. self:_on_res_update(uid, list, reason)
  82. -- 事件
  83. self:_dispatch_event(uid, list)
  84. -- 日志-资源变化
  85. self:_log_resource(uid, list, reason)
  86. -- 英雄卡的自动激活、自动溢出转化
  87. self:_check_hero_item(uid, list, reason)
  88. return list
  89. end
  90. -- 判断 兑换随机英雄 资源是否足够
  91. function root:check_hero_random_exchange(uid, sourceId, times)
  92. local conf = resAdapt:get_hero_exchange_conf(sourceId)
  93. if not conf then
  94. return false, code.RES.ID_ERROR
  95. end
  96. local sourceCount = conf.sourceCount
  97. local needCount = sourceCount * times
  98. local count = self:get_item_count(uid, sourceId)
  99. if count < needCount then
  100. return false, code.RES.NOT_ENOUGH
  101. end
  102. return true
  103. end
  104. -- 请求兑换英雄。物品类型200的使用这个接口
  105. function root:hero_random_exchange(uid, sourceId, times)
  106. -- log.info("hero_random_exchange uid:%s sourceId:%s times:%s", uid, sourceId, times)
  107. local conf = resAdapt:get_hero_exchange_conf(sourceId)
  108. if not conf then
  109. log.error("兑换英雄的配置异常")
  110. return
  111. end
  112. local sourceCount = conf.sourceCount
  113. local targetQa = conf.targetQa
  114. local targetColor = conf.targetColor
  115. local targetCamp = conf.targetCamp
  116. local targetCount = conf.targetCount
  117. local heroItemIds = nil
  118. -- 限定英雄
  119. if not is_empty(conf.baseRandomHeros) then
  120. -- 玩家已获得同品质英雄
  121. heroItemIds = table.copy(conf.baseRandomHeros)
  122. local ownQaHeroList = heroData:get_qa_hero_id_list(uid, targetQa)
  123. if not is_empty(ownQaHeroList) then
  124. for _, v in ipairs(ownQaHeroList) do
  125. local heroId = v + 10000
  126. table.unique_insert(heroItemIds, heroId)
  127. end
  128. end
  129. else
  130. heroItemIds = resAdapt:get_hero_item_ids(targetQa, targetColor, targetCamp)
  131. end
  132. log.info(
  133. "hero_random_exchange uid[%s] sourceId[%s] heroItemIds[%s]",
  134. tostring(uid),
  135. tostring(sourceId),
  136. tostring(heroItemIds)
  137. )
  138. if not heroItemIds then
  139. log.error("兑换英雄的配置异常 找不到可以随机的英雄卡id")
  140. return
  141. end
  142. local targetItems = {}
  143. local totalCount = targetCount * times
  144. for i = 1, totalCount do
  145. local heroItemId = heroItemIds[math.random(#heroItemIds)]
  146. table.insert(targetItems, {id = heroItemId, count = 1})
  147. end
  148. local reason = gameConst.RES_REASON_PRE.HERO_RANDOM_EXCHANGE
  149. local needCount = sourceCount * times
  150. local sourceItems = {
  151. {id = sourceId, count = needCount}
  152. }
  153. self:consume_items(uid, sourceItems, reason)
  154. self:add_items(uid, targetItems, reason)
  155. return targetItems
  156. end
  157. -- 请求兑换英雄。物品类型220的使用这个接口
  158. function root:hero_compound(uid, resId, times)
  159. -- log.info("hero_random_exchange uid:%s sourceId:%s times:%s", uid, resId, times)
  160. local conf = resAdapt:get_hero_exchange_conf(resId)
  161. if not conf then
  162. log.error("兑换英雄的配置异常")
  163. return
  164. end
  165. local sourceCount = conf.sourceCount or 1
  166. local targetCount = conf.targetCount or 1
  167. local targetItems = {}
  168. local reason = gameConst.RES_REASON_PRE.HERO_COMPOUND
  169. do
  170. local needCount = sourceCount * times
  171. local sourceItems = {
  172. {id = resId, count = needCount}
  173. }
  174. self:consume_items(uid, sourceItems, reason)
  175. end
  176. do
  177. local totalCount = targetCount * times
  178. table.insert(targetItems, {id = conf.appointHero, count = totalCount})
  179. self:add_items(uid, targetItems, reason)
  180. end
  181. return targetItems
  182. end
  183. -- 判断 兑换自选英雄 资源是否足够
  184. function root:check_hero_optional_exchange(uid, sourceId, items)
  185. local conf = resAdapt:get_hero_exchange_conf(sourceId)
  186. if not conf then
  187. return false, code.RES.ID_ERROR
  188. end
  189. local sourceCount = conf.sourceCount
  190. local targetQa = conf.targetQa
  191. local targetColor = conf.targetColor
  192. local targetCamp = conf.targetCamp
  193. local targetCount = conf.targetCount
  194. local heroItemIds = resAdapt:get_hero_item_ids(targetQa, targetColor, targetCamp)
  195. if not heroItemIds then
  196. log.error("兑换英雄的配置异常 找不到可以自选的英雄卡id")
  197. return false, code.RES.CONFIG_ERROR
  198. end
  199. -- 兑换卡的数量
  200. local totalCount = 0
  201. for _, v in ipairs(items) do
  202. totalCount = totalCount + v.count
  203. end
  204. local times = math.floor(totalCount / targetCount)
  205. -- 兑换数量检查
  206. local needCount = sourceCount * times
  207. local count = self:get_item_count(uid, sourceId)
  208. if count < needCount then
  209. return false, code.RES.NOT_ENOUGH
  210. end
  211. -- 兑换卡属性检查
  212. local ok = true
  213. for _, v in ipairs(items) do
  214. if not table.indexof(heroItemIds, v.id) then
  215. ok = false
  216. break
  217. end
  218. end
  219. if not ok then
  220. return false, code.RES.LOGIC_ERROR
  221. end
  222. return true
  223. end
  224. -- 请求兑换自选英雄。物品类型210的使用这个接口
  225. function root:hero_optional_exchange(uid, sourceId, items)
  226. log.info("hero_optional_exchange uid:%s sourceId:%s items:%s", uid, sourceId, tostring(items))
  227. local conf = resAdapt:get_hero_exchange_conf(sourceId)
  228. if not conf then
  229. log.error("兑换英雄的配置异常")
  230. return
  231. end
  232. local sourceCount = conf.sourceCount
  233. local targetCount = conf.targetCount
  234. local targetItems = {}
  235. local totalCount = 0 -- 兑换卡的次数
  236. for _, v in ipairs(items) do
  237. table.insert(targetItems, {id = v.id, count = v.count})
  238. totalCount = totalCount + v.count
  239. end
  240. local times = math.floor(totalCount / targetCount)
  241. local reason = gameConst.RES_REASON_PRE.HERO_OPTIONAL_EXCHANGE
  242. local needCount = sourceCount * times
  243. local sourceItems = {
  244. {id = sourceId, count = needCount}
  245. }
  246. self:consume_items(uid, sourceItems, reason)
  247. self:add_items(uid, targetItems, reason)
  248. return targetItems
  249. end
  250. -- 判断 兑换资源包 资源是否足够
  251. function root:check_item_count(uid, sourceId, times)
  252. local needCount = times
  253. local count = self:get_item_count(uid, sourceId)
  254. if count < needCount then
  255. return false, code.RES.NOT_ENOUGH
  256. end
  257. return true
  258. end
  259. -- 请求兑换资源包。物品类型30的使用这个接口
  260. function root:onhook_exchange(uid, sourceId, times)
  261. log.info("onhook_exchange uid:%s sourceId:%s times:%s", uid, sourceId, times)
  262. local conf = resAdapt:get_onhook_exchange_conf(sourceId)
  263. if not conf then
  264. log.error("兑换资源包的配置异常")
  265. return
  266. end
  267. local targetType = conf.targetType
  268. local duration = conf.duration
  269. -- 获取挂机效率
  270. local rateItems = hookData:get_hook_minute_rate_items(uid)
  271. -- 灵晶
  272. local speedItem1 = table.key_find(rateItems, "id", gameConst.ITEM_ID.SPAR) or {}
  273. local speed1 = speedItem1.count or 0
  274. -- 突破石
  275. local speedItem2 = table.key_find(rateItems, "id", gameConst.ITEM_ID.BREAK) or {}
  276. local speed2 = speedItem2.count or 0
  277. -- 帝国币
  278. local speedItem3 = table.key_find(rateItems, "id", gameConst.ITEM_ID.GOLD) or {}
  279. local speed3 = speedItem3.count or 0
  280. -- 装备强化石
  281. local speedItem4 = table.key_find(rateItems, "id", gameConst.ITEM_ID.EQ_LEVEL) or {}
  282. local speed4 = speedItem4.count or 0
  283. -- 装备精练石
  284. local speedItem5 = table.key_find(rateItems, "id", gameConst.ITEM_ID.EQ_BREAK) or {}
  285. local speed5 = speedItem5.count or 0
  286. local targetItems
  287. if targetType == gameConst.ONHOOK_TYPE.ALL then
  288. targetItems = {
  289. {id = gameConst.ITEM_ID.SPAR, count = duration * speed1 * times},
  290. {id = gameConst.ITEM_ID.BREAK, count = duration * speed2 * times},
  291. {id = gameConst.ITEM_ID.GOLD, count = duration * speed3 * times}
  292. }
  293. elseif targetType == gameConst.ONHOOK_TYPE.SPAR then
  294. targetItems = {
  295. {id = gameConst.ITEM_ID.SPAR, count = duration * speed1 * times}
  296. }
  297. elseif targetType == gameConst.ONHOOK_TYPE.BREAK then
  298. targetItems = {
  299. {id = gameConst.ITEM_ID.BREAK, count = duration * speed2 * times}
  300. }
  301. elseif targetType == gameConst.ONHOOK_TYPE.GOLD then
  302. targetItems = {
  303. {id = gameConst.ITEM_ID.GOLD, count = duration * speed3 * times}
  304. }
  305. elseif targetType == gameConst.ONHOOK_TYPE.QIANGHUA then
  306. targetItems = {
  307. {id = gameConst.ITEM_ID.EQ_LEVEL, count = duration * speed4 * times}
  308. }
  309. elseif targetType == gameConst.ONHOOK_TYPE.JINGJIAN then
  310. targetItems = {
  311. {id = gameConst.ITEM_ID.EQ_BREAK, count = duration * speed5 * times}
  312. }
  313. else
  314. log.error("不支持的挂机资源类型")
  315. end
  316. if targetItems then
  317. local reason = gameConst.RES_REASON_PRE.ONHOOK_EXCHANGE
  318. local needCount = times
  319. local sourceItems = {
  320. {id = sourceId, count = needCount}
  321. }
  322. self:consume_items(uid, sourceItems, reason)
  323. self:add_items(uid, targetItems, reason)
  324. end
  325. return targetItems
  326. end
  327. -- 请求兑换英雄。物品类型200的使用这个接口
  328. function root:use_equip_box(uid, resId, times)
  329. -- log.debug("use_equip_box uid:%s resId:%s times:%s \n", uid, resId, tostring(times))
  330. local configs = resAdapt:getResEquipBoxConfig(resId)
  331. if not configs then
  332. log.error(string.format("获取到相应的装备宝箱的配置异常,resId:[%s]", tostring(resId)))
  333. return
  334. end
  335. local function keyRandWeight(array, totalWeight)
  336. local weight = {}
  337. local t = 0
  338. if not totalWeight then
  339. for _, v in ipairs(array) do
  340. if v.totalWeight then
  341. totalWeight = v.totalWeight
  342. break
  343. end
  344. end
  345. end
  346. if not totalWeight then
  347. return
  348. end
  349. for _, v in ipairs(array) do
  350. local ranNum = math.random(0, totalWeight)
  351. if v.weight and ranNum <= v.weight then
  352. return v.equipBag
  353. end
  354. end
  355. end
  356. local targetItems = {}
  357. do
  358. -- 可能一次性开N个宝箱
  359. -- 先排序
  360. sortAsc(configs, "seq")
  361. for i = 1, (times or 0), 1 do
  362. local equipBag = keyRandWeight(configs)
  363. local reqbConfigs = resAdapt:getResEquipBagConfig(equipBag or 0)
  364. if reqbConfigs then
  365. -- 随机配置
  366. local index = random_list_by_weight(reqbConfigs)
  367. local conf = reqbConfigs[index]
  368. log.debug(
  369. "use_equip_box uid[%s] resId[%s] equipBag[%s] index[%s], conf[%s]",
  370. tostring(self.uid),
  371. tostring(resId),
  372. tostring(equipBag),
  373. tostring(index),
  374. tostring(conf)
  375. )
  376. if conf and conf.resId then
  377. table.insert(targetItems, {id = conf.resId, count = conf.count or 1})
  378. end
  379. else
  380. log.error(string.format("获取到相应的装备包的配置异常,resId:[%s]", tostring(resId)))
  381. end
  382. end
  383. end
  384. local reason = "equip_box_random"
  385. local sourceItems = {
  386. {id = resId, count = times}
  387. }
  388. if #targetItems > 0 then
  389. self:consume_items(uid, sourceItems, reason)
  390. self:add_items(uid, targetItems, reason)
  391. end
  392. return targetItems
  393. end
  394. function root:useEquipBoxDebris(uid, resId, times)
  395. local resConfig = baseAdapt:getOneConfig("ResEquipExchangeConfig", resId, "id")
  396. if not resConfig or not resConfig.sourceCount or not resConfig.targetId then
  397. return code.CONFIG_ERROR
  398. end
  399. local getCount = math.floor(times / resConfig.sourceCount)
  400. local totalCount = (resConfig.sourceCount * getCount)
  401. if totalCount > times or getCount <= 0 then
  402. return code.RES.NOT_ENOUGH
  403. end
  404. local haveCount = self:get_item_count(uid, resId)
  405. if haveCount < times then
  406. return code.RES.NOT_ENOUGH
  407. end
  408. local reason = "equip_box_random_debris"
  409. local sourceItems = {
  410. {id = resId, count = totalCount}
  411. }
  412. local targetItems = {
  413. {id = resConfig.targetId, count = getCount}
  414. }
  415. self:consume_items(uid, sourceItems, reason)
  416. self:add_items(uid, targetItems, reason)
  417. targetItems = self:use_equip_box(uid, resConfig.targetId, getCount)
  418. return code.OK, targetItems
  419. end
  420. function root:use_equip_optional_box(uid, resId, items, times)
  421. -- log.debug("use_equip_optional_box uid:%s resId:%s items:%s", uid, resId, tostring(items))
  422. local configs = resAdapt:getResEquipChoiceConfig(resId)
  423. if not configs then
  424. log.error("装备包配置的配置异常")
  425. return
  426. end
  427. -- log.debug("use_equip_optional_box uid:%s items:%sm v.id:%s", uid, tostring(resId), tostring(configs))
  428. local targetItems = {}
  429. for _, v in ipairs(items or {}) do
  430. local config = table.key_find(configs, "equipId", v.id)
  431. -- log.debug("use_equip_optional_box uid:%s items:%sm v.id:%s", uid, tostring(v), tostring(config))
  432. if config then
  433. for i = 1, (times or 1), 1 do
  434. table.insert(targetItems, {id = v.id, count = (config.count or 1)})
  435. end
  436. end
  437. end
  438. local reason = "use_equip_optional_box"
  439. local sourceItems = {
  440. {id = resId, count = times or 1}
  441. }
  442. if #targetItems > 0 then
  443. self:consume_items(uid, sourceItems, reason)
  444. self:add_items(uid, targetItems, reason)
  445. end
  446. return targetItems
  447. end
  448. ----------------------------------------------------------------
  449. -- 判断资源是否足够
  450. function root:is_enough(uid, items)
  451. -- 求和
  452. local tempMap = {}
  453. for _, v in ipairs(items) do
  454. if not tempMap[v.id] then
  455. tempMap[v.id] = v.count
  456. else
  457. tempMap[v.id] = tempMap[v.id] + v.count
  458. end
  459. end
  460. local itemInfo = moduleData:hget_json(uid, MODULE_NAME, "itemInfo") or {}
  461. local ret = true
  462. for k, v in pairs(tempMap) do
  463. local item = table.key_find(itemInfo, "id", k)
  464. if not item or not item.count or item.count < v then
  465. ret = false
  466. break
  467. end
  468. end
  469. return ret
  470. end
  471. function root:is_enough_by_bid(uid, items)
  472. -- 求和
  473. local tempMap = {}
  474. for _, v in ipairs(items) do
  475. if not tempMap[v.bid] then
  476. tempMap[v.bid] = v.count
  477. else
  478. tempMap[v.bid] = tempMap[v.bid] + v.count
  479. end
  480. end
  481. local itemInfo = moduleData:hget_json(uid, MODULE_NAME, "itemInfo") or {}
  482. local ret = true
  483. for k, v in pairs(tempMap) do
  484. local item = table.key_find(itemInfo, "bid", k)
  485. if not item or not item.count or item.count < v then
  486. ret = false
  487. break
  488. end
  489. end
  490. return ret
  491. end
  492. --[[ 增加资源
  493. 返回的是 DataResUpdateItem 列表
  494. ]]
  495. function root:add_items(uid, items, reason)
  496. return self:update_items(uid, items, reason)
  497. end
  498. --[[ 消耗资源。资源不足也会消耗,消耗到0为止
  499. 返回的是 DataResUpdateItem 列表
  500. ]]
  501. function root:consume_items(uid, items, reason)
  502. local tempList = {}
  503. for _, v in ipairs(items) do
  504. table.insert(tempList, {id = v.id, count = -v.count, bid = v.bid})
  505. end
  506. return self:update_items(uid, tempList, reason)
  507. end
  508. -- 获取资源数量
  509. function root:get_item_count(uid, itemId)
  510. -- log.info("get_item_count uid:%s itemId:%s", uid, itemId)
  511. local itemInfo = moduleData:hget_json(uid, MODULE_NAME, "itemInfo") or {}
  512. local item = table.key_find(itemInfo, "id", itemId) or {}
  513. return item.count or 0
  514. end
  515. -- 获取资源数量
  516. function root:get_item_by_bid(uid, bid)
  517. -- log.info("get_item_by_bid uid:%s bid:%s", uid, bid)
  518. local itemInfo = moduleData:hget_json(uid, MODULE_NAME, "itemInfo") or {}
  519. local item = table.key_find(itemInfo, "bid", bid) or {}
  520. return item
  521. end
  522. -- 获取帝国币
  523. function root:get_gold(uid)
  524. return self:get_item_count(uid, gameConst.ITEM_ID.GOLD)
  525. end
  526. -- 获取宝石
  527. function root:get_diamond(uid)
  528. return self:get_item_count(uid, gameConst.ITEM_ID.DIAMOND)
  529. end
  530. -- 获取友情点
  531. function root:get_friend_coin(uid)
  532. return self:get_item_count(uid, gameConst.ITEM_ID.FRIEND_COIN)
  533. end
  534. -- 获取祈愿币
  535. function root:get_hero_coin(uid)
  536. return self:get_item_count(uid, gameConst.ITEM_ID.HERO_COIN)
  537. end
  538. -- 获取公会声望
  539. function root:get_club_coin(uid)
  540. return self:get_item_count(uid, gameConst.ITEM_ID.CLUB_COIN)
  541. end
  542. -- 获取灵晶
  543. function root:get_spar(uid)
  544. return self:get_item_count(uid, gameConst.ITEM_ID.SPAR)
  545. end
  546. -- 获取突破石
  547. function root:get_break(uid)
  548. return self:get_item_count(uid, gameConst.ITEM_ID.BREAK)
  549. end
  550. -- 获取觉醒石
  551. function root:get_awake(uid)
  552. return self:get_item_count(uid, gameConst.ITEM_ID.AWAKE)
  553. end
  554. -- 获取装备强化石
  555. function root:get_eqlevel(uid)
  556. return self:get_item_count(uid, gameConst.ITEM_ID.EQ_LEVEL)
  557. end
  558. -- 获取装备精练石
  559. function root:get_eqbreak(uid)
  560. return self:get_item_count(uid, gameConst.ITEM_ID.EQ_BREAK)
  561. end
  562. -- 获取试炼券
  563. function root:get_trial_ticket(uid)
  564. return self:get_item_count(uid, gameConst.ITEM_ID.TRIAL_TICKET)
  565. end
  566. -- 判断是否开启了指定的头像
  567. function root:is_open_album(uid, albumId)
  568. local conf = resAdapt:get_album_conf(albumId)
  569. if not conf then
  570. return false
  571. end
  572. local type = conf.type
  573. if type == 1 then
  574. -- 默认解锁的
  575. return true
  576. elseif type == 2 then
  577. -- 拥有英雄解锁的
  578. local heroId = conf.heroId
  579. return heroData:user_is_own_hero(uid, heroId)
  580. else
  581. -- 拥有物品解锁的
  582. local count = self:get_item_count(uid, albumId)
  583. return count > 0
  584. end
  585. end
  586. -- 判断是否开启了指定的头像框
  587. function root:is_open_frame(uid, frameId)
  588. local conf = resAdapt:get_frame_conf(frameId)
  589. if not conf then
  590. return false
  591. end
  592. local type = conf.type
  593. if type == 1 then
  594. -- 默认解锁的
  595. return true
  596. elseif type == 2 then
  597. -- 拥有英雄解锁的
  598. local heroId = conf.heroId
  599. return heroData:user_is_own_hero(uid, heroId)
  600. else
  601. -- 拥有物品解锁的
  602. local count = self:get_item_count(uid, frameId)
  603. return count > 0
  604. end
  605. end
  606. -- 判断是否开启了指定的称号
  607. function root:is_open_title(uid, titleId)
  608. local conf = resAdapt:get_title_conf(titleId)
  609. if not conf then
  610. return false
  611. end
  612. local type = conf.type
  613. if type == 1 then
  614. -- 默认解锁的
  615. return true
  616. elseif type == 2 then
  617. -- 拥有英雄解锁的
  618. local heroId = conf.heroId
  619. return heroData:user_is_own_hero(uid, heroId)
  620. else
  621. -- 拥有物品解锁的
  622. local count = self:get_item_count(uid, titleId)
  623. return count > 0
  624. end
  625. end
  626. -- 判断是否开启了指定的表情
  627. function root:is_open_face(uid, faceId)
  628. local conf = resAdapt:get_face_conf(faceId)
  629. if not conf then
  630. return false
  631. end
  632. local type = conf.type
  633. if type == 1 then
  634. -- 默认解锁的
  635. return true
  636. elseif type == 2 then
  637. -- 拥有英雄解锁的
  638. local heroId = conf.heroId
  639. return heroData:user_is_own_hero(uid, heroId)
  640. else
  641. -- 拥有物品解锁的
  642. local count = self:get_item_count(uid, faceId)
  643. return count > 0
  644. end
  645. end
  646. -- 判断是否开启了指定的看板娘
  647. function root:is_open_signboard(uid, signboardId)
  648. -- 拥有英雄解锁的
  649. local heroId = signboardId
  650. return heroData:user_is_own_hero(uid, heroId)
  651. end
  652. -- 已获得类型物品ID列表
  653. function root:get_own_type_item_id_list(uid, resType)
  654. if uid == nil or resType == nil then
  655. return
  656. end
  657. local resIdList = {}
  658. local itemInfo = moduleData:hget_json(uid, MODULE_NAME, "itemInfo") or {}
  659. for k, v in ipairs(itemInfo) do
  660. if resAdapt:is_type(v.id, resType) then
  661. table.insert(resIdList, v.id)
  662. end
  663. end
  664. return resIdList
  665. end
  666. -- 获取英雄拥有数量(包括觉醒)
  667. function root:get_hero_count(uid, heroId)
  668. if uid == nil or heroId == nil then
  669. return
  670. end
  671. -- 背包剩余数量
  672. local count = self:get_item_count(uid, heroId)
  673. -- 觉醒消耗数量
  674. local awakeCount = heroData:get_hero_awake_cost_count(uid, heroId)
  675. count = count + (awakeCount or 0)
  676. return count
  677. end
  678. ----------------------------------------
  679. -- 以下是私有方法
  680. ----------------------------------------
  681. -- 获取新的bid
  682. function root:_new_bid(uid)
  683. local lastBid = moduleData:hget_int(uid, MODULE_NAME, "lastBid") or 0
  684. lastBid = lastBid + 1
  685. moduleData:hset(uid, MODULE_NAME, "lastBid", lastBid)
  686. return lastBid
  687. end
  688. -- 最后初始化资源的时间戳
  689. function root:_get_last_init_time(uid)
  690. return moduleData:hget_int(uid, MODULE_NAME, "lastInitTime") or 0
  691. end
  692. function root:_set_last_init_time(uid, timestamp)
  693. moduleData:hset(uid, MODULE_NAME, "lastInitTime", timestamp)
  694. end
  695. function root:_AddExpirationTime(resConfing, uid)
  696. -- 计算失效时间/过期时间,expiration time ==> expTime;
  697. local expTime
  698. if resConfing then
  699. local curTime = timeUtil.now(uid)
  700. if resConfing.activeDays then
  701. expTime = curTime + (expTime or 0) + 86400 * tonumber(resConfing.activeDays)
  702. end
  703. if resConfing.termOfValidity then
  704. expTime = curTime + (expTime or 0) + tonumber(resConfing.termOfValidity)
  705. end
  706. end
  707. return expTime
  708. end
  709. function root:_AddItemByBid(uid, itemInfo, itemId, count, bid, resConfing, composite)
  710. local item
  711. if count > 0 and composite then
  712. local newBid = self:_new_bid(uid)
  713. item = {
  714. id = itemId,
  715. count = 0,
  716. bid = newBid
  717. }
  718. table.insert(itemInfo, item)
  719. else
  720. -- 不可叠加的物品只能通过bid来获取,但只有装备是通过bid来获取.
  721. item = table.key_find(itemInfo, "bid", bid)
  722. if not item and not bid then
  723. item = table.key_find(itemInfo, "id", itemId)
  724. end
  725. end
  726. -- 都没有获取到,就新建一个.
  727. if not item then
  728. local newBid = self:_new_bid(uid)
  729. item = {
  730. id = itemId,
  731. count = 0,
  732. bid = newBid
  733. }
  734. table.insert(itemInfo, item)
  735. end
  736. local oldCount = item.count or 0
  737. local newCount = oldCount + count
  738. newCount = math.max(newCount, 0)
  739. item.count = newCount
  740. item.expTime = self:_AddExpirationTime(resConfing, uid)
  741. return {{id = itemId, deltaCount = count, count = newCount, bid = item.bid}}
  742. end
  743. -- 更新单个资源
  744. function root:_update_item(uid, itemInfo, itemId, count, bid)
  745. local conf = resAdapt:get_item_conf(itemId)
  746. if not conf then
  747. log.error("更新资源异常,未找到[%s]资源配置", tostring(itemId))
  748. return
  749. end
  750. -- 处理特殊id的资源
  751. local otherModule = conf.otherModule
  752. local isSpecial = self:_update_special_item(uid, itemId, count, otherModule)
  753. if isSpecial then
  754. -- 特殊资源,不返回新数量
  755. return
  756. end
  757. local updateItems
  758. -- 必须执行
  759. if 1 == 1 then
  760. -- 资源可叠加
  761. if conf.enableComposite then
  762. updateItems = self:_AddItemByBid(uid, itemInfo, itemId, count, bid, conf)
  763. else
  764. updateItems = self:_AddItemByBid(uid, itemInfo, itemId, count, bid, conf, true)
  765. end
  766. else
  767. local itemType = conf.type
  768. if itemType == gameConst.RES_TYPE.COIN then
  769. updateItems = self:_update_coin_type_item(uid, itemInfo, itemId, count)
  770. elseif itemType == gameConst.RES_TYPE.EXP then
  771. updateItems = self:_update_exp_type_item(uid, itemInfo, itemId, count)
  772. elseif itemType == gameConst.RES_TYPE.CONSUME then
  773. updateItems = self:_update_consume_type_item(uid, itemInfo, itemId, count)
  774. elseif itemType == gameConst.RES_TYPE.LOTTERY then
  775. updateItems = self:_update_lottery_type_item(uid, itemInfo, itemId, count)
  776. elseif itemType == gameConst.RES_TYPE.TICKET then
  777. updateItems = self:_update_ticket_type_item(uid, itemInfo, itemId, count)
  778. elseif itemType == gameConst.RES_TYPE.PAY then
  779. updateItems = self:_update_pay_type_item(uid, itemInfo, itemId, count)
  780. elseif itemType == gameConst.RES_TYPE.ONHOOK then
  781. updateItems = self:_update_onhook_type_item(uid, itemInfo, itemId, count)
  782. elseif itemType == gameConst.RES_TYPE.HERO_CARD then
  783. updateItems = self:_update_hero_type_item(uid, itemInfo, itemId, count)
  784. elseif itemType == gameConst.RES_TYPE.RANDOM_HERO_CARD then
  785. updateItems = self:_update_random_hero_type_item(uid, itemInfo, itemId, count)
  786. elseif itemType == gameConst.RES_TYPE.OPTIONAL_HERO_CARD then
  787. updateItems = self:_update_optional_hero_type_item(uid, itemInfo, itemId, count)
  788. elseif itemType == gameConst.RES_TYPE.ALBUM then
  789. updateItems = self:_update_album_type_item(uid, itemInfo, itemId, count)
  790. elseif itemType == gameConst.RES_TYPE.FACE then
  791. updateItems = self:_update_face_type_item(uid, itemInfo, itemId, count)
  792. elseif itemType == gameConst.RES_TYPE.FRAME then
  793. updateItems = self:_update_frame_type_item(uid, itemInfo, itemId, count)
  794. elseif itemType == gameConst.RES_TYPE.TITLE then
  795. updateItems = self:_update_title_type_item(uid, itemInfo, itemId, count)
  796. elseif itemType == gameConst.RES_TYPE.EQUIP then
  797. updateItems = self:_update_unable_composit_type_item(uid, itemInfo, itemId, count, bid)
  798. elseif itemType == gameConst.RES_TYPE.EQUIP_BOX then
  799. updateItems = self:_update_enable_composit_type_item(uid, itemInfo, itemId, count)
  800. elseif itemType == gameConst.RES_TYPE.EQUIP_CHOICE_BOX then
  801. updateItems = self:_update_enable_composit_type_item(uid, itemInfo, itemId, count)
  802. elseif itemType == gameConst.RES_TYPE.EQUIP_BOX_DEBRIS then
  803. updateItems = self:_update_enable_composit_type_item(uid, itemInfo, itemId, count)
  804. else
  805. log.error("更新资源异常,未知资源类型 itemId:%s", itemId)
  806. end
  807. end
  808. return updateItems
  809. end
  810. -- 特殊资源更新
  811. function root:_update_special_item(uid, itemId, count, otherModule)
  812. log.debug("-- DEBUG INFO : uid, itemId, count, otherModule :[%s]", tostring({uid, itemId, count, otherModule}))
  813. if otherModule == "pass" then
  814. self:_update_special_item_pay_pass(uid, itemId, count)
  815. return true
  816. elseif otherModule == "adventure" then
  817. self:_update_special_item_adventure_active(uid, itemId, count)
  818. return true
  819. elseif itemId == gameConst.ITEM_ID.USER_EXP then
  820. self:_update_special_item_user_exp(uid, itemId, count)
  821. return true
  822. elseif itemId == gameConst.ITEM_ID.VIP_EXP then
  823. self:_update_special_item_vip_exp(uid, itemId, count)
  824. return true
  825. elseif itemId == gameConst.ITEM_ID.ARENA_CUP then
  826. self:_update_special_item_arena_cup(uid, itemId, count)
  827. return true
  828. elseif itemId == gameConst.ITEM_ID.PASS_EXP then
  829. self:_update_special_item_pass_exp(uid, itemId, count)
  830. return true
  831. elseif itemId == gameConst.ITEM_ID.ADVENTURE_POINTS then
  832. self:_update_special_item_adventure_points(uid, itemId, count)
  833. return true
  834. elseif itemId == gameConst.ITEM_ID.ARENA then
  835. local arenaData = require("data.arena")
  836. arenaData:active_vip_status(uid, itemId)
  837. return true
  838. elseif itemId == gameConst.ITEM_ID.PETBORN_AWARD then
  839. self:_update_special_item_pve_vip_status(uid, itemId, count)
  840. return true
  841. else
  842. return false
  843. end
  844. end
  845. function root:_update_special_item_user_exp(uid, itemId, count)
  846. local playerData = require("data.player")
  847. playerData:add_level_exp(uid, count)
  848. end
  849. function root:_update_special_item_vip_exp(uid, itemId, count)
  850. local playerData = require("data.player")
  851. playerData:add_vip_level_exp(uid, count)
  852. end
  853. function root:_update_special_item_arena_cup(uid, itemId, count)
  854. local arenaData = require("data.arena")
  855. arenaData:user_add_cups(uid, count, "gm")
  856. end
  857. -- 悬赏经验
  858. function root:_update_special_item_pass_exp(uid, itemId, count)
  859. local exploitData = require("data.exploit")
  860. exploitData:add_progress(uid, count)
  861. end
  862. function root:_update_special_item_pay_pass(uid, itemId, count)
  863. local exploitData = require("data.exploit")
  864. exploitData:active_vip_award(uid)
  865. end
  866. -- 冒险积分
  867. function root:_update_special_item_adventure_points(uid, itemId, count)
  868. local pveStarsData = require("data.pveStars")
  869. pveStarsData:add_adventure_points(uid, count)
  870. end
  871. -- 冒险经费付费奖励激活
  872. function root:_update_special_item_adventure_active(uid, itemId, count)
  873. local adventureData = require("data.adventure")
  874. adventureData:active_vip_award(uid, itemId)
  875. end
  876. function root:_update_special_item_pve_vip_status(uid, itemId, count)
  877. local THIS_MODULE_NAME = "tb_pve"
  878. local contractData = moduleData:hget_json(uid, THIS_MODULE_NAME, "contractData")
  879. contractData = contractData or {}
  880. contractData.statusVip = 1
  881. moduleData:hset(uid, THIS_MODULE_NAME, "contractData", contractData)
  882. local currSeasonId = moduleData:hget_int(uid, THIS_MODULE_NAME, "pveId") or 1
  883. -- 通知玩家
  884. local msg = {
  885. statusVip = 1,
  886. seasonId = currSeasonId
  887. }
  888. util_user:user_proto_notify(uid, "on_pve_vip_status", msg)
  889. local keyEvent = string.format("pve-active-vip-item")
  890. local cnt = string.format("%s", tostring(itemId))
  891. util_user:log_event(uid, keyEvent, cnt)
  892. end
  893. -- 通用资源更新。仅数量累计
  894. function root:_update_common_type_item(uid, itemInfo, itemId, deltaCount)
  895. local item = table.key_find(itemInfo, "id", itemId)
  896. if not item then
  897. local newBid = self:_new_bid(uid)
  898. item = {id = itemId, count = 0, bid = newBid}
  899. table.insert(itemInfo, item)
  900. end
  901. local bid = item.bid
  902. local oldCount = item.count or 0
  903. local newCount = oldCount + deltaCount
  904. newCount = math.max(newCount, 0)
  905. item.count = newCount
  906. return {id = itemId, deltaCount = deltaCount, count = newCount, bid = bid}
  907. end
  908. -- 通用资源更新。不可叠加的物品,仅数量累计
  909. function root:_update_ones_type_item(uid, itemInfo, itemId, deltaCount, bId)
  910. local item = table.key_find(itemInfo or {}, "bid", bId or 0)
  911. if not item then
  912. local newBid = self:_new_bid(uid)
  913. item = {id = itemId, count = 0, bid = newBid}
  914. table.insert(itemInfo, item)
  915. end
  916. local bid = item.bid
  917. local oldCount = item.count or 0
  918. local newCount = oldCount + deltaCount
  919. newCount = math.max(newCount, 0)
  920. item.count = newCount
  921. return {id = itemId, deltaCount = deltaCount, count = newCount, bid = bid}
  922. end
  923. -- 更新代币类资源
  924. function root:_update_coin_type_item(uid, itemInfo, itemId, count)
  925. local updateItem = self:_update_common_type_item(uid, itemInfo, itemId, count)
  926. return {updateItem}
  927. end
  928. -- 更新经验类资源
  929. function root:_update_exp_type_item(uid, itemInfo, itemId, count)
  930. log.error("更新资源异常:经验类资源都是特殊资源,不应该进入本逻辑")
  931. local updateItem = self:_update_common_type_item(uid, itemInfo, itemId, count)
  932. return {updateItem}
  933. end
  934. -- 更新消耗类类资源
  935. function root:_update_consume_type_item(uid, itemInfo, itemId, count)
  936. local updateItem = self:_update_common_type_item(uid, itemInfo, itemId, count)
  937. return {updateItem}
  938. end
  939. -- 更新祈愿券类资源
  940. function root:_update_lottery_type_item(uid, itemInfo, itemId, count)
  941. local updateItem = self:_update_common_type_item(uid, itemInfo, itemId, count)
  942. return {updateItem}
  943. end
  944. -- 更新门票类资源
  945. function root:_update_ticket_type_item(uid, itemInfo, itemId, count)
  946. local updateItem = self:_update_common_type_item(uid, itemInfo, itemId, count)
  947. return {updateItem}
  948. end
  949. -- 更新付费订阅道具类资源
  950. function root:_update_pay_type_item(uid, itemInfo, itemId, count)
  951. log.error("更新资源异常:付费订阅道具类资源都是特殊资源,不应该进入本逻辑")
  952. local updateItem = self:_update_common_type_item(uid, itemInfo, itemId, count)
  953. return {updateItem}
  954. end
  955. -- 更新挂机资源包类资源
  956. function root:_update_onhook_type_item(uid, itemInfo, itemId, count)
  957. local updateItem = self:_update_common_type_item(uid, itemInfo, itemId, count)
  958. return {updateItem}
  959. end
  960. -- 更新英雄卡类资源。
  961. function root:_update_hero_type_item(uid, itemInfo, itemId, count)
  962. local updateItem = self:_update_common_type_item(uid, itemInfo, itemId, count)
  963. return {updateItem}
  964. end
  965. -- 更新随机英雄卡类资源
  966. function root:_update_random_hero_type_item(uid, itemInfo, itemId, count)
  967. local updateItem = self:_update_common_type_item(uid, itemInfo, itemId, count)
  968. return {updateItem}
  969. end
  970. -- 更新自选英雄卡类资源
  971. function root:_update_optional_hero_type_item(uid, itemInfo, itemId, count)
  972. local updateItem = self:_update_common_type_item(uid, itemInfo, itemId, count)
  973. return {updateItem}
  974. end
  975. -- 更新头像类资源
  976. function root:_update_album_type_item(uid, itemInfo, itemId, count)
  977. local updateItem = self:_update_common_type_item(uid, itemInfo, itemId, count)
  978. return {updateItem}
  979. end
  980. -- 更新表情类资源
  981. function root:_update_face_type_item(uid, itemInfo, itemId, count)
  982. local updateItem = self:_update_common_type_item(uid, itemInfo, itemId, count)
  983. return {updateItem}
  984. end
  985. -- 更新头像框类资源
  986. function root:_update_frame_type_item(uid, itemInfo, itemId, count)
  987. local updateItem = self:_update_common_type_item(uid, itemInfo, itemId, count)
  988. return {updateItem}
  989. end
  990. -- 更新称号类资源
  991. function root:_update_title_type_item(uid, itemInfo, itemId, count)
  992. local updateItem = self:_update_common_type_item(uid, itemInfo, itemId, count)
  993. return {updateItem}
  994. end
  995. -- 可叠加的物品
  996. function root:_update_enable_composit_type_item(uid, itemInfo, itemId, count)
  997. local updateItem = self:_update_common_type_item(uid, itemInfo, itemId, count)
  998. return {updateItem}
  999. end
  1000. -- 不可叠加的物品
  1001. function root:_update_unable_composit_type_item(uid, itemInfo, itemId, count, bid)
  1002. local updateItem = self:_update_ones_type_item(uid, itemInfo, itemId, count, bid)
  1003. return {updateItem}
  1004. end
  1005. -- 检查英雄列表。检查自动激活、自动溢出兑换
  1006. function root:_check_hero_item(uid, updateItems, reason)
  1007. local itemIdMap = {}
  1008. for _, v in ipairs(updateItems) do
  1009. if v.deltaCount and v.deltaCount > 0 then
  1010. if resAdapt:is_type(v.id, gameConst.RES_TYPE.HERO_CARD) then
  1011. itemIdMap[v.id] = true
  1012. end
  1013. end
  1014. end
  1015. for k, _ in pairs(itemIdMap) do
  1016. self:_check_hero_active(uid, k, reason)
  1017. self:_check_hero_overflow(uid, k, reason)
  1018. end
  1019. end
  1020. -- 检查英雄自动激活
  1021. function root:_check_hero_active(uid, itemId, srcReason)
  1022. local conf = resAdapt:get_hero_conf(itemId)
  1023. if not conf then
  1024. log.error("检查英雄激活异常:没找到对应英雄配置")
  1025. return
  1026. end
  1027. local count = self:get_item_count(uid, itemId)
  1028. if count <= 0 then
  1029. return
  1030. end
  1031. local heroId = conf.heroId
  1032. -- 是否拥有该英雄
  1033. local hasHero = heroData:user_is_own_hero(uid, heroId)
  1034. if not hasHero then
  1035. local ok = heroData:user_active_hero(uid, heroId)
  1036. if ok then
  1037. local reason = gameConst.RES_REASON_PRE.AUTO_HERO_ACTIVE
  1038. local cardItem = {id = itemId, count = 1}
  1039. self:consume_items(uid, {cardItem}, reason)
  1040. self:_on_res_auto_hero_active(uid, cardItem, heroId, srcReason)
  1041. end
  1042. end
  1043. end
  1044. -- 检查英雄自动激活
  1045. function root:_check_hero_overflow(uid, itemId, srcReason)
  1046. local conf = resAdapt:get_hero_conf(itemId)
  1047. if not conf then
  1048. log.error("检查英雄溢出异常:没找到对应英雄配置")
  1049. return
  1050. end
  1051. local count = self:get_item_count(uid, itemId)
  1052. if count <= 0 then
  1053. return
  1054. end
  1055. local heroId = conf.heroId
  1056. local overflowItems = conf.overflowItems
  1057. -- 英雄觉醒满还需要的卡牌数量
  1058. local needCardCount = heroData:get_hero_full_awake_card_count(uid, heroId)
  1059. local overflowCount = count - needCardCount
  1060. if overflowCount <= 0 then
  1061. return
  1062. end
  1063. local reason = gameConst.RES_REASON_PRE.AUTO_EXCHANGE
  1064. local fromItems = {{id = itemId, count = overflowCount}}
  1065. local toItems = {}
  1066. for _, v in ipairs(overflowItems) do
  1067. local count = v.count * overflowCount
  1068. table.insert(toItems, {id = v.id, count = count})
  1069. end
  1070. self:consume_items(uid, fromItems, reason)
  1071. self:add_items(uid, toItems, reason)
  1072. self:_on_res_auto_exchange(uid, fromItems, toItems, srcReason)
  1073. end
  1074. -- 推送资源变化
  1075. function root:_on_res_update(uid, updateItems, reason)
  1076. local msg = {
  1077. reason = reason,
  1078. items = updateItems
  1079. }
  1080. util_user:user_proto_notify(uid, "on_res_update", msg)
  1081. end
  1082. -- 推送资源自动激活英雄
  1083. function root:_on_res_auto_hero_active(uid, cardItem, heroId, reason)
  1084. local msg = {
  1085. reason = reason,
  1086. cardItem = cardItem,
  1087. heroId = heroId
  1088. }
  1089. util_user:user_proto_notify(uid, "on_res_auto_hero_active", msg)
  1090. end
  1091. -- 推送资源自动兑换
  1092. function root:_on_res_auto_exchange(uid, fromItems, toItems, reason)
  1093. local msg = {
  1094. reason = reason,
  1095. fromItems = fromItems,
  1096. toItems = toItems
  1097. }
  1098. util_user:user_proto_notify(uid, "on_res_auto_exchange", msg)
  1099. end
  1100. -- 资源变化事件
  1101. function root:_dispatch_event(uid, updateItems)
  1102. for _, v in ipairs(updateItems) do
  1103. local conf = resAdapt:get_item_conf(v.id) or {}
  1104. local evtParams = {
  1105. times = 1,
  1106. id = v.id,
  1107. count = math.abs(v.deltaCount),
  1108. add = v.deltaCount,
  1109. type = conf.type,
  1110. bId = v.bid,
  1111. totalCount = v.count
  1112. }
  1113. util_user:user_dispatch_event(uid, gameConst.EVENT_ID.RES, evtParams)
  1114. end
  1115. end
  1116. -- 资源变化事件
  1117. function root:_log_resource(uid, updateItems, reason)
  1118. for _, v in ipairs(updateItems) do
  1119. util_user:log_resource(uid, reason, v.id, v.deltaCount, v.count)
  1120. end
  1121. end
  1122. return root