From bbbcaaaf29bdbf61c5f89db8cea0ac68a2595e7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Gro=C3=9F?= Date: Mon, 26 Sep 2022 13:15:40 +0200 Subject: [PATCH] User/Debug/Profile: Add primary and secondary currency --- commands/debug.js | 16 ++++++++++ commands/profile.js | 3 ++ config/constants.js | 14 ++++++++- ...-primary-and-secondary-currency-to-user.js | 31 +++++++++++++++++++ models/user.js | 27 ++++++++++++++++ 5 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 migrations/20220926103033-add-primary-and-secondary-currency-to-user.js diff --git a/commands/debug.js b/commands/debug.js index d206f97..3d3691e 100644 --- a/commands/debug.js +++ b/commands/debug.js @@ -21,6 +21,8 @@ module.exports = { { name: 'bot', value: 'bot' }, { name: 'reset_cd', value: 'reset_cd' }, { name: 'add_xp', value: 'add_xp' }, + { name: 'add_primary', value: 'add_primary' }, + { name: 'add_secondary', value: 'add_secondary' }, { name: 'toggle_maintenance', value: 'toggle_maintenance' }, ) ) @@ -120,6 +122,20 @@ module.exports = { ephemeral: false }); break; + case "add_primary": + await extUser.addPrimaryCurrency(interaction.options.getString("value"), `Debug command ran by ${interaction.member.displayName}`); + interaction.editReply({ + content: `Added ${interaction.options.getString("value")} Primary to <@${extUser.discordId}>`, + ephemeral: false + }); + break; + case "add_secondary": + await extUser.addSecondaryCurrency(interaction.options.getString("value"), `Debug command ran by ${interaction.member.displayName}`); + interaction.editReply({ + content: `Added ${interaction.options.getString("value")} Secondary to <@${extUser.discordId}>`, + ephemeral: false + }); + break; case "toggle_maintenance": let maintenance = await GeneralUtils.getBotProperty("maintenance"); await GeneralUtils.setBotProperty("maintenance", !maintenance); diff --git a/commands/profile.js b/commands/profile.js index 28bd50e..4d4a6cb 100644 --- a/commands/profile.js +++ b/commands/profile.js @@ -3,6 +3,7 @@ const { Card, User, Character } = require("../models"); const { UserUtils, Compositing, Rendering } = require("../util"); const axios = require("axios"); const sharp = require("sharp"); +const { CURRENCY_NAMES } = require("../config/constants"); const fs = require('fs'); const pageSize = 8; @@ -34,6 +35,8 @@ module.exports = { profileTemplate = profileTemplate.replace(/{{HEADER_COLOR}}/g, '190,31,97'); profileTemplate = profileTemplate.replace(/{{CC}}/g, await Card.count({where: {userId: user.id}})); profileTemplate = profileTemplate.replace(/{{LVL}}/g, await user.level().currentLevel); + profileTemplate = profileTemplate.replace(/{{CUR_1}}/g, `${await user.primaryCurrency} ${CURRENCY_NAMES[1]}`); + profileTemplate = profileTemplate.replace(/{{CUR_2}}/g, `${await user.secondaryCurrency} ${CURRENCY_NAMES[2]}`); let userImageBuffer = await axios.get(discordUser.displayAvatarURL({format: 'png', size: 128}), { responseType: 'arraybuffer' }); userImage = await sharp(userImageBuffer.data); diff --git a/config/constants.js b/config/constants.js index c25e4ce..8a263ef 100644 --- a/config/constants.js +++ b/config/constants.js @@ -16,5 +16,17 @@ const QUALITY_NAMES = { 6 : "Shiny" } +const CURRENCY_SYMBOLS = { + 1 : "🎶", + 2 : "💎" +} + +const CURRENCY_NAMES = { + 1 : "Notes", + 2 : "Gems" +} + exports.QUALITY = QUALITY; -exports.QUALITY_NAMES = QUALITY_NAMES; \ No newline at end of file +exports.QUALITY_NAMES = QUALITY_NAMES; +exports.CURRENCY_SYMBOLS = CURRENCY_SYMBOLS; +exports.CURRENCY_NAMES = CURRENCY_NAMES; \ No newline at end of file diff --git a/migrations/20220926103033-add-primary-and-secondary-currency-to-user.js b/migrations/20220926103033-add-primary-and-secondary-currency-to-user.js new file mode 100644 index 0000000..1ec1e09 --- /dev/null +++ b/migrations/20220926103033-add-primary-and-secondary-currency-to-user.js @@ -0,0 +1,31 @@ +'use strict'; + +module.exports = { + async up (queryInterface, Sequelize) { + /** + * Add altering commands here. + * + * Example: + * await queryInterface.createTable('users', { id: Sequelize.INTEGER }); + */ + await queryInterface.addColumn('Users', 'primaryCurrency', { + type: Sequelize.INTEGER, + defaultValue: 0 + }); + await queryInterface.addColumn('Users', 'secondaryCurrency', { + type: Sequelize.INTEGER, + defaultValue: 0 + }); + }, + + async down (queryInterface, Sequelize) { + /** + * Add reverting commands here. + * + * Example: + * await queryInterface.dropTable('users'); + */ + await queryInterface.removeColumn('Users', 'primaryCurrency'); + await queryInterface.removeColumn('Users', 'secondaryCurrency'); + } +}; diff --git a/models/user.js b/models/user.js index fedcc06..2937259 100644 --- a/models/user.js +++ b/models/user.js @@ -31,6 +31,31 @@ module.exports = (sequelize, DataTypes) => { experience: this.experience + parseInt(amount) }); } + async addCurrency(amount, type, source='unknown') { + let typeStr = ''; + if (type == 1) { + typeStr = `primaryCurrency`; + } + if (type == 2) { + typeStr = `secondaryCurrency`; + } + console.log(`Adding ${amount} ${typeStr} to user ${this.id}`); + await sequelize.models.CurrencyHistory.create({ + userId: this.id, + currency: type, + delta: amount, + previous: this[typeStr], + source: source + }); + this[typeStr] += parseInt(amount); + await this.save(); + } + async addPrimaryCurrency(amount, source='unknown') { + return await this.addCurrency(amount, 1, source); + } + async addSecondaryCurrency(amount, source='unknown') { + return await this.addCurrency(amount, 2, source); + } level() { let currentLevel = Math.floor(levelModifier * Math.sqrt(this.experience)); let nextLevelExperience = Math.pow((currentLevel + 1) / levelModifier, 2); @@ -74,6 +99,8 @@ module.exports = (sequelize, DataTypes) => { active: DataTypes.INTEGER, privacy: DataTypes.INTEGER, experience: DataTypes.INTEGER, + primaryCurrency: DataTypes.INTEGER, + secondaryCurrency: DataTypes.INTEGER, nextDrop: DataTypes.DATE, nextPull: DataTypes.DATE, nextDaily: DataTypes.DATE