diff --git a/src/lib/riitag/neo/std/Covers.ts b/src/lib/riitag/neo/std/Covers.ts index 4ab3d35..19e3cdc 100644 --- a/src/lib/riitag/neo/std/Covers.ts +++ b/src/lib/riitag/neo/std/Covers.ts @@ -11,6 +11,16 @@ import { user } from '@prisma/client' const xml2js = require('xml2js') +const pg = require('pg'); +const { Client } = pg +const client = new Client({ + host: 'localhost', + user: 'postgres', + password: 'postgres', + database: 'riitag', + port: 2345 +}) + export default class Covers extends ModuleBase { x: number y: number @@ -186,12 +196,32 @@ export default class Covers extends ModuleBase { return cover } + async fix (userId: number) { + await client.connect() + const res = await client.query('SELECT * FROM playlog WHERE user_id=$1', [userId]) + console.log(res.rows) // Hello world! + + for (const row of res.rows) { + console.log(row) + + const res = await client.query('SELECT * FROM game WHERE game_pk=$1', [row.game_pk]) + if (res.rows.length === 0) { + console.log('No game found for game_pk', row.game_pk) + await client.query('DELETE FROM playlog WHERE game_pk=$1', [row.game_pk]) + } + console.log(res.rows) + } + + await client.end() + } + /** * Get all of the covers for a user. * @param user The user to get the covers for. * @returns */ async getAllUserCovers (user: user): Promise { + await this.fix(user.id) const playlog = await prisma.playlog.findMany({ where: { user: { @@ -199,8 +229,12 @@ export default class Covers extends ModuleBase { } }, select: { - game_pk: true, - playlog_id: true + game: { + select: { + game_id: true, + console: true + } + } }, orderBy: { played_on: 'desc' @@ -214,45 +248,9 @@ export default class Covers extends ModuleBase { return [] } - for (const entry of playlog) { - const doesntmatter = await prisma.game.findFirst({ - where: { - game_pk: entry.game_pk - } - }) - if (!doesntmatter || doesntmatter.console === CONSOLE.THREEDS) { - await prisma.playlog.delete({ - where: { - playlog_id: entry.playlog_id - } - }) - } - } - - const otherPlaylog = await prisma.playlog.findMany({ - where: { - user: { - id: user.id - } - }, - select: { - game: { - select: { - console: true, - game_id: true - } - } - }, - orderBy: { - played_on: 'desc' - }, - distinct: ['game_pk'], - take: this.max - }) - const coverPaths = [] - for (const logEntry of otherPlaylog) { + for (const logEntry of playlog) { coverPaths.push(this.getCover( logEntry.game.console, user.cover_type,