Add lastNobilityAdvanceAt field and update logic in FalukantService
- Introduced a new field `lastNobilityAdvanceAt` in the FalukantUser model to track the last time a user advanced in nobility. - Updated the `FalukantService` to enforce a one-week cooldown between nobility advancements, throwing an error if the user attempts to advance too soon. - Ensured the `lastNobilityAdvanceAt` field is updated with the current date upon a successful nobility advancement.
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
up: async (queryInterface, Sequelize) => {
|
||||
await queryInterface.addColumn(
|
||||
{
|
||||
tableName: 'falukant_user',
|
||||
schema: 'falukant_data'
|
||||
},
|
||||
'last_nobility_advance_at',
|
||||
{
|
||||
type: Sequelize.DATE,
|
||||
allowNull: true
|
||||
}
|
||||
);
|
||||
},
|
||||
|
||||
down: async (queryInterface, Sequelize) => {
|
||||
await queryInterface.removeColumn(
|
||||
{
|
||||
tableName: 'falukant_user',
|
||||
schema: 'falukant_data'
|
||||
},
|
||||
'last_nobility_advance_at'
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -44,6 +44,10 @@ FalukantUser.init({
|
||||
key: 'id',
|
||||
schema: 'falukant_data'
|
||||
}
|
||||
},
|
||||
lastNobilityAdvanceAt: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: true
|
||||
}
|
||||
}, {
|
||||
sequelize,
|
||||
|
||||
@@ -2931,8 +2931,16 @@ class FalukantService extends BaseService {
|
||||
if (!nobility || !nobility.next) {
|
||||
throw new Error('User does not have a nobility');
|
||||
}
|
||||
const nextTitle = nobility.next.toJSON();
|
||||
const user = await this.getFalukantUserByHashedId(hashedUserId);
|
||||
const now = new Date();
|
||||
if (user.lastNobilityAdvanceAt) {
|
||||
const oneWeekAgo = new Date(now.getTime());
|
||||
oneWeekAgo.setDate(oneWeekAgo.getDate() - 7);
|
||||
if (user.lastNobilityAdvanceAt > oneWeekAgo) {
|
||||
throw new Error('too soon');
|
||||
}
|
||||
}
|
||||
const nextTitle = nobility.next.toJSON();
|
||||
let fulfilled = true;
|
||||
let cost = 0;
|
||||
for (const requirement of nextTitle.requirements) {
|
||||
@@ -2959,6 +2967,7 @@ class FalukantService extends BaseService {
|
||||
});
|
||||
const character = await FalukantCharacter.findOne({ where: { userId: user.id } });
|
||||
await character.update({ titleOfNobility: newTitle.id });
|
||||
await user.update({ lastNobilityAdvanceAt: now });
|
||||
if (cost > 0) {
|
||||
updateFalukantUserMoney(user.id, -cost, 'new nobility title', user.id);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user