import * as crc from 'crc'; interface ServerInfo { id: string; serverType: string; host: string; port: number; clientHost?: string; clientPort?: number; frontend ?: boolean; args ?: string | string[]; cpu ?: number; } export async function dispatch(redis: any, uid: string, connectors: ServerInfo[], serverType: string = '') { connectors.sort((a, b) => a.id > b.id? 1: -1); if (serverType && needRoleRoute(serverType)) { let appid: string = await redis.hgetAsync('roleRoute', `${uid}_${serverType}`); if(!appid) { let index = Math.abs(crc.crc32(uid)) % connectors.length; let connector = connectors[index]; appid = connector.id; redis.hsetAsync('roleRoute', `${uid}_${serverType}`, appid); } let roleRoute = connectors.find(obj => obj.id == appid); return roleRoute; } let index = Math.abs(crc.crc32(uid)) % connectors.length; return connectors[index]; } function needRoleRoute(serverType: string) { switch (serverType) { case 'connector': case 'role': case 'battle': case 'activity': case 'order': case 'gm': case 'chat': return false; case 'comBattle': case 'guild': return true; default: return true; } } export async function clearComBattleRoute(redis: any, teamCode: string) { try { await redis.hdelAsync('roleRoute', `${teamCode}_comBattle`); } catch(e) { console.log(e) } }