浏览代码

新增开盲盒

neo 1 年之前
父节点
当前提交
a69b434fcf
共有 3 个文件被更改,包括 203 次插入0 次删除
  1. 35 0
      dev/adapt/boxAdapt.lua
  2. 97 0
      dev/utils/util_box.lua
  3. 71 0
      nodes/game/interface/box.lua

+ 35 - 0
dev/adapt/boxAdapt.lua

@@ -0,0 +1,35 @@
1
+-- 箱子配置
2
+local baseAdapt = require "base.baseAdapt"
3
+
4
+local root = {}
5
+
6
+----------------------------------------
7
+-- 盲盒
8
+----------------------------------------
9
+-- 箱子物品列表
10
+function root:blind_get_box_item_list(boxId)
11
+    local confList = {}
12
+    local conf = baseAdapt:getConfig("BlindBoxAwardConfig")
13
+    for _, v in ipairs(conf) do
14
+        if v.boxId == boxId then
15
+            table.insert(confList, table.copy(v))
16
+        end
17
+    end
18
+    return confList
19
+end
20
+
21
+-- 获取箱子信息
22
+function root:blind_get_box_key_info(boxId, key)
23
+    local conf = baseAdapt:getConfig("BlindBoxConfig")
24
+    for _, v in ipairs(conf) do
25
+        if v.boxId == boxId then
26
+            if is_empty(key) then
27
+                return v
28
+            else
29
+                return v[key]
30
+            end
31
+        end
32
+    end
33
+end
34
+
35
+return root

+ 97 - 0
dev/utils/util_box.lua

@@ -0,0 +1,97 @@
1
+--[[
2
+Descripttion:玩家事件
3
+version:
4
+Author: Neo,Huang
5
+Date: 2022-07-05 19:59:16
6
+LastEditors: Neo,Huang
7
+LastEditTime: 2022-07-05 20:08:31
8
+--]]
9
+local lib_game_redis = require("lib_game_redis")
10
+local timeUtil = require("utils.timeUtil")
11
+local redisUtil = require("utils.redisUtil")
12
+local util_player = require("utils.util_player")
13
+
14
+local baseAdapt = require("base.baseAdapt")
15
+local boxAdapt = require("adapt.boxAdapt")
16
+local resAdapt = require("adapt.resAdapt")
17
+
18
+local MODULE_NAME = "box"
19
+
20
+local root = {}
21
+
22
+local function _get_box_key(boxId)
23
+    return string.format("%s:%s", MODULE_NAME, tostring(boxId))
24
+end
25
+
26
+-- 获取开箱物品
27
+function root:get_box_drop_item_and_count(boxId)
28
+    local confList = boxAdapt:blind_get_box_item_list(boxId)
29
+    if is_empty(confList) then
30
+        return
31
+    end
32
+    -- 更新权重
33
+    for _, v in ipairs(confList) do
34
+        v.weight = 0
35
+        local price = resAdapt:get_item_price(v.itemId)
36
+        if not is_empty(price) then
37
+            v.weight = math.floor(v.weightByPrice / price)
38
+        end
39
+    end
40
+    local index = random_list_by_weight(confList)
41
+    return confList[index].itemId, confList[index].count
42
+end
43
+
44
+-- 新增开箱记录
45
+function root:add_record(uid, boxId, itemId, count)
46
+    if is_empty(uid) or is_empty(boxId) or is_empty(itemId) then
47
+        return false
48
+    end
49
+    local currTime = timeUtil.now(uid)
50
+    local key = _get_box_key(boxId)
51
+    local dropList = redisUtil.hget_json(key, "dropList")
52
+    table.insert(dropList, {uid = uid, time = currTime, itemId = itemId, count = count})
53
+    -- 限制数量
54
+    if #dropList > 20 then
55
+        table.remove(dropList, 1)
56
+    end
57
+    redisUtil.hset(key, "dropList", dropList)
58
+    return true
59
+end
60
+
61
+-- 打包箱子信息
62
+function root:pack_box_info(boxId)
63
+    if is_empty(boxId) then
64
+        return
65
+    end
66
+    local key = _get_box_key(boxId)
67
+    local dropList = redisUtil.hget_json(key, "dropList")
68
+    for _, v in ipairs(dropList) do
69
+        v.playerInfo = util_player:get_base_info(v.uid)
70
+    end
71
+    local info = {
72
+        id = boxId,
73
+        dropList = dropList
74
+    }
75
+    return info
76
+end
77
+-- 打包所有箱子信息
78
+function root:pack_box_info_list(boxId)
79
+    local boxInfoList = {}
80
+    if is_empty(boxId) then
81
+        local conf = baseAdapt:getConfig("BlindBoxConfig")
82
+        for _, v in ipairs(conf) do
83
+            local info = self:pack_box_info(v.boxId)
84
+            if not is_empty(info) then
85
+                table.insert(boxInfoList, info)
86
+            end
87
+        end
88
+    else
89
+        local info = self:pack_box_info(boxId)
90
+        if not is_empty(info) then
91
+            table.insert(boxInfoList, info)
92
+        end
93
+    end
94
+    return boxInfoList
95
+end
96
+
97
+return root

+ 71 - 0
nodes/game/interface/box.lua

@@ -0,0 +1,71 @@
1
+--[[
2
+Descripttion:接口 - 开箱子
3
+version:
4
+Author: Neo,Huang
5
+Date: 2022-07-05 17:32:01
6
+LastEditors: Neo,Huang
7
+LastEditTime: 2022-07-05 20:29:29
8
+--]]
9
+local code = require("code")
10
+local util_box = require("utils.util_box")
11
+local gameConst = require("const.gameConst")
12
+local itemUtil = require("utils.itemUtil")
13
+
14
+local boxAdapt = require("adapt.boxAdapt")
15
+
16
+local bagData = require("data.bag")
17
+
18
+local root = {}
19
+
20
+-- 盲盒 - 模块信息
21
+function root:box_blind_get_info(role, msg)
22
+    local id = msg.id
23
+    local ret = {
24
+        boxInfoList = util_box:pack_box_info_list(id)
25
+    }
26
+    return code.OK, ret
27
+end
28
+
29
+-- 盲盒 -开盲盒
30
+function root:box_blind_open(role, msg)
31
+    local id = msg.id
32
+    local count = msg.count or 1
33
+    if is_empty(id) then
34
+        return code.PARAMTER_ERROR
35
+    end
36
+    local uid = role.uid
37
+    local conf = boxAdapt:blind_get_box_key_info(id)
38
+    local items = nil
39
+    for i = 1, count do
40
+        -- 掉落物品
41
+        local itemId, count = util_box:get_box_drop_item_and_count(id)
42
+        if not is_empty(itemId) and not is_empty(count) then
43
+            local costItems = {{id = gameConst.ITEM_ID.DIAMOND, count = conf.diamondPrice}}
44
+            if not bagData:is_enough(uid, costItems) then
45
+                -- 绑金不足,使用金币
46
+                costItems = {{id = gameConst.ITEM_ID.GOLD, count = conf.goldPrice}}
47
+                if not bagData:is_enough(uid, costItems) then
48
+                    break
49
+                end
50
+            end
51
+            -- 消耗
52
+            local eventId = string.format("box-blind-%s", tostring(id))
53
+            bagData:consume_items(uid, costItems, eventId)
54
+            -- 获取物品
55
+            local _items = {{id = itemId, count = count}}
56
+            bagData:add_items(uid, _items, eventId)
57
+
58
+            items = itemUtil.items_merge(items, _items)
59
+        end
60
+    end
61
+    if is_empty(items) then
62
+        return code.BAG.ITEM_NOT_ENOUGH
63
+    end
64
+    local ret = {
65
+        items = items,
66
+        boxInfo = util_box:pack_box_info(id)
67
+    }
68
+    return code.OK, ret
69
+end
70
+
71
+return root