feat(api): parse Dakao commands
This commit is contained in:
1 parent
ba3894cba2
commit
404f7713b9
4 files changed
+485
No files matched your search
@@ -0,0 +1,166 @@
|
||||
import { CommandParseError, parseCommand } from './command.parser';
|
||||
|
||||
describe('parseCommand', () => {
|
||||
it('parses match-list command (목록)', () => {
|
||||
expect(parseCommand('!내전 목록')).toEqual({ kind: 'match-list' });
|
||||
});
|
||||
|
||||
it('parses match-list command (리스트)', () => {
|
||||
expect(parseCommand('!내전 리스트')).toEqual({ kind: 'match-list' });
|
||||
});
|
||||
|
||||
it('parses match-list command (일정)', () => {
|
||||
expect(parseCommand('!내전 일정')).toEqual({ kind: 'match-list' });
|
||||
});
|
||||
|
||||
it('parses match-join command with simple riot id', () => {
|
||||
const result = parseCommand(
|
||||
'!내전 신청 69b63bc96da18dfdb218ee8c 프리리#KR1 미드,탑',
|
||||
);
|
||||
expect(result).toMatchObject({
|
||||
kind: 'match-join',
|
||||
matchAndRiotId: '69b63bc96da18dfdb218ee8c 프리리#KR1',
|
||||
});
|
||||
if (result.kind === 'match-join') {
|
||||
expect(result.positions.positions).toEqual(['MID', 'TOP']);
|
||||
}
|
||||
});
|
||||
|
||||
it('handles riot game name with spaces', () => {
|
||||
const result = parseCommand(
|
||||
'!내전 신청 69b63bc96da18dfdb218ee8c hello world#KR1 미드,서폿',
|
||||
);
|
||||
expect(result).toMatchObject({
|
||||
kind: 'match-join',
|
||||
matchAndRiotId: '69b63bc96da18dfdb218ee8c hello world#KR1',
|
||||
});
|
||||
});
|
||||
|
||||
it('parses hidden unicode differences in riot id input', () => {
|
||||
const decomposedGameName = '프리리'.normalize('NFD');
|
||||
const result = parseCommand(
|
||||
`!내전 신청 69b63bc96da18dfdb218ee8c ${decomposedGameName}\u200B#KR1\uFEFF 미드,탑`,
|
||||
);
|
||||
|
||||
expect(result).toMatchObject({
|
||||
kind: 'match-join',
|
||||
matchAndRiotId: `69b63bc96da18dfdb218ee8c ${decomposedGameName}\u200B#KR1`,
|
||||
});
|
||||
});
|
||||
|
||||
it('parses match-join command without explicit match id', () => {
|
||||
const result = parseCommand('!내전 신청 프리리#KR1 미드,탑');
|
||||
expect(result).toMatchObject({
|
||||
kind: 'match-join',
|
||||
matchAndRiotId: '프리리#KR1',
|
||||
});
|
||||
});
|
||||
|
||||
it('parses match-join command with title prefix', () => {
|
||||
const result = parseCommand('!내전 신청 저녁 프리리#KR1 미드,탑');
|
||||
expect(result).toMatchObject({
|
||||
kind: 'match-join',
|
||||
matchAndRiotId: '저녁 프리리#KR1',
|
||||
});
|
||||
});
|
||||
|
||||
it('parses abbreviated match-join command with only match code', () => {
|
||||
expect(parseCommand('!내전 신청 cab1')).toEqual({
|
||||
kind: 'match-join',
|
||||
matchAndRiotId: 'cab1',
|
||||
});
|
||||
});
|
||||
|
||||
it('parses match-join command with registered account and explicit positions', () => {
|
||||
const result = parseCommand('!내전 신청 cab1 미드,서포터');
|
||||
expect(result).toMatchObject({
|
||||
kind: 'match-join',
|
||||
matchAndRiotId: 'cab1',
|
||||
});
|
||||
if (result.kind === 'match-join') {
|
||||
expect(result.positions?.positions).toEqual(['MID', 'SUP']);
|
||||
}
|
||||
});
|
||||
|
||||
it('parses preferred position registration command', () => {
|
||||
const result = parseCommand('!내전 선호포지션등록 미드,SPT');
|
||||
expect(result).toMatchObject({
|
||||
kind: 'preferred-position-register',
|
||||
});
|
||||
if (result.kind === 'preferred-position-register') {
|
||||
expect(result.positions.positions).toEqual(['MID', 'SUP']);
|
||||
}
|
||||
});
|
||||
|
||||
it('parses match-cancel command (취소)', () => {
|
||||
expect(parseCommand('!내전 취소 69b63bc96da18dfdb218ee8c')).toEqual({
|
||||
kind: 'match-cancel',
|
||||
matchId: '69b63bc96da18dfdb218ee8c',
|
||||
});
|
||||
});
|
||||
|
||||
it('parses match-cancel command (참가취소)', () => {
|
||||
expect(parseCommand('!내전 참가취소 69b63bc96da18dfdb218ee8c')).toEqual({
|
||||
kind: 'match-cancel',
|
||||
matchId: '69b63bc96da18dfdb218ee8c',
|
||||
});
|
||||
});
|
||||
|
||||
it('parses match-cancel command with short code', () => {
|
||||
expect(parseCommand('!내전 취소 AB12')).toEqual({
|
||||
kind: 'match-cancel',
|
||||
matchId: 'AB12',
|
||||
});
|
||||
});
|
||||
|
||||
it('parses account register command', () => {
|
||||
expect(parseCommand('!내전 계정등록 hello world#KR1')).toEqual({
|
||||
kind: 'account-register',
|
||||
gameName: 'hello world',
|
||||
tagLine: 'KR1',
|
||||
});
|
||||
});
|
||||
|
||||
it('normalizes account register riot id', () => {
|
||||
const decomposedGameName = '프리리'.normalize('NFD');
|
||||
expect(
|
||||
parseCommand(`!내전 계정등록 ${decomposedGameName}\u200B#KR1\uFEFF`),
|
||||
).toEqual({
|
||||
kind: 'account-register',
|
||||
gameName: '프리리',
|
||||
tagLine: 'KR1',
|
||||
});
|
||||
});
|
||||
|
||||
it('throws not_a_command for non-prefix messages', () => {
|
||||
expect(() => parseCommand('안녕하세요')).toThrow(/not_a_command/);
|
||||
});
|
||||
|
||||
it('throws unknown_command for unrecognized subcommands', () => {
|
||||
expect(() => parseCommand('!내전 폭파')).toThrow(/unknown_command/);
|
||||
});
|
||||
|
||||
it('allows non-objectid join match reference for title matching', () => {
|
||||
expect(
|
||||
parseCommand('!내전 신청 not-an-id 프리리#KR1 미드,탑'),
|
||||
).toMatchObject({
|
||||
kind: 'match-join',
|
||||
matchAndRiotId: 'not-an-id 프리리#KR1',
|
||||
});
|
||||
});
|
||||
|
||||
it('allows match-join without riot id for registered-account flow', () => {
|
||||
expect(
|
||||
parseCommand('!내전 신청 69b63bc96da18dfdb218ee8c 미드,탑'),
|
||||
).toMatchObject({
|
||||
kind: 'match-join',
|
||||
matchAndRiotId: '69b63bc96da18dfdb218ee8c',
|
||||
});
|
||||
});
|
||||
|
||||
it('throws invalid_position for too few positions', () => {
|
||||
expect(() => {
|
||||
parseCommand('!내전 신청 69b63bc96da18dfdb218ee8c 프리리#KR1 미드');
|
||||
}).toThrow(CommandParseError);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,181 @@
|
||||
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: '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 '선호포지션등록':
|
||||
case '포지션등록':
|
||||
return parsePreferredPositionRegisterArgs(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 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,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import { parsePositionList, PositionParseError } from './position.parser';
|
||||
|
||||
describe('parsePositionList', () => {
|
||||
it('parses two korean positions in priority order', () => {
|
||||
const result = parsePositionList('미드,탑');
|
||||
expect(result.positions).toEqual(['MID', 'TOP']);
|
||||
});
|
||||
|
||||
it('parses english aliases case-insensitively', () => {
|
||||
const result = parsePositionList('JGL, SPT');
|
||||
expect(result.positions).toEqual(['JGL', 'SUP']);
|
||||
});
|
||||
|
||||
it('accepts up to 5 distinct positions', () => {
|
||||
const result = parsePositionList('탑,정글,미드,원딜,서폿');
|
||||
expect(result.positions).toEqual(['TOP', 'JGL', 'MID', 'ADC', 'SUP']);
|
||||
});
|
||||
|
||||
it('rejects when fewer than 2 positions', () => {
|
||||
expect(() => parsePositionList('미드')).toThrow(PositionParseError);
|
||||
expect(() => parsePositionList('미드')).toThrow(/too_few/);
|
||||
});
|
||||
|
||||
it('rejects when more than 5 positions', () => {
|
||||
expect(() => parsePositionList('탑,정글,미드,원딜,서폿,바텀')).toThrow(
|
||||
/too_many/,
|
||||
);
|
||||
});
|
||||
|
||||
it('rejects duplicates after alias normalization', () => {
|
||||
expect(() => parsePositionList('미드,mid')).toThrow(/duplicate/);
|
||||
});
|
||||
|
||||
it('rejects unknown tokens', () => {
|
||||
expect(() => parsePositionList('미드,캐리')).toThrow(/unknown/);
|
||||
});
|
||||
|
||||
it('rejects empty input', () => {
|
||||
expect(() => parsePositionList('')).toThrow(/empty/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,97 @@
|
||||
import { Position } from '../../global/database/schema';
|
||||
|
||||
const POSITION_ALIASES: Record<string, Position> = {
|
||||
// TOP
|
||||
top: 'TOP',
|
||||
탑: 'TOP',
|
||||
top레인: 'TOP',
|
||||
// JUNGLE
|
||||
jgl: 'JGL',
|
||||
jungle: 'JGL',
|
||||
jng: 'JGL',
|
||||
jg: 'JGL',
|
||||
정글: 'JGL',
|
||||
정글러: 'JGL',
|
||||
// MID
|
||||
mid: 'MID',
|
||||
middle: 'MID',
|
||||
미드: 'MID',
|
||||
// ADC (BOTTOM)
|
||||
adc: 'ADC',
|
||||
ad: 'ADC',
|
||||
bot: 'ADC',
|
||||
bottom: 'ADC',
|
||||
원딜: 'ADC',
|
||||
바텀: 'ADC',
|
||||
// SUP
|
||||
sup: 'SUP',
|
||||
supp: 'SUP',
|
||||
spt: 'SUP',
|
||||
sp: 'SUP',
|
||||
support: 'SUP',
|
||||
서폿: 'SUP',
|
||||
서포터: 'SUP',
|
||||
};
|
||||
|
||||
export interface ParsedPositions {
|
||||
positions: Position[];
|
||||
}
|
||||
|
||||
export class PositionParseError extends Error {
|
||||
constructor(
|
||||
public readonly reason:
|
||||
| 'too_few'
|
||||
| 'too_many'
|
||||
| 'duplicate'
|
||||
| 'unknown'
|
||||
| 'empty',
|
||||
public readonly token?: string,
|
||||
) {
|
||||
super(`Position parse error: ${reason}${token ? ` (${token})` : ''}`);
|
||||
}
|
||||
}
|
||||
|
||||
export function parsePositionList(input: string): ParsedPositions {
|
||||
const trimmed = input.trim();
|
||||
if (trimmed.length === 0) {
|
||||
throw new PositionParseError('empty');
|
||||
}
|
||||
|
||||
const tokens = trimmed
|
||||
.split(',')
|
||||
.map((t) => t.trim())
|
||||
.filter((t) => t.length > 0);
|
||||
|
||||
if (tokens.length < 2) {
|
||||
throw new PositionParseError('too_few');
|
||||
}
|
||||
if (tokens.length > 5) {
|
||||
throw new PositionParseError('too_many');
|
||||
}
|
||||
|
||||
const positions: Position[] = [];
|
||||
const seen = new Set<Position>();
|
||||
|
||||
for (const token of tokens) {
|
||||
const key = normalizePositionAlias(token);
|
||||
const mapped = POSITION_ALIASES[key];
|
||||
if (!mapped) {
|
||||
throw new PositionParseError('unknown', token);
|
||||
}
|
||||
if (seen.has(mapped)) {
|
||||
throw new PositionParseError('duplicate', token);
|
||||
}
|
||||
seen.add(mapped);
|
||||
positions.push(mapped);
|
||||
}
|
||||
|
||||
return { positions };
|
||||
}
|
||||
|
||||
export function isPositionAlias(input: string): boolean {
|
||||
return Boolean(POSITION_ALIASES[normalizePositionAlias(input)]);
|
||||
}
|
||||
|
||||
function normalizePositionAlias(input: string): string {
|
||||
return input.toLowerCase().replace(/\s+/g, '');
|
||||
}
|
||||
Reference in new issue
Block a user