203 lines
5.2 KiB
TypeScript
203 lines
5.2 KiB
TypeScript
import {
|
|
ParsedPositions,
|
|
isPositionAlias,
|
|
parsePositionList,
|
|
} from './position.parser';
|
|
import { normalizeRiotIdPart } from '../riot-id.util';
|
|
|
|
export type DakaoCommand =
|
|
| { kind: 'match-list' }
|
|
| {
|
|
kind: 'account-register';
|
|
gameName: string;
|
|
tagLine: string;
|
|
}
|
|
| {
|
|
kind: 'primary-account-change';
|
|
gameName: string;
|
|
tagLine: string;
|
|
}
|
|
| {
|
|
kind: 'match-join';
|
|
matchAndRiotId: string;
|
|
positions?: ParsedPositions;
|
|
}
|
|
| {
|
|
kind: 'match-cancel';
|
|
matchId: string;
|
|
}
|
|
| {
|
|
kind: 'preferred-position-register';
|
|
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 '리스트':
|
|
case '일정':
|
|
return { kind: 'match-list' };
|
|
case '신청':
|
|
return parseJoinArgs(args);
|
|
case '취소':
|
|
case '참가취소':
|
|
return parseCancelArgs(args);
|
|
case '계정등록':
|
|
return parseAccountRegisterArgs(args);
|
|
case '대표계정변경':
|
|
return parsePrimaryAccountChangeArgs(args);
|
|
case '선호포지션등록':
|
|
case '포지션등록':
|
|
return parsePreferredPositionRegisterArgs(args);
|
|
default:
|
|
throw new CommandParseError('unknown_command', subCommand);
|
|
}
|
|
}
|
|
|
|
function parseAccountRegisterArgs(args: string): DakaoCommand {
|
|
const riotId = parseRiotIdArgs(args);
|
|
return {
|
|
kind: 'account-register',
|
|
gameName: riotId.gameName,
|
|
tagLine: riotId.tagLine,
|
|
};
|
|
}
|
|
|
|
function parsePrimaryAccountChangeArgs(args: string): DakaoCommand {
|
|
const riotId = parseRiotIdArgs(args);
|
|
return {
|
|
kind: 'primary-account-change',
|
|
gameName: riotId.gameName,
|
|
tagLine: riotId.tagLine,
|
|
};
|
|
}
|
|
|
|
function parseRiotIdArgs(args: string): { gameName: string; tagLine: string } {
|
|
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 { gameName, tagLine };
|
|
}
|
|
|
|
function parseCancelArgs(args: string): DakaoCommand {
|
|
const matchId = args.trim();
|
|
if (matchId.length === 0) {
|
|
throw new CommandParseError('usage');
|
|
}
|
|
|
|
return {
|
|
kind: 'match-cancel',
|
|
matchId,
|
|
};
|
|
}
|
|
|
|
function parseJoinArgs(args: string): DakaoCommand {
|
|
const trimmed = args.trim();
|
|
if (trimmed.length === 0) {
|
|
return { kind: 'match-join', matchAndRiotId: '' };
|
|
}
|
|
|
|
// 마지막 토큰이 포지션 리스트(쉼표 포함). 그 앞은 내전 식별자(선택)와 라이엇 ID.
|
|
// 형식: [<matchId|title...>] <gameName...>#<tagLine> <pos1>,<pos2>...
|
|
// 끝에서부터 토큰을 분리해 안정적으로 파싱한다.
|
|
const lastSpace = trimmed.lastIndexOf(' ');
|
|
if (lastSpace === -1) {
|
|
const singlePartPositions = tryParseTrailingPositions(trimmed);
|
|
if (singlePartPositions) {
|
|
return {
|
|
kind: 'match-join',
|
|
matchAndRiotId: '',
|
|
positions: singlePartPositions,
|
|
};
|
|
}
|
|
return { kind: 'match-join', matchAndRiotId: trimmed };
|
|
}
|
|
|
|
const trailingPart = trimmed.slice(lastSpace + 1).trim();
|
|
const matchAndRiotId = trimmed.slice(0, lastSpace).trim();
|
|
const positions = tryParseTrailingPositions(trailingPart);
|
|
if (positions) {
|
|
if (!matchAndRiotId) throw new CommandParseError('usage');
|
|
return {
|
|
kind: 'match-join',
|
|
matchAndRiotId,
|
|
positions,
|
|
};
|
|
}
|
|
|
|
return {
|
|
kind: 'match-join',
|
|
matchAndRiotId: trimmed,
|
|
};
|
|
}
|
|
|
|
function parsePreferredPositionRegisterArgs(args: string): DakaoCommand {
|
|
if (args.trim().length === 0) {
|
|
throw new CommandParseError('usage');
|
|
}
|
|
|
|
return {
|
|
kind: 'preferred-position-register',
|
|
positions: parsePositionsOrThrow(args),
|
|
};
|
|
}
|
|
|
|
function tryParseTrailingPositions(input: string): ParsedPositions | undefined {
|
|
if (!input.includes(',') && !isPositionAlias(input)) return undefined;
|
|
return parsePositionsOrThrow(input);
|
|
}
|
|
|
|
function parsePositionsOrThrow(input: string): ParsedPositions {
|
|
try {
|
|
return parsePositionList(input);
|
|
} catch (err) {
|
|
throw new CommandParseError(
|
|
'invalid_position',
|
|
(err as Error)?.message ?? input,
|
|
);
|
|
}
|
|
}
|