util_global.lua 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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-16 23:30:37
  8. --]]
  9. local lib_game_redis = require("lib_game_redis")
  10. local lib_game_mysql = require("lib_game_mysql")
  11. local userData = require("data.user")
  12. local MODULE_NAME = "global"
  13. local root = {}
  14. -- 创建玩家id
  15. function root:gen_user_id()
  16. local subKey = "user:max:id"
  17. local uid = lib_game_redis:hincrby(MODULE_NAME, subKey, 1)
  18. if uid < 1000000 then
  19. uid = uid + 1000000
  20. lib_game_redis:hset(MODULE_NAME, subKey, uid)
  21. end
  22. return uid
  23. end
  24. -- 创建邮件id
  25. function root:gen_mail_id()
  26. local id = lib_game_redis:hincrby(MODULE_NAME, "mail:max:id", 1)
  27. return id
  28. end
  29. -- 创建邀请码
  30. function root:gen_share_code()
  31. local function _gen_code()
  32. local r = {}
  33. for i = 1, 6 do
  34. table.insert(r, string.char(math.random(65, 90)))
  35. end
  36. return table.concat(r)
  37. end
  38. local sharecode = _gen_code()
  39. while self:is_sharecode_active(sharecode) do
  40. sharecode = _gen_code()
  41. end
  42. -- 绑定
  43. lib_game_redis:sadd("sharecode", sharecode)
  44. return sharecode
  45. end
  46. -- 邀请码是否使用
  47. function root:is_sharecode_active(sharecode)
  48. if is_empty(sharecode) then
  49. return false
  50. end
  51. local ret = lib_game_redis:sismember("sharecode", sharecode)
  52. return ret == 1
  53. end
  54. -- 删除邀请码
  55. function root:del_sharecode(sharecode)
  56. local sql = string.format("delete from `mdl_sharecode` where `sharecode` = '%s'", tostring(sharecode))
  57. lib_game_mysql:query(sql)
  58. lib_game_redis:srem("sharecode", sharecode)
  59. end
  60. -- 更新邀请码
  61. function root:update_sharecode(uid, sharecode)
  62. if is_empty(uid) or is_empty(sharecode) then
  63. return false
  64. end
  65. -- 删除旧邀请码
  66. local shareCode = userData:get_share_code(uid)
  67. if not is_empty(shareCode) then
  68. self:del_sharecode(shareCode)
  69. end
  70. -- 更新
  71. lib_game_redis:sadd("sharecode", sharecode)
  72. local values = string.format("sharecode='%s', uid=%s", tostring(sharecode), tostring(uid))
  73. local sql = string.format("insert into mdl_sharecode set %s;", tostring(values))
  74. lib_game_mysql:query(sql)
  75. return true
  76. end
  77. return root