123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- --[[
- Descripttion:邮件
- version:
- Author: Neo,Huang
- Date: 2022-08-19 20:10:44
- LastEditors: Neo,Huang
- LastEditTime: 2022-08-19 20:10:56
- --]]
- local code = require("code")
- local util_user = require("utils.util_user")
- local nodeMgr = require("nodeMgr")
- local lib_game_mysql = require("lib_game_mysql")
- local util_global = require("utils.util_global")
- local moduleData = require("data.module")
- local root = {}
- -- 新增邮件
- -- uid:0为全服邮件 >1000000玩家邮件 -1:渠道邮件
- -- status: 邮件状态 个位:0:新增 1:已读 2:删除 十位:奖励状态 0:未领取 1:已领取
- function root:add_mail(uid, channel, title, cnt, items, ty, createTime, expireTime, src)
- if uid == nil or title == nil or is_empty(cnt) then
- return false
- end
- local id = util_global:gen_mail_id()
- channel = channel or 0
- ty = ty or 1
- createTime = createTime or skynet_time()
- expireTime = expireTime or (createTime + 7 * 24 * 60 * 60)
- src = src or "sys"
- -- 邮件物品
- if is_empty(items) then
- items = {}
- end
- items = cjson_encode(items)
- -- 插入数据库
- local values =
- string.format(
- "id=%s, uid=%s, channel=%s, title='%s', cnt='%s', items='%s', ty=%s, createTime=%s, expireTime=%s, src='%s'",
- tostring(id),
- tostring(uid),
- tostring(channel),
- tostring(title),
- tostring(cnt),
- tostring(items),
- tostring(ty),
- tostring(createTime),
- tostring(expireTime),
- tostring(src)
- )
- local sql = string.format("insert into mdl_mail set %s;", tostring(values))
- local ret = lib_game_mysql:query(sql)
- if ret.errno and ret.errno > 0 then
- log.error("add_mail 新增邮件失败 ret[%s] sql[%s]", tostring(ret), tostring(sql))
- return false
- end
- -- 通知新邮件
- if uid == 0 then
- -- 全服
- nodeMgr.broadcast_game_agent("mail", "add_new_mail", {id = id})
- elseif uid == -1 then
- -- 渠道
- nodeMgr.broadcast_game_agent("mail", "add_new_mail", {id = id, channel = channel})
- elseif not is_robot(uid) then
- -- 通知玩家 - 新增个人邮件
- self:on_new_mail(uid, id)
- end
- return true
- end
- -- 获取玩家邮件
- function root:user_get_mail_info_list(uid, lastTime)
- if uid == nil then
- return
- end
- local currTime = skynet_time()
- local channel = moduleData:get_channel(uid)
- lastTime = lastTime or 0
- -- 全局邮件,个人邮件
- local sectionUser =
- string.format("((`uid`=%s or `uid`=0) and `expireTime` >= %s)", tostring(uid), tostring(currTime))
- -- 渠道邮件
- local sectionChannel =
- string.format("(`uid`=-1 and `channel` = %s and `expireTime` >= %s)", tostring(channel), tostring(currTime))
- local sql =
- string.format(
- "SELECT * FROM `mdl_mail` WHERE createTime >= %s and (%s or %s);",
- lastTime,
- sectionUser,
- sectionChannel
- )
- -- log.info("user_get_mail_info_list uid[%s] sql[%s]", tostring(uid), tostring(sql))
- local ret = lib_game_mysql:query(sql)
- if ret.errno and ret.errno > 0 then
- log.error("user_get_mail_info_list 获取玩家邮件列表失败 ret[%s] sql[%s]", tostring(ret), tostring(sql))
- return
- end
- return ret
- end
- -- 玩家获取邮件物品
- function root:user_get_mail_award(uid, id)
- if uid == nil or id == nil then
- return code.PARAMTER_ERROR
- end
- -- 获取邮件信息
- local sql = string.format("SELECT * FROM `mdl_mail` WHERE id=%s;", tostring(id))
- log.info("user_get_mail_award uid[%s] sql[%s]", tostring(uid), tostring(sql))
- local ret = lib_game_mysql:query(sql)
- if is_empty(ret) or (ret.errno and ret.errno > 0) then
- log.error("user_get_mail_award 邮件不存在 ret[%s] sql[%s]", tostring(ret), tostring(sql))
- return code.MAIL.NOT_FOUND
- end
- -- 邮件没有物品
- if is_empty(ret[1].items) then
- log.error("user_get_mail_award 邮件物品不存在 ret[%s] sql[%s]", tostring(ret), tostring(sql))
- return code.MAIL.NOT_MORE_ITEMS
- end
- local items = cjson_decode(ret[1].items)
- return code.OK, items
- end
- -- 玩家是否已领取邮件物品
- function root:user_is_awarded_mail(uid, id)
- if uid == nil or id == nil then
- return false
- end
- local awardIdList = moduleData:hget_json(uid, "pmail", "awardIdList")
- return table.include(awardIdList, id)
- end
- -- 玩家新增已领取邮件
- function root:user_add_award_mail(uid, id)
- if uid == nil or id == nil then
- return false
- end
- local awardIdList = moduleData:hget_json(uid, "pmail", "awardIdList")
- table.insert(awardIdList, id)
- moduleData:hset(uid, "pmail", "awardIdList", awardIdList)
- return true
- end
- -- 通知玩家 - 新增邮件
- function root:on_new_mail(uid, id)
- if uid == nil or is_robot(uid) or id == nil then
- return false
- end
- local pack = {id = id}
- util_user:user_proto_notify(uid, "on_new_mail", pack)
- return true
- end
- return root
|