activitytimemod.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. --获取限时活动时间
  2. local skynet = require "skynet"
  3. local logger = require "logger"
  4. local stringify = require "stringify"
  5. local schema = require "model.schema"
  6. local asset = require "model.asset"
  7. local common_fun = require "model.common_fun"
  8. local REQUEST = {}
  9. local CMD = {}
  10. local activitytime = {}
  11. local activity = skynet.localname(".activitytime")
  12. local activitylist = {
  13. }
  14. local _M = schema.new('activitytimemod', {
  15. activity_token = { --所有活动的token
  16. --[[
  17. [activity_type] = {
  18. state = STATE_CLOSE,
  19. activity_type = 活动类型,
  20. sid = 活动sid,
  21. opentime = 活动开启时间,
  22. endtime = 活动结束时间,
  23. activity_type = 活动开启类型,
  24. switch = 1 活动临时开关
  25. }
  26. ]]
  27. },
  28. })
  29. local THIS = {}
  30. local function func_ret(fname, character, args)
  31. local f = THIS[fname]
  32. if not f then
  33. logger.error("func_ret not fname:%s !!!", fname)
  34. return {errno = STD_ERR.COMMON_SYS_ERR}
  35. end
  36. local errno, ret = f(character, args)
  37. if errno ~= 0 then
  38. return {errno = errno}
  39. end
  40. ret = ret or {}
  41. ret.errno = 0
  42. return ret
  43. end
  44. function activitytime.list_request_interests() return REQUEST end
  45. function activitytime.list_command_interests() return CMD end
  46. function activitytime.parse(character) _M.load(character) end
  47. -- TODO: 处理活动状态切换
  48. --[[
  49. activity_data_time: 开启活动数据
  50. ]]
  51. local function jqui_activity(character, activity_data_time)
  52. local d = _M.assert_get(character)
  53. local player_activ = d.activity_token
  54. local save_token = false
  55. -- 通知活动更新
  56. local function open_update(key, vey)
  57. local ok, info_add = xpcall(activitytime.open, debug.traceback, character, key, vey)
  58. if ok then
  59. player_activ[key] = info_add
  60. save_token = true
  61. logger.test("activitytimemod open_update. atype:%s ok:%s, info_add:%s",key,ok, stringify(info_add))
  62. else
  63. logger.test("activitytimemod open_update err. atype:%s ok:%s, info_add:%s",key,ok, info_add)
  64. end
  65. return ok
  66. end
  67. -- 活动继开 不清除玩家老的数据
  68. local function continue_open_update(key, vey)
  69. local ok, info_add = xpcall(activitytime.continue_open, debug.traceback, character,key,vey)
  70. if ok then
  71. player_activ[key] = info_add
  72. save_token = true
  73. logger.test("activitytimemod continue_open_update atype:%s ok:%s, info_add:%s",key,ok, stringify(info_add))
  74. else
  75. logger.test("activitytimemod continue_open_update err atype:%s ok:%s, info_add:%s",key,ok, info_add)
  76. end
  77. return ok
  78. end
  79. -- 活动关闭
  80. local function close_update(key, vey)
  81. local ok, str = xpcall(activitytime.close, debug.traceback,character, key, vey)
  82. if ok then
  83. player_activ[key].state = STATE_CLOSE
  84. save_token = true
  85. logger.test("activitytimemod close_update atype:%s ok:%s.",key, ok)
  86. else
  87. logger.test("activitytimemod close_update err atype:%s ok:%s, str:%s",key, ok, str)
  88. end
  89. return ok
  90. end
  91. -- 已存在活动对比
  92. local function compare_info(atype, ainfo, pinfo)
  93. local ok
  94. if pinfo.sid ~= ainfo.sid then -- 活动id变化
  95. -- 先通知关闭
  96. if pinfo.state == STATE_OPEN then
  97. ok = close_update(atype, pinfo)
  98. if not ok then
  99. return ok
  100. end
  101. end
  102. -- 再通知开启
  103. ok = open_update(atype, ainfo)
  104. else
  105. if pinfo.state == STATE_CLOSE then
  106. ok = continue_open_update(atype, ainfo) -- 活动续开
  107. else
  108. ok = true
  109. end
  110. end
  111. return ok
  112. end
  113. local err_type = {} -- 活动处理过程出现异常的活动
  114. -- TODO: 活动处理主流程
  115. for _, ainfo in pairs(activity_data_time) do
  116. -- 玩家数据对比 新传入活动数据
  117. local atype = ainfo.activity_type -- 传入活动类型
  118. local pinfo = player_activ[atype]
  119. local ok
  120. if not pinfo then -- 新增活动
  121. ok = open_update(atype,ainfo)
  122. else -- 老活动的处理
  123. -- 对比玩家数据
  124. ok = compare_info(atype, ainfo, pinfo)
  125. end
  126. if not ok then
  127. err_type[atype] = atype
  128. end
  129. end
  130. local del_close = {}
  131. -- 对比玩家数据
  132. for _, sinfo in pairs(d.activity_token) do
  133. local stype = sinfo.activity_type
  134. -- 玩家有得活动, 传入数据没有, 活动已经关闭
  135. if not activity_data_time[stype] and sinfo.state ~= STATE_CLOSE then
  136. logger.test(" == 关闭得活动 %s",stype)
  137. if close_update(stype,sinfo) then -- 通知活动关闭
  138. del_close[stype] = sinfo
  139. else
  140. err_type[stype] = stype
  141. end
  142. end
  143. end
  144. logger.test(" -- 玩家对比传入数据后, 已经关闭得活动 %s", stringify(del_close))
  145. if next(err_type or {}) then
  146. logger.warn(" 活动处理异常的活动: %s", stringify(err_type))
  147. end
  148. -- 是否有活动发生改变
  149. if save_token then
  150. _M.persist(character)
  151. end
  152. end
  153. function activitytime.launch(character)
  154. end
  155. local function project(character, finishtime_data)
  156. if finishtime_data and next(finishtime_data) then
  157. local finishtime_new = {}
  158. for k, v in pairs(finishtime_data) do
  159. local state = v.state
  160. if activitylist[v.activity_type] and activitylist[v.activity_type].check_over then
  161. if activitylist[v.activity_type].check_over(character) then
  162. state = STATE_OVER
  163. end
  164. end
  165. logger.test("activitytimemod --- 活动数据:%s,%s", k,stringify(v))
  166. if v.switch == 1 then -- 关闭
  167. local temp = {
  168. type = v.activity_type,
  169. state = state,
  170. opentime = v.opentime,
  171. endtime = v.endtime,
  172. name = v.name,
  173. sort = v.sort,
  174. icon = v.icon,
  175. }
  176. table.insert(finishtime_new,temp)
  177. end
  178. end
  179. return finishtime_new
  180. end
  181. end
  182. function activitytime.ready(character)
  183. local activity_finishtime = skynet.call(activity, "lua", "getactivitytime")
  184. jqui_activity(character, activity_finishtime)--将活动状态分发下去
  185. local finishtime = project(character, activity_finishtime)
  186. character.send("activity_time_list_nty",{list = finishtime})
  187. local d = _M.assert_get(character)
  188. logger.test("%s:ready, %s", "activitytime", stringify(d or {}))
  189. end
  190. function activitytime.saybye(character)
  191. end
  192. -- TODO: 规范数据处理
  193. local function set_activi_player(ainfo)
  194. return {
  195. state = ainfo.state,
  196. activity_type = ainfo.activity_type,
  197. sid = ainfo.sid,
  198. opentime = ainfo.opentime,
  199. endtime = ainfo.endtime,
  200. open_type = ainfo.open_type,
  201. }
  202. end
  203. -- TODO: 活动开启
  204. function activitytime.open(character,key,ainfo)
  205. activitylist[key].open(character,ainfo)
  206. return set_activi_player(ainfo)
  207. end
  208. -- TODO: 活动续开
  209. function activitytime.continue_open(character,key,ainfo)
  210. if activitylist[key] and activitylist[key].continue_open then
  211. activitylist[key].continue_open(character,ainfo)
  212. end
  213. return set_activi_player(ainfo)
  214. end
  215. function activitytime.close(character, key, pinfo)
  216. activitylist[key].close(character, pinfo)
  217. end
  218. function CMD.update(character,finishtime)--活动开始
  219. logger.test("activitytimemod 开始更新活动配置时间\n:%s",stringify(finishtime))
  220. jqui_activity(character, finishtime)
  221. character.send("activity_time_list_nty",{list = project(character, finishtime)})
  222. end
  223. function activitytime.get_conf(character, model_id)
  224. local d = _M.assert_get(character)
  225. local sid = d.activity_token[model_id] and d.activity_token[model_id].sid
  226. if sid then
  227. return asset.ActivityConfig_proto[sid]
  228. end
  229. end
  230. function activitytime.get_state(character, model_id)
  231. local d = _M.assert_get(character)
  232. local sid = d.activity_token[model_id] and d.activity_token[model_id].sid
  233. if sid then
  234. return d.activity_token[model_id].state
  235. end
  236. return STATE_CLOSE
  237. end
  238. function activitytime.get_info(character, model_id)
  239. local d = _M.assert_get(character)
  240. local sid = d.activity_token[model_id] and d.activity_token[model_id].sid
  241. return d.activity_token[model_id]
  242. end
  243. function THIS.activity_get_conf_list(character, args)
  244. if not args.list then
  245. return 1 -- 参数异常
  246. end
  247. local list = {}
  248. local d = _M.assert_get(character)
  249. for _, v in ipairs(args.list) do
  250. local atype = tonumber(v)
  251. if atype then
  252. local data = d.activity_token[atype]
  253. logger.trace("data:"..stringify(data or {}))
  254. if data and data.state == STATE_OPEN then
  255. local conf = asset.ActivityConfig_proto[data.sid]
  256. local temp = {
  257. type = atype,
  258. describe = conf.describe,
  259. parameter1 = conf.parameter1,
  260. parameter2 = conf.parameter2,
  261. parameter3 = conf.parameter3,
  262. }
  263. for _, award_id in ipairs(conf.reward)do
  264. if award_id <= 0 then
  265. break
  266. end
  267. local _, list = common_fun.get_award(award_id,"NewawardConfig_proto")
  268. if list then
  269. temp.reward_list = temp.reward_list or {}
  270. table.insert(temp.reward_list, {
  271. id = award_id,
  272. list = list,
  273. })
  274. end
  275. end
  276. for _, gift_id in ipairs(conf.gift) do
  277. if gift_id <= 0 then
  278. break
  279. end
  280. local gift_conf = asset.GiftConfig_proto[gift_id]
  281. if gift_conf then
  282. temp.gift_list = temp.gift_list or {}
  283. table.insert(temp.gift_list, {
  284. ID = gift_conf.ID,
  285. library = gift_conf.library,
  286. sort = gift_conf.sort,
  287. spendtype = gift_conf.spendtype,
  288. spend = gift_conf.spend,
  289. num = gift_conf.num,
  290. daylimit = gift_conf.daylimit,
  291. weeklimit = gift_conf.weeklimit,
  292. video = gift_conf.video,
  293. icon = gift_conf.icon,
  294. parameter = gift_conf.parameter,
  295. })
  296. end
  297. end
  298. table.insert(list, temp)
  299. end
  300. end
  301. end
  302. return 0, {list = list}
  303. end
  304. function REQUEST.activity_get_conf_list(character, args)
  305. return func_ret("activity_get_conf_list", character, args)
  306. end
  307. return activitytime