Files
ZYZ/game-server/app/servers/chat/remote/guildRemote.ts
2021-03-11 19:29:50 +08:00

225 lines
6.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { Application, ChannelService, FrontendSession, RemoterClass, pinus } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { STATUS } from '../../../consts';
import { GuildType } from '../../../db/Guild';
import { RoleType } from '../../../db/Role';
import { GuildRecType } from '../../../db/GuildRec';
import { UserGuildModel } from '../../../db/UserGuild';
export default function (app: Application) {
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';
/**
* 封装军团相关channel名: 'guild'+guildCode
* @param guildCode 军团code
* @param create 是否创建channel
*/
private getChannel(guildCode: string, create: boolean = false) {
let channelName = `guild${guildCode}`;
let channel = this.channelService.getChannel(channelName, create);
return channel
}
/**
* 封装,向单人发推送
* @param guildCode 军团code
* @param path 推送地址
* @param roleId 玩家id
* @param message 推送信息
* @param sid 玩家服
*/
private pushMessageByUids(guildCode: string, path: string, roleId: string, message: {code: string, roleId?: string}, sid?: string) {
let channel = this.getChannel(guildCode);
let uids = [];
if(!sid) {
sid = channel && channel.getMember(roleId)['sid'];
}
if(sid) {
uids.push({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
}
/**
* 玩家上线
* @param roleId 玩家id
* @param sid 玩家所在服 connector
*/
public async enterMyChannel(roleId: string, sid: string) {
let myGuild = await UserGuildModel.getMyGuild(roleId, 'guildCode');
if(!!myGuild) {
this.enterChannel(myGuild.guildCode, roleId, sid);
}
}
/**
* 加入军团channel
* @param guildCode 军团code
* @param roleId 玩家id
* @param sid 玩家所在服 connector
*/
public enterChannel(guildCode: string, roleId: string, sid: string) {
let channel = this.getChannel(guildCode, true);
// TODO 后续可能添加上下线通知功能
// let param = {
// roleId
// };
// channel.pushMessage('onMemberAdd', resResult(STATUS.SUCCESS, param));
let users = channel.getMembers();
if (users.indexOf(roleId) === -1) {
channel.add(roleId, sid);
}
}
/**
* 玩家下线
* @param roleId 玩家id
* @param sid 玩家所在服 connector
*/
public async leaveMyChannel(roleId: string, sid: string) {
let myGuild = await UserGuildModel.getMyGuild(roleId, 'guildCode');
if(!!myGuild) {
this.leaveChannel(myGuild.guildCode, roleId, sid);
}
}
/**
* 退出军团channel
* @param guildCode 军团code
* @param roleId 玩家id
* @param sid 玩家所在服 connector
*/
public leaveChannel(guildCode: string, roleId: string, sid?: string) {
let channel = this.getChannel(guildCode, false);
if(!sid) {
sid = channel && channel.getMember(roleId)['sid'];
}
// leave channel
if (!!channel) {
channel.leave(roleId, sid);
}
// TODO 后续可能添加上下线通知功能
// let param = {
// roleId
// };
// channel.pushMessage('onMemberLeave', resResult(STATUS.SUCCESS, param));
}
/**
* 踢出某个成员
* @param guildCode 军团code
* @param roleId 踢出玩家roleId
* @param guild 军团信息
* @param sid 玩家sid
*/
public memberQuit (guildCode: string, roleId: string, guild: GuildType, sid?: string) {
// 被踢出公会
this.pushMessageByUids(guildCode, this.MEMBER_QUIT, roleId, {
code: guildCode,
roleId
}, sid);
// 更新人数减少
this.updateInfo(guildCode, { memberCnt: guild.memberCnt, guildCe: guild.guildCe });
this.leaveChannel(guildCode, roleId, sid);
}
/**
* 解散军团
* @param guildCode 军团code
*/
public dismiss(guildCode: string) {
let channel = this.pushMessage(guildCode, this.DISMISS, {code: guildCode});
if(!!channel) {
channel.destroy();
}
}
/**
* 更新军团信息
* @param guildCode 军团code
* @param info 军团相关信息
*/
public updateInfo (guildCode: string, info: any) {
this.pushMessage(guildCode, this.GUILD_INFO_UPDATE, info);
}
/**
* 更换团长信息
* @param guildCode 军团code
* @param managerCnt 管理员人数
* @param newLeader 新的团长
* @param oldLeaderId 旧团长
*/
public changeLeader (guildCode: string, managerCnt: number, newLeader: RoleType, oldLeaderId: string) {
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);
}
/**
* 向旧团长推送降职信息
* @param guildCode 军团code
* @param roleId 旧团长id
*/
public demotion (guildCode: string, roleId: string) {
this.pushMessageByUids(guildCode, this.DEMOTION, roleId, {
code: guildCode
});
}
/**
* 向新团长推送升职信息
* @param guildCode 军团code
* @param roleId 新团长id
*/
public promotion (guildCode: string, roleId: string) {
this.pushMessageByUids(guildCode, this.PROMOTION, roleId, {
code: guildCode
});
}
/**
* 添加动态
* @param guildRecType 动态
*/
public addRec (guildRecType: GuildRecType) {
let { guildCode, type, params, createTime } = guildRecType;
this.pushMessage(guildCode, this.GUILD_REC_ADD, {type, params, createTime});
}
}