Files
ZYZ/game-server/app/services/chatChannelService.ts
2022-04-08 20:39:01 +08:00

134 lines
5.0 KiB
TypeScript

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);
}
export async function addRoleToCityChannel(roleId: string, sid: string, cityId: number) {
const roomId = groupRoomId(CHANNEL_PREFIX.CITY, cityId);
await addRoleToChannel(roomId, roleId, sid);
}
export async function addRoleToGuildAuctionChannel(roleId: string, sid: string, guildCode: string) {
const roomId = groupRoomId(CHANNEL_PREFIX.GUILD_AUCTION, guildCode);
await addRoleToChannel(roomId, roleId, sid);
}
export async function addRoleToWorldAuctionChannel(roleId: string, sid: string, serverId: number) {
const roomId = groupRoomId(CHANNEL_PREFIX.WORLD_AUCTION, serverId);
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 leaveCityChannel(roleId: string, sid: string, cityId: number) {
const roomId = groupRoomId(CHANNEL_PREFIX.CITY, cityId);
await leaveChannel(roomId, roleId, sid);
}
export async function leaveGuildAuctionChannel(roleId: string, sid: string, guildCode: string) {
const roomId = groupRoomId(CHANNEL_PREFIX.CITY, guildCode);
await leaveChannel(roomId, roleId, sid);
}
export async function leaveWorldAuctionChannel(roleId: string, sid: string, serverId: number) {
const roomId = groupRoomId(CHANNEL_PREFIX.CITY, serverId);
await leaveChannel(roomId, roleId, sid);
}
export async function leaveGuildChannel(roleId: string, sid: string, guildCode: string) {
if (!guildCode) return;
const roomId = groupRoomId(CHANNEL_PREFIX.GUILD, guildCode);
await leaveChannel(roomId, roleId, sid);
}
export async function getWorldChannelSid(serverId: number) {
const roomId = groupRoomId(CHANNEL_PREFIX.WORLD, serverId);
const channelSid = await channelServer(roomId);
return channelSid;
}
export async function getGuildChannelSid(guildCode: string) {
const roomId = groupRoomId(CHANNEL_PREFIX.GUILD, guildCode);
const channelSid = await channelServer(roomId);
return channelSid;
}
export async function getCityChannelSid(cityId: number) {
const roomId = groupRoomId(CHANNEL_PREFIX.CITY, cityId);
const channelSid = await channelServer(roomId);
return channelSid;
}
export async function getGuildAuctionChannelSid(guildCode: string) {
const roomId = groupRoomId(CHANNEL_PREFIX.GUILD_AUCTION, guildCode);
const channelSid = await channelServer(roomId);
return channelSid;
}
export async function getWorldAuctionChannelSid(serverId: number) {
const roomId = groupRoomId(CHANNEL_PREFIX.CITY, serverId);
const channelSid = await channelServer(roomId);
return channelSid;
}
async function delChannel(roomId: string) {
const channelSid = await channelServer(roomId);
await pinus.app.rpc.chat.chatRemote.deleteChannel.toServer(channelSid, roomId);
}
export async function delGuildChannel(guildCode: string) {
if (!guildCode) return;
const roomId = groupRoomId(CHANNEL_PREFIX.GUILD, guildCode);
await delChannel(roomId);
}