relic_manual.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. -- 遗迹战令
  2. local skynet = require "skynet"
  3. require "skynet.manager"
  4. local logger = require "logger"
  5. local skynet_retpack = skynet.retpack
  6. local asset = require "model.asset"
  7. local redisdriver = require "skynet.db.redis"
  8. local util = require "util"
  9. local common_fun = require "model.common_fun"
  10. local redis
  11. local cjson = require "cjson"
  12. local cjson_decode = cjson.decode -- 解码
  13. local cjson_encode = cjson.encode -- 编码
  14. local INIT_FLAG = false
  15. local data_list = {
  16. -- round = 1, -- 版本
  17. -- start_time = 0, -- 开始时间
  18. -- end_time = 0, -- 结束时间
  19. }
  20. local CMD = {}
  21. local function save_data()
  22. local temp = {}
  23. temp.round = data_list.round
  24. temp.start_time = common_fun.time_str(data_list.start_time)
  25. redis:hset(OTHER_REDIS_KEY, "relic_manual", cjson_encode(temp))
  26. end
  27. local function init_data()
  28. local activitytime = skynet.localname(".activitytime")
  29. local open_time = util.today(skynet.call(activitytime, "lua", "query"))
  30. local data_conf = asset.DataConfig_proto[22]
  31. local today = util.today()
  32. if open_time > today then
  33. return
  34. end
  35. local round_time = DAY_SEC*data_conf.data2
  36. local round = math.floor((today - open_time)/(round_time))+1
  37. local start_time = open_time + (round-1)*round_time
  38. local end_time = start_time + round_time
  39. data_list.round = round
  40. data_list.start_time = start_time
  41. data_list.end_time = end_time
  42. save_data()
  43. INIT_FLAG = true
  44. end
  45. local function check_data()
  46. local now = os.time()
  47. -- 提前2s,与玩家时间做差异处理
  48. if now > data_list.end_time-2 then
  49. local data_conf = asset.DataConfig_proto[22]
  50. local round_time = DAY_SEC*data_conf.data2
  51. data_list.round = data_list.round + 1
  52. data_list.start_time = data_list.end_time
  53. data_list.end_time = data_list.start_time + round_time
  54. save_data()
  55. end
  56. end
  57. local function optimize()
  58. local res = redis:hget(OTHER_REDIS_KEY, "relic_manual")
  59. if res then
  60. local temp = cjson_decode(res)
  61. if not temp then
  62. return
  63. end
  64. data_list.round = temp.round
  65. data_list.start_time = common_fun.split(temp.start_time)
  66. local data_conf = asset.DataConfig_proto[22]
  67. local round_time = DAY_SEC*data_conf.data2
  68. data_list.end_time = data_list.start_time + round_time
  69. if data_list.round and data_list.start_time then
  70. INIT_FLAG = true
  71. end
  72. end
  73. end
  74. local function watchtime()
  75. skynet.fork(function()
  76. while true do
  77. if not INIT_FLAG then
  78. init_data()
  79. else
  80. check_data()
  81. end
  82. skynet.sleep(50)
  83. end
  84. end)
  85. end
  86. function CMD.start()
  87. local conf = assert(option.redis)
  88. redis = redisdriver.connect(conf)
  89. redis:select(15)
  90. optimize()
  91. watchtime()
  92. end
  93. function CMD.relic_manual_data()
  94. if not INIT_FLAG then
  95. return
  96. else
  97. return {
  98. round = data_list.round,
  99. start_time = data_list.start_time + 12*3600 -- //防止冬夏令时的问题
  100. }
  101. end
  102. end
  103. skynet.init(function()
  104. skynet.register(".relic_manual")
  105. end)
  106. skynet.start(function()
  107. logger.label("<Relic_manual>,")
  108. skynet.dispatch("lua", function(session,_, cmd, ...)
  109. local f = assert(CMD[cmd])
  110. if session == 0 then
  111. f(...)
  112. else
  113. skynet_retpack(f(...))
  114. end
  115. end)
  116. end)