Commit 504dff53 authored by nanahira's avatar nanahira

add currentmatch

parent 1cfb1453
......@@ -177,9 +177,23 @@ export class TournamentWrapper {
);
}
getCurrentMatchFromId(id: number) {
return this.tournament.matches.find(
(m) => m.match.state !== 'complete' && m.hasPlayer(id),
getParticipantFromParticipantId(id: number) {
return this.tournament.participants.find((p) => p.participant.id === id);
}
getCurrentMatchFromId(id: number, fetchLastCompleted = false) {
const allRelatedMatches = this.tournament.matches.filter((m) =>
m.hasPlayer(id),
);
const currentMatch = allRelatedMatches.find(
(m) => m.match.state !== 'complete',
);
if (currentMatch) {
return currentMatch;
}
if (!fetchLastCompleted) {
return;
}
return allRelatedMatches[allRelatedMatches.length - 1];
}
}
......@@ -130,6 +130,29 @@ export class YGOTournamentPlugin {
}
}
private async onUserGetCurrentMatch(userId: string) {
const tournament = await this.fetchChallongeData();
const participant = tournament.getParticipantFromId(userId);
if (!participant) {
return '未找到您的参赛信息。';
}
const currentMatch = tournament.getCurrentMatchFromId(
participant.participant.id,
true,
);
if (!currentMatch) {
return '您没有正在进行的比赛。';
}
const currentMatchInfo = currentMatch.match;
const player1Name = tournament.getParticipantFromParticipantId(
currentMatchInfo.player1_id,
).participant.name;
const player2Name = tournament.getParticipantFromParticipantId(
currentMatchInfo.player2_id,
).participant.name;
return `您当前的对局信息:\n${player1Name}\t${currentMatchInfo.scores_csv}\t${player2Name}`;
}
private async onUserDeclareLate(userId: string) {
const timeUnix = await this.ctx.cache.get('lateDeclarationTime', 'current');
// this.ctx.logger('test').warn(JSON.stringify(timeUnix));
......@@ -257,6 +280,24 @@ export class YGOTournamentPlugin {
this.ctx.on('service/cache', () => {
this.ctx.cache.table('lateDeclarationTime', { maxAge: 3600 * 1000 });
});
this.ctx
.command('tournament/currentmatch', '获取当前对局')
.shortcut('获取当前对局')
.usage('获取自己的当前对局和比分。')
.action(async ({ session }) => {
try {
return await this.onUserGetCurrentMatch(session.userId);
} catch (e) {
this.ctx
.logger('challonge')
.error(
`Failed to fetch current match of ${
session.userId
}: ${e.toString()}`,
);
return '获取比赛出现了一些问题,请与技术人员联系。';
}
});
this.ctx
.command('tournament/late', '迟到杀')
.shortcut('迟到杀')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment