聊天:加入玩家发起的群聊和测试用例

This commit is contained in:
liangtongchuan
2021-03-05 21:23:28 +08:00
parent d43fd51e9a
commit 5842723458
10 changed files with 195 additions and 23 deletions
@@ -1,7 +1,7 @@
import {Application, BackendSession} from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { DEFAULT_MSG_PER_PAGE, STATUS } from '../../../consts';
import { createPrivateMsg, getPrivateMessages, pushMsgToRole } from '../../../services/chatService';
import { createGroupMsg, createPrivateMsg, getPrivateMessages, groupRoomId, pushGroupMsgToRoom, pushMsgToRole } from '../../../services/chatService';
export default function(app: Application) {
@@ -79,6 +79,12 @@ export class ChatHandler {
*/
async sendGroupMessage(msg: { channel: string, channelId: string, type: number, content: string, targetRoleId: string, targetMsgCode: string }, session: BackendSession) {
const { channel, channelId, type, content, targetRoleId, targetMsgCode } = msg;
const roleId = session.get('roleId');
const roleName = session.get('roleName');
const msgData = await createGroupMsg(roleId, roleName, channel, channelId, type, content, targetRoleId, targetMsgCode);
await pushGroupMsgToRoom(groupRoomId(channel, channelId), msgData);
if (!msgData) return resResult(STATUS.WRONG_PARMS);
return resResult(STATUS.SUCCESS);
}
/**
@@ -1,8 +1,10 @@
import { GroupMessageType } from './../../../db/GroupMessage';
import { Application, ChannelService, FrontendSession, RemoterClass, pinus } from 'pinus';
import { Application, ChannelService } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { STATUS } from '../../../consts';
import { ON_ADD_CHANNEL_ROUTE, ON_GROUP_MSG_ROUTE, STATUS } from '../../../consts';
import { PrivateMessageType } from '../../../db/PrivateMessage';
import { addUserToChannel } from '../../../services/roleService';
import { ChannelUser } from '../../../domain/ChannelUser';
export default function (app: Application) {
return new ChatRemote(app);
@@ -40,15 +42,13 @@ export class ChatRemote {
public async addWorldChannel(roleId: string, serverId: number, sid: string, flag: boolean = true) {
const name = `world${serverId}`;
let channel = this.channelService.getChannel(name, flag);
if (!channel) return;
channel.add(roleId, sid);
let param = {
roleId
};
channel.pushMessage('onWorldAdd', resResult(STATUS.SUCCESS, param));
if (!!channel) {
channel.add(roleId, sid);
}
return this.getWorldChannel(name);
}
@@ -86,10 +86,8 @@ export class ChatRemote {
let channel = this.channelService.getChannel(name, false);
// leave channel
if (!!channel) {
channel.leave(roleId, sid);
}
if (!channel) return;
channel.leave(roleId, sid);
let param = {
roleId
};
@@ -120,7 +118,10 @@ export class ChatRemote {
* @memberof ChatRemote
*/
public async addChannel(channelName: string, roleId: string, sid: string) {
let channel = this.channelService.getChannel(channelName, true);
if (!channel) return;
addUserToChannel(channel, new ChannelUser(roleId, sid));
channel.pushMessage(ON_ADD_CHANNEL_ROUTE, resResult(STATUS.SUCCESS, { roleId, channelName }));
}
/**
@@ -131,7 +132,15 @@ export class ChatRemote {
* @memberof ChatRemote
*/
public async leaveChannel(channelName: string, roleId: string, sid: string) {
let channel = this.channelService.getChannel(channelName, false);
// leave channel
if (!channel) return;
channel.leave(roleId, sid);
let param = {
roleId,
channelName
};
channel.pushMessage('onLeave', resResult(STATUS.SUCCESS, param));
}
/**
@@ -139,8 +148,10 @@ export class ChatRemote {
* @param {Partial<GroupMessageType>} msg
* @memberof ChatRemote
*/
public async sendGroupMsg(msg: Partial<GroupMessageType>) {
public async sendGroupMsg(roomId: string, msg: Partial<GroupMessageType>) {
let channel = this.channelService.getChannel(roomId, false);
if (!channel) return;
channel.pushMessage(ON_GROUP_MSG_ROUTE, resResult(STATUS.SUCCESS, msg));
}
/**
@@ -151,5 +162,4 @@ export class ChatRemote {
public async sendPrivateMsg(msg: Partial<PrivateMessageType>) {
}
}
}
@@ -19,6 +19,7 @@ import { GuildModel } from '../../../db/Guild';
import { gameData } from '../../../pubUtils/data';
import { getMails } from '../../../services/mailService';
import { addRoleToGuildChannel, addRoleToSysChannel, addRoleToWorldChannel, leaveGuildChannel, leaveSysChannel, leaveWorldChannel } from '../../../services/chatService';
export default function (app: Application) {
return new EntryHandler(app);
}
@@ -88,7 +89,8 @@ export class EntryHandler {
// put user into channel
// console.log(JSON.stringify(self.app.rpc.battle))
// await self.app.rpc.battle.battleRemote.add.route(session)(role.roleId, self.app.get('serverId'), role.serverId, true);
addRoleToSysChannel(role.roleId, self.app.get('serverId'), role.serverId);
addRoleToWorldChannel(role.roleId, self.app.get('serverId'), role.serverId);
await self.app.rpc.chat.chatRemote.addWorldChannel.route(session)(role.roleId, serverId, self.app.get('serverId'));
await self.app.rpc.chat.guildRemote.enterMyChannel.route(session)(role.roleId, self.app.get('serverId'));
let heros = await HeroModel.findByRole(role.roleId, [], HERO_SELECT.ENTRY, true);
@@ -110,6 +112,7 @@ export class EntryHandler {
if(userGuild) {
let guild = await GuildModel.findGuild(userGuild.guildCode, role.serverId, GUILD_SELECT.ENTRY);
if(guild) {
addRoleToGuildChannel(role.roleId, self.app.get('serverId'), guild.code);
role['guildAuth'] = userGuild.auth;
let {lv: guildLv, memberCnt} = guild;
let dicGuild = gameData.centerBase.get(guildLv);
@@ -132,7 +135,7 @@ export class EntryHandler {
* @param {Object} session current session object
*
*/
onUserLeave(session: FrontendSession, reason: string) {
async onUserLeave(session: FrontendSession, reason: string) {
if (!session || !session.uid) {
return;
}
@@ -148,6 +151,9 @@ export class EntryHandler {
// this.app.rpc.battle.battleRemote.kick.route(session)(roleId, this.app.get('serverId'), serverId);
this.app.rpc.chat.chatRemote.kickWorldChannel.route(session, true)(roleId, sid, serverId);
this.app.rpc.chat.guildRemote.leaveMyChannel.route(session)(roleId, sid);
await leaveSysChannel(roleId, sid, serverId);
await leaveWorldChannel(roleId, sid, serverId);
await leaveGuildChannel(roleId, sid);
RoleModel.updateRoleInfo(roleId, { quitTime: nowSeconds() });
}