util_global.lua 2.2 KB

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