bag.lua 1.8 KB

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