优化:抽象推送方法

This commit is contained in:
luying
2022-04-08 20:38:54 +08:00
parent a64faac7cd
commit f486a8d8a5
38 changed files with 475 additions and 1257 deletions

View File

@@ -1,20 +1,9 @@
import { GroupMessageType } from './../../../db/GroupMessage';
import { Application, ChannelService, HandlerService, } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { ON_ADD_CHANNEL_ROUTE, ON_GROUP_MSG_ROUTE, ON_LEAVE_CHANNEL_ROUTE, STATUS, CHANNEL_PREFIX } from '../../../consts';
import { PrivateMessageType } from '../../../db/PrivateMessage';
import { addUserToChannel, getSimpleRoleInfo } from '../../../services/roleService';
import { ChannelUser } from '../../../domain/ChannelUser';
import { getWorldChannelSid, groupRoomId } from '../../../services/chatService';
import { reloadResources } from '../../../pubUtils/data';
import { GeneralRankParam } from '../../../domain/rank';
import { getAllGuildActivityStatus } from '../../../services/guildActivity/guildActivityService';
import { MailParam } from '../../../domain/roleField/mail';
import { RankFirstType } from '../../../db/RankFirst';
import { LotType } from '../../../db/Lot';
import { _checkFilterWords, getTire, taflush } from '../../../services/sdkService';
import { _checkFilterWords, taflush } from '../../../services/sdkService';
import { getServerMainten, setServerMainten, stopServerMainten } from '../../../services/gmService';
import { errlogger } from '../../../util/logger';
import { addUserToChannel, sendMessageToChannel, sendMessgeToChannelByBatch } from '../../../services/pushService';
export default function (app: Application) {
new HandlerService(app, {});
@@ -41,10 +30,6 @@ export class ChatRemote {
}
private channelService: ChannelService;
private GUILD_ACTIVITY_END = 'onGuildActivityEnd';
private RACE_ACTIVITY_START = 'onRaceStart';
private RANK_TOP_UPDATE = 'onRankTopUpdated';
private GUILD_ACTIVITY_STATUS_UPDATE = 'onGuildActivityStatus'; // 军团活动状态变化
/**
* @description 添加用户到频道
@@ -57,7 +42,7 @@ export class ChatRemote {
try {
let channel = this.channelService.getChannel(channelName, true);
if (!channel) return;
addUserToChannel(channel, new ChannelUser(roleId, sid));
addUserToChannel(channel, roleId, sid);
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
@@ -82,80 +67,36 @@ export class ChatRemote {
}
/**
* @description 推送消息到频道
* @param {Partial<GroupMessageType>} msg
* @memberof ChatRemote
* 向频道推送消息
* @param roomId 房间号
* @param path 推送地址
* @param data 推送数据
* @returns
*/
public async sendGroupMsg(roomId: string, msg: Partial<GroupMessageType>) {
public async pushMessage(roomId: string, path: string, data: any, isBatch = false) {
try {
let channel = this.channelService.getChannel(roomId, false);
if (!channel) return;
const roleInfo = await getSimpleRoleInfo(msg.roleId);
channel.pushMessage(ON_GROUP_MSG_ROUTE, resResult(STATUS.SUCCESS, { ...msg, roleInfo }));
if(isBatch) { // 分批推送
sendMessgeToChannelByBatch(channel, path, data);
} else {
sendMessageToChannel(channel, path, data);
}
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* @description 推送私聊消息
* @param {Partial<PrivateMessageType>} msg
* @memberof ChatRemote
*/
public async sendPrivateMsg(msg: Partial<PrivateMessageType>) {
public async deleteChannel(roomId: string) {
try {
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* @description 全服推送活动结束通知
* @param serverId
*/
public async sendGuildActivityEnd(serverId: number) {
try {
let roomId = groupRoomId(CHANNEL_PREFIX.WORLD, serverId);
let channel = this.channelService.getChannel(roomId, false);
if (!channel) return;
channel.pushMessage(this.GUILD_ACTIVITY_END, resResult(STATUS.SUCCESS, {}));
channel.destroy();
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* @description 全服推送竞赛活动开始通知
* @param serverId
*/
public async sendRaceActivityStart(serverId: number) {
try {
let roomId = groupRoomId(CHANNEL_PREFIX.WORLD, serverId);
let channel = this.channelService.getChannel(roomId, false);
if (!channel) return;
channel.pushMessage(this.RACE_ACTIVITY_START, resResult(STATUS.SUCCESS, {}));
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* @description 全服推送排行榜更新信息
* @param serverId
*/
public async sendRankTopUpdated(serverId: number, ranks: (RankFirstType & {status: number})[]) {
try {
let roomId = groupRoomId(CHANNEL_PREFIX.WORLD, serverId);
let channel = this.channelService.getChannel(roomId, false);
if (!channel) return;
channel.pushMessage(this.RANK_TOP_UPDATE, resResult(STATUS.SUCCESS, { ranks }));
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* 重载json资源
*/
@@ -167,113 +108,6 @@ export class ChatRemote {
}
}
/**
* @description 全服推送军团活动状态变化
* @param serverId
*/
public async sendGuildActivityUpdate(serverId: number) {
try {
let roomId = groupRoomId(CHANNEL_PREFIX.WORLD, serverId);
let channel = this.channelService.getChannel(roomId, false);
if (!channel) return;
let guildActivities = getAllGuildActivityStatus();
channel.pushMessage(this.GUILD_ACTIVITY_STATUS_UPDATE, resResult(STATUS.SUCCESS, { guildActivities }));
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* 向全服推送邮件
* @param serverId 服务器id
* @param path 推送地址
* @param mails 邮件内容
*/
public async sendMail(serverId: number, path: string, data: { mails: MailParam[] }) {
try {
let roomId = groupRoomId(CHANNEL_PREFIX.WORLD, serverId);
let channel = this.channelService.getChannel(roomId, false);
if (!channel) return;
channel.pushMessage(path, resResult(STATUS.SUCCESS, data));
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* @description 全服推送维护
* @param serverId
*/
public async sendServerMaintenance(serverId: number) {
try {
let roomId = groupRoomId(CHANNEL_PREFIX.WORLD, serverId);
let channel = this.channelService.getChannel(roomId, false);
if (!channel) return { result: false, serverId };
channel.pushMessage('onServerMaintenance', resResult(STATUS.SERVER_MAINTENANCE));
return { result: true, serverId };
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
public async pushCurrentTime(serverId: number, time: number) {
try {
let roomId = groupRoomId(CHANNEL_PREFIX.WORLD, serverId);
let channel = this.channelService.getChannel(roomId, false);
if (!channel) return { result: false, serverId };
channel.pushMessage('onPushCurrentTime', resResult(STATUS.SUCCESS, { time }));
return { result: true, serverId };
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* @description 实时推送军团拍卖出价
* @param guildCode
*/
public async sendGuildAuction(guildCode: string, lot: LotType) {
try {
let roomId = groupRoomId(CHANNEL_PREFIX.GUILD, guildCode);
let channel = this.channelService.getChannel(roomId, false);
if(!channel) return false;
channel.pushMessage('onAuctionOver', resResult(STATUS.SUCCESS, { lot }));
return true;
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
public async sendWorldAuction(serverId: number, lot: LotType) {
try {
let roomId = groupRoomId(CHANNEL_PREFIX.WORLD, serverId);
let channel = this.channelService.getChannel(roomId, false);
if(!channel) return false;
channel.pushMessage('onAuctionOver', resResult(STATUS.SUCCESS, { lot }));
return true;
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
public async sendAuctionUpdate(serverId: number, params: { lots: LotType[] }) {
try {
let roomId = groupRoomId(CHANNEL_PREFIX.WORLD, serverId);
let channel = this.channelService.getChannel(roomId, false);
if(!channel) return false;
channel.pushMessage('onAuctionUpdate', resResult(STATUS.SUCCESS, params));
return true;
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
public async checkFilterWords(word: string) {
try {
return await _checkFilterWords(word);

View File

@@ -1,463 +0,0 @@
import { Application, ChannelService, HandlerService, } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { STATUS, CHANNEL_PREFIX } from '../../../consts';
import { GuildType } from '../../../db/Guild';
import { RoleType } from '../../../db/Role';
import { GuildRecType } from '../../../db/GuildRec';
import { leaveGuildChannel, groupRoomId } from '../../../services/chatService';
import { GuildRankParams, WoodenHorse, Event } from '../../../domain/battleField/guildActivity';
import { MailParam } from '../../../domain/roleField/mail';
import { pick } from 'underscore';
import { LotType } from '../../../db/Lot';
import { DividendType } from '../../../db/Dividend';
import { getRoleOnlineInfo } from '../../../services/redisService';
import { errlogger } from '../../../util/logger';
export default function (app: Application) {
new HandlerService(app, {});
return new GuildRemote(app);
}
export class GuildRemote {
constructor(private app: Application) {
this.app = app;
this.channelService = app.get('channelService');
}
private channelService: ChannelService;
private MEMBER_QUIT = 'onMemberQuit';
private DISMISS = 'onDismiss';
private GUILD_INFO_UPDATE = 'onGuildInfoUpdate';
private DEMOTION = 'onDemotion';
private PROMOTION = 'onPromotion';
private GUILD_REC_ADD = 'onGuildRecAdd';
private GUILD_BOSS_OPEN = 'onGuildBossStatus';
private GATE_ACT_RANK = 'onGuildGateRankUpdate'; // 军团活动排行榜
private CITY_ACT_RANK = 'onGuildCityRankUpdate'; // 军团活动排行榜
private GUILD_GATE_ACT_HP = 'onGuildGateHpUpdate'; // 军团活动蛮夷入侵排行榜
private GUILD_ACTIVITY_END = 'onGuildActivityEnd'; // 军团活动结束
private GUILD_CITY_DECLARE = 'onGuildCityDeclare'; // 有军团对这个城池进行宣战了
private GUILD_CITY_ACT_HP = 'onGuildCityGateHpUpdate'; // 诸侯入侵城门血条
private GUILD_RACE_UPDATE = 'onRaceHorseUpdate'; /// 更新木牛流马
private GUILD_RACE_JOIN = 'onRaceHorseJoin'
private GUILD_RACE_EVENT = 'onRaceEventUpdate'; /// 更新木牛流马
private GUILD_POP_UP_ACTIVITY = 'onActivityTaskUpdate'; /// 向军团成员发送弹窗礼包
private GUILD_TRAIN_RESET = 'onGuildTainReset'; // 试炼场重置
private GUILD_BOSS_ENCOURAGE = 'onGuildBossEncourage'; // 鼓舞
private AUCTION_UPDATE = 'onAuctionUpdate'; // 拍卖行更新
private AUCTION_ADD = 'onAuctionAdd'; // 拍卖行增加
private DIVIDEND_UPDATE = 'onDividendsUpdate'; // 拍卖行更新
private DIVIDEND_ADD = 'onDividendsAdd';
/**
* 封装军团相关channel名: 'guild'+guildCode
* @param guildCode 军团code
* @param create 是否创建channel
*/
private getChannel(guildCode: string, create: boolean = false) {
let channelName = groupRoomId(CHANNEL_PREFIX.GUILD, guildCode);
let channel = this.channelService.getChannel(channelName, create);
return channel
}
/**
* 封装,向单人发推送
* @param guildCode 军团code
* @param path 推送地址
* @param roleId 玩家id
* @param message 推送信息
* @param sid 玩家服
*/
private async pushMessageByUids(guildCode: string, path: string, roleId: string, message: { code: string, roleId?: string }, sid?: string) {
let channel = this.getChannel(guildCode);
let uids = [];
if (!sid) {
let onlineUser = await getRoleOnlineInfo(roleId);
sid = onlineUser.sid;
}
if (sid) {
uids.push({ uid: roleId, sid });
this.channelService.pushMessageByUids(path, resResult(STATUS.SUCCESS, message), uids);
}
return channel
}
/**
* 封装向channel发推送
* @param guildCode 军团code
* @param path 推送地址
* @param message 推送信息
*/
private pushMessage(guildCode: string, path: string, message: any) {
let channel = this.getChannel(guildCode);
if (!!channel) {
channel.pushMessage(path, resResult(STATUS.SUCCESS, message));
}
return channel
}
/**
* 诸侯混战按城池区分channel
* @param cityId 城池id
* @param create 是否创建channel
*/
private getCityChannel(cityId: number, create: boolean = false) {
let channelName = groupRoomId(CHANNEL_PREFIX.CITY, cityId);
let channel = this.channelService.getChannel(channelName, create);
return channel
}
/**
* 封装向channel发推送
* @param cityId 城池id
* @param path 推送地址
* @param message 推送信息
*/
private pushMessageToCity(cityId: number, path: string, message: any) {
let channel = this.getCityChannel(cityId);
if (!!channel) {
channel.pushMessage(path, resResult(STATUS.SUCCESS, message));
}
return channel
}
/**
* 踢出某个成员
* @param guildCode 军团code
* @param roleId 踢出玩家roleId
* @param guild 军团信息
* @param sid 玩家sid
*/
public memberQuit(guildCode: string, roleId: string, guild: GuildType, sid?: string) {
try {
// 被踢出公会
this.pushMessageByUids(guildCode, this.MEMBER_QUIT, roleId, {
code: guildCode,
roleId
}, sid);
// 更新人数减少
this.updateInfo(guildCode, { memberCnt: guild.memberCnt, guildCe: guild.guildCe });
leaveGuildChannel(roleId, sid, guildCode);
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* 解散军团
* @param guildCode 军团code
*/
public dismiss(guildCode: string) {
try {
let channel = this.pushMessage(guildCode, this.DISMISS, { code: guildCode });
if (!!channel) {
channel.destroy();
}
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* 更新军团信息
* @param guildCode 军团code
* @param info 军团相关信息
*/
public updateInfo(guildCode: string, info: any) {
try {
this.pushMessage(guildCode, this.GUILD_INFO_UPDATE, info);
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* 更换团长信息
* @param guildCode 军团code
* @param managerCnt 管理员人数
* @param newLeader 新的团长
* @param oldLeaderId 旧团长
*/
public changeLeader(guildCode: string, managerCnt: number, newLeader: RoleType, oldLeaderId: string) {
try{
let { roleId, roleName, frame, head, spine, lv, quitTime } = newLeader;
this.updateInfo(guildCode, { managerCnt, leader: { roleId, roleName, frame, head, spine, lv, quitTime } });
this.demotion(guildCode, oldLeaderId);
this.promotion(guildCode, roleId);
}catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* 向旧团长推送降职信息
* @param guildCode 军团code
* @param roleId 旧团长id
*/
public demotion(guildCode: string, roleId: string) {
try {
this.pushMessageByUids(guildCode, this.DEMOTION, roleId, {
code: guildCode
});
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* 向新团长推送升职信息
* @param guildCode 军团code
* @param roleId 新团长id
*/
public promotion(guildCode: string, roleId: string) {
try {
this.pushMessageByUids(guildCode, this.PROMOTION, roleId, {
code: guildCode
});
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* 添加动态
* @param guildRecType 动态
*/
public addRec(guildRecType: GuildRecType) {
try {
let { guildCode, type, params, createTime } = guildRecType;
this.pushMessage(guildCode, this.GUILD_REC_ADD, { type, params, createTime });
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* 向军团推送城门血量
* @param guildCode
*/
public pushGateHp(guildCode: string, gateHp: number) {
try {
this.pushMessage(guildCode, this.GUILD_GATE_ACT_HP, { gateHp });
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* 向军团推送排行榜名次
* @param guildCode
*/
public pushRank(guildCode: string, msg: GuildRankParams) {
try {
this.pushMessage(guildCode, this.GATE_ACT_RANK, msg);
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* 向军团推送排行榜名次
* @param guildCode
*/
public pushCityActRank(guildCode: string, msg: GuildRankParams) {
try {
this.pushMessage(guildCode, this.CITY_ACT_RANK, msg);
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* @description 向军团推送结束活动
* @param guildCode
*/
public async sendGuildActivityEnd(guildCode: string) {
try {
console.log("MSG END")
this.pushMessage(guildCode, this.GUILD_ACTIVITY_END, {});
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* @description 向城池推送有宣战
* @param cityId 城池
* @param declareGuildCode 宣战的军团code
* @param declareCount 宣战数量
*/
public async sendGuildCityDeclare(cityId: number, declareGuildCode: string, declareCount: number) {
try {
this.pushMessageToCity(cityId, this.GUILD_CITY_DECLARE, {
cityId,
declareGuildCode,
declareCount
});
this.pushMessage(declareGuildCode, this.GUILD_CITY_DECLARE, {
cityId,
declareGuildCode,
declareCount
});
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* @description 推送城池城门血条
* @param cityId 城池
* @param gateHp 血条
*/
public async pushCityGateHp(cityId: number, gateHp: number, maxHp: number) {
try {
this.pushMessageToCity(cityId, this.GUILD_CITY_ACT_HP, {
cityId,
gateHp,
maxHp
});
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* @description 推送木牛流马状态
* @param guildCode 军团
* @param woodenHorseList 木马
* @param ranks 军团排名和自己所在的排名
*/
public async pushRaceHorseUpdate(guildCode: string, woodenHorseList: WoodenHorse[], ranks: GuildRankParams, events: Event[]) {
try {
this.pushMessage(guildCode, this.GUILD_RACE_UPDATE, {
timestamp: Date.now(),
woodenHorseList,
...ranks,
events
});
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* @description 当有人加入这个木牛流马
* @param guildCode 军团
* @param woodenHorse 木马
*/
public async pushRaceHorseJoin(guildCode: string, woodenHorse: WoodenHorse) {
try {
this.pushMessage(guildCode, this.GUILD_RACE_JOIN, {
timestamp: Date.now(),
woodenHorse
});
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* @description 向军团推送事件
* @param guildCode
*/
public async sendRaceEvent(guildCode: string, events: Event[]) {
try {
this.pushMessage(guildCode, this.GUILD_RACE_EVENT, { timestamp: Date.now(), events });
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* @description 当团长开启演武台时
* @param guildCode
* @param warId
* @param bossHp
* @param status
*/
public async pushBossStatus(guildCode: string, result: any) {
try {
this.pushMessage(guildCode, this.GUILD_BOSS_OPEN, pick(result, ['status', 'bossInfo', 'leaderOpenCnt']));
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* @description 向军团推送弹窗礼包数据活动
* @param guildCode
*/
public async sendPopUpActivity(guildCode: string, data: any[]) {
try {
console.log("sendPopUpActivity")
this.pushMessage(guildCode, this.GUILD_POP_UP_ACTIVITY, data);
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* @description 军团邮件
* @param guildCode 军团code
* @param path 推送地址
* @param data 军团内容
*/
public async sendMailToGuild(guildCode: string, path: string, data: { mails: MailParam[] }) {
try {
this.pushMessage(guildCode, path, data);
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
public async sendTrainReset(guildCode: string) {
try {
this.pushMessage(guildCode, this.GUILD_TRAIN_RESET, {});
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
public async pushEncourage(guildCode: string, param: {encourageCnt: number; encourageMax: number; myEncourageCnt: number;}) {
try {
this.pushMessage(guildCode, this.GUILD_BOSS_ENCOURAGE, param);
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
public async pushAuctionUpdate(guildCode: string, param: { lots: LotType[]; dividends: DividendType[] }) {
try {
if(param.lots) this.pushMessage(guildCode, this.AUCTION_UPDATE, { lots: param.lots });
if(param.dividends) this.pushMessage(guildCode, this.DIVIDEND_UPDATE, { dividends: param.dividends });
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
public async pushAuctionAdd(guildCode: string, param: { lots: LotType[]; dividends: DividendType[] }) {
try {
if(param.lots) this.pushMessage(guildCode, this.AUCTION_ADD, { lots: param.lots });
if(param.dividends) this.pushMessage(guildCode, this.DIVIDEND_ADD, { dividends: param.dividends });
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* @description 活动推送
* @param guildCode 军团code
* @param path 推送地址
* @param param 内容
*/
public async pushActivityToGuild(guildCode: string, path: string, param: any) {
try {
this.pushMessage(guildCode, path, param);
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
}