聊天:拆分聊天逻辑到不同文件,精简接口参数,增加注释
This commit is contained in:
64
game-server/app/services/chatChannelService.ts
Normal file
64
game-server/app/services/chatChannelService.ts
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user