Skip to content
This repository has been archived by the owner on May 18, 2022. It is now read-only.

Commit

Permalink
Begin work on a new Library Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Mafrans committed Sep 10, 2020
1 parent 34e6e9d commit 19dc578
Show file tree
Hide file tree
Showing 9 changed files with 784 additions and 1,642 deletions.
1,517 changes: 435 additions & 1,082 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/Component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Logger from "./Logger";
import { SyncStorage } from "./Storage";
import { Language } from "./Language";
import { StadiaGameDB } from "./StadiaGameDB";

/**
* A generic component of Stadia+
Expand Down
148 changes: 148 additions & 0 deletions src/StadiaGameDB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { WebDatabase } from "./WebDatabase";

export namespace StadiaGameDB {

export function update() {
return DB.update();
}

export function get(uuid: string): StadiaGameDB.Game {
return DB.get(uuid);
}

export function random() {
return DB.random();
}

export function getGames() {
return DB.games;
}

class DB {
static games: {[uuid: string]: StadiaGameDB.Game} = {};
private static gameDB: WebDatabase;
private static uuidMap: WebDatabase;

private static async connect(): Promise<void> {
DB.gameDB = new WebDatabase('https://stadiagamedb.com/data/gamedb.json');
DB.uuidMap = new WebDatabase('https://stadiagamedb.com/data/uuidmap.json');

await DB.gameDB.connect();
await DB.uuidMap.connect();
}

static async update() {
await DB.connect();

const uuids = DB.uuidMap.getConnection().uuidMap;
const games = DB.gameDB.getConnection().data;

console.log({uuids, games});

for(const uuid in uuids) {
const entry = games[uuids[uuid]];
let game: StadiaGameDB.Game = {
uuid: uuid,
img: 'https://stadiagamedb.com/' + entry[0].match(/(images\/posters\/[a-z0-9_.-]+.png)/g),
name: entry[1],
tags: entry[2].split(', ').map((e: string) => StadiaGameDB.Tag.fromId(e.toLowerCase())),
date: entry[3],
resolution: entry[4],
onlineTypes: entry[5].split(', ').map((e: string) => StadiaGameDB.OnlineType.fromId(e.toLowerCase())),
rating: parseInt(entry[6]) === NaN ? null : parseInt(entry[6])
}

DB.games[uuid] = game;
}
}

static get(uuid: string) {
return DB.games[uuid];
}

static random() {
return DB.games[Object.keys(DB.games)[Math.floor(Math.random() * Object.keys(DB.games).length)]];
}
}

export interface Game {
uuid: string;
img: string;
name: string;
tags: Tag[];
date: string;
resolution: string;
onlineTypes: OnlineType[];
rating: number;
}

export class OnlineType {
public id: string;
public name: string;

static SINGLEPLAYER: OnlineType = { id: 'single player', name: 'Singleplayer', };
static MULTIPLAYER: OnlineType = { id: 'online multiplayer', name: 'Multiplayer', };
static ONLINE_COOP: OnlineType = { id: 'online co-op', name: 'Co-op' };
static LOCAL_MULTIPLAYER: OnlineType = { id: 'local multiplayer', name: 'Local Multiplayer', };
static LOCAL_COOP: OnlineType = { id: 'local co-op', name: 'Local Co-op', };
static SPLITSCREEN: OnlineType = { id: 'split screen', name: 'Splitscreen', };
static COMPETITIVE: OnlineType = { id: 'competitive', name: 'Competitive', };
static CROSS_PLATFORM: OnlineType = { id: 'cross platform multiplayer', name: 'Cross Platform', };

private static types: OnlineType[] = [
OnlineType.SINGLEPLAYER,
OnlineType.MULTIPLAYER,
OnlineType.ONLINE_COOP,
OnlineType.LOCAL_MULTIPLAYER,
OnlineType.LOCAL_COOP,
OnlineType.SPLITSCREEN,
OnlineType.COMPETITIVE,
OnlineType.CROSS_PLATFORM,
];

static fromId(id: string) {
return OnlineType.types.find(e => e.id === id.toLowerCase());
}
}

export class Tag {
public id: string;
public name: string;

static ACTION: Tag = { id: 'action', name: 'Action' };
static ADVENTURE: Tag = { id: 'adventure', name: 'Adventure' };
static SHOOTER: Tag = { id: 'shooter', name: 'Shooter' };
static ROLEPLAYING_GAME: Tag = { id: 'role-playing game', name: 'Role-playing Game', };
static ARCADE: Tag = { id: 'arcade', name: 'Arcade' };
static PUZZLE: Tag = { id: 'puzzle', name: 'Puzzle' };
static KIDS_AND_FAMILY: Tag = { id: 'kids & family', name: 'Kids & Family', };
static RACING: Tag = { id: 'racing', name: 'Racing' };
static FIGHTING: Tag = { id: 'fighting', name: 'Fighting' };
static MUSIC: Tag = { id: 'music or rhythm', name: 'Music' };
static SIMULATION: Tag = { id: 'simulation', name: 'Simulation' };
static SPORTS: Tag = { id: 'sports', name: 'Sports' };
static STRATEGY: Tag = { id: 'strategy', name: 'Strategy' };
static TABLETOP: Tag = { id: 'board game', name: 'Tabletop' };

private static tags: Tag[] = [
Tag.ACTION,
Tag.ADVENTURE,
Tag.SHOOTER,
Tag.ROLEPLAYING_GAME,
Tag.ARCADE,
Tag.PUZZLE,
Tag.KIDS_AND_FAMILY,
Tag.RACING,
Tag.FIGHTING,
Tag.MUSIC,
Tag.SIMULATION,
Tag.SPORTS,
Tag.STRATEGY,
Tag.TABLETOP,
];

static fromId(id: string) {
return Tag.tags.find(e => e.id === id.toLowerCase());
}
}
}
63 changes: 33 additions & 30 deletions src/WebDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,44 @@ import Logger from "./Logger";

export class WebDatabase {
url: string;
connected: boolean;
connected: boolean = false;
connection: any;

constructor(url: string) {
this.url = url;
}

connect(callback?: (connection:any) => {}) {
if(this.connected) {
Logger.error('Error: Already connected to the database.');
return;
}

const self = this;
const xhr = new XMLHttpRequest();
xhr.open("GET", this.url, true);
xhr.onload = function (e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
self.connected = true;
self.connection = JSON.parse(xhr.responseText);
if(callback)
callback(self.connection);
}
else {
self.connected = false;
Logger.error('Error when connecting to database:', xhr.statusText);
}
connect(): Promise<any> {
return new Promise((resolve, reject) => {
if(this.connected) {
Logger.error('Error: Already connected to the database.');
return;
}
};
xhr.onerror = function (e) {
self.connected = false;
Logger.error('Error when connecting to database:', xhr.statusText);
};
xhr.send(null);

const self = this;
const xhr = new XMLHttpRequest();
xhr.open("GET", this.url, true);
xhr.onload = function (e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
self.connected = true;
self.connection = JSON.parse(xhr.responseText);
resolve(self.connection);
}
else {
self.connected = false;
reject();
Logger.error('Error when connecting to database:', xhr.statusText);
}
}
};
xhr.onerror = function (e) {
self.connected = false;
reject();
Logger.error('Error when connecting to database:', xhr.statusText);
};
xhr.send(null);
})
}

getConnection(): any {
Expand All @@ -52,8 +55,8 @@ export class WebDatabase {
this.connected = false;
}

reconnect(callback?: (connection:object) => {}) {
async reconnect(): Promise<any> {
this.disconnect();
this.connect(callback);
return this.connect();
}
}
Loading

0 comments on commit 19dc578

Please sign in to comment.