Files
lolympus-tournament-support/apps/api/src/modules/dakao/parsers/command.parser.ts
T

148 lines
4.0 KiB
TypeScript

import { ParsedPositions, parsePositionList } from './position.parser';
import { normalizeRiotIdPart } from '../riot-id.util';
export type DakaoCommand =
| { kind: 'match-list' }
| {
kind: 'account-register';
gameName: string;
tagLine: string;
}
| {
kind: 'match-join';
matchId: string;
gameName: string;
tagLine: string;
positions: ParsedPositions;
};
export class CommandParseError extends Error {
constructor(
public readonly code:
| 'not_a_command'
| 'unknown_command'
| 'usage'
| 'invalid_riot_id'
| 'invalid_position'
| 'invalid_match_id',
public readonly detail?: string,
) {
super(`Command parse error: ${code}${detail ? ` (${detail})` : ''}`);
}
}
const COMMAND_PREFIX = '!내전';
export function parseCommand(rawMessage: string): DakaoCommand {
const message = rawMessage.trim();
if (!message.startsWith(COMMAND_PREFIX)) {
throw new CommandParseError('not_a_command');
}
const rest = message.slice(COMMAND_PREFIX.length).trim();
if (rest.length === 0) {
throw new CommandParseError('usage');
}
const firstSpace = rest.search(/\s/);
const subCommand = firstSpace === -1 ? rest : rest.slice(0, firstSpace);
const args = firstSpace === -1 ? '' : rest.slice(firstSpace + 1).trim();
switch (subCommand) {
case '목록':
case '리스트':
return { kind: 'match-list' };
case '신청':
return parseJoinArgs(args);
case '계정등록':
return parseAccountRegisterArgs(args);
default:
throw new CommandParseError('unknown_command', subCommand);
}
}
function parseAccountRegisterArgs(args: string): DakaoCommand {
if (args.length === 0) {
throw new CommandParseError('usage');
}
const hashIndex = args.lastIndexOf('#');
if (hashIndex <= 0 || hashIndex === args.length - 1) {
throw new CommandParseError('invalid_riot_id', args);
}
const gameName = normalizeRiotIdPart(args.slice(0, hashIndex));
const tagLine = normalizeRiotIdPart(args.slice(hashIndex + 1));
if (gameName.length === 0 || tagLine.length === 0) {
throw new CommandParseError('invalid_riot_id', args);
}
return {
kind: 'account-register',
gameName,
tagLine,
};
}
function parseJoinArgs(args: string): DakaoCommand {
if (args.length === 0) {
throw new CommandParseError('usage');
}
// 마지막 토큰이 포지션 리스트(쉼표 포함). 그 앞이 라이엇 ID(공백 포함 가능). 그 앞이 matchId.
// 형식: <matchId> <gameName...>#<tagLine> <pos1>,<pos2>...
// 끝에서부터 토큰을 분리해 안정적으로 파싱한다.
const lastSpace = args.lastIndexOf(' ');
if (lastSpace === -1) {
throw new CommandParseError('usage');
}
const positionPart = args.slice(lastSpace + 1).trim();
const head = args.slice(0, lastSpace).trim();
if (positionPart.length === 0 || head.length === 0) {
throw new CommandParseError('usage');
}
const headSpace = head.indexOf(' ');
if (headSpace === -1) {
throw new CommandParseError('usage');
}
const matchId = head.slice(0, headSpace).trim();
const riotId = head.slice(headSpace + 1).trim();
if (!isObjectIdHex(matchId)) {
throw new CommandParseError('invalid_match_id', matchId);
}
const hashIndex = riotId.lastIndexOf('#');
if (hashIndex <= 0 || hashIndex === riotId.length - 1) {
throw new CommandParseError('invalid_riot_id', riotId);
}
const gameName = normalizeRiotIdPart(riotId.slice(0, hashIndex));
const tagLine = normalizeRiotIdPart(riotId.slice(hashIndex + 1));
if (gameName.length === 0 || tagLine.length === 0) {
throw new CommandParseError('invalid_riot_id', riotId);
}
let positions: ParsedPositions;
try {
positions = parsePositionList(positionPart);
} catch (err) {
throw new CommandParseError(
'invalid_position',
(err as Error)?.message ?? positionPart,
);
}
return {
kind: 'match-join',
matchId,
gameName,
tagLine,
positions,
};
}
function isObjectIdHex(value: string): boolean {
return /^[a-f0-9]{24}$/i.test(value);
}