263 lines
10 KiB
TypeScript
263 lines
10 KiB
TypeScript
import { RewardInter, pushMail, mailData } from "../pubUtils/interface";
|
||
import { MailModel, MailType } from "../db/Mail";
|
||
import { GroupMailModel, GroupMailType } from "../db/GroupMail";
|
||
import { ServerMailModel, ServerMailType } from '../db/ServerMail';
|
||
import { getRoleOnlineInfo } from "./redisService";
|
||
import { pinus } from "pinus";
|
||
import { gameData } from "../pubUtils/data";
|
||
import { nowSeconds } from '../pubUtils/timeUtil';
|
||
import { STATUS } from '../consts/statusCode';
|
||
import { resResult } from '../pubUtils/util';
|
||
import { GM_MAIL_TYPE, ITID, MAIL_STATUS, MAIL_TIME_TYPE, MAIL_TYPE, SEND_NAME, SEND_TITLE } from "../consts";
|
||
import { MailParam } from '../domain/roleField/mail';
|
||
import { GMMailType, GMMailModel, GMMailTypeParam } from "../db/GMMail";
|
||
import { getGuildChannelSid, getWorldChannelSid } from "./chatChannelService";
|
||
import { GMMailRecordModel } from "../db/GMMailRecord";
|
||
import { BAG } from "../pubUtils/dicParam";
|
||
import { GuildModel, GuildType } from "../db/Guild";
|
||
import moment = require("moment");
|
||
|
||
/**
|
||
* 获取邮件信息
|
||
* @param {String} roleId 玩家id
|
||
* @param {Number} serverId 服务器id
|
||
*/
|
||
export async function getMails(roleId: string, serverId: number) {
|
||
let mails = await MailModel.findByRoleId(roleId)||[];
|
||
let groupMails = await GroupMailModel.findByRoleId(roleId)||[];
|
||
let serverMails = await ServerMailModel.findByRoleId(roleId, serverId)||[];
|
||
|
||
let list = getMailInfos(roleId, mails, groupMails, serverMails);
|
||
list.sort((a, b) => a.sendTime - b.sendTime);
|
||
return list;
|
||
}
|
||
|
||
/**
|
||
* 根据数据库数据,生成返回数据
|
||
* @param {String} roleId 玩家id
|
||
* @param {MailType[]} mails 单人邮件
|
||
* @param {GroupMailType[]} groupMails 多人邮件
|
||
* @param {ServerMailType[]} serverMails 全服邮件
|
||
*/
|
||
function getMailInfos(roleId: string, mails: MailType[], groupMails: GroupMailType[], serverMails: ServerMailType[]) {
|
||
let list: MailParam[] = []; // 结果
|
||
for(let mail of mails) {
|
||
list.push(new MailParam( GM_MAIL_TYPE.SINGLE, mail, roleId));
|
||
}
|
||
for(let mail of groupMails) {
|
||
list.push(new MailParam(GM_MAIL_TYPE.GROUP, mail, roleId));
|
||
}
|
||
for(let mail of serverMails) {
|
||
list.push(new MailParam(GM_MAIL_TYPE.SERVER, mail, roleId));
|
||
}
|
||
return list;
|
||
}
|
||
|
||
export async function sendMailByContent(contentId: MAIL_TYPE, hisRoleId: string, params: { sendName?: string, endTime?: number, params?: string[], goods?: RewardInter[], notPush?: boolean }) {
|
||
let f = new SendMailFun();
|
||
f.setWithContentId(contentId, params);
|
||
await f.sendToUsers(GM_MAIL_TYPE.SINGLE, [hisRoleId]);
|
||
}
|
||
|
||
export async function sendMailToGuildByContent(contentId: MAIL_TYPE, guildCode: string, params: { sendName?: string, endTime?: number, params?: string[], goods?: RewardInter[], notPush?: boolean }, guild?: GuildType) {
|
||
if(!guild) guild = await GuildModel.findByCode(guildCode, null, '+members');
|
||
if(!guild) return false;
|
||
let f = new SendMailFun();
|
||
f.setWithContentId(contentId, params);
|
||
await f.sendToGuild(guildCode, guild.members);
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 发送邮件方法类
|
||
*/
|
||
export class SendMailFun {
|
||
private mailType: GM_MAIL_TYPE = GM_MAIL_TYPE.SINGLE; // 邮件类型 1-单人 2-多人 3-全服
|
||
private contentId: MAIL_TYPE = MAIL_TYPE.SEND_MAIL; // 0-读GmMail,1以上读dicMail
|
||
private sendName: string = SEND_NAME;
|
||
private title: string = SEND_TITLE;
|
||
private content: string = '';
|
||
private gmmail: GMMailTypeParam;
|
||
private hasGoods: boolean = false; // 是否内含奖励
|
||
private goods: RewardInter[] = []; // 发送的奖励
|
||
private sendTime: number;
|
||
private endTime: number;
|
||
private notPush: boolean = false;
|
||
private mails: MailType[] = [];
|
||
private groupMails: GroupMailType[] = [];
|
||
private serverMails: ServerMailType[] = [];
|
||
|
||
// 从dicMail读取数据
|
||
public setWithContentId(contentId: MAIL_TYPE, params: { sendName?: string, params?: string[], goods?: RewardInter[] }) {
|
||
let dicMail = gameData.mail.get(contentId);
|
||
this.contentId = contentId;
|
||
this.sendTime = nowSeconds();
|
||
this.endTime = this.sendTime + dicMail?.time||0;
|
||
this.content = this.getContent(dicMail.content, params.params);
|
||
if(dicMail.title) this.title = dicMail.title;
|
||
if(dicMail.sendName) this.sendName = dicMail.sendName;
|
||
this.hasGoods = params.goods?.length > 0;
|
||
this.goods = params.goods||[];
|
||
}
|
||
|
||
getContent(content: string, params: string[] = []) {
|
||
if(!content) content = '%d';
|
||
for(let p of params) {
|
||
content = content.replace(/%d/, p);
|
||
}
|
||
return content
|
||
}
|
||
|
||
// 从GMMail表读取数据
|
||
public async setWithGmMail(gmmail: GMMailType) {
|
||
this.gmmail = gmmail;
|
||
this.contentId = 0;
|
||
if(gmmail.timeType == MAIL_TIME_TYPE.IMMEDIATE) {
|
||
this.sendTime = nowSeconds();
|
||
} else if (gmmail.timeType == MAIL_TIME_TYPE.DELAY) {
|
||
this.sendTime = gmmail.startTime;
|
||
} else if (gmmail.timeType == MAIL_TIME_TYPE.CIRCLE) {
|
||
this.sendTime = moment(moment().format('YYYY-MM-DD ' + gmmail.circleHour)).unix();
|
||
}
|
||
this.endTime = this.sendTime + gmmail.expire * 60 * 60;
|
||
this.sendName = gmmail.sendName;
|
||
this.title = gmmail.title;
|
||
this.content = gmmail.content;
|
||
this.hasGoods = gmmail.hasGoods;
|
||
this.goods = gmmail.goods;
|
||
return gmmail;
|
||
}
|
||
|
||
private getCreateMailParams() {
|
||
return {
|
||
contentId: this.contentId,
|
||
mail: this.gmmail?._id,
|
||
sendTime: this.sendTime,
|
||
endTime: this.endTime,
|
||
goods: this.goods,
|
||
title: this.title,
|
||
sendName: this.sendName,
|
||
content: this.content,
|
||
hasGoods: this.hasGoods
|
||
}
|
||
}
|
||
|
||
// 生成单人邮件
|
||
private async createSingleMails(roleIds: string[]) {
|
||
let mails = new Map<string, MailParam>();
|
||
for(let roleId of roleIds) {
|
||
let originMail = await MailModel.addMail({ roleId, ...this.getCreateMailParams() });
|
||
this.mails.push(originMail);
|
||
let mail = new MailParam(this.mailType, originMail);
|
||
mails.set(roleId, mail);
|
||
}
|
||
|
||
return mails;
|
||
}
|
||
|
||
// 生成多人邮件
|
||
private async createGroupMails(roleIds: string[]) {
|
||
let roleStatus = roleIds.map(roleId => {
|
||
return { roleId, status: MAIL_STATUS.CREATE }
|
||
})
|
||
let originMail = await GroupMailModel.addMail({ roleStatus, ...this.getCreateMailParams() });
|
||
this.groupMails.push(originMail);
|
||
return new MailParam(this.mailType, originMail);
|
||
}
|
||
|
||
// 生成全服邮件
|
||
private async createServerMails(serverIds: number[]) {
|
||
let mails = new Map<number, MailParam>();
|
||
for(let serverId of serverIds) {
|
||
let originMail = await ServerMailModel.addMail({ serverId, ...this.getCreateMailParams() });
|
||
mails.set(serverId, new MailParam(this.mailType, originMail));
|
||
this.serverMails.push(originMail);
|
||
}
|
||
return mails
|
||
}
|
||
|
||
// 向某几个玩家推送
|
||
public async sendToUsers(mailType: GM_MAIL_TYPE.SINGLE|GM_MAIL_TYPE.GROUP,roleIds: string[], myRoleId?: string, mySid?: string) {
|
||
this.mailType = mailType;
|
||
let uids: { uid: string, sid: string }[] = []
|
||
for(let roleId of roleIds) {
|
||
let sid = '';
|
||
if(roleId == myRoleId) {
|
||
sid = mySid;
|
||
} else {
|
||
let hisOnlineInfo = await getRoleOnlineInfo(roleId);
|
||
if(hisOnlineInfo.isOnline) {
|
||
sid = hisOnlineInfo.sid;
|
||
}
|
||
}
|
||
if(sid) {
|
||
uids.push({ uid: roleId, sid });
|
||
}
|
||
}
|
||
|
||
if(mailType == GM_MAIL_TYPE.SINGLE) {
|
||
let mails = await this.createSingleMails(roleIds);
|
||
for(let { uid, sid } of uids) {
|
||
let mail = mails.get(uid);
|
||
if(uids.length > 0 && mail && !this.notPush) pinus.app.channelService.pushMessageByUids('onMailsAdd', resResult(STATUS.SUCCESS, { mails: [mail] }), [{ uid, sid }]);
|
||
}
|
||
} else if (mailType == GM_MAIL_TYPE.GROUP) {
|
||
let mail = await this.createGroupMails(roleIds);
|
||
if(uids.length > 0 && !this.notPush) pinus.app.channelService.pushMessageByUids('onMailsAdd', resResult(STATUS.SUCCESS, { mails: [mail] }), uids);
|
||
} else { // mailType错误
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
// 向军团推送
|
||
public async sendToGuild(guildCode: string, members: string[]) {
|
||
this.mailType = GM_MAIL_TYPE.GROUP;
|
||
let mails = await this.createGroupMails(members);
|
||
let chatSid = await getGuildChannelSid(guildCode);
|
||
if(!this.notPush) pinus.app.rpc.chat.guildRemote.sendMailToGuild.toServer(chatSid, guildCode, 'onMailsAdd', { mails });
|
||
}
|
||
|
||
// 向全服推送
|
||
public async sendToServer(serverIds: number[]) {
|
||
this.mailType = GM_MAIL_TYPE.SERVER;
|
||
let mails = await this.createServerMails(serverIds);
|
||
for(let serverId of serverIds) {
|
||
let mail = mails.get(serverId);
|
||
if(mail && !this.notPush) {
|
||
let chatSid = await getWorldChannelSid(serverId);
|
||
pinus.app.rpc.chat.chatRemote.sendMail.toServer(chatSid, serverId, 'onMailsAdd', { mails: [mail] });
|
||
}
|
||
}
|
||
}
|
||
|
||
public async saveRecord(createUid: number) {
|
||
for(let mail of this.mails) {
|
||
await GMMailRecordModel.createRecord(GM_MAIL_TYPE.SINGLE, mail, createUid);
|
||
}
|
||
for(let mail of this.groupMails) {
|
||
await GMMailRecordModel.createRecord(GM_MAIL_TYPE.GROUP, mail, createUid);
|
||
}
|
||
for(let mail of this.serverMails) {
|
||
await GMMailRecordModel.createRecord(GM_MAIL_TYPE.SERVER, mail, createUid);
|
||
}
|
||
}
|
||
}
|
||
|
||
export function checkMailGoods(mail: MailType | GroupMailType | ServerMailType, jewelCount: number) {
|
||
let isEquipOver = false;
|
||
for (let good of mail.goods) {
|
||
let dicGoods = gameData.goods.get(good.id);
|
||
let dicItid = ITID.get(dicGoods.itid);
|
||
if (dicItid.table == 'jewel') { // 装备
|
||
if (++jewelCount > BAG.BAG_EQUIP_UPLIMITED) {
|
||
isEquipOver = true;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
return {
|
||
isEquipOver, jewelCount
|
||
}
|
||
} |