namecenter.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. local skynet = require "skynet"
  2. require "skynet.manager"
  3. local redisdriver = require "skynet.db.redis"
  4. local pipeline = require "pipeline"
  5. local logger = require "logger"
  6. local asset = require "model.asset"
  7. local queue = require "skynet.queue"
  8. local redis
  9. local NAMELIST = "namelist"
  10. local stringify = require "stringify"
  11. local DEFAULT_NAME = "role"
  12. local skynet_retpack = skynet.retpack
  13. local synchronized = queue()
  14. local population = 0 -- 服务器人口数量
  15. local nickname = {}
  16. local duplicate_name = {}
  17. local function load_registered_name()
  18. local conf = assert(option.redis)
  19. local character_redis = redisdriver.connect(conf)
  20. character_redis:select(0)
  21. -- 载入角色名字和帐号
  22. local block = pipeline(1024)
  23. local keys = character_redis:keys("character:*")
  24. if #keys > 0 then
  25. for _, k in ipairs(keys) do
  26. block.add("hmget", k, "nickname", "uid")
  27. end
  28. population = population + #keys
  29. end
  30. local resp = block.execute(character_redis, {})
  31. nickname[DEFAULT_NAME] = ""
  32. for i, v in ipairs(resp) do
  33. assert(v.ok)
  34. local k = v.out[1]
  35. if k and k ~= DEFAULT_NAME then
  36. nickname[k] = v.out[2]
  37. end
  38. end
  39. character_redis:disconnect()
  40. end
  41. local function init()
  42. local conf = assert(option.redis)
  43. redis = redisdriver.connect(conf)
  44. redis:select(14)
  45. local retnuma = redis:hgetall(NAMELIST)
  46. for k = 1, #retnuma, 2 do
  47. duplicate_name[retnuma[k]] = tonumber(retnuma[k+1])
  48. end
  49. end
  50. local function get_name(name)
  51. local realname = name
  52. for i = 1, 100 do
  53. if nickname[realname] then
  54. duplicate_name[name] = (duplicate_name[name] or 0) + 1
  55. realname = name .. "#" .. (duplicate_name[name]+100)
  56. break
  57. end
  58. end
  59. if duplicate_name[name] > 0 then
  60. redis:hset(NAMELIST, name, duplicate_name[name])
  61. end
  62. if not nickname[realname] then
  63. return realname
  64. end
  65. end
  66. local CMD = {}
  67. function CMD.start()
  68. init()
  69. load_registered_name()
  70. end
  71. function CMD.register_nickname(name, uid)
  72. local name = name
  73. if not name or name == "" then
  74. name = DEFAULT_NAME
  75. end
  76. if nickname[name] then
  77. name = get_name(name)
  78. end
  79. if not name then
  80. return
  81. end
  82. nickname[name] = uid
  83. population = population + 1
  84. logger.trace(" #### 服务器人口数量增加 %s",population)
  85. return true, name
  86. end
  87. --@TODO 服务器人口数量
  88. function CMD.get_population()
  89. return population
  90. end
  91. -- 通过名字查找玩家的基础信息
  92. -- uid: 角色id
  93. -- account: 帐号id
  94. function CMD.findby(name)
  95. if name == DEFAULT_NAME then
  96. return
  97. end
  98. local uid = nickname[name]
  99. if uid and type(uid) == "string" then
  100. return uid
  101. end
  102. end
  103. -- 玩家改名
  104. -- old_name:以前的名
  105. -- new_name: 新的名字
  106. -- uid :玩家的uid
  107. function CMD.rename(old_name, new_name, uid)
  108. return synchronized(function ()
  109. if new_name == DEFAULT_NAME then
  110. return STD_ERR.PLYAER_DUPLICATION_NAME -- 名字已经被使用
  111. end
  112. -- 没有找到改玩家
  113. if old_name ~= DEFAULT_NAME then
  114. if not nickname[tostring(old_name)] or nickname[tostring(old_name)] ~= uid then
  115. return STD_ERR.PLYAER_NO_PLAYER -- 不存在的玩家
  116. end
  117. end
  118. -- 名字已经被使用
  119. if nickname[new_name] then
  120. return STD_ERR.PLYAER_DUPLICATION_NAME -- 名字已经被使用
  121. end
  122. nickname[new_name] = uid
  123. return 0
  124. end)
  125. end
  126. -- 撤销改名
  127. function CMD.revocation_rename(new_name, uid)
  128. return synchronized(function ()
  129. -- nickname[old_name] = uid
  130. nickname[new_name] = nil
  131. return 0
  132. end)
  133. end
  134. -- 取消对name的锁定
  135. function CMD.unlock_name(name)
  136. return synchronized(function ()
  137. nickname[name] = nil
  138. return 0
  139. end)
  140. end
  141. skynet.init(function()
  142. skynet.register(".namecenter")
  143. end)
  144. skynet.start(function()
  145. logger.label("<Namecenter>")
  146. skynet.dispatch("lua", function(session, _, cmd, ...)
  147. local f = assert(CMD[cmd])
  148. if session == 0 then
  149. f(...)
  150. else
  151. skynet_retpack(f(...))
  152. end
  153. end)
  154. end)