Compare commits

...

2 Commits

Author SHA1 Message Date
baz 8ba04b5735 Convert BeatenGames to LoggedGames 2024-02-11 19:19:43 +00:00
baz 15a1a950c2 Update .gitignore 2024-02-11 19:19:32 +00:00
4 changed files with 17 additions and 16 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ node_modules
config.json
*.sqlite
backups
*.idea

View File

@ -1,6 +1,6 @@
const { Sequelize, DataTypes } = require('sequelize');
require('./dbObjects.js');
const { sequelize, BeatenGames } = require ('./dbObjects.js');
const { sequelize, LoggedGames } = require ('./dbObjects.js');
// Add a new column to the existing table
(async () => {

View File

@ -1,4 +1,4 @@
const { Users, Games, BeatenGames } = require ('./dbObjects.js');
const { Users, Games, LoggedGames } = require ('./dbObjects.js');
const fs = require('fs');
async function checkUserRegistration(user) {
@ -68,14 +68,14 @@ async function checkGameStorageId(id) {
}
async function createBeatenGameEntry(user, game) {
let bg = await BeatenGames.findOne({ where: { userId: user.id, gameId: game.id } })
let bg = await LoggedGames.findOne({ where: { userId: user.id, gameId: game.id, status: 'beat' } })
.catch((err) => {
console.log(err);
});
if (bg) return false;
await BeatenGames.create({ userId: user.id, gameId: game.id })
await LoggedGames.create({ userId: user.id, gameId: game.id })
.then((data) => {
bg = data;
})
@ -102,7 +102,7 @@ async function getBeatenGameCount(user) {
}
async function deleteBeatenGameId(id, user) {
const bg = await BeatenGames.findOne({ where: { gameId: id, userId: user.id } })
const bg = await LoggedGames.findOne({ where: { gameId: id, userId: user.id, status: 'beat' } })
.catch((err) => {
console.log(err);
});
@ -116,7 +116,7 @@ async function deleteBeatenGameId(id, user) {
}
async function deleteBeatenGameNum(num, user) {
const bg = await BeatenGames.findAll({ where: { userId: user.id } })
const bg = await LoggedGames.findAll({ where: { userId: user.id, status: 'beat' } })
.catch((err) => {
console.log(err);
});
@ -140,7 +140,7 @@ async function getLeaderboardEntries() {
const results = [];
for (let i = 0; i < users.length; i++) {
const count = await BeatenGames.count({ where: { userId: users[i].id } });
const count = await LoggedGames.count({ where: { userId: users[i].id, status: 'beat' } });
const res = await Users.findOne({ where: { id: users[i].id } })
.catch((err) => {
@ -156,7 +156,7 @@ async function getLeaderboardEntries() {
}
async function getRecentGameEntry(userId) {
const beatenGameEntry = await BeatenGames.findOne({ where: { userId: userId }, order: [ [ 'createdAt', 'DESC' ]] })
const beatenGameEntry = await LoggedGames.findOne({ where: { userId: userId, status: 'beat' }, order: [ [ 'createdAt', 'DESC' ]] })
.catch((err) => {
console.log(err);
});
@ -174,7 +174,7 @@ async function getRecentGameEntry(userId) {
}
async function getGames(id) {
const beatenGameEntry = await BeatenGames.findAll({ where: { userId: id } })
const beatenGameEntry = await LoggedGames.findAll({ where: { userId: id, status: 'beat' } })
.catch((err) => {
console.log(err);
});

View File

@ -9,15 +9,15 @@ const sequelize = new Sequelize('database', 'username', 'password', {
const Users = require('./models/users.js')(sequelize, Sequelize.DataTypes);
const Games = require('./models/games.js')(sequelize, Sequelize.DataTypes);
const BeatenGames = require('./models/beatenGames.js')(sequelize, Sequelize.DataTypes);
const LoggedGames = require('./models/beatenGames.js')(sequelize, Sequelize.DataTypes);
// Create userId foreignKey
Users.hasMany(BeatenGames);
BeatenGames.belongsTo(Users);
Users.hasMany(LoggedGames);
LoggedGames.belongsTo(Users);
// Create gameId foreignKey
Games.hasMany(BeatenGames);
BeatenGames.belongsTo(Games);
Games.hasMany(LoggedGames);
LoggedGames.belongsTo(Games);
Reflect.defineProperty(Users.prototype, 'addUser', {
value: async function addUser(userData) {
@ -44,4 +44,4 @@ sequelize.sync({ alter: true })
console.log(err);
});
module.exports = { sequelize, Users, Games, BeatenGames };
module.exports = { sequelize, Users, Games, LoggedGames };