diff --git a/commands/debug.js b/commands/debug.js index ed4410c..d206f97 100644 --- a/commands/debug.js +++ b/commands/debug.js @@ -114,7 +114,7 @@ module.exports = { }); break; case "add_xp": - await extUser.addExperience(interaction.options.getString("value")) + await extUser.addExperience(interaction.options.getString("value"), `Debug command ran by ${interaction.member.displayName}`); interaction.editReply({ content: `Added ${interaction.options.getString("value")} XP to <@${extUser.discordId}>`, ephemeral: false diff --git a/commands/drop.js b/commands/drop.js index 49d374b..ddfb098 100644 --- a/commands/drop.js +++ b/commands/drop.js @@ -86,7 +86,7 @@ module.exports = { ); } //add 10 experience to the user - await user.addExperience(10); + await user.addExperience(10, 'drop'); const file = new AttachmentBuilder(deckImage); @@ -147,7 +147,7 @@ module.exports = { dropData: JSON.stringify(historyEntry), type: 1 }); - await claimUser.addExperience(5); + await claimUser.addExperience(5, "claim"); //fetch character name from database given the character id let character = await Character.findOne({ attributes: ["name"], diff --git a/migrations/20220921145901-create-currency-history.js b/migrations/20220921145901-create-currency-history.js new file mode 100644 index 0000000..a53ca9a --- /dev/null +++ b/migrations/20220921145901-create-currency-history.js @@ -0,0 +1,39 @@ +'use strict'; +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.createTable('CurrencyHistories', { + id: { + allowNull: false, + autoIncrement: true, + primaryKey: true, + type: Sequelize.INTEGER + }, + userId: { + type: Sequelize.INTEGER + }, + currency: { + type: Sequelize.INTEGER + }, + delta: { + type: Sequelize.INTEGER + }, + previous: { + type: Sequelize.INTEGER + }, + source: { + type: Sequelize.STRING + }, + createdAt: { + allowNull: false, + type: Sequelize.DATE + }, + updatedAt: { + allowNull: false, + type: Sequelize.DATE + } + }); + }, + async down(queryInterface, Sequelize) { + await queryInterface.dropTable('CurrencyHistories'); + } +}; \ No newline at end of file diff --git a/models/currencyhistory.js b/models/currencyhistory.js new file mode 100644 index 0000000..c49ad96 --- /dev/null +++ b/models/currencyhistory.js @@ -0,0 +1,28 @@ +'use strict'; +const { + Model +} = require('sequelize'); +module.exports = (sequelize, DataTypes) => { + class CurrencyHistory extends Model { + /** + * Helper method for defining associations. + * This method is not a part of Sequelize lifecycle. + * The `models/index` file will call this method automatically. + */ + static associate(models) { + // define association here + CurrencyHistory.belongsTo(models.User, { foreignKey: 'userId' }); + } + } + CurrencyHistory.init({ + userId: DataTypes.INTEGER, + currency: DataTypes.INTEGER, + delta: DataTypes.INTEGER, + previous: DataTypes.INTEGER, + source: DataTypes.STRING + }, { + sequelize, + modelName: 'CurrencyHistory', + }); + return CurrencyHistory; +}; \ No newline at end of file diff --git a/models/user.js b/models/user.js index 3906c38..fedcc06 100644 --- a/models/user.js +++ b/models/user.js @@ -15,10 +15,18 @@ module.exports = (sequelize, DataTypes) => { static associate(models) { // define association here User.hasMany(models.Card); + User.hasMany(models.CurrencyHistory); User.hasOne(models.Profile); } - async addExperience(amount) { + async addExperience(amount, source='unknown') { console.log(`Adding ${amount} experience to user ${this.id}`); + await sequelize.models.CurrencyHistory.create({ + userId: this.id, + currency: 0, + delta: amount, + previous: this.experience, + source: source + }); await this.update({ experience: this.experience + parseInt(amount) });