query_forbit.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. --[[
  2. 查询玩家指定的key值数据
  3. ]] local skynet = require "skynet"
  4. require "skynet.manager"
  5. local logger = require "logger"
  6. local stringify = require "stringify"
  7. local usercenter = skynet.localname(".usercenter")
  8. local logind = skynet.localname(".loginserver")
  9. local redisdriver = require "skynet.db.redis"
  10. local namecenter = skynet.localname(".namecenter")
  11. local cjson = require "cjson"
  12. local md5 = require "md5"
  13. local authz = {acc = "yytx", pwd = "lee@YY-Games.520"}
  14. -- Generate request data
  15. local content = {acc = authz.acc, pwd = authz.pwd, sign = false}
  16. content.sign = string.sub(md5.sumhexa(content.acc .. content.pwd), 9, 24)
  17. local whitelist = {
  18. ["192.168.1.23"] = true,
  19. ["192.168.1.127"] = true,
  20. ["192.168.1.41"] = true,
  21. ["222.212.88.4"] = true,
  22. }
  23. local pipeline = require "pipeline"
  24. function getgameinfo(redis)--得到所有玩家信息
  25. local accounts = {}
  26. local block = pipeline(1024)
  27. local keys = redis:keys("character:*")
  28. if #keys > 0 then
  29. for _, k in ipairs(keys) do
  30. block.add("hmget", k, "nickname", "uid", "level","sex","vip", "mission", "dress","title","forbidden","silent" ,"power")
  31. end
  32. end
  33. local resp = block.execute(redis, {})
  34. for i, v in ipairs(resp) do
  35. assert(v.ok)
  36. local rs = v.out
  37. if rs[2] then
  38. local namecgf = {name = rs[1], uid = rs[2],level = tonumber(rs[3]),sex = tonumber(rs[4]),vip = tonumber(rs[5])}
  39. if rs[6] then
  40. namecgf.mission = tonumber(rs[6])
  41. end
  42. if rs[7] then
  43. local dress = unpack(rs[7])
  44. namecgf.dress = dress.carry or nil--玩家身上的时装
  45. end
  46. if rs[8] then
  47. local title = unpack(rs[8])
  48. namecgf.title = title.settitle or nil--玩家的称号
  49. end
  50. if rs[9] then
  51. namecgf.forbidden =rs[9]
  52. end
  53. if rs[10] then
  54. namecgf.silent=rs[10]
  55. end
  56. if rs[11] then
  57. namecgf.power = tonumber(rs[11])
  58. end
  59. accounts[namecgf.uid] = namecgf
  60. end
  61. end
  62. return accounts
  63. end
  64. -- example:
  65. --[[
  66. http://192.168.1.51:9001/field_inquire?code=xxx&uid=00-6395766577223962624&key=elfdata
  67. ]]
  68. local function query_forbit(args, ipaddr, header)
  69. logger.trace("处理来自主机 %s 的 获取 玩家指定key--%s的数据", ipaddr,args.key)
  70. if not whitelist[ipaddr] then
  71. -- return { "403 - Forbidden" }
  72. -- return cjson.encode({errno = 403, host = header.host, info = "不信任ip"})
  73. end
  74. -- 验证gm账号
  75. local code = string.sub(args.code, 1, 16)
  76. if code ~= content.sign then
  77. return cjson.encode({state = 403, msg = "账号或密码错误"})
  78. end
  79. local conf = assert(option.redis)
  80. local redis
  81. redis = redisdriver.connect(conf)
  82. redis:select(0)
  83. local myacc=getgameinfo(redis)
  84. redis:disconnect() -- 断开数据库连接
  85. logger.trace("===================myacc=%s",stringify(myacc))
  86. local list_all={}
  87. local list_forbidden={}
  88. local list_silent={}
  89. for k,v in pairs(myacc) do
  90. if v.forbidden or v.silent then
  91. table.insert( list_all,v)
  92. end
  93. if v.forbidden then
  94. table.insert( list_forbidden,v)
  95. end
  96. if v.silent then
  97. table.insert( list_silent,v)
  98. end
  99. end
  100. if args.cmd=="*" then
  101. return cjson.encode({state = 0, msg = "成功",list=list_all})
  102. elseif args.cmd=="forbidden" then
  103. return cjson.encode({state = 0, msg = "成功",list=list_forbidden})
  104. elseif args.cmd=="silent" then
  105. return cjson.encode({state = 0, msg = "成功",list=list_silent})
  106. else
  107. return cjson.encode({state = 400, msg = "错误的cmd参数"})
  108. end
  109. end
  110. return query_forbit