TheStarsDBLogic.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. -----------------------------------------------
  2. -- 星空争霸/王者争霸 db
  3. -- getThroneList 获取王座列表
  4. -- getThroneByVestId 根据_id获取王座信息
  5. -----------------------------------------------
  6. local ThroneExcel = require("excel.throne")
  7. local LuaMongo = _G.lua_mongo
  8. local Config = require("Config")
  9. local DB = require("common.DB")
  10. ID_2_THRONE = ID_2_THRONE or {} -- id->throne
  11. VESTID_2_THRONE = VESTID_2_THRONE or {} -- _id->throne
  12. local DBUpdate = {_id = nil}
  13. -- 初始化单个王座信息
  14. local function initThrone(id)
  15. local throne = {}
  16. throne.id = id -- 王座id
  17. throne.lv = 1 -- 挑战等级
  18. throne.record = {} -- 记录
  19. throne.uuid = nil -- 占领者uuid
  20. throne.body = nil -- 形象
  21. throne.exp = nil -- 当前经验值
  22. throne.evolve = nil -- 已进化x次
  23. return throne
  24. end
  25. -- 初始化
  26. function init()
  27. if #ID_2_THRONE > 0 then return end
  28. LuaMongo.find(DB.db_the_stars)
  29. while true do
  30. local throne = {}
  31. if not LuaMongo.next(throne) then
  32. break
  33. end
  34. ID_2_THRONE[throne.id] = throne
  35. end
  36. for id in ipairs(ThroneExcel.throne) do
  37. local throne = ID_2_THRONE[id]
  38. if not throne then
  39. throne = initThrone(id)
  40. LuaMongo.insert(DB.db_the_stars, throne)
  41. ID_2_THRONE[id] = throne
  42. end
  43. VESTID_2_THRONE[throne._id] = throne
  44. end
  45. end
  46. function getThroneList()
  47. return ID_2_THRONE
  48. end
  49. function getThroneByVestId(vestId)
  50. return vestId and VESTID_2_THRONE[vestId]
  51. end
  52. function updateThroneDB(throne)
  53. if not throne then return end
  54. if not throne._id then return end
  55. DBUpdate._id = throne._id
  56. LuaMongo.update(DB.db_the_stars, DBUpdate, throne)
  57. return true
  58. end
  59. -- 设置占领者
  60. function setThroneRole(id, uuid, body, lv, exp, evolve)
  61. local throne = id and ID_2_THRONE[id]
  62. if not throne then return end
  63. unsetThroneRole(uuid) -- 取消之前的占领
  64. throne.uuid = uuid
  65. throne.body = body
  66. throne.lv = lv
  67. throne.exp = exp
  68. throne.evolve = evolve
  69. updateThroneDB(throne)
  70. end
  71. -- 取消占领者
  72. function unsetThroneRole(uuid)
  73. if not uuid then return end
  74. for _, throne in ipairs(ID_2_THRONE) do
  75. if throne.uuid == uuid then
  76. throne.uuid = nil
  77. throne.body = nil
  78. updateThroneDB(throne)
  79. end
  80. end
  81. end