Add vehicle management features in Falukant

- Introduced vehicle types and transport management in the backend, including new models and associations for vehicles and transports.
- Implemented service methods to retrieve vehicle types and handle vehicle purchases, ensuring user validation and transaction management.
- Updated the FalukantController and router to expose new endpoints for fetching vehicle types and buying vehicles.
- Enhanced the frontend with a new transport tab in BranchView, allowing users to buy vehicles, and added localization for vehicle-related terms.
- Included initialization logic for vehicle types in the database setup.
This commit is contained in:
Torsten Schulz (local)
2025-11-24 20:15:45 +01:00
parent 5ed27e5a6a
commit 3f043fc315
14 changed files with 544 additions and 12 deletions

View File

@@ -181,6 +181,12 @@ class FalukantController {
});
});
this.getVehicleTypes = this._wrapWithUser((userId) => this.service.getVehicleTypes(userId));
this.buyVehicles = this._wrapWithUser(
(userId, req) => this.service.buyVehicles(userId, req.body),
{ successStatus: 201 }
);
}

View File

@@ -95,6 +95,9 @@ import PoliticalOfficeHistory from './falukant/log/political_office_history.js';
import ElectionHistory from './falukant/log/election_history.js';
import Underground from './falukant/data/underground.js';
import UndergroundType from './falukant/type/underground.js';
import VehicleType from './falukant/type/vehicle.js';
import Vehicle from './falukant/data/vehicle.js';
import Transport from './falukant/data/transport.js';
import Blog from './community/blog.js';
import BlogPost from './community/blog_post.js';
import Campaign from './match3/campaign.js';
@@ -421,6 +424,71 @@ export default function setupAssociations() {
PromotionalGiftLog.belongsTo(FalukantCharacter, { foreignKey: 'recipientCharacterId', as: 'recipient' });
FalukantCharacter.hasMany(PromotionalGiftLog, { foreignKey: 'recipientCharacterId', as: 'giftlogs' });
// Vehicles & Transports
VehicleType.hasMany(Vehicle, {
foreignKey: 'vehicleTypeId',
as: 'vehicles',
});
Vehicle.belongsTo(VehicleType, {
foreignKey: 'vehicleTypeId',
as: 'type',
});
FalukantUser.hasMany(Vehicle, {
foreignKey: 'falukantUserId',
as: 'vehicles',
});
Vehicle.belongsTo(FalukantUser, {
foreignKey: 'falukantUserId',
as: 'owner',
});
RegionData.hasMany(Vehicle, {
foreignKey: 'regionId',
as: 'vehicles',
});
Vehicle.belongsTo(RegionData, {
foreignKey: 'regionId',
as: 'region',
});
Transport.belongsTo(RegionData, {
foreignKey: 'sourceRegionId',
as: 'sourceRegion',
});
Transport.belongsTo(RegionData, {
foreignKey: 'targetRegionId',
as: 'targetRegion',
});
RegionData.hasMany(Transport, {
foreignKey: 'sourceRegionId',
as: 'outgoingTransports',
});
RegionData.hasMany(Transport, {
foreignKey: 'targetRegionId',
as: 'incomingTransports',
});
Transport.belongsTo(ProductType, {
foreignKey: 'productId',
as: 'productType',
});
ProductType.hasMany(Transport, {
foreignKey: 'productId',
as: 'transports',
});
Transport.belongsTo(Vehicle, {
foreignKey: 'vehicleId',
as: 'vehicle',
});
Vehicle.hasMany(Transport, {
foreignKey: 'vehicleId',
as: 'transports',
});
PromotionalGift.hasMany(PromotionalGiftCharacterTrait, { foreignKey: 'gift_id', as: 'characterTraits' });
PromotionalGift.hasMany(PromotionalGiftMood, { foreignKey: 'gift_id', as: 'promotionalgiftmoods' });

View File

@@ -0,0 +1,41 @@
import { Model, DataTypes } from 'sequelize';
import { sequelize } from '../../../utils/sequelize.js';
class Transport extends Model {}
Transport.init(
{
sourceRegionId: {
type: DataTypes.INTEGER,
allowNull: false,
},
targetRegionId: {
type: DataTypes.INTEGER,
allowNull: false,
},
productId: {
type: DataTypes.INTEGER,
allowNull: false,
},
size: {
type: DataTypes.INTEGER,
allowNull: false,
},
vehicleId: {
type: DataTypes.INTEGER,
allowNull: false,
},
},
{
sequelize,
modelName: 'Transport',
tableName: 'transport',
schema: 'falukant_data',
timestamps: true,
underscored: true,
}
);
export default Transport;

View File

@@ -0,0 +1,33 @@
import { Model, DataTypes } from 'sequelize';
import { sequelize } from '../../../utils/sequelize.js';
class Vehicle extends Model {}
Vehicle.init(
{
vehicleTypeId: {
type: DataTypes.INTEGER,
allowNull: false,
},
falukantUserId: {
type: DataTypes.INTEGER,
allowNull: false,
},
regionId: {
type: DataTypes.INTEGER,
allowNull: false,
},
},
{
sequelize,
modelName: 'Vehicle',
tableName: 'vehicle',
schema: 'falukant_data',
timestamps: true,
underscored: true,
}
);
export default Vehicle;

View File

@@ -0,0 +1,46 @@
import { Model, DataTypes } from 'sequelize';
import { sequelize } from '../../../utils/sequelize.js';
class VehicleType extends Model {}
VehicleType.init(
{
tr: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
cost: {
// base purchase cost of the vehicle
type: DataTypes.INTEGER,
allowNull: false,
},
capacity: {
// transport capacity (e.g. in units of goods)
type: DataTypes.INTEGER,
allowNull: false,
},
transportMode: {
// e.g. 'land', 'water', 'air'
type: DataTypes.STRING,
allowNull: false,
},
speed: {
// abstract speed value, higher = faster
type: DataTypes.INTEGER,
allowNull: false,
},
},
{
sequelize,
modelName: 'VehicleType',
tableName: 'vehicle',
schema: 'falukant_type',
timestamps: false,
underscored: true,
}
);
export default VehicleType;

View File

@@ -113,6 +113,9 @@ import PoliticalOfficeHistory from './falukant/log/political_office_history.js';
import ElectionHistory from './falukant/log/election_history.js';
import UndergroundType from './falukant/type/underground.js';
import Underground from './falukant/data/underground.js';
import VehicleType from './falukant/type/vehicle.js';
import Vehicle from './falukant/data/vehicle.js';
import Transport from './falukant/data/transport.js';
import Room from './chat/room.js';
import ChatUser from './chat/user.js';
@@ -207,6 +210,9 @@ const models = {
Credit,
DebtorsPrism,
HealthActivity,
VehicleType,
Vehicle,
Transport,
PoliticalOfficeType,
PoliticalOfficeRequirement,
PoliticalOfficeBenefitType,

View File

@@ -69,6 +69,8 @@ router.post('/politics/elections', falukantController.vote);
router.get('/politics/open', falukantController.getOpenPolitics);
router.post('/politics/open', falukantController.applyForElections);
router.get('/cities', falukantController.getRegions);
router.get('/vehicles/types', falukantController.getVehicleTypes);
router.post('/vehicles', falukantController.buyVehicles);
router.get('/underground/types', falukantController.getUndergroundTypes);
router.get('/notifications', falukantController.getNotifications);
router.get('/notifications/all', falukantController.getAllNotifications);

View File

@@ -57,6 +57,8 @@ import UndergroundType from '../models/falukant/type/underground.js';
import Notification from '../models/falukant/log/notification.js';
import PoliticalOffice from '../models/falukant/data/political_office.js';
import Underground from '../models/falukant/data/underground.js';
import VehicleType from '../models/falukant/type/vehicle.js';
import Vehicle from '../models/falukant/data/vehicle.js';
function calcAge(birthdate) {
const b = new Date(birthdate); b.setHours(0, 0);
@@ -448,6 +450,55 @@ class FalukantService extends BaseService {
return FalukantStock.findAll({ where: { regionId: b.regionId, userId: u.id } });
}
async getVehicleTypes(hashedUserId) {
// Validate user existence, but we don't filter by user here
await getFalukantUserOrFail(hashedUserId);
return VehicleType.findAll();
}
async buyVehicles(hashedUserId, { vehicleTypeId, quantity, regionId }) {
const user = await getFalukantUserOrFail(hashedUserId);
const qty = Math.max(1, parseInt(quantity, 10) || 0);
const type = await VehicleType.findByPk(vehicleTypeId);
if (!type) {
throw new Error('Vehicle type not found');
}
const totalCost = type.cost * qty;
if (user.money < totalCost) {
throw new PreconditionError('insufficientFunds');
}
// Ensure the region exists (and is part of Falukant map)
const region = await RegionData.findByPk(regionId);
if (!region) {
throw new Error('Region not found');
}
// Update money and create vehicles in a transaction
await sequelize.transaction(async (tx) => {
await updateFalukantUserMoney(
user.id,
-totalCost,
'buy_vehicles',
user.id
);
const records = [];
for (let i = 0; i < qty; i++) {
records.push({
vehicleTypeId: type.id,
falukantUserId: user.id,
regionId: region.id,
});
}
await Vehicle.bulkCreate(records, { transaction: tx });
});
return { success: true };
}
async createStock(hashedUserId, branchId, stockData) {
const u = await getFalukantUserOrFail(hashedUserId);
const b = await getBranchOrFail(u.id, branchId);

View File

@@ -12,6 +12,7 @@ import TitleOfNobility from "../../models/falukant/type/title_of_nobility.js";
import PartyType from "../../models/falukant/type/party.js";
import MusicType from "../../models/falukant/type/music.js";
import BanquetteType from "../../models/falukant/type/banquette.js";
import VehicleType from "../../models/falukant/type/vehicle.js";
import LearnRecipient from "../../models/falukant/type/learn_recipient.js";
import PoliticalOfficeType from "../../models/falukant/type/political_office_type.js";
import PoliticalOfficeBenefitType from "../../models/falukant/type/political_office_benefit_type.js";
@@ -41,6 +42,7 @@ export const initializeFalukantTypes = async () => {
await initializePoliticalOfficeTypes();
await initializePoliticalOfficePrerequisites();
await initializeUndergroundTypes();
await initializeVehicleTypes();
};
const regionTypes = [];
@@ -273,6 +275,16 @@ const learnerTypes = [
{ tr: 'director', },
];
const vehicleTypes = [
{ tr: 'cargo_cart', name: 'Lastkarren', cost: 100, capacity: 20, transportMode: 'land', speed: 1 },
{ tr: 'ox_cart', name: 'Ochsenkarren', cost: 200, capacity: 50, transportMode: 'land', speed: 2 },
{ tr: 'small_carriage', name: 'kleine Pferdekutsche', cost: 300, capacity: 35, transportMode: 'land', speed: 3 },
{ tr: 'large_carriage', name: 'große Pferdekutsche', cost: 1000, capacity: 100, transportMode: 'land', speed: 3 },
{ tr: 'four_horse_carriage', name: 'Vierspänner', cost: 5000, capacity: 200, transportMode: 'land', speed: 4 },
{ tr: 'raft', name: 'Floß', cost: 100, capacity: 25, transportMode: 'water', speed: 1 },
{ tr: 'sailing_ship', name: 'Segelschiff', cost: 500, capacity: 200, transportMode: 'water', speed: 3 },
];
const politicalOfficeBenefitTypes = [
{ tr: 'salary' },
{ tr: 'reputation' },
@@ -883,6 +895,21 @@ export const initializeLearnerTypes = async () => {
}
};
export const initializeVehicleTypes = async () => {
for (const v of vehicleTypes) {
await VehicleType.findOrCreate({
where: { tr: v.tr },
defaults: {
tr: v.tr,
cost: v.cost,
capacity: v.capacity,
transportMode: v.transportMode,
speed: v.speed,
},
});
}
};
export const initializePoliticalOfficeBenefitTypes = async () => {
for (const benefitType of politicalOfficeBenefitTypes) {
await PoliticalOfficeBenefitType.findOrCreate({