exchange_account.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. -- 映射账号(老帐号映射到新账号上)
  2. local skynet = require "skynet"
  3. require "skynet.manager"
  4. local redisdriver = require "skynet.db.redis"
  5. local logger = require "logger"
  6. local queue = require "skynet.queue"
  7. local cjson = require "cjson"
  8. local stringify = require "stringify"
  9. local synchronized = queue()
  10. local namecenter
  11. local usercenter
  12. local skynet_send = skynet.send
  13. local skynet_call = skynet.call
  14. local md5 = require "md5"
  15. local THIS = {}
  16. local state = 0
  17. local redis
  18. local whitelist = {
  19. }
  20. local authz = {acc = "yytx", pwd = "lee@YY-Games.520"}
  21. local content = {acc = authz.acc, pwd = authz.pwd, sign = false}
  22. content.sign = string.sub(md5.sumhexa(content.acc .. content.pwd), 9, 24)
  23. local function initialize()
  24. state = 1
  25. local conf = assert(option.redis)
  26. redis = redisdriver.connect(conf)
  27. redis:select(0)
  28. namecenter = skynet.localname(".namecenter")
  29. usercenter = skynet.localname(".usercenter")
  30. end
  31. -- http://192.168.108.10:9002/exchange_account?code=xxx&type=1&json={"attach":["00-6658977989381873664","00-6659002777164865536"]}
  32. local account = function(args, ipaddr, header)
  33. return synchronized(function()
  34. logger.trace("处理来自主机 %s 的GM管理账号映射请求",ipaddr)
  35. if state == 0 then initialize() end
  36. --验证gm账号
  37. local code = string.sub(args.code, 1, 16)
  38. if code ~= content.sign then
  39. return cjson.encode({state = 403, msg = "账号或密码错误"})
  40. end
  41. if not whitelist[ipaddr] then
  42. -- return { code=403, msg="Forbidden" }
  43. -- return cjson.encode({errno = 403, host = header.host, info = "不信任ip"})
  44. end
  45. local json = cjson.decode(args.json)
  46. local attach = json.attach or {}
  47. local type = args.type
  48. local new,old = attach[1],attach[2]
  49. local o_string = string.format("character:%s",old)
  50. local n_string = string.format("character:%s",new)
  51. logger.trace(" %s %s %s",type,new,old)
  52. function THIS.account_ys()
  53. local o_num = redis:HGET(o_string,'sid')
  54. local n_num = redis:HGET(n_string,'sid')
  55. logger.trace("#### ,account_ys %s %s",o_num,n_num)
  56. if o_num ~= n_num then
  57. logger.trace("#### 账号区服不一致,禁止映射")
  58. local context = "账号区服不一致,禁止映射"
  59. return -1,context
  60. end
  61. local o_acc = redis:HGET(o_string,'account')
  62. local n_acc = redis:HGET(n_string,'account')
  63. local migration_account_key = "migration_account:%s:%s" -- 区服+新账号
  64. local acco_string = string.format(migration_account_key,o_num,o_acc)
  65. local ret = redis:HGET(acco_string,'account')
  66. if ret then
  67. logger.trace("#### 新账号已有映射账号 不可再次映射")
  68. local context = "新账号已有映射账号 不可再次映射"
  69. return -1,context
  70. end
  71. local accn_string = string.format(migration_account_key,n_num,n_acc)
  72. ret = redis:HGET(accn_string,'account')
  73. if ret and ret ~= n_acc then
  74. logger.trace("#### 老账号已有映射账号 不可再次映射")
  75. local context = "老账号已有映射账号 不可再次映射"
  76. return -1,context
  77. end
  78. local err = redis:HSET(accn_string,'account',o_acc)
  79. return err
  80. end
  81. --取消绑定
  82. function THIS.account_qx()
  83. local n_num = redis:HGET(n_string,'sid')
  84. local n_acc = redis:HGET(n_string,'account')
  85. local migration_account_key = "migration_account:%s:%s" -- 区服+新账号
  86. logger.trace("#### ,account_qx server:%s account:%s ",n_num,n_acc )
  87. local accn_string = string.format(migration_account_key,n_num,n_acc)
  88. local ret = redis:HGET(accn_string,'account')
  89. if not ret then
  90. logger.trace("#### 账号没有映射账号 不可取消")
  91. local context = "账号没有映射账号 不可取消"
  92. return -1,context
  93. end
  94. local num = redis:HLEN(accn_string)
  95. num = (num or 0) + 1
  96. local add_string = 'account' ..num
  97. redis:HSET(accn_string,add_string,ret)
  98. redis:HSET(accn_string,'account',n_acc)
  99. return 0
  100. end
  101. if tonumber(type) == 1 then
  102. local erron,context = THIS.account_ys() --映射
  103. if erron == 1 then
  104. return cjson.encode({state=0,msg="success"})
  105. else
  106. return cjson.encode({state=-1,msg = context})
  107. end
  108. elseif tonumber(type) == 2 then
  109. local erron,context = THIS.account_qx() --取消
  110. if erron == 0 then
  111. return cjson.encode({state=0,msg="success"})
  112. else
  113. return cjson.encode({state=-1,msg=context})
  114. end
  115. end
  116. end)
  117. end
  118. return account