From e1c12cd85283508c1b24e002e8ce395b3271a12b Mon Sep 17 00:00:00 2001 From: baz Date: Sat, 13 Jul 2024 17:10:13 +0100 Subject: [PATCH] Add date field to plangame, startplaying, beatgame commands --- commands/100-games/beatGame.js | 16 +++++++++++++++- commands/100-games/planGame.js | 18 ++++++++++++++++-- commands/100-games/startplaying.js | 16 +++++++++++++++- databaseHelperFunctions.js | 10 +++++----- 4 files changed, 51 insertions(+), 9 deletions(-) diff --git a/commands/100-games/beatGame.js b/commands/100-games/beatGame.js index fd5522d..07a7259 100644 --- a/commands/100-games/beatGame.js +++ b/commands/100-games/beatGame.js @@ -7,6 +7,7 @@ module.exports = { .setName('beatgame') .setDescription('Log a game that you have beat towards the 100 game challenge!') .addStringOption(option => option.setName('gamename').setDescription('The name of the game.').setRequired(true)) + .addStringOption(option => option.setName('date').setDescription('The date to be logged. (YYYY/MM/DD)')) .addNumberOption(option => option.setName('gameid').setDescription('The IGDB game id.').setMinValue(0)), async execute(interaction) { @@ -20,6 +21,7 @@ module.exports = { const gamename = interaction.options.getString('gamename'); const gameid = interaction.options.getNumber('gameid'); + const date = interaction.options.getString('date'); if (!gamename && !gameid) return interaction.editReply({ content: 'No gamename or gameid supplied, please supply an option to register a game!', ephemeral: true }); @@ -46,7 +48,19 @@ module.exports = { const gameDatabaseEntry = await checkGameStorage(game); - await createBeatenGameEntry(userDatabaseEntry, gameDatabaseEntry); + let gameDate = new Date(); + + if (date) { + const parsedDate = new Date(date); + if (!isNaN(parsedDate.getTime())) { + gameDate = parsedDate; + } + else { + gameDate = new Date(); + } + } + + await createBeatenGameEntry(userDatabaseEntry, gameDatabaseEntry, gameDate); const beatNum = await getBeatenGameCount(userDatabaseEntry); const planNum = await getPlanningGameCount(userDatabaseEntry); diff --git a/commands/100-games/planGame.js b/commands/100-games/planGame.js index e4ce3e9..622706a 100644 --- a/commands/100-games/planGame.js +++ b/commands/100-games/planGame.js @@ -7,7 +7,8 @@ module.exports = { .setName('plangame') .setDescription('Log a game that you plan beat towards the 100 game challenge!') .addStringOption(option => option.setName('gamename').setDescription('The name of the game.').setRequired(true)) - .addNumberOption(option => option.setName('gameid').setDescription('The IGDB game id.').setMinValue(0)), + .addNumberOption(option => option.setName('gameid').setDescription('The IGDB game id.').setMinValue(0)) + .addStringOption(option => option.setName('date').setDescription('The date to be logged. (YYYY/MM/DD)')), async execute(interaction) { await interaction.deferReply(); @@ -17,6 +18,7 @@ module.exports = { const gamename = interaction.options.getString('gamename'); const gameid = interaction.options.getNumber('gameid'); + const date = interaction.options.getString('date'); if (!gamename && !gameid) return interaction.editReply({ content: 'No gamename or gameid supplied, please supply an option to register a game!', ephemeral: true }); @@ -40,7 +42,19 @@ module.exports = { const game = res[0]; const gameDatabaseEntry = await checkGameStorage(game); - await createPlanningGameEntry(userDatabaseEntry, gameDatabaseEntry); + let gameDate = new Date(); + + if (date) { + const parsedDate = new Date(date); + if (!isNaN(parsedDate.getTime())) { + gameDate = parsedDate; + } + else { + gameDate = new Date(); + } + } + + await createPlanningGameEntry(userDatabaseEntry, gameDatabaseEntry, gameDate); const beatNum = await getBeatenGameCount(userDatabaseEntry); const planNum = await getPlanningGameCount(userDatabaseEntry); diff --git a/commands/100-games/startplaying.js b/commands/100-games/startplaying.js index ff86258..1909c89 100644 --- a/commands/100-games/startplaying.js +++ b/commands/100-games/startplaying.js @@ -7,6 +7,7 @@ module.exports = { .setName('startplaying') .setDescription('Log a game that you have started playing towards the 100 game challenge!') .addStringOption(option => option.setName('gamename').setDescription('The name of the game.').setRequired(true)) + .addStringOption(option => option.setName('date').setDescription('The date to be logged. (YYYY/MM/DD)')) .addNumberOption(option => option.setName('gameid').setDescription('The IGDB game id.').setMinValue(0)), async execute(interaction) { @@ -17,6 +18,7 @@ module.exports = { const gamename = interaction.options.getString('gamename'); const gameid = interaction.options.getNumber('gameid'); + const date = interaction.options.getString('date'); if (!gamename && !gameid) return interaction.editReply({ content: 'No gamename or gameid supplied, please supply an option to register a game!', ephemeral: true }); @@ -43,7 +45,19 @@ module.exports = { const gameDatabaseEntry = await checkGameStorage(game); - await createPlayingGameEntry(userDatabaseEntry, gameDatabaseEntry); + let gameDate = new Date(); + + if (date) { + const parsedDate = new Date(date); + if (!isNaN(parsedDate.getTime())) { + gameDate = parsedDate; + } + else { + gameDate = new Date(); + } + } + + await createPlayingGameEntry(userDatabaseEntry, gameDatabaseEntry, gameDate); const beatNum = await getBeatenGameCount(userDatabaseEntry); const planNum = await getPlanningGameCount(userDatabaseEntry); diff --git a/databaseHelperFunctions.js b/databaseHelperFunctions.js index 6fb2880..5f2284a 100644 --- a/databaseHelperFunctions.js +++ b/databaseHelperFunctions.js @@ -81,7 +81,7 @@ async function checkGameStorageId(id) { async function createPlanningGameEntry(user, game, date) { const entry = await checkLoggedGameEntry(user, game); - if (!entry) return await createLoggedGameEntry(user, game, 'planning'); + if (!entry) return await createLoggedGameEntry(user, game, 'planning', date); if (entry.status == 'planning') return false; @@ -102,7 +102,7 @@ async function createPlanningGameEntry(user, game, date) { async function createPlayingGameEntry(user, game, date) { const entry = await checkLoggedGameEntry(user, game); - if (!entry) return await createLoggedGameEntry(user, game, 'playing'); + if (!entry) return await createLoggedGameEntry(user, game, 'playing', date); if (entry.status == 'playing') return false; @@ -123,7 +123,7 @@ async function createPlayingGameEntry(user, game, date) { async function createBeatenGameEntry(user, game, date) { const entry = await checkLoggedGameEntry(user, game); - if (!entry) return await createLoggedGameEntry(user, game, 'beat'); + if (!entry) return await createLoggedGameEntry(user, game, 'beat', date); if (entry.status == 'beat') return false; @@ -152,10 +152,10 @@ async function checkLoggedGameEntry(user, game) { return bg; } -async function createLoggedGameEntry(user, game, status) { +async function createLoggedGameEntry(user, game, status, date) { let bg; - await LoggedGames.create({ userId: user.id, gameId: game.id, status: status, statusLastChanged: new Date() }) + await LoggedGames.create({ userId: user.id, gameId: game.id, status: status, statusLastChanged: date }) .then((data) => { bg = data; })