优化:抽象推送方法

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

@@ -20,141 +20,6 @@ export class ComBattleRemote {
private channelService: ChannelService;
public async create(uid: string, sid: string, name: string, flag: boolean) {
console.log('comBattleRemote create: ', name, flag);
let channel = this.channelService.getChannel(name, flag);
if (!!channel) {
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);
}
/**
* 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) {
console.log('comBattleRemote add: ', name, flag);
let channel = this.channelService.getChannel(name, flag);
if (!!channel && !this.get(name, false).includes(uid)) {
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);
}
public async available(uid: string, sid: string, name: string, flag: boolean) {
let channel = this.channelService.getChannel(name, flag);
if (!!channel) {
const users = this.get(name, false);
if (users.includes(uid)) {
console.log('不得重复加入');
return false;
}
}
return true;
}
/**
* 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 async hurt(uid: string, sid: string, name: string, bossHurt: number, actorHurt: [{ actorId: number, actorHurt: number }]) {
try {
console.log('hurt channel name: ', name);
let channelService = this.app.get('channelService');
this.bossHp -= bossHurt;
let channel = channelService.getChannel(name, false);
if (!!channel) {
if (this.bossHp < 0) {
this.bossHp = 0;
}
channel.pushMessage('bossHp', resResult(STATUS.SUCCESS, { bossHp: this.bossHp }));
}
return this.bossHp;
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
// public async leaveFromBattle(roleId: string, teamCode: string ) {
// try {
// let teamMap: Map<string, MemComBtlTeam> = this.app.get('teamMap');
// if(teamMap && teamMap.has(teamCode)) {
// let myTeam = teamMap.get(teamCode);
// for(let roleStatus of myTeam.roleStatus) {
// if(roleStatus.roleId == roleId) {
// roleStatus.battleStatus = 2; // 自己失败了
// }
// }
// let channelService = this.app.get('channelService');
// let channel = channelService.getChannel(teamCode, false);
// await handleComBtlProgress(myTeam, this.app.get('robotHurtTimer'), teamMap, channel); // 结算
// }
// } catch(e) {
// errlogger.error(`remote ${__filename} \n ${e.stack}`);
// }
// }
public async checkMyTeam(roleId: string ) {
try {
let teamMap: Map<string, MemComBtlTeam> = this.app.get('teamMap');