From 757face0fce2ef3d7d1b4e013ae563f2a779a7df Mon Sep 17 00:00:00 2001 From: Vince Au Date: Sun, 29 Oct 2023 18:40:14 +1100 Subject: [PATCH] clean up code --- src/database/create_database.ts | 1 - src/database/migrate_to_latest.ts | 1 - .../database_replay_provider.ts | 14 ++++++-------- .../database_replay_provider/record_mapper.ts | 6 +++--- 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/database/create_database.ts b/src/database/create_database.ts index 955f28563..51eb8ecd0 100644 --- a/src/database/create_database.ts +++ b/src/database/create_database.ts @@ -29,7 +29,6 @@ export async function createDatabase(databasePath?: string): Promise { if (result.status === "Success") { log.info(`Migration "${result.migrationName}" was executed successfully`); diff --git a/src/database/migrate_to_latest.ts b/src/database/migrate_to_latest.ts index fddaddf8c..78c71bf90 100644 --- a/src/database/migrate_to_latest.ts +++ b/src/database/migrate_to_latest.ts @@ -16,7 +16,6 @@ export async function migrateToLatest(db: Kysely, migrationFolder: str }); const { error, results } = await migrator.migrateToLatest(); - if (error || !results) { throw new Error(`Error migrating database: ${error}`); } diff --git a/src/replays/database_replay_provider/database_replay_provider.ts b/src/replays/database_replay_provider/database_replay_provider.ts index 77c9299c0..d5d0094f8 100644 --- a/src/replays/database_replay_provider/database_replay_provider.ts +++ b/src/replays/database_replay_provider/database_replay_provider.ts @@ -1,19 +1,19 @@ import { chunk } from "@common/chunk"; import { partition } from "@common/partition"; -import type { FileLoadResult, FileResult, PlayerInfo, Progress, ReplayProvider } from "@replays/types"; +import type { FileLoadResult, FileResult, Progress, ReplayProvider } from "@replays/types"; import type { StadiumStatsType, StatsType } from "@slippi/slippi-js"; import { SlippiGame } from "@slippi/slippi-js"; import * as GameRepository from "database/repositories/game_repository"; import * as PlayerRepository from "database/repositories/player_repository"; import * as ReplayRepository from "database/repositories/replay_repository"; -import type { Database, NewGame, NewPlayer, NewReplay } from "database/schema"; +import type { Database, NewGame, NewPlayer, NewReplay, Player } from "database/schema"; import log from "electron-log"; import * as fs from "fs-extra"; import type { Kysely } from "kysely"; import path from "path"; import { extractPlayerNames } from "../file_system_replay_provider/extract_player_names"; -import { mapGameRecordToFileResult, mapPlayerRecordToPlayerInfo } from "./record_mapper"; +import { mapGameRecordToFileResult } from "./record_mapper"; const NUM_REPLAYS_TO_RETURN = 200; const INSERT_REPLAY_BATCH_SIZE = 200; @@ -40,8 +40,7 @@ export class DatabaseReplayProvider implements ReplayProvider { } const playerRecords = await PlayerRepository.findAllPlayersByGame(this.db, gameRecord._id); - const players = playerRecords.map(mapPlayerRecordToPlayerInfo); - return mapGameRecordToFileResult(gameRecord, players); + return mapGameRecordToFileResult(gameRecord, playerRecords); } public async loadFolder(folder: string, onProgress?: (progress: Progress) => void): Promise { @@ -59,10 +58,9 @@ export class DatabaseReplayProvider implements ReplayProvider { const gameRecords = await GameRepository.findGamesByFolder(this.db, folder, NUM_REPLAYS_TO_RETURN); const players = await Promise.all( - gameRecords.map(async ({ _id: gameId }): Promise<[number, PlayerInfo[]]> => { + gameRecords.map(async ({ _id: gameId }): Promise<[number, Player[]]> => { const playerRecords = await PlayerRepository.findAllPlayersByGame(this.db, gameId); - const playerInfos = playerRecords.map(mapPlayerRecordToPlayerInfo); - return [gameId, playerInfos]; + return [gameId, playerRecords]; }), ); const playerMap = new Map(players); diff --git a/src/replays/database_replay_provider/record_mapper.ts b/src/replays/database_replay_provider/record_mapper.ts index 9dba34617..bdf172103 100644 --- a/src/replays/database_replay_provider/record_mapper.ts +++ b/src/replays/database_replay_provider/record_mapper.ts @@ -3,7 +3,7 @@ import type { Game, Player, Replay } from "database/schema"; import moment from "moment"; import path from "path"; -export function mapPlayerRecordToPlayerInfo(player: Player): PlayerInfo { +function mapPlayerRecordToPlayerInfo(player: Player): PlayerInfo { return { playerIndex: player.index, port: player.index + 1, @@ -19,14 +19,14 @@ export function mapPlayerRecordToPlayerInfo(player: Player): PlayerInfo { }; } -export function mapGameRecordToFileResult(gameRecord: Game & Replay, players: PlayerInfo[]): FileResult { +export function mapGameRecordToFileResult(gameRecord: Game & Replay, playerRecords: Player[]): FileResult { const fullPath = path.resolve(gameRecord.folder, gameRecord.file_name); return { id: `${gameRecord._id}-${gameRecord.replay_id}`, fileName: gameRecord.file_name, fullPath, game: { - players, + players: playerRecords.map(mapPlayerRecordToPlayerInfo), isTeams: Boolean(gameRecord.is_teams), stageId: gameRecord.stage, startTime: inferStartTime(gameRecord.start_time, gameRecord.file_name, gameRecord.birth_time),