diff --git a/src/match/controller.ts b/src/match/controller.ts index 44dc79c..c49e6cc 100644 --- a/src/match/controller.ts +++ b/src/match/controller.ts @@ -66,9 +66,6 @@ export class MatchController { } async getMatchByType(matchType: string) { - if (matchType !== "primary") { - return Result.err(new Error("not implemented")); - } const res = await this.getService.findByType(matchType); if (Result.isErr(res)) { return Result.err(res[1]); diff --git a/src/match/service/generate.test.ts b/src/match/service/generate.test.ts index 1691e6b..db500a6 100644 --- a/src/match/service/generate.test.ts +++ b/src/match/service/generate.test.ts @@ -23,7 +23,7 @@ const generateDummyData = (n: number): Entry[] => { return res; }; -describe("予選の対戦表を正しく生成できる", () => { +describe("予選の対戦表を正しく生成できる", async () => { const repository = new DummyRepository(); const matchRepository = new DummyMatchRepository(); const service = new GenerateMatchService(repository, matchRepository); @@ -89,6 +89,6 @@ describe("本選の対戦表を正しく生成できる", async () => { }); it("本選の対戦表を正しく生成できる", async () => { - await service.generateFinalMatch("open"); + await service.generateFinalMatch("elementary"); }); }); diff --git a/src/match/service/generate.ts b/src/match/service/generate.ts index dc2ba9c..2ef6b56 100644 --- a/src/match/service/generate.ts +++ b/src/match/service/generate.ts @@ -24,6 +24,7 @@ type Tuple = BaseTuple; export class GenerateMatchService { private readonly COURSE_COUNT = 3; + private readonly FINAL_TOURNAMENT_COUNT = 8; private readonly entryRepository: EntryRepository; private readonly matchRepository: MatchRepository; @@ -67,7 +68,8 @@ export class GenerateMatchService { for (let k = 0; k < courses[i].length; k++) { const courseLength = courses[i].length; const gap = Math.floor(courseLength / 2); - const opponentIndex = k + gap >= courseLength ? (k+gap) - courseLength : k + gap; + const opponentIndex = + k + gap >= courseLength ? k + gap - courseLength : k + gap; const match = Match.new({ id: crypto.randomUUID(), matchType: "primary", @@ -88,7 +90,6 @@ export class GenerateMatchService { return Result.ok(tmpMatches); } - // ToDo: 本選トーナメント対戦表の生成 async generateFinalMatch( category: "elementary" | "open", ): Promise> { @@ -97,10 +98,14 @@ export class GenerateMatchService { 1 vs 8, 4 vs 5, 2 vs 7, 3 vs 6 (数字は順位) */ - const [elementaryRank, openRank] = await this.generateRanking(); + const [elementaryRank] = await this.generateRanking(); + const openRank = await this.generateOpenTournament(); const [elementaryTournament, openTournament] = [ this.generateTournamentPair(this.generateTournament(elementaryRank)), - this.generateTournamentPair(this.generateTournament(openRank)), + // fixme: unwrapやめる + this.generateTournamentPair( + this.generateTournament(Result.unwrap(openRank)), + ), ]; const matches: Match[] = []; @@ -117,12 +122,15 @@ export class GenerateMatchService { } } else { for (const v of openTournament) { - Match.new({ - id: crypto.randomUUID(), - matchType: "final", - teams: { Left: v[0].entry, Right: v[1].entry }, - courseIndex: 0, - }); + console.log(v[0].entry.id, v[1].entry.id); + matches.push( + Match.new({ + id: crypto.randomUUID(), + matchType: "final", + teams: { Left: v[0].entry, Right: v[1].entry }, + courseIndex: 0, + }), + ); } } @@ -135,7 +143,6 @@ export class GenerateMatchService { // ToDo: 本戦の順位を計算できるようにする // - ToDo: (予選)タイムと得点が同じ場合だったときの順位決定処理 - // - ToDo: 部門ごとにランキングを生成できるように -> OK async generateRanking(): Promise { const res = await this.matchRepository.findAll(); if (Result.isErr(res)) { @@ -215,11 +222,36 @@ export class GenerateMatchService { return v; }); }; - return [sortAndRanking(categoryRank[0]), sortAndRanking(categoryRank[1])]; } + // generateOpenTournament オープン部門は予選を行わないのでランキング(?)を無理やり作る + private async generateOpenTournament(): Promise< + Result.Result + > { + const res = await this.entryRepository.findAll(); + if (Result.isErr(res)) { + return Result.err(res[1]); + } + + return Result.ok( + res[1] + .filter((v) => v.category === "Open") + .map((v, i): TournamentRank => { + return { + rank: i, + points: 0, + time: 0, + entry: v, + }; + }), + ); + } + private generateTournament(ranks: TournamentRank[]): TournamentPermutation { + // ランキング上位8チームを取得 + ranks = ranks.slice(0, this.FINAL_TOURNAMENT_COUNT); + const genTournament = ( ids: TournamentRank[] | Tournament[] | Tournament, ): TournamentPermutation => {