History: Add currency and XP tracking
This commit is contained in:
28
models/currencyhistory.js
Normal file
28
models/currencyhistory.js
Normal file
@@ -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;
|
||||
};
|
||||
@@ -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)
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user