Some extensions and fixes

This commit is contained in:
Torsten Schulz
2025-01-28 09:55:36 +01:00
parent 2f60741116
commit 90b4f51dcb
27 changed files with 910 additions and 53 deletions

View File

@@ -24,6 +24,7 @@ import { updateFalukantUserMoney } from '../utils/sequelize.js';
import BuyableStock from '../models/falukant/data/buyable_stock.js';
import DirectorProposal from '../models/falukant/data/director_proposal.js';
import Director from '../models/falukant/data/director.js';
import DaySell from '../models/falukant/log/daysell.js';
function calcAge(birthdate) {
@@ -227,7 +228,12 @@ class FalukantService extends BaseService {
async getProducts(hashedUserId) {
const u = await getFalukantUserOrFail(hashedUserId);
console.log(u);
const c = await FalukantCharacter.findOne({ where: { userId: u.id } });
console.log(c);
if (!c) {
throw new Error(`No FalukantCharacter found for user with id ${u.id}`);
}
const ps = await ProductType.findAll({
where: { category: { [Op.lte]: u.certificate } },
include: [{ model: Knowledge, as: 'knowledges', attributes: ['knowledge'], where: { characterId: c.id } }],
@@ -284,10 +290,8 @@ class FalukantService extends BaseService {
const branch = await getBranchOrFail(user.id, branchId);
const character = await FalukantCharacter.findOne({ where: { userId: user.id } });
if (!character) throw new Error('No character found for user');
const stock = await FalukantStock.findOne({ where: { branchId: branch.id } });
if (!stock) throw new Error('Stock not found');
const inventory = await Inventory.findAll({
where: { stockId: stock.id, quality },
include: [
@@ -307,18 +311,14 @@ class FalukantService extends BaseService {
}
]
});
if (!inventory.length) throw new Error('No inventory found');
const available = inventory.reduce((sum, i) => sum + i.quantity, 0);
if (available < quantity) throw new Error('Not enough inventory available');
const item = inventory[0].productType;
const knowledgeVal = item.knowledges?.[0]?.knowledge || 0;
const revenue = quantity * calcSellPrice(item, knowledgeVal);
const moneyResult = await updateFalukantUserMoney(user.id, revenue, 'Product sale', user.id);
if (!moneyResult.success) throw new Error('Failed to update money');
let remaining = quantity;
for (const inv of inventory) {
if (inv.quantity <= remaining) {
@@ -330,7 +330,7 @@ class FalukantService extends BaseService {
break;
}
}
await this.addSellItem(branchId, falukantUser.id, productId, quantity);
notifyUser(user.user.hashedId, 'falukantUpdateStatus', {});
notifyUser(user.user.hashedId, 'falukantBranchUpdate', { branchId: branch.id });
return { success: true };
@@ -384,6 +384,7 @@ class FalukantService extends BaseService {
for (const item of inventory) {
const knowledgeVal = item.productType.knowledges[0]?.knowledge || 0;
total += item.quantity * calcSellPrice(item.productType, knowledgeVal);
await this.addSellItem(item.stock[0].branch[0].id, falukantUser.id, item.productType.id, item.quantity);
}
const moneyResult = await updateFalukantUserMoney(
falukantUser.id,
@@ -396,10 +397,35 @@ class FalukantService extends BaseService {
await Inventory.destroy({ where: { id: item.id } });
}
notifyUser(falukantUser.user.hashedId, 'falukantUpdateStatus', {});
notifyUser(falukantUser.user.hashedId, 'falukantBranchUpdate', { branchId: branch.id });
notifyUser(falukantUser.user.hashedId, 'falukantBranchUpdate', {});
return { success: true, revenue: total };
}
async addSellItem(branchId, userId, productId, quantity) {
const branch = await Branch.findOne({
where: { id: branchId },
})
;
const daySell = await DaySell.findOne({
where: {
regionId: regionId,
productId: productId,
sellerId: userId,
}
});
if (daySell) {
daySell.quantity += quantity;
await daySell.save();
} else {
await DaySell.create({
regionId: regionId,
productId: productId,
sellerId: userId,
quantity: quantity,
});
}
}
async moneyHistory(hashedUserId, page = 1, filter = '') {
const u = await getFalukantUserOrFail(hashedUserId);
const limit = 25, offset = (page - 1) * limit;
@@ -905,6 +931,61 @@ class FalukantService extends BaseService {
});
return { result: 'ok' };
}
async getMarriageProposals(hashedUserId) {
const user = await this.getFalukantUserByHashedId(hashedUserId);
const character = await FalukantCharacter.findOne({ where: { userId: user.id } });
if (!character) {
throw new Error('Character not found for this user');
}
const midnight = new Date();
midnight.setHours(0, 0, 0, 0);
await MarriageProposal.destroy({
where: {
[Op.or]: [
{ requesterCharacterId: character.id },
{ proposedCharacterId: character.id },
],
createdAt: {
[Op.lt]: midnight,
},
},
});
let proposals = await MarriageProposal.findAll({
where: {
[Op.or]: [
{ requesterCharacterId: character.id },
{ proposedCharacterId: character.id },
],
},
});
if (proposals.length === 0) {
const proposalCount = Math.floor(Math.random() * 4) + 3; // 36
const thirteenDaysAgo = new Date(Date.now() - 13 * 24 * 60 * 60 * 1000);
const possiblePartners = await FalukantCharacter.findAll({
where: {
id: { [Op.ne]: character.id },
createdAt: { [Op.lt]: thirteenDaysAgo },
},
order: [sequelize.fn('RANDOM')],
});
if (possiblePartners.length === 0) {
return [];
}
const newProposals = [];
for (let i = 0; i < proposalCount; i++) {
const partner = possiblePartners[i % possiblePartners.length];
const createdProposal = await MarriageProposal.create({
requesterCharacterId: character.id,
proposedCharacterId: partner.id,
courtingProgress: 0,
});
newProposals.push(createdProposal);
}
proposals = newProposals;
}
return proposals;
}
}
export default new FalukantService();