Files
ZYZ/game-server/app/servers/gm/remote/gmRemote.ts
2021-02-07 18:15:08 +08:00

197 lines
7.4 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';
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);
}
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) { //模板邮件
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) { //模板邮件
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;
}
public getMails(updatedMailAt: number, serverId: number) {
let gmMails = getGmMails(updatedMailAt, serverId);
return gmMails;
}
public getUseMails(serverId: number, groupMailRewards: GroupMailType[], mailRewards: MailType[]) {
let nowTime = nowSeconds();
let mailGoods = [];
let mails = [];
let mailIds: string[] = [];
let groupMailIds: string[] = [];
groupMailRewards.map(({_id, goods, mailId, mailTemType})=>{
if (mailTemType == MAIL_TEM_TYPE.GMTYPE) {
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) {
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};
}
/**
*
* @param mailId
* @param serverId
* @param nowTime
*/
public getUseGmMailById(mailId: string, serverId: number, nowTime: number ) {
let gmMail = getGmMailById(mailId, serverId, nowTime);
return gmMail;
}
}