217 lines
8.3 KiB
TypeScript
217 lines
8.3 KiB
TypeScript
import { Application, ChannelService } from 'pinus';
|
||
import { resResult } from '../../../pubUtils/util';
|
||
import { STATUS } from '../../../consts';
|
||
import { setGmMails, getGmMailById, getGmMails, getGmUseMails } from '../../../pubUtils/gmData/gmDataUtil';
|
||
import { MAIL_TYPE, MAIL_TEM_TYPE, MAIL_STATUS } from "../../../consts/constModules/mailConst";
|
||
import { mailData } from "../../../pubUtils/interface";
|
||
import { getContent } from '../../../services/mailService';
|
||
import { findWhere } from 'underscore';
|
||
import { nowSeconds } from '../../../pubUtils/timeUtil';
|
||
import { GroupMailType } from '../../../db/GroupMail';
|
||
import { MailType } from '../../../db/Mail';
|
||
import { mongoose } from '@typegoose/typegoose';
|
||
const { ObjectId } = mongoose.Types;
|
||
export default function (app: Application) {
|
||
return new GMRemote(app);
|
||
}
|
||
|
||
// rpc 定义挪到单独的定义文件(user.rpc.define.ts)。解决ts-node 有可能找不到定义的问题。
|
||
// 你也可以用其它方法解决,或者没有遇到过这个问题的话,定义还是可以放在这里。
|
||
|
||
// UserRpc的命名空间自动合并
|
||
// declare global {
|
||
// interface UserRpc {
|
||
// chat: {
|
||
// GMRemote: RemoterClass<FrontendSession, GMRemote>;
|
||
// };
|
||
// }
|
||
// }
|
||
export class GMRemote {
|
||
|
||
constructor(private app: Application) {
|
||
this.app = app;
|
||
this.channelService = app.get('channelService');
|
||
}
|
||
|
||
private channelService: ChannelService;
|
||
|
||
/**
|
||
* Add user into chat channel.
|
||
*
|
||
* @param {String} uid unique id for user
|
||
* @param {String} sid server id
|
||
* @param {String} name channel name
|
||
* @param {boolean} flag channel parameter
|
||
*
|
||
*/
|
||
public async add(uid: string, sid: string, name: string, flag: boolean) {
|
||
let channel = this.channelService.getChannel(name, flag);
|
||
let username = uid.split('*')[0];
|
||
let param = {
|
||
user: username
|
||
};
|
||
channel.pushMessage('onAdd', resResult(STATUS.SUCCESS, param));
|
||
|
||
if (!!channel) {
|
||
channel.add(uid, sid);
|
||
}
|
||
|
||
return this.get(name, flag);
|
||
}
|
||
|
||
/**
|
||
* Get user from chat channel.
|
||
*
|
||
* @param {Object} opts parameters for request
|
||
* @param {String} name channel name
|
||
* @param {boolean} flag channel parameter
|
||
* @return {Array} users uids in channel
|
||
*
|
||
*/
|
||
private get(name: string, flag: boolean) {
|
||
let users: string[] = [];
|
||
let channel = this.channelService.getChannel(name, flag);
|
||
if (!!channel) {
|
||
users = channel.getMembers();
|
||
}
|
||
for (let i = 0; i < users.length; i++) {
|
||
users[i] = users[i].split('*')[0];
|
||
}
|
||
return users;
|
||
}
|
||
|
||
/**
|
||
* Kick user out chat channel.
|
||
*
|
||
* @param {String} uid unique id for user
|
||
* @param {String} sid server id
|
||
* @param {String} name channel name
|
||
*
|
||
*/
|
||
public async kick(uid: string, sid: string, name: string) {
|
||
let channel = this.channelService.getChannel(name, false);
|
||
// leave channel
|
||
if (!!channel) {
|
||
channel.leave(uid, sid);
|
||
}
|
||
let username = uid.split('*')[0];
|
||
let param = {
|
||
user: username
|
||
};
|
||
channel.pushMessage('onLeave', resResult(STATUS.SUCCESS, param));
|
||
}
|
||
|
||
public refreshGmMails(mails:[any]) {
|
||
setGmMails(mails);
|
||
}
|
||
/**
|
||
* 获得邮件详情信息
|
||
* @param roleId
|
||
* @param serverId
|
||
* @param mails
|
||
* @param groupMails
|
||
*/
|
||
public getMailInfos(roleId: string, serverId: number, mails: [any], groupMails: [any]) {
|
||
let list: mailData[] = [];
|
||
let nowTime = nowSeconds();
|
||
//单个邮件
|
||
mails.map(function({ mailId, goods, sendTime, params, status, _id, mailTemType, sendName, endTime}) {
|
||
if (mailTemType == MAIL_TEM_TYPE.GAMEMAIL) { //gm邮件,检查是否超时,并获得gm邮件的content,sendName endTime sendTime goods等信息
|
||
if (endTime < nowTime)
|
||
return;
|
||
let { content } = getContent(parseInt(mailId), params);
|
||
if (!content)
|
||
return;
|
||
list.push({ id: _id, goods, sendTime, endTime, content, status, mailType: MAIL_TYPE.SINGLEMAIL, sendName });
|
||
} else if (mailTemType == MAIL_TEM_TYPE.GMTYPE) { //系统邮件
|
||
let gmMail = getGmMailById(mailId, serverId, nowTime);
|
||
if (!gmMail)
|
||
return;
|
||
let { goods, sendTime, content, endTime, sendName } = gmMail;
|
||
list.push({ id: _id, goods, sendTime, endTime, content, status, mailType: MAIL_TYPE.SINGLEMAIL, sendName });
|
||
}
|
||
});
|
||
//群体邮件
|
||
groupMails.map(function({ mailId, goods, sendTime, endTime, params, sendRoles, _id, mailTemType, sendName }) {
|
||
let { status } = findWhere(sendRoles, {roleId});
|
||
if (mailTemType == MAIL_TEM_TYPE.GAMEMAIL) { //gm邮件,检查是否超时,并获得gm邮件的content,sendName endTime sendTime goods等信息
|
||
if (endTime < nowTime)
|
||
return;
|
||
let { content } = getContent( parseInt(mailId), params);
|
||
if (!content)
|
||
return;
|
||
list.push({ id: _id, goods, sendTime, content, endTime, status, mailType: MAIL_TYPE.GROUPMAIL, sendName });
|
||
} else if (mailTemType == MAIL_TEM_TYPE.GMTYPE) { //系统邮件
|
||
let gmMail = getGmMailById(mailId, serverId, nowTime);
|
||
if (!gmMail)
|
||
return;
|
||
let { goods, sendTime, content, endTime, sendName } = gmMail;
|
||
list.push({ id: _id, goods, sendTime, endTime, content, status, mailType: MAIL_TYPE.GROUPMAIL, sendName });
|
||
}
|
||
});
|
||
return list;
|
||
}
|
||
/**
|
||
* 获得据上次刷新后,可以加入的邮件
|
||
* @param updatedMailAt
|
||
* @param serverId
|
||
*/
|
||
public getMailsByTime(updatedMailAt: number, serverId: number) {
|
||
let gmMails = getGmMails(updatedMailAt, serverId);
|
||
return gmMails;
|
||
}
|
||
/**
|
||
* 获得可以领取的邮件奖
|
||
* @param serverId
|
||
* @param groupMailRewards
|
||
* @param mailRewards
|
||
*/
|
||
public getUseMails(serverId: number, groupMailRewards: GroupMailType[], mailRewards: MailType[]) {
|
||
let nowTime = nowSeconds();
|
||
let mailGoods = [];
|
||
let mails = [];
|
||
let mailIds = [];
|
||
let groupMailIds = [];
|
||
groupMailRewards.map(({_id, goods, mailId, mailTemType})=>{
|
||
if (mailTemType == MAIL_TEM_TYPE.GMTYPE) {//gm邮件检查是否有奖励以及是否可以领取
|
||
let gmMail = getGmMailById(mailId, serverId, nowTime);
|
||
if (!gmMail || !gmMail.goods.length|| gmMail.endTime < nowSeconds()) {
|
||
return;
|
||
}
|
||
mailGoods.push(...gmMail.goods);
|
||
} else {
|
||
if (!goods.length)//系统邮件,检查是否有奖励
|
||
return;
|
||
mailGoods.push(...goods);
|
||
}
|
||
mails.push({id: _id, status: MAIL_STATUS.RECEIVED, mailType: MAIL_TYPE.GROUPMAIL});
|
||
groupMailIds.push(_id);
|
||
});
|
||
mailRewards.map(({_id, goods, mailId, mailTemType})=>{
|
||
if (mailTemType == MAIL_TEM_TYPE.GMTYPE) {//gm邮件检查是否有奖励以及是否可以领取
|
||
let gmMail = getGmMailById(mailId, serverId, nowTime);
|
||
if (!gmMail || !gmMail.goods.length|| gmMail.endTime < nowSeconds()) {
|
||
return;
|
||
}
|
||
mailGoods.push(...gmMail.goods);
|
||
} else {
|
||
if (!goods.length)//系统邮件,检查是否有奖励
|
||
return;
|
||
mailGoods.push(...goods);
|
||
}
|
||
mails.push({id: _id, status: MAIL_STATUS.RECEIVED, mailType: MAIL_TYPE.SINGLEMAIL});
|
||
mailIds.push(_id);
|
||
});
|
||
return {mailIds, groupMailIds, mails, mailGoods};
|
||
}
|
||
/**
|
||
* 通过模板mailId获得gm邮件
|
||
* @param mailId
|
||
* @param serverId
|
||
* @param nowTime
|
||
*/
|
||
public getUseGmMailById(mailId: string, serverId: number, nowTime: number ) {
|
||
let gmMail = getGmMailById(mailId, serverId, nowTime);
|
||
return gmMail;
|
||
}
|
||
} |