后台添加推送奇遇事件功能

This commit is contained in:
luying
2020-10-19 17:31:45 +08:00
parent 99e50c9c36
commit 0da6b7c169
10 changed files with 227 additions and 9 deletions
@@ -0,0 +1,61 @@
import {Application, BackendSession} from 'pinus';
import { RoleModel } from '../../../db/Role';
import { getEventTime, refreshEvent } from '../../../services/eventSercive';
import EventRecord, { EventRecordModel } from '../../../db/EventRecord';
import { EVENT_STATUS, EVENT_RECORD_STATUS } from '../../../consts/consts';
import { resResult } from '../../../pubUtils/util';
import { STATUS } from '../../../consts/statusCode';
export default function(app: Application) {
return new GmHandler(app);
}
export class GmHandler {
constructor(private app: Application) {
}
async pushEvent(msg: { uid: number, serverId: number }, session: BackendSession) {
let sid = session.get('sid');
let { uid, serverId } = msg;
console.log(uid, serverId)
let role = await RoleModel.findByUid(uid, serverId);
if(!role) return resResult(STATUS.GM_ROLE_NOT_FOUND);
let {roleId, roleName, eventStatus } = role;
let channelService = this.app.get('channelService');
let channel = channelService.getChannel(roleId, true);
let users = channel.getMembers();
if (users.indexOf(roleId) === -1) {
channel.add(roleId, sid);
}
let now = new Date();
let t = getEventTime(now);
let event: Array<EventRecord>;
if (eventStatus == EVENT_STATUS.STARTING) {
event = await EventRecordModel.getEventRecordByTime(roleId, 0);
} else if( eventStatus == EVENT_STATUS.OPEN ) {
event = await EventRecordModel.getEventRecordByTime(roleId, t);
if(event.length == 0) { // 刷新
const num = 3; // 每次刷3个
event = await refreshEvent(num, roleId, roleName, t);
}
}
let tsid = channel.getMember(roleId)['sid'];
channelService.pushMessageByUids('onSpecialEvent', {
msg: { event }
}, [{
uid: roleId,
sid: tsid
}]);
return resResult(STATUS.SUCCESS, { list: event });
}
}
@@ -0,0 +1,92 @@
import { Application, ChannelService, FrontendSession, RemoterClass } from 'pinus';
export default function (app: Application) {
return new ChatRemote(app);
}
// rpc 定义挪到单独的定义文件(user.rpc.define.ts)。解决ts-node 有可能找不到定义的问题。
// 你也可以用其它方法解决,或者没有遇到过这个问题的话,定义还是可以放在这里。
// UserRpc的命名空间自动合并
// declare global {
// interface UserRpc {
// chat: {
// chatRemote: RemoterClass<FrontendSession, ChatRemote>;
// };
// }
// }
export class ChatRemote {
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', 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', param);
}
}