邮件:定时邮件优化
This commit is contained in:
@@ -7,7 +7,7 @@ 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 { genCode, 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";
|
||||
@@ -53,26 +53,25 @@ function getMailInfos(roleId: string, mails: MailType[], groupMails: GroupMailTy
|
||||
return list;
|
||||
}
|
||||
|
||||
export async function sendMailByContent(contentId: MAIL_TYPE, hisRoleId: string, params: { sendName?: string, endTime?: number, params?: string[], goods?: RewardInter[], notPush?: boolean }) {
|
||||
export async function sendMailByContent(contentId: MAIL_TYPE, hisRoleId: string, params: { sendName?: string, endTime?: number, params?: string[], goods?: RewardInter[] }) {
|
||||
let f = new SendMailFun();
|
||||
f.setWithContentId(contentId, params);
|
||||
await f.sendToUsers(GM_MAIL_TYPE.SINGLE, [hisRoleId]);
|
||||
let code = f.setWithContentId(contentId, params);
|
||||
await f.createSingleMails(code, [hisRoleId]);
|
||||
await f.pushToUsers();
|
||||
}
|
||||
|
||||
export async function sendMailToGuildByContent(contentId: MAIL_TYPE, guildCode: string, params: { sendName?: string, endTime?: number, params?: string[], goods?: RewardInter[], notPush?: boolean }, guild?: GuildType) {
|
||||
export async function sendMailToGuildByContent(contentId: MAIL_TYPE, guildCode: string, params: { sendName?: string, endTime?: number, params?: string[], goods?: RewardInter[] }, 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);
|
||||
let code = f.setWithContentId(contentId, params);
|
||||
await f.createGroupMails(code, guild.members, guildCode);
|
||||
await f.pushToUsers();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送邮件方法类
|
||||
*/
|
||||
export class SendMailFun {
|
||||
private mailType: GM_MAIL_TYPE = GM_MAIL_TYPE.SINGLE; // 邮件类型 1-单人 2-多人 3-全服
|
||||
class MailTemp {
|
||||
public code: string;
|
||||
private contentId: MAIL_TYPE = MAIL_TYPE.SEND_MAIL; // 0-读GmMail,1以上读dicMail
|
||||
private sendName: string = SEND_NAME;
|
||||
private title: string = SEND_TITLE;
|
||||
@@ -82,13 +81,10 @@ export class SendMailFun {
|
||||
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[] }) {
|
||||
this.code = genCode(8);
|
||||
let dicMail = gameData.mail.get(contentId);
|
||||
this.contentId = contentId;
|
||||
this.sendTime = nowSeconds();
|
||||
@@ -100,16 +96,8 @@ export class SendMailFun {
|
||||
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.code = genCode(8);
|
||||
this.gmmail = gmmail;
|
||||
this.contentId = 0;
|
||||
if(gmmail.timeType == MAIL_TIME_TYPE.IMMEDIATE) {
|
||||
@@ -128,7 +116,15 @@ export class SendMailFun {
|
||||
return gmmail;
|
||||
}
|
||||
|
||||
private getCreateMailParams() {
|
||||
private getContent(content: string, params: string[] = []) {
|
||||
if(!content) content = '%d';
|
||||
for(let p of params) {
|
||||
content = content.replace(/%d/, p);
|
||||
}
|
||||
return content
|
||||
}
|
||||
|
||||
public getCreateMailParams() {
|
||||
return {
|
||||
contentId: this.contentId,
|
||||
mail: this.gmmail?._id,
|
||||
@@ -141,94 +137,146 @@ export class SendMailFun {
|
||||
hasGoods: this.hasGoods
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送邮件方法类
|
||||
*/
|
||||
export class SendMailFun {
|
||||
private mailTemps = new Map<string, MailTemp>();
|
||||
private mails: MailType[] = [];
|
||||
private groupMails: GroupMailType[] = [];
|
||||
private serverMails: ServerMailType[] = [];
|
||||
|
||||
// 从dicMail读取数据
|
||||
public setWithContentId(contentId: MAIL_TYPE, params: { sendName?: string, params?: string[], goods?: RewardInter[] }) {
|
||||
let mailTemp = new MailTemp();
|
||||
mailTemp.setWithContentId(contentId, params);
|
||||
this.mailTemps.set(mailTemp.code, mailTemp);
|
||||
return mailTemp.code;
|
||||
}
|
||||
|
||||
// 从GMMail表读取数据
|
||||
public async setWithGmMail(gmmail: GMMailType) {
|
||||
let mailTemp = new MailTemp();
|
||||
mailTemp.setWithGmMail(gmmail);
|
||||
this.mailTemps.set(mailTemp.code, mailTemp);
|
||||
return mailTemp.code;
|
||||
}
|
||||
|
||||
// 生成单人邮件
|
||||
private async createSingleMails(roleIds: string[]) {
|
||||
let mails = new Map<string, MailParam>();
|
||||
public async createSingleMails(code: string, roleIds: string[]) {
|
||||
let mapTemp = this.mailTemps.get(code);
|
||||
for(let roleId of roleIds) {
|
||||
let originMail = await MailModel.addMail({ roleId, ...this.getCreateMailParams() });
|
||||
let originMail = await MailModel.addMail({ roleId, ...mapTemp.getCreateMailParams() });
|
||||
this.mails.push(originMail);
|
||||
let mail = new MailParam(this.mailType, originMail);
|
||||
mails.set(roleId, mail);
|
||||
}
|
||||
|
||||
return mails;
|
||||
}
|
||||
|
||||
// 生成多人邮件
|
||||
private async createGroupMails(roleIds: string[]) {
|
||||
public async createGroupMails(code: string, roleIds: string[], guildCode?: string) {
|
||||
let mapTemp = this.mailTemps.get(code);
|
||||
let roleStatus = roleIds.map(roleId => {
|
||||
return { roleId, status: MAIL_STATUS.CREATE }
|
||||
})
|
||||
let originMail = await GroupMailModel.addMail({ roleStatus, ...this.getCreateMailParams() });
|
||||
let originMail = await GroupMailModel.addMail({ roleStatus, guildCode, ...mapTemp.getCreateMailParams() });
|
||||
this.groupMails.push(originMail);
|
||||
return new MailParam(this.mailType, originMail);
|
||||
}
|
||||
|
||||
// 生成全服邮件
|
||||
private async createServerMails(serverIds: number[]) {
|
||||
let mails = new Map<number, MailParam>();
|
||||
public async createServerMails(code: string, serverIds: number[]) {
|
||||
let mapTemp = this.mailTemps.get(code);
|
||||
for(let serverId of serverIds) {
|
||||
let originMail = await ServerMailModel.addMail({ serverId, ...this.getCreateMailParams() });
|
||||
mails.set(serverId, new MailParam(this.mailType, originMail));
|
||||
let originMail = await ServerMailModel.addMail({ serverId, ...mapTemp.getCreateMailParams() });
|
||||
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;
|
||||
public async setMails(mails: MailType[], groupMails: GroupMailType[], serverMails: ServerMailType[]) {
|
||||
this.mails.push(...mails);
|
||||
this.groupMails.push(...groupMails);
|
||||
this.serverMails.push(...serverMails);
|
||||
}
|
||||
|
||||
// 将存储的邮件发出
|
||||
public async pushToUsers(myRoleId?: string, mySid?: string) {
|
||||
// 将可以发的邮件发出
|
||||
let pushByRoleId = new Map<string, MailParam[]>();
|
||||
let pushByGuildCode = new Map<string, MailParam[]>();
|
||||
let pushByServerId = new Map<number, MailParam[]>();
|
||||
|
||||
for(let mail of this.mails) {
|
||||
console.log('#####', mail.sendTime)
|
||||
if(mail.sendTime <= nowSeconds() && mail.endTime > nowSeconds()) {
|
||||
let mailParam = new MailParam(GM_MAIL_TYPE.SINGLE, mail);
|
||||
if(!pushByRoleId.has(mail.roleId)) pushByRoleId.set(mail.roleId, []);
|
||||
pushByRoleId.get(mail.roleId).push(mailParam);
|
||||
}
|
||||
}
|
||||
console.log('#####pushByRoleId', pushByRoleId)
|
||||
for(let mail of this.groupMails) {
|
||||
if(mail.sendTime <= nowSeconds() && mail.endTime > nowSeconds()) {
|
||||
let mailParam = new MailParam(GM_MAIL_TYPE.GROUP, mail);
|
||||
if(!!mail.guildCode) {
|
||||
if(!pushByGuildCode.has(mail.guildCode)) pushByGuildCode.set(mail.guildCode, []);
|
||||
pushByGuildCode.get(mail.guildCode).push(mailParam);
|
||||
} else {
|
||||
for(let { roleId } of mail.roleStatus) {
|
||||
if(!pushByRoleId.has(roleId)) pushByRoleId.set(roleId, []);
|
||||
pushByRoleId.get(roleId).push(mailParam);
|
||||
}
|
||||
}
|
||||
}
|
||||
if(sid) {
|
||||
uids.push({ uid: roleId, sid });
|
||||
}
|
||||
for(let mail of this.serverMails) {
|
||||
if(mail.sendTime <= nowSeconds() && mail.endTime > nowSeconds()) {
|
||||
let mailParam = new MailParam(GM_MAIL_TYPE.SERVER, mail);
|
||||
if(!pushByServerId.has(mail.serverId)) pushByServerId.set(mail.serverId, []);
|
||||
pushByServerId.get(mail.serverId).push(mailParam);
|
||||
}
|
||||
}
|
||||
|
||||
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 }]);
|
||||
for(let [roleId, mails ] of pushByRoleId) {
|
||||
let uids = await this.getUids(roleId, myRoleId, mySid);
|
||||
console.log('###### onMailsAdd', uids, mails);
|
||||
if(uids.length > 0 && mails.length > 0) {
|
||||
pinus.app.channelService.pushMessageByUids('onMailsAdd', resResult(STATUS.SUCCESS, { mails }), uids);
|
||||
}
|
||||
} 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;
|
||||
|
||||
for(let [guildCode, mails] of pushByGuildCode) {
|
||||
let chatSid = await getGuildChannelSid(guildCode);
|
||||
if(!!chatSid && mails.length > 0) {
|
||||
pinus.app.rpc.chat.guildRemote.sendMailToGuild.toServer(chatSid, guildCode, 'onMailsAdd', { mails });
|
||||
}
|
||||
}
|
||||
|
||||
for(let [serverId, mails] of pushByServerId) {
|
||||
let chatSid = await getWorldChannelSid(serverId);
|
||||
if(!!chatSid && mails.length > 0) {
|
||||
pinus.app.rpc.chat.chatRemote.sendMail.toServer(chatSid, serverId, 'onMailsAdd', { mails });
|
||||
}
|
||||
}
|
||||
|
||||
// 延时邮件,设置定时器
|
||||
pinus.app.rpc.systimer.systimerRemote.addMailsToSchedule.broadcast(this.mails, this.groupMails, this.serverMails);
|
||||
}
|
||||
|
||||
// 向军团推送
|
||||
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] });
|
||||
private async getUids(roleId: string, myRoleId?: string, mySid?: string) {
|
||||
let uids: { uid: string, sid: string }[] = [];
|
||||
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 });
|
||||
}
|
||||
return uids;
|
||||
}
|
||||
|
||||
public async saveRecord(createUid: number) {
|
||||
|
||||
Reference in New Issue
Block a user