TheOchulus/databaseHelperFunctions.js

450 lines
10 KiB
JavaScript
Raw Permalink Normal View History

2024-07-13 22:00:14 +02:00
const { Users, Games, LoggedGames, Changelog } = require ('./dbObjects.js');
2023-12-30 23:45:52 +01:00
const fs = require('fs');
async function checkUserRegistration(user) {
2023-12-27 21:43:40 +01:00
const u = await Users.findOne({ where: { discord_id: user.id } })
.catch((err) => {
console.log(err);
});
if (u) return true;
return false;
}
2023-12-19 00:53:12 +01:00
async function getUserRegistration(user) {
let u = await Users.findOne({ where: { discord_id: user.id } })
.catch((err) => {
console.log(err);
});
if (u) return u;
await Users.create({ discord_id: user.id, username: user.username })
.then((data) => {
u = data;
})
.catch((err) => {
console.log(err);
});
if (u) return u;
return null;
}
2024-02-16 18:04:29 +01:00
async function getUserFromId(id) {
const u = await Users.findOne({ where: { id: id } })
.catch((err) => {
console.log(err);
});
if (u) return u;
return null;
}
2023-12-19 00:53:12 +01:00
async function checkGameStorage(game) {
let g = await Games.findOne({ where: { igdb_id: game.id, name: game.name } })
.catch((err) => {
console.log(err);
});
if (g) return g;
await Games.create({ igdb_id: game.id, name: game.name })
.then((data) => {
g = data;
})
.catch((err) => {
console.log(err);
});
if (g) return g;
return null;
}
2023-12-19 21:42:54 +01:00
async function checkGameStorageId(id) {
const g = await Games.findOne({ where: { id: id } })
.catch((err) => {
console.log(err);
});
if (g) return g;
return null;
}
async function createPlanningGameEntry(user, game, date) {
const entry = await checkLoggedGameEntry(user, game);
if (!entry) return await createLoggedGameEntry(user, game, 'planning', date);
if (entry.status == 'planning') return false;
await createChangelogEntry(user, game, entry.status, 'planning');
2024-02-11 22:04:55 +01:00
entry.status = 'planning';
if (!date) {
entry.statusLastChanged = new Date();
}
else {
entry.statusLastChanged = date;
}
2024-02-11 22:04:55 +01:00
await entry.save();
return entry;
2024-02-11 20:35:22 +01:00
}
async function createPlayingGameEntry(user, game, date) {
const entry = await checkLoggedGameEntry(user, game);
if (!entry) return await createLoggedGameEntry(user, game, 'playing', date);
if (entry.status == 'playing') return false;
await createChangelogEntry(user, game, entry.status, 'playing');
2024-02-11 22:04:55 +01:00
entry.status = 'playing';
if (!date) {
entry.statusLastChanged = new Date();
}
else {
entry.statusLastChanged = date;
}
2024-02-11 22:04:55 +01:00
await entry.save();
2024-02-11 22:17:21 +01:00
return entry;
2024-02-11 20:35:22 +01:00
}
async function createBeatenGameEntry(user, game, date) {
const entry = await checkLoggedGameEntry(user, game);
if (!entry) return await createLoggedGameEntry(user, game, 'beat', date);
if (entry.status == 'beat') return false;
await createChangelogEntry(user, game, entry.status, 'beat');
2024-02-11 22:04:55 +01:00
entry.status = 'beat';
if (!date) {
entry.statusLastChanged = new Date();
}
else {
entry.statusLastChanged = date;
}
2024-02-11 22:04:55 +01:00
await entry.save();
return entry;
2024-02-11 20:35:22 +01:00
}
async function createChangelogEntry(user, game, oldStatus, newStatus) {
return await Changelog.create({ userId: user.id, gameId: game.id, newStatus: newStatus, oldStatus: oldStatus })
.catch((err) => {
console.log(err);
});
}
async function checkLoggedGameEntry(user, game) {
const bg = await LoggedGames.findOne({ where: { userId: user.id, gameId: game.id } })
2023-12-19 00:53:12 +01:00
.catch((err) => {
console.log(err);
});
if (!bg) return false;
return bg;
}
async function createLoggedGameEntry(user, game, status, date) {
let bg;
2023-12-19 00:53:12 +01:00
await LoggedGames.create({ userId: user.id, gameId: game.id, status: status, statusLastChanged: date })
2023-12-19 00:53:12 +01:00
.then((data) => {
bg = data;
})
.catch((err) => {
console.log(err);
});
if (bg) {
await Changelog.create({ userId: user.id, gameId: game.id, newStatus: status })
.catch((err) => {
console.log(err);
});
return true;
}
2023-12-19 00:53:12 +01:00
return false;
}
2024-02-11 20:35:22 +01:00
async function getPlanningGameCount(user) {
return await getLoggedGameCount(user, 'planning');
2024-02-11 20:35:22 +01:00
}
async function getPlayingGameCount(user) {
return await getLoggedGameCount(user, 'playing');
2024-02-11 20:35:22 +01:00
}
2023-12-19 00:53:12 +01:00
async function getBeatenGameCount(user) {
return await getLoggedGameCount(user, 'beat');
2024-02-11 20:35:22 +01:00
}
async function getLoggedGameCount(user, status) {
2023-12-19 00:53:12 +01:00
const u = await Users.findOne({ where: { id: user.id } })
.catch((err) => {
console.log(err);
});
if (!u) return -1;
2024-02-11 20:35:22 +01:00
const count = await u.countBeatenGames({ where: { status: status } });
2023-12-19 00:53:12 +01:00
return count;
}
2024-02-11 20:35:22 +01:00
async function deletePlanningGameId(id, user) {
return await deleteLoggedGameId(id, user, 'planning');
2024-02-11 20:35:22 +01:00
}
async function deletePlayingGameId(id, user) {
return await deleteLoggedGameId(id, user, 'playing');
2024-02-11 20:35:22 +01:00
}
2023-12-19 21:42:54 +01:00
async function deleteBeatenGameId(id, user) {
return await deleteLoggedGameId(id, user, 'beat');
2024-02-11 20:35:22 +01:00
}
async function deleteLoggedGameId(id, user, status) {
const bg = await LoggedGames.findOne({ where: { gameId: id, userId: user.id, status: status } })
2023-12-19 21:42:54 +01:00
.catch((err) => {
console.log(err);
});
if (!bg) return false;
const entry = bg;
await bg.destroy();
if (bg) {
await Changelog.create({ userId: user.id, gameId: entry.gameId, oldStatus: status })
.catch((err) => {
console.log(err);
});
}
2023-12-19 21:42:54 +01:00
return entry;
}
2024-02-11 20:35:22 +01:00
async function deletePlanningGameNum(num, user) {
return await deleteLoggedGameNum(num, user, 'planning');
}
async function deletePlayingGameNum(num, user) {
return await deleteLoggedGameNum(num, user, 'playing');
}
2023-12-19 21:42:54 +01:00
async function deleteBeatenGameNum(num, user) {
2024-02-11 20:35:22 +01:00
return await deleteLoggedGameNum(num, user, 'beat');
}
async function deleteLoggedGameNum(num, user, status) {
const bg = await LoggedGames.findAll({ where: { userId: user.id, status: status } })
2023-12-19 21:42:54 +01:00
.catch((err) => {
console.log(err);
});
if (!bg) return false;
if (bg.length < num) return false;
const entry = bg[num - 1];
await bg[num - 1].destroy();
if (bg) {
await Changelog.create({ userId: user.id, gameId: entry.gameId, oldStatus: status })
.catch((err) => {
console.log(err);
});
}
2023-12-19 21:42:54 +01:00
return entry;
}
2023-12-19 23:19:08 +01:00
async function getLeaderboardEntries() {
const users = await Users.findAll()
.catch((err) => {
console.log(err);
});
const results = [];
for (let i = 0; i < users.length; i++) {
2024-02-11 20:19:43 +01:00
const count = await LoggedGames.count({ where: { userId: users[i].id, status: 'beat' } });
2023-12-19 23:19:08 +01:00
const res = await Users.findOne({ where: { id: users[i].id } })
.catch((err) => {
console.log(err);
});
const username = res.username;
const fun = { username, count };
results.push(fun);
}
return results;
}
2024-02-11 20:35:22 +01:00
async function getRecentPlanningGameEntry(userId) {
return await getRecentGameEntry(userId, 'planning');
2024-02-11 20:35:22 +01:00
}
async function getRecentPlayingGameEntry(userId) {
return await getRecentGameEntry(userId, 'playing');
2024-02-11 20:35:22 +01:00
}
async function getRecentBeatenGameEntry(userId) {
return await getRecentGameEntry(userId, 'beat');
2024-02-11 20:35:22 +01:00
}
async function getRecentGameEntry(userId, status) {
const beatenGameEntry = await LoggedGames.findOne({ where: { userId: userId, status: status }, order: [ [ 'statusLastChanged', 'DESC' ]] })
2023-12-27 21:43:40 +01:00
.catch((err) => {
console.log(err);
});
if (!beatenGameEntry) return false;
const game = await Games.findOne({ where: { id: beatenGameEntry.gameId } })
.catch((err) => {
console.log(err);
});
if (game) return game;
return false;
}
async function getRecentEntry(userId) {
const beatenGameEntry = await LoggedGames.findOne({ where: { userId: userId }, order: [ [ 'statusLastChanged', 'DESC' ]] })
.catch((err) => {
console.log(err);
});
if (!beatenGameEntry) return false;
return beatenGameEntry;
}
2024-02-11 22:04:55 +01:00
async function getPlanningGames(id) {
return await getGames(id, 'planning');
}
async function getPlayingGames(id) {
return await getGames(id, 'playing');
}
async function getBeatenGames(id) {
return await getGames(id, 'beat');
}
async function getGames(id, status) {
const gameEntries = await LoggedGames.findAll({ where: { userId: id, status: status }, order: [ [ 'statusLastChanged', 'ASC' ]] })
2023-12-27 23:27:55 +01:00
.catch((err) => {
console.log(err);
});
2024-02-11 22:04:55 +01:00
if (gameEntries) return gameEntries;
2023-12-27 23:27:55 +01:00
return false;
}
2024-02-16 18:04:29 +01:00
async function getAllBeatenGames() {
const gameEntries = await LoggedGames.findAll({ where: { status: 'beat' }, order: [ [ 'statusLastChanged', 'ASC' ]] })
2024-02-16 18:04:29 +01:00
.catch((err) => {
console.log(err);
});
if (gameEntries) return gameEntries;
return false;
}
2023-12-30 23:45:52 +01:00
async function backupDatabase() {
const date = new Date().toJSON().slice(0, 10);
if (fs.existsSync('./database.sqlite')) {
// I know that this is probably not the best way to do this but for now it is fine.
fs.copyFile('./database.sqlite', String.prototype.concat('./backups/database-', date, '.sqlite'), (err) => {
console.log(err);
});
}
}
2024-07-13 23:03:16 +02:00
async function getChangelog(id) {
const changelogEntries = await Changelog.findAll({where: {userId: id}, order: [ [ 'updatedAt', 'DESC' ]] })
.catch((err) => {
console.log(err);
});
if (changelogEntries) return changelogEntries;
return false;
}
2024-07-13 23:15:58 +02:00
async function getAllChangelog() {
const changelogEntries = await Changelog.findAll({ order: [ [ 'updatedAt', 'DESC' ]] })
.catch((err) => {
console.log(err);
});
if (changelogEntries) return changelogEntries;
return false;
}
module.exports = {
checkUserRegistration,
2023-12-19 00:53:12 +01:00
getUserRegistration,
2024-02-16 18:04:29 +01:00
getUserFromId,
2023-12-19 00:53:12 +01:00
checkGameStorage,
2024-02-11 20:35:22 +01:00
createPlanningGameEntry,
createPlayingGameEntry,
2023-12-19 00:53:12 +01:00
createBeatenGameEntry,
2024-02-11 20:35:22 +01:00
createLoggedGameEntry,
getPlanningGameCount,
getPlayingGameCount,
2023-12-19 00:53:12 +01:00
getBeatenGameCount,
2024-02-11 20:35:22 +01:00
getLoggedGameCount,
deletePlanningGameId,
deletePlayingGameId,
2023-12-19 21:42:54 +01:00
deleteBeatenGameId,
2024-02-11 20:35:22 +01:00
deletePlanningGameNum,
deletePlayingGameNum,
2023-12-19 21:42:54 +01:00
deleteBeatenGameNum,
checkGameStorageId,
2023-12-19 23:19:08 +01:00
getLeaderboardEntries,
2024-02-11 20:35:22 +01:00
getRecentPlanningGameEntry,
getRecentPlayingGameEntry,
getRecentBeatenGameEntry,
2023-12-27 21:43:40 +01:00
getRecentGameEntry,
getRecentEntry,
2024-02-11 22:04:55 +01:00
getPlanningGames,
getPlayingGames,
getBeatenGames,
2023-12-27 23:27:55 +01:00
getGames,
2024-02-16 18:04:29 +01:00
getAllBeatenGames,
2023-12-30 23:45:52 +01:00
backupDatabase,
2024-07-13 23:03:16 +02:00
getChangelog,
2024-07-13 23:15:58 +02:00
getAllChangelog,
};