Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 試合走行結果取得の実装 #390

Merged
merged 9 commits into from
Oct 4, 2024
37 changes: 37 additions & 0 deletions packages/kcms/src/match/service/fetchRunResult.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, expect, it } from 'vitest';
import { FetchRunResultService } from './fetchRunResult';
import { DummyMainMatchRepository } from '../adaptor/dummy/mainMatchRepository';
import { DummyPreMatchRepository } from '../adaptor/dummy/preMatchRepository';
import { testRankingMainMatchData, testRankingPreMatchData } from '../../testData/match';
import { MainMatchID } from '../model/main';
import { PreMatchID } from '../model/pre';
import { Result } from '@mikuroxina/mini-fn';

describe('FetchRunResultService', () => {
const dummyMainMatchRepository = new DummyMainMatchRepository(testRankingMainMatchData);
const dummyPreMatchRepository = new DummyPreMatchRepository(testRankingPreMatchData);
const fetchRunResultService = new FetchRunResultService(
dummyMainMatchRepository,
dummyPreMatchRepository
);
it('MainMatchの走行結果が取得できる', async () => {
const res = await fetchRunResultService.handle('main', '900' as MainMatchID);
expect(Result.isErr(res)).toBe(false);
const mainMatch = Result.unwrap(res);
expect(mainMatch).toStrictEqual(testRankingMainMatchData[0].getRunResults());
});
it('PreMatchの走行結果が取得できる', async () => {
const res = await fetchRunResultService.handle('pre', '100' as PreMatchID);
expect(Result.isErr(res)).toBe(false);
const preMatch = Result.unwrap(res);
expect(preMatch).toStrictEqual(testRankingPreMatchData[0].getRunResults());
});
it('MainMatchの走行結果が無ければエラーを返す', async () => {
const res = await fetchRunResultService.handle('main', '0' as MainMatchID);
expect(Result.isErr(res)).toBe(true);
});
it('PreMatchの走行結果が無ければエラーを返す', async () => {
const res = await fetchRunResultService.handle('pre', '0' as PreMatchID);
expect(Result.isErr(res)).toBe(true);
});
});
33 changes: 33 additions & 0 deletions packages/kcms/src/match/service/fetchRunResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { MatchType } from 'config';
import { RunResult } from '../model/runResult';
import { MainMatchID } from '../model/main';
import { PreMatchID } from '../model/pre';
import { Option, Result } from '@mikuroxina/mini-fn';
import { MainMatchRepository, PreMatchRepository } from '../model/repository';

export class FetchRunResultService {
constructor(
private readonly mainMatchRepository: MainMatchRepository,
private readonly preMatchRepository: PreMatchRepository
) {}
async handle(
matchType: MatchType,
matchID: MainMatchID | PreMatchID
): Promise<Result.Result<Error, RunResult[]>> {
if (matchType === 'main') {
const mainMatchRes = await this.mainMatchRepository.findByID(matchID as MainMatchID);
if (Option.isNone(mainMatchRes)) {
return Result.err(new Error('MainMatch not found'));
}
const mainMatch = Option.unwrap(mainMatchRes);
return Result.ok(mainMatch.getRunResults());
} else {
const preMatchRes = await this.preMatchRepository.findByID(matchID as PreMatchID);
if (Option.isNone(preMatchRes)) {
return Result.err(new Error('PreMatch not found'));
}
const preMatch = Option.unwrap(preMatchRes);
return Result.ok(preMatch.getRunResults());
}
}
}
39 changes: 39 additions & 0 deletions packages/kcms/src/testData/match.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TeamID } from '../team/models/team.js';
import { RunResult, RunResultID } from '../match/model/runResult.js';
import { PreMatch, PreMatchID } from '../match/model/pre.js'; // ToDo: もっとデータ数を増やす
import { MainMatch, MainMatchID } from '../match/model/main.js';

// ランキング生成用の試合データ
// Openは予選がないので、予選の試合データはない
Expand Down Expand Up @@ -234,4 +235,42 @@ export const testRankingPreMatchData = [

export const testRankingMainMatchData = [
// ToDo: L/Rを入れるとL/Rを入れ替えたデータも作れるようにしたい
MainMatch.new({
id: '900' as MainMatchID,
courseIndex: 1,
matchIndex: 1,
teamId1: '91' as TeamID,
teamId2: '92' as TeamID,
winnerId: '91' as TeamID,
runResults: [
RunResult.new({
id: '80' as RunResultID,
teamId: '91' as TeamID,
points: 12,
goalTimeSeconds: 60,
finishState: 'GOAL',
}),
RunResult.new({
id: '81' as RunResultID,
teamId: '92' as TeamID,
points: 10,
goalTimeSeconds: Infinity,
finishState: 'FINISHED',
}),
RunResult.new({
id: '82' as RunResultID,
teamId: '91' as TeamID,
points: 12,
goalTimeSeconds: Infinity,
finishState: 'FINISHED',
}),
RunResult.new({
id: '83' as RunResultID,
teamId: '92' as TeamID,
points: 1,
goalTimeSeconds: 80,
finishState: 'GOAL',
}),
],
}),
];