bag.lua 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 23:54:11
  8. --]]
  9. local code = require("code")
  10. local resAdapt = require("adapt.resAdapt")
  11. local bagData = require("data.bag")
  12. local gameConst = require("const.gameConst")
  13. local root = class("bag", require("base.baseModule"))
  14. function root:ctor(uid)
  15. root.super.ctor(self, uid, "bag", "uid", true)
  16. self.uid = uid
  17. end
  18. function root:mysql_get_init_columns()
  19. return {
  20. uid = "int(11) unsigned NOT NULL",
  21. itemList = "JSON COMMENT '道具信息'",
  22. lastBid = "int(11) DEFAULT 0 COMMENT '最后使用的bid'",
  23. lastInitTime = "int(11) DEFAULT 0 COMMENT '最后初始化背包的时间戳'"
  24. }
  25. end
  26. ----------------------------------------
  27. -- 接口
  28. ----------------------------------------
  29. -- 请求获取背包信息
  30. function root:itf_get_info(role, msg)
  31. local items = self:redis_get_key_info("itemList")
  32. return code.OK, {items = items}
  33. end
  34. -- 分解
  35. function root:itf_exchange(role, msg)
  36. local items = msg.items
  37. if is_empty(items) then
  38. return code.PARAMTER_ERROR
  39. end
  40. local goldCount = 0
  41. for _, v in ipairs(items) do
  42. local price = resAdapt:get_item_price(v.id)
  43. if not is_empty(price) and not is_empty(v.count) then
  44. -- 可分解
  45. local costItems = {{id = v.id, count = v.count}}
  46. if bagData:is_enough(self.uid, costItems) then
  47. local addCount = price * v.count
  48. goldCount = goldCount + addCount
  49. bagData:consume_items(self.uid, costItems, "item-exchange")
  50. local eventId = string.format("item-exchange-%s", tostring(v.id))
  51. bagData:add_items(self.uid, {{id = gameConst.ITEM_ID.GOLD, count = addCount}}, eventId)
  52. end
  53. end
  54. end
  55. return code.OK, {goldCount = goldCount}
  56. end
  57. -- 提取
  58. function root:itf_draw(role, msg)
  59. return code.OK
  60. end
  61. return root