util_mail.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. --[[
  2. Descripttion:邮件
  3. version:
  4. Author: Neo,Huang
  5. Date: 2022-08-19 20:10:44
  6. LastEditors: Neo,Huang
  7. LastEditTime: 2022-08-19 20:10:56
  8. --]]
  9. local code = require("code")
  10. local util_user = require("utils.util_user")
  11. local nodeMgr = require("nodeMgr")
  12. local lib_game_mysql = require("lib_game_mysql")
  13. local util_global = require("utils.util_global")
  14. local moduleData = require("data.module")
  15. local root = {}
  16. -- 新增邮件
  17. -- uid:0为全服邮件 >1000000玩家邮件 -1:渠道邮件
  18. -- status: 邮件状态 个位:0:新增 1:已读 2:删除 十位:奖励状态 0:未领取 1:已领取
  19. function root:add_mail(uid, channel, title, cnt, items, ty, createTime, expireTime, src)
  20. if uid == nil or title == nil or is_empty(cnt) then
  21. return false
  22. end
  23. local id = util_global:gen_mail_id()
  24. channel = channel or 0
  25. ty = ty or 1
  26. createTime = createTime or skynet_time()
  27. expireTime = expireTime or (createTime + 7 * 24 * 60 * 60)
  28. src = src or "sys"
  29. -- 邮件物品
  30. if is_empty(items) then
  31. items = {}
  32. end
  33. items = cjson_encode(items)
  34. -- 插入数据库
  35. local values =
  36. string.format(
  37. "id=%s, uid=%s, channel=%s, title='%s', cnt='%s', items='%s', ty=%s, createTime=%s, expireTime=%s, src='%s'",
  38. tostring(id),
  39. tostring(uid),
  40. tostring(channel),
  41. tostring(title),
  42. tostring(cnt),
  43. tostring(items),
  44. tostring(ty),
  45. tostring(createTime),
  46. tostring(expireTime),
  47. tostring(src)
  48. )
  49. local sql = string.format("insert into mdl_mail set %s;", tostring(values))
  50. local ret = lib_game_mysql:query(sql)
  51. if ret.errno and ret.errno > 0 then
  52. log.error("add_mail 新增邮件失败 ret[%s] sql[%s]", tostring(ret), tostring(sql))
  53. return false
  54. end
  55. -- 通知新邮件
  56. if uid == 0 then
  57. -- 全服
  58. nodeMgr.broadcast_game_agent("mail", "add_new_mail", {id = id})
  59. elseif uid == -1 then
  60. -- 渠道
  61. nodeMgr.broadcast_game_agent("mail", "add_new_mail", {id = id, channel = channel})
  62. elseif not is_robot(uid) then
  63. -- 通知玩家 - 新增个人邮件
  64. self:on_new_mail(uid, id)
  65. end
  66. return true
  67. end
  68. -- 获取玩家邮件
  69. function root:user_get_mail_info_list(uid, lastTime)
  70. if uid == nil then
  71. return
  72. end
  73. local currTime = skynet_time()
  74. local channel = moduleData:get_channel(uid)
  75. lastTime = lastTime or 0
  76. -- 全局邮件,个人邮件
  77. local sectionUser =
  78. string.format("((`uid`=%s or `uid`=0) and `expireTime` >= %s)", tostring(uid), tostring(currTime))
  79. -- 渠道邮件
  80. local sectionChannel =
  81. string.format("(`uid`=-1 and `channel` = %s and `expireTime` >= %s)", tostring(channel), tostring(currTime))
  82. local sql =
  83. string.format(
  84. "SELECT * FROM `mdl_mail` WHERE createTime >= %s and (%s or %s);",
  85. lastTime,
  86. sectionUser,
  87. sectionChannel
  88. )
  89. -- log.info("user_get_mail_info_list uid[%s] sql[%s]", tostring(uid), tostring(sql))
  90. local ret = lib_game_mysql:query(sql)
  91. if ret.errno and ret.errno > 0 then
  92. log.error("user_get_mail_info_list 获取玩家邮件列表失败 ret[%s] sql[%s]", tostring(ret), tostring(sql))
  93. return
  94. end
  95. return ret
  96. end
  97. -- 玩家获取邮件物品
  98. function root:user_get_mail_award(uid, id)
  99. if uid == nil or id == nil then
  100. return code.PARAMTER_ERROR
  101. end
  102. -- 获取邮件信息
  103. local sql = string.format("SELECT * FROM `mdl_mail` WHERE id=%s;", tostring(id))
  104. log.info("user_get_mail_award uid[%s] sql[%s]", tostring(uid), tostring(sql))
  105. local ret = lib_game_mysql:query(sql)
  106. if is_empty(ret) or (ret.errno and ret.errno > 0) then
  107. log.error("user_get_mail_award 邮件不存在 ret[%s] sql[%s]", tostring(ret), tostring(sql))
  108. return code.MAIL.NOT_FOUND
  109. end
  110. -- 邮件没有物品
  111. if is_empty(ret[1].items) then
  112. log.error("user_get_mail_award 邮件物品不存在 ret[%s] sql[%s]", tostring(ret), tostring(sql))
  113. return code.MAIL.NOT_MORE_ITEMS
  114. end
  115. local items = cjson_decode(ret[1].items)
  116. return code.OK, items
  117. end
  118. -- 玩家是否已领取邮件物品
  119. function root:user_is_awarded_mail(uid, id)
  120. if uid == nil or id == nil then
  121. return false
  122. end
  123. local awardIdList = moduleData:hget_json(uid, "pmail", "awardIdList")
  124. return table.include(awardIdList, id)
  125. end
  126. -- 玩家新增已领取邮件
  127. function root:user_add_award_mail(uid, id)
  128. if uid == nil or id == nil then
  129. return false
  130. end
  131. local awardIdList = moduleData:hget_json(uid, "pmail", "awardIdList")
  132. table.insert(awardIdList, id)
  133. moduleData:hset(uid, "pmail", "awardIdList", awardIdList)
  134. return true
  135. end
  136. -- 通知玩家 - 新增邮件
  137. function root:on_new_mail(uid, id)
  138. if uid == nil or is_robot(uid) or id == nil then
  139. return false
  140. end
  141. local pack = {id = id}
  142. util_user:user_proto_notify(uid, "on_new_mail", pack)
  143. return true
  144. end
  145. return root