Browse Source

新增道具分解

neo 1 year ago
parent
commit
fbb0e2058d
5 changed files with 81 additions and 26 deletions
  1. 6 0
      dev/adapt/resAdapt.lua
  2. 6 0
      dev/const/gameConst.lua
  3. 26 26
      dev/data/bag.lua
  4. 33 0
      dev/modules/bag.lua
  5. 10 0
      nodes/game/interface/bag.lua

+ 6 - 0
dev/adapt/resAdapt.lua

@@ -38,4 +38,10 @@ function root:is_type(id, resType)
38 38
     return self:get_item_type(id) == resType
39 39
 end
40 40
 
41
+-- 获取道具价值
42
+function root:get_item_price(id)
43
+    local item = self:get_item_conf(id)
44
+    return item and item.price
45
+end
46
+
41 47
 return root

+ 6 - 0
dev/const/gameConst.lua

@@ -21,6 +21,12 @@ root.EVENT_ID = {
21 21
     PAY = 102 -- 支付
22 22
 }
23 23
 
24
+-- 常用资源物品ID
25
+root.ITEM_ID = {
26
+    GOLD = 101, -- 金币
27
+    DIAMOND = 102 -- 宝石
28
+}
29
+
24 30
 -- 战斗类型
25 31
 root.BATTLE_TYPE = {}
26 32
 

+ 26 - 26
dev/data/bag.lua

@@ -38,12 +38,12 @@ end
38 38
 -- 请求获取背包信息
39 39
 function root:get_info(uid)
40 40
     log.info("get_info uid:%s", uid)
41
-    local itemInfo = moduleData:hget_json(uid, MODULE_NAME, "itemInfo") or {}
41
+    local itemList = moduleData:hget_json(uid, MODULE_NAME, "itemList") or {}
42 42
     local isUpdate = false
43 43
 
44 44
     local curTime = timeUtil.now(uid)
45
-    for i = #(itemInfo or {}), 1, -1 do
46
-        local item = itemInfo[i]
45
+    for i = #(itemList or {}), 1, -1 do
46
+        local item = itemList[i]
47 47
         if item.expTime and curTime > item.expTime then
48 48
             -- 已经过期
49 49
             isUpdate = true
@@ -52,16 +52,16 @@ function root:get_info(uid)
52 52
 
53 53
         if item.count == 0 then
54 54
             isUpdate = true
55
-            table.remove(itemInfo, i)
55
+            table.remove(itemList, i)
56 56
         end
57 57
     end
58 58
 
59 59
     if isUpdate then
60 60
         -- 存储
61
-        moduleData:hset(uid, MODULE_NAME, "itemInfo", itemInfo)
61
+        moduleData:hset(uid, MODULE_NAME, "itemList", itemList)
62 62
     end
63 63
 
64
-    return itemInfo
64
+    return itemList
65 65
 end
66 66
 
67 67
 --[[ 请求增减资源列表
@@ -70,18 +70,18 @@ end
70 70
 function root:update_items(uid, items, reason)
71 71
     -- log.info("update_items uid:%s items:%s reason:%s", uid, tostring(items), tostring(reason))
72 72
 
73
-    local itemInfo = moduleData:hget_json(uid, MODULE_NAME, "itemInfo") or {}
73
+    local itemList = moduleData:hget_json(uid, MODULE_NAME, "itemList") or {}
74 74
 
75 75
     local list = {}
76 76
     for _, v in ipairs(items) do
77
-        local updateItems = self:_update_item(uid, itemInfo, v.id, v.count, v.bid)
77
+        local updateItems = self:_update_item(uid, itemList, v.id, v.count, v.bid)
78 78
         if updateItems then
79 79
             table.arry_merge(list, updateItems)
80 80
         end
81 81
     end
82 82
 
83 83
     -- 存储
84
-    moduleData:hset(uid, MODULE_NAME, "itemInfo", itemInfo)
84
+    moduleData:hset(uid, MODULE_NAME, "itemList", itemList)
85 85
     -- 推送
86 86
     self:_on_res_update(uid, list, reason)
87 87
     -- 事件
@@ -104,11 +104,11 @@ function root:is_enough(uid, items)
104 104
         end
105 105
     end
106 106
 
107
-    local itemInfo = moduleData:hget_json(uid, MODULE_NAME, "itemInfo") or {}
107
+    local itemList = moduleData:hget_json(uid, MODULE_NAME, "itemList") or {}
108 108
 
109 109
     local ret = true
110 110
     for k, v in pairs(tempMap) do
111
-        local item = table.key_find(itemInfo, "id", k)
111
+        local item = table.key_find(itemList, "id", k)
112 112
         if not item or not item.count or item.count < v then
113 113
             ret = false
114 114
             break
@@ -129,11 +129,11 @@ function root:is_enough_by_bid(uid, items)
129 129
         end
130 130
     end
131 131
 
132
-    local itemInfo = moduleData:hget_json(uid, MODULE_NAME, "itemInfo") or {}
132
+    local itemList = moduleData:hget_json(uid, MODULE_NAME, "itemList") or {}
133 133
 
134 134
     local ret = true
135 135
     for k, v in pairs(tempMap) do
136
-        local item = table.key_find(itemInfo, "bid", k)
136
+        local item = table.key_find(itemList, "bid", k)
137 137
 
138 138
         if not item or not item.count or item.count < v then
139 139
             ret = false
@@ -165,8 +165,8 @@ end
165 165
 -- 获取资源数量
166 166
 function root:get_item_count(uid, itemId)
167 167
     -- log.info("get_item_count uid:%s itemId:%s", uid, itemId)
168
-    local itemInfo = moduleData:hget_json(uid, MODULE_NAME, "itemInfo") or {}
169
-    local item = table.key_find(itemInfo, "id", itemId) or {}
168
+    local itemList = moduleData:hget_json(uid, MODULE_NAME, "itemList") or {}
169
+    local item = table.key_find(itemList, "id", itemId) or {}
170 170
 
171 171
     return item.count or 0
172 172
 end
@@ -174,8 +174,8 @@ end
174 174
 -- 获取资源数量
175 175
 function root:get_item_by_bid(uid, bid)
176 176
     -- log.info("get_item_by_bid uid:%s bid:%s", uid, bid)
177
-    local itemInfo = moduleData:hget_json(uid, MODULE_NAME, "itemInfo") or {}
178
-    local item = table.key_find(itemInfo, "bid", bid) or {}
177
+    local itemList = moduleData:hget_json(uid, MODULE_NAME, "itemList") or {}
178
+    local item = table.key_find(itemList, "bid", bid) or {}
179 179
 
180 180
     return item
181 181
 end
@@ -186,8 +186,8 @@ function root:get_own_type_item_id_list(uid, resType)
186 186
         return
187 187
     end
188 188
     local resIdList = {}
189
-    local itemInfo = moduleData:hget_json(uid, MODULE_NAME, "itemInfo") or {}
190
-    for k, v in ipairs(itemInfo) do
189
+    local itemList = moduleData:hget_json(uid, MODULE_NAME, "itemList") or {}
190
+    for k, v in ipairs(itemList) do
191 191
         if resAdapt:is_type(v.id, resType) then
192 192
             table.insert(resIdList, v.id)
193 193
         end
@@ -233,7 +233,7 @@ function root:_AddExpirationTime(resConfing, uid)
233 233
     return expTime
234 234
 end
235 235
 
236
-function root:_AddItemByBid(uid, itemInfo, itemId, count, bid, resConfing, composite)
236
+function root:_AddItemByBid(uid, itemList, itemId, count, bid, resConfing, composite)
237 237
     local item
238 238
     if count > 0 and composite then
239 239
         local newBid = self:_new_bid(uid)
@@ -242,12 +242,12 @@ function root:_AddItemByBid(uid, itemInfo, itemId, count, bid, resConfing, compo
242 242
             count = 0,
243 243
             bid = newBid
244 244
         }
245
-        table.insert(itemInfo, item)
245
+        table.insert(itemList, item)
246 246
     else
247 247
         -- 不可叠加的物品只能通过bid来获取,但只有装备是通过bid来获取.
248
-        item = table.key_find(itemInfo, "bid", bid)
248
+        item = table.key_find(itemList, "bid", bid)
249 249
         if not item and not bid then
250
-            item = table.key_find(itemInfo, "id", itemId)
250
+            item = table.key_find(itemList, "id", itemId)
251 251
         end
252 252
     end
253 253
 
@@ -259,7 +259,7 @@ function root:_AddItemByBid(uid, itemInfo, itemId, count, bid, resConfing, compo
259 259
             count = 0,
260 260
             bid = newBid
261 261
         }
262
-        table.insert(itemInfo, item)
262
+        table.insert(itemList, item)
263 263
     end
264 264
 
265 265
     local oldCount = item.count or 0
@@ -271,14 +271,14 @@ function root:_AddItemByBid(uid, itemInfo, itemId, count, bid, resConfing, compo
271 271
 end
272 272
 
273 273
 -- 更新单个资源
274
-function root:_update_item(uid, itemInfo, itemId, count, bid)
274
+function root:_update_item(uid, itemList, itemId, count, bid)
275 275
     local conf = resAdapt:get_item_conf(itemId)
276 276
     if not conf then
277 277
         log.error("更新资源异常,未找到[%s]资源配置", tostring(itemId))
278 278
         return
279 279
     end
280 280
 
281
-    return self:_AddItemByBid(uid, itemInfo, itemId, count, bid, conf, conf.enableComposite)
281
+    return self:_AddItemByBid(uid, itemList, itemId, count, bid, conf, conf.enableComposite)
282 282
 end
283 283
 
284 284
 -- 推送资源变化

+ 33 - 0
dev/modules/bag.lua

@@ -1,3 +1,6 @@
1
+local bagData = require("data.bag")
2
+local gameConst = require("const.gameConst")
3
+
1 4
 local root = class("moduleBag", require("base.baseModule"))
2 5
 
3 6
 function root:ctor(uid)
@@ -23,4 +26,34 @@ function root:itf_get_info(role, msg)
23 26
     return code.OK, {items = items}
24 27
 end
25 28
 
29
+-- 分解
30
+function root:itf_exchange(role, msg)
31
+    local items = msg.items
32
+    if is_empty(items) then
33
+        return code.PARAMTER_ERROR
34
+    end
35
+    local goldCount = 0
36
+    for _, v in ipairs(items) do
37
+        local price = resAdapt:get_item_price(v.id)
38
+        if not is_empty(price) and not is_empty(v.count) then
39
+            -- 可分解
40
+            local costItems = {{id = v.id, count = v.count}}
41
+            if bagData:is_enough(self.uid, costItems) then
42
+                local addCount = price * v.count
43
+                goldCount = goldCount + addCount
44
+                bagData:consume_items(self.uid, costItems, "item-exchange")
45
+
46
+                local eventId = string.format("item-exchange-%s", tostring(v.id))
47
+                bagData:add_items(self.uid, {{id = gameConst.ITEM_ID.GOLD, count = addCount}}, eventId)
48
+            end
49
+        end
50
+    end
51
+    return code.OK, {goldCount = goldCount}
52
+end
53
+
54
+-- 提取
55
+function root:itf_draw(role, msg)
56
+    return code.OK
57
+end
58
+
26 59
 return root

+ 10 - 0
nodes/game/interface/bag.lua

@@ -15,4 +15,14 @@ function root:bag_get_info(role, msg)
15 15
     return role.bag:itf_get_info(role, msg)
16 16
 end
17 17
 
18
+-- 分解
19
+function root:bag_exchange(role, msg)
20
+    return role.bag:itf_exchange(role, msg)
21
+end
22
+
23
+-- 提取
24
+function root:bag_draw(role, msg)
25
+    return role.bag:itf_draw(role, msg)
26
+end
27
+
18 28
 return root