聊天:拆分聊天逻辑到不同文件,精简接口参数,增加注释

This commit is contained in:
liangtongchuan
2021-03-11 11:48:34 +08:00
parent 7da2044d0f
commit dda0c96e44
5 changed files with 344 additions and 278 deletions

View File

@@ -0,0 +1,64 @@
import { addRedisChannel, redisChannelServer } from './redisService';
import { groupRoomId } from './chatService';
import { pinus } from 'pinus';
import { crc32 } from 'crc';
import { CHANNEL_PREFIX } from '../consts';
import { RoleModel } from './../db/Role';
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(CHANNEL_PREFIX.SYS, serverId);
await addRoleToChannel(roomId, roleId, sid);
}
export async function addRoleToWorldChannel(roleId: string, sid: string, serverId: number) {
const roomId = groupRoomId(CHANNEL_PREFIX.WORLD, serverId);
await addRoleToChannel(roomId, roleId, sid);
}
export async function addRoleToGuildChannel(roleId: string, sid: string, guildCode: string) {
const roomId = groupRoomId(CHANNEL_PREFIX.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(CHANNEL_PREFIX.SYS, serverId);
await leaveChannel(roomId, roleId, sid);
}
export async function leaveWorldChannel(roleId: string, sid: string, serverId: number) {
const roomId = groupRoomId(CHANNEL_PREFIX.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(CHANNEL_PREFIX.GUILD, guildCode);
await leaveChannel(roomId, roleId, sid);
}