Skip to content

Commit

Permalink
clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
vinceau committed Oct 30, 2023
1 parent 3f6db61 commit 469f333
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 13 deletions.
1 change: 0 additions & 1 deletion src/database/create_database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export async function createDatabase(databasePath?: string): Promise<Kysely<Data
log.info(`Running migrations in ${migrationsFolder}`);

const results = await migrateToLatest(database, migrationsFolder);

results.forEach((result) => {
if (result.status === "Success") {
log.info(`Migration "${result.migrationName}" was executed successfully`);
Expand Down
1 change: 0 additions & 1 deletion src/database/migrate_to_latest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export async function migrateToLatest(db: Kysely<Database>, migrationFolder: str
});

const { error, results } = await migrator.migrateToLatest();

if (error || !results) {
throw new Error(`Error migrating database: ${error}`);
}
Expand Down
14 changes: 6 additions & 8 deletions src/replays/database_replay_provider/database_replay_provider.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<FileLoadResult> {
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/replays/database_replay_provider/record_mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand Down

0 comments on commit 469f333

Please sign in to comment.