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

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
+79 -1
View File
@@ -1,3 +1,5 @@
import { RoleModel } from './../db/Role';
import { GroupMessageModel } from './../db/GroupMessage';
import { CounterModel } from './../db/Counter';
import { STATUS } from './../consts/statusCode';
import { PrivateMessageModel, PrivateMessageParam, PrivateMessageType } from './../db/PrivateMessage';
@@ -5,7 +7,8 @@ import { GroupMessageParam, GroupMessageType } from '../db/GroupMessage';
import { genCode, resResult } from '../pubUtils/util';
import { pinus } from 'pinus';
import { MSG_CODE_LEN, MSG_STATUS, ON_MSG_ROUTE } from '../consts';
import { getRoleOnlineInfo } from './redisService';
import { addRedisChannel, getRoleOnlineInfo, redisChannelServer } from './redisService';
import { crc32 } from 'crc';
/**
* @description 生成私聊房间号
@@ -63,6 +66,13 @@ async function createPrivateMsgData(roleId: string, roleName: string, type: numb
return result;
}
async function createGroupMsgData(roleId: string, roleName: string, channel: string, channelId: string, type: number, content: string, targetRoleId: string, targetMsgCode: string) {
const result: GroupMessageParam = {roleId, roleName, channel, channelId, type, content, targetRoleId, targetMsgCode};
result.roomId = groupRoomId(channel, channelId);
await setupBaseMsgParm(result);
return result;
}
/**
* @description 数据库中创建私聊数据
* @export
@@ -105,4 +115,72 @@ export async function pushMsgToRole(targetRoleId: string, msg: PrivateMessageTyp
export async function getPrivateMessages(roleId: string, targetRoleId: string, fromSeqId: number, count: number) {
const result = await PrivateMessageModel.getMsgs(privateRoomId(roleId, targetRoleId), fromSeqId, count);
return result;
}
export async function createGroupMsg(roleId: string, roleName: string, channel: string, channelId: string, type: number, content: string, targetRoleId: string, targetMsgCode: string) {
const msgData: GroupMessageParam = await createGroupMsgData(roleId, roleName, channel, channelId, type, content, targetRoleId, targetMsgCode);
const result: GroupMessageType = await GroupMessageModel.createMsg(msgData);
return result;
}
export async function pushGroupMsgToRoom(roomId: string, msg: GroupMessageType) {
const channelSid = await channelServer(roomId);
await pinus.app.rpc.chat.chatRemote.sendGroupMsg.toServer(channelSid, roomId, msg);
}
export async function channelServer(roomId: string) {
const existSid = await redisChannelServer(roomId);
if (existSid) {
return existSid;
}
const servers = pinus.app.getServersByType('chat');
if (!servers || !servers.length) return null;
let index = Math.abs(crc32(roomId)) % servers.length;
const newSid = servers[index].id;
const addResult = await addRedisChannel(roomId, newSid);
if (!addResult) return null;
return newSid;
}
async function addRoleToChannel(roomId: string, roleId: string, sid: string) {
const channelSid = await channelServer(roomId);
await pinus.app.rpc.chat.chatRemote.addChannel.toServer(channelSid, roomId, roleId, sid);
}
export async function addRoleToSysChannel(roleId: string, sid: string, serverId: number) {
const roomId = groupRoomId('sys', `${serverId}`);
await addRoleToChannel(roomId, roleId, sid);
}
export async function addRoleToWorldChannel(roleId: string, sid: string, serverId: number) {
const roomId = groupRoomId('new_world', `${serverId}`);
await addRoleToChannel(roomId, roleId, sid);
}
export async function addRoleToGuildChannel(roleId: string, sid: string, guildCode: string) {
const roomId = groupRoomId('new_guild', guildCode);
await addRoleToChannel(roomId, roleId, sid);
}
async function leaveChannel(roomId: string, roleId: string, sid: string) {
const channelSid = await channelServer(roomId);
await pinus.app.rpc.chat.chatRemote.leaveChannel.toServer(channelSid, roomId, roleId, sid);
}
export async function leaveSysChannel(roleId: string, sid: string, serverId: number) {
const roomId = groupRoomId('sys', `${serverId}`);
await leaveChannel(roomId, roleId, sid);
}
export async function leaveWorldChannel(roleId: string, sid: string, serverId: number) {
const roomId = groupRoomId('new_world', `${serverId}`);
await leaveChannel(roomId, roleId, sid);
}
export async function leaveGuildChannel(roleId: string, sid: string) {
const { guildCode } = await RoleModel.findByRoleId(roleId, 'guildCode');
if (!guildCode) return;
const roomId = groupRoomId('new_guild', guildCode);
await leaveChannel(roomId, roleId, sid);
}