util_box.lua 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. --[[
  2. Descripttion:开箱子
  3. version:
  4. Author: Neo,Huang
  5. Date: 2023-11-15 22:34:35
  6. LastEditors: Neo,Huang
  7. LastEditTime: 2023-11-15 22:41:18
  8. --]]
  9. local timeUtil = require("utils.timeUtil")
  10. local redisUtil = require("utils.redisUtil")
  11. local util_player = require("utils.util_player")
  12. local mysqlUtil = require("utils.mysqlUtil")
  13. local lib_game_redis = require("lib_game_redis")
  14. local baseAdapt = require("base.baseAdapt")
  15. local boxAdapt = require("adapt.boxAdapt")
  16. local resAdapt = require("adapt.resAdapt")
  17. local MODULE_NAME = "box"
  18. local root = {}
  19. ----------------------------------------
  20. -- 盲盒
  21. ----------------------------------------
  22. local function _get_box_key(boxId)
  23. return string.format("%s:%s", MODULE_NAME, tostring(boxId))
  24. end
  25. -- 获取开箱物品
  26. function root:get_box_drop_item_and_count(boxId)
  27. local confList = boxAdapt:blind_get_box_item_list(boxId)
  28. if is_empty(confList) then
  29. return
  30. end
  31. -- 更新权重
  32. for _, v in ipairs(confList) do
  33. v.weight = 0
  34. local price = resAdapt:get_item_price(v.itemId)
  35. if not is_empty(price) then
  36. v.weight = math.floor(v.weightByPrice / price)
  37. end
  38. end
  39. local index = random_list_by_weight(confList)
  40. return confList[index].itemId, confList[index].count
  41. end
  42. -- 新增开箱记录
  43. function root:add_record(uid, boxId, itemId, count)
  44. if is_empty(uid) or is_empty(boxId) or is_empty(itemId) then
  45. return false
  46. end
  47. local currTime = timeUtil.now(uid)
  48. local key = _get_box_key(boxId)
  49. local dropList = redisUtil.hget_json(key, "dropList")
  50. table.insert(dropList, {uid = uid, time = currTime, itemId = itemId, count = count})
  51. -- 限制数量
  52. if #dropList > 20 then
  53. table.remove(dropList, 1)
  54. end
  55. redisUtil.hset(key, "dropList", dropList)
  56. return true
  57. end
  58. -- 打包箱子信息
  59. function root:pack_box_info(boxId)
  60. if is_empty(boxId) then
  61. return
  62. end
  63. local key = _get_box_key(boxId)
  64. local dropList = redisUtil.hget_json(key, "dropList")
  65. for _, v in ipairs(dropList) do
  66. v.playerInfo = util_player:get_base_info(v.uid)
  67. end
  68. local info = {
  69. id = boxId,
  70. dropList = dropList
  71. }
  72. return info
  73. end
  74. -- 打包所有箱子信息
  75. function root:pack_box_info_list(boxId)
  76. local boxInfoList = {}
  77. if is_empty(boxId) then
  78. local conf = baseAdapt:getConfig("BlindBoxConfig")
  79. for _, v in ipairs(conf) do
  80. local info = self:pack_box_info(v.boxId)
  81. if not is_empty(info) then
  82. table.insert(boxInfoList, info)
  83. end
  84. end
  85. else
  86. local info = self:pack_box_info(boxId)
  87. if not is_empty(info) then
  88. table.insert(boxInfoList, info)
  89. end
  90. end
  91. return boxInfoList
  92. end
  93. ----------------------------------------
  94. -- 追梦
  95. ----------------------------------------
  96. local function _get_dream_key()
  97. return "box:dream"
  98. end
  99. local function _get_dream_odds_tims_key(ti)
  100. return string.format("box:dream:odds-times:%s", timeUtil.toDate(ti))
  101. end
  102. -- 新增记录
  103. function root:dream_add_record(uid, itemId, odds, dropItem, isWin)
  104. if is_empty(uid) or is_empty(itemId) or is_empty(odds) or is_empty(dropItem) then
  105. return false
  106. end
  107. local price = resAdapt:get_item_price(itemId)
  108. if is_empty(price) then
  109. return false
  110. end
  111. local dropTime = timeUtil.now()
  112. local expireTime = dropItem + timeUtil.SEC_PER_DAY * 365
  113. local sql =
  114. string.format(
  115. "INSERT INTO `mdl_dream` (`uid`,`itemId`,`dropItem`,`price`,`odds`,`isWin`,`dropTime`,`expireTime`)" ..
  116. " VALUES (%s,%s,'%s',%s,%s,%s,%s,%s); ",
  117. tostring(uid),
  118. tostring(itemId),
  119. cjson_encode(dropItem),
  120. tostring(price),
  121. tostring(odds),
  122. tostring(isWin and 1 or 0),
  123. tostring(dropTime),
  124. tostring(expireTime)
  125. )
  126. return mysqlUtil:insert(sql)
  127. end
  128. -- 新增概率次数
  129. function root:dream_add_odds_times(odds, isWin)
  130. if is_empty(odds) then
  131. return false
  132. end
  133. local key = _get_dream_odds_tims_key()
  134. local subKey = string.format("odds:%s:%s", tostring(odds), tostring(isWin and 1 or 0))
  135. local times = redisUtil.hget_int(key, subKey)
  136. times = times + 1
  137. redisUtil.hset(key, subKey, times)
  138. end
  139. -- 获取精彩瞬间记录
  140. function root:dream_get_brilliants()
  141. local minPrice = 100000
  142. local count = 20
  143. local sql =
  144. string.format(
  145. "SELECT * FROM `mdl_dream` WHERE `price`>=%s AND `isWin`=1 ORDER BY dropTime DESC limit %s",
  146. tostring(minPrice),
  147. tostring(count)
  148. )
  149. local ok, ret = mysqlUtil:select(sql)
  150. if not ok or is_empty(ret) then
  151. return
  152. end
  153. local list = {}
  154. for _, v in ipairs(ret) do
  155. local uid = tonumber(v.uid)
  156. local info = {
  157. playerInfo = util_player:get_base_info(uid),
  158. itemId = tonumber(v.itemId),
  159. dropItem = cjson_decode(v.dropItem),
  160. price = tonumber(v.price),
  161. odds = tonumber(v.odds),
  162. dropTime = tonumber(v.dropTime)
  163. }
  164. table.insert(list, info)
  165. end
  166. return list
  167. end
  168. -- 掉落记录
  169. function root:dream_get_records()
  170. local count = 20
  171. local sql = string.format("SELECT * FROM `mdl_dream` ORDER BY dropTime DESC limit %s", tostring(count))
  172. local ok, ret = mysqlUtil:select(sql)
  173. if not ok or is_empty(ret) then
  174. return
  175. end
  176. local list = {}
  177. for _, v in ipairs(ret) do
  178. local uid = tonumber(v.uid)
  179. local info = {
  180. playerInfo = util_player:get_base_info(uid),
  181. itemId = tonumber(v.itemId),
  182. dropItem = cjson_decode(v.dropItem),
  183. price = tonumber(v.price),
  184. odds = tonumber(v.odds),
  185. dropTime = tonumber(v.dropTime)
  186. }
  187. table.insert(list, info)
  188. end
  189. return list
  190. end
  191. -- 次数统计
  192. function root:dream_get_statement()
  193. local days = 3
  194. local mapOddsWinTims = {}
  195. local mapOddsLoseTims = {}
  196. local currTime = timeUtil.now()
  197. for i = 0, days - 2 do
  198. local ti = currTime - timeUtil.SEC_PER_DAY * i
  199. local key = _get_dream_odds_tims_key(ti)
  200. for odds = 5, 85 do
  201. -- 中奖
  202. local subKey = string.format("odds:%s:1", tostring(odds))
  203. local times = redisUtil.hget_int(key, subKey)
  204. mapOddsWinTims[odds] = (mapOddsWinTims[odds] or 0) + times
  205. -- 不中奖
  206. subKey = string.format("odds:%s:0", tostring(odds))
  207. times = redisUtil.hget_int(key, subKey)
  208. mapOddsLoseTims[odds] = (mapOddsLoseTims[odds] or 0) + times
  209. end
  210. end
  211. local oddsTimesList = {}
  212. for odds = 5, 85 do
  213. if not is_empty(mapOddsWinTims[odds]) or not is_empty(mapOddsLoseTims[odds]) then
  214. local info = {
  215. odds = odds,
  216. winTimes = mapOddsWinTims[odds] or 0,
  217. loseTimes = mapOddsLoseTims[odds] or 0
  218. }
  219. table.insert(oddsTimesList, info)
  220. end
  221. end
  222. return oddsTimesList
  223. end
  224. -- 删除天统计
  225. function root:dream_del_day_statement(ti)
  226. if is_empty(ti) then
  227. return
  228. end
  229. local key = _get_dream_odds_tims_key(ti)
  230. lib_game_redis:del(key)
  231. end
  232. return root