Skip to content

Commit

Permalink
fix: Playlist importing
Browse files Browse the repository at this point in the history
  • Loading branch information
colin969 committed Oct 21, 2023
1 parent f24630e commit ea1830c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 47 deletions.
48 changes: 46 additions & 2 deletions src/back/PlaylistFile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { deepCopy, overwritePlaylistData, readJsonFile, readJsonFileSync, stringifyJsonDataFile } from '@shared/Util';
import { Playlist } from 'flashpoint-launcher';
import { deepCopy, readJsonFile, readJsonFileSync, stringifyJsonDataFile } from '@shared/Util';
import { IObjectParserProp, ObjectParser } from '@shared/utils/ObjectParser';
import { Playlist, PlaylistGame } from 'flashpoint-launcher';
import * as fs from 'fs';
import { uuid } from './util/uuid';
import { str } from '@shared/utils/Coerce';

export namespace PlaylistFile {
export function readFile(filePath: string, onError?: (error: string) => void): Promise<Playlist> {
Expand Down Expand Up @@ -89,3 +92,44 @@ const DEFAULT_PLAYLIST_DATA: Playlist = {
icon: '',
extreme: false
};

export function overwritePlaylistData(
source: Playlist,
data: Partial<Playlist>,
onError?: (error: string) => void
): Playlist {
const parser = new ObjectParser({
input: data,
onError: onError && (e => onError(`Error while parsing Playlist: ${e.toString()}`)),
});
parser.prop('id', v => source.id = str(v), true);
parser.prop('title', v => source.title = str(v));
parser.prop('description', v => source.description = str(v));
parser.prop('icon', v => source.icon = str(v));
parser.prop('library', v => source.library = str(v));
parser.prop('author', v => source.author = str(v));
parser.prop('extreme', v => source.extreme = !!v);
if (!source.id) {
source.id = uuid();
}
if (data.games) {
const newGames: PlaylistGame[] = [];
parser.prop('games').array((item, index) => newGames.push(parsePlaylistGame(item as IObjectParserProp<PlaylistGame>)));
source.games = newGames;
}
return source;
}

function parsePlaylistGame(parser: IObjectParserProp<any>): PlaylistGame {
const game: PlaylistGame = {
notes: '',
gameId: ''
};
parser.prop('id', v => game.gameId = str(v), true);
parser.prop('gameId', v => game.gameId = str(v), true);
parser.prop('notes', v => game.notes = str(v));
if (!game.gameId) {
throw 'No ID for playlist game';
}
return game;
}
46 changes: 1 addition & 45 deletions src/shared/Util.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import * as axiosImport from 'axios';
import { Game, Playlist, PlaylistGame, Tag, TagCategory, TagFilterGroup } from 'flashpoint-launcher';
import { Game, Tag, TagCategory, TagFilterGroup } from 'flashpoint-launcher';
import * as fs from 'fs';
import { camelCase, snakeCase, transform } from 'lodash';
import * as path from 'path';
import { DownloadDetails } from './back/types';
import { AppConfigData } from './config/interfaces';
import { str } from './utils/Coerce';
import { IObjectParserProp, ObjectParser } from './utils/ObjectParser';
import { parseVariableString } from './utils/VariableString';
import { throttle } from './utils/throttle';
import { uuid } from './utils/uuid';

const axios = axiosImport.default;

Expand Down Expand Up @@ -498,47 +495,6 @@ export function compare(a: string, b: string): number {
}
}

export function overwritePlaylistData(
source: Playlist,
data: Partial<Playlist>,
onError?: (error: string) => void
): Playlist {
const parser = new ObjectParser({
input: data,
onError: onError && (e => onError(`Error while parsing Playlist: ${e.toString()}`)),
});
parser.prop('id', v => source.id = str(v), true);
parser.prop('title', v => source.title = str(v));
parser.prop('description', v => source.description = str(v));
parser.prop('icon', v => source.icon = str(v));
parser.prop('library', v => source.library = str(v));
parser.prop('author', v => source.author = str(v));
parser.prop('extreme', v => source.extreme = !!v);
if (!source.id) {
source.id = uuid();
}
if (data.games) {
const newGames: PlaylistGame[] = [];
parser.prop('games').array((item, index) => newGames.push(parsePlaylistGame(item as IObjectParserProp<PlaylistGame>)));
source.games = newGames;
}
return source;
}

function parsePlaylistGame(parser: IObjectParserProp<any>): PlaylistGame {
const game: PlaylistGame = {
notes: '',
gameId: ''
};
parser.prop('id', v => game.gameId = str(v), true);
parser.prop('gameId', v => game.gameId = str(v), true);
parser.prop('notes', v => game.notes = str(v));
if (!game.gameId) {
throw 'No ID for playlist game';
}
return game;
}

export function mapFpfssGameToLocal(data: any, categories: TagCategory[]): Game {
const game = camelify(data) as unknown as Game;
// Initialize nil variables which should be arrays
Expand Down

0 comments on commit ea1830c

Please sign in to comment.