2026-02-07 03:34:22 +00:00

310 lines
3.3 KiB
JavaScript

const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');
module.exports = function(sequelize, DataTypes) {
const units = sequelize.define(
'units',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: {
type: DataTypes.TEXT,
},
callsign: {
type: DataTypes.TEXT,
},
echelon: {
type: DataTypes.ENUM,
values: [
"team",
"squad",
"platoon",
"company",
"battalion",
"brigade",
"division",
"corps",
"fleet",
"wing"
],
},
readiness: {
type: DataTypes.ENUM,
values: [
"fresh",
"ready",
"tired",
"exhausted",
"broken"
],
},
posture: {
type: DataTypes.ENUM,
values: [
"idle",
"moving",
"attacking",
"defending",
"retreating",
"patrolling",
"entrenched",
"resupplying"
],
},
health: {
type: DataTypes.DECIMAL,
},
morale: {
type: DataTypes.DECIMAL,
},
experience: {
type: DataTypes.DECIMAL,
},
fuel_level: {
type: DataTypes.DECIMAL,
},
ammo_level: {
type: DataTypes.DECIMAL,
},
supply_level: {
type: DataTypes.DECIMAL,
},
is_destroyed: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
notes: {
type: DataTypes.TEXT,
},
importHash: {
type: DataTypes.STRING(255),
allowNull: true,
unique: true,
},
},
{
timestamps: true,
paranoid: true,
freezeTableName: true,
},
);
units.associate = (db) => {
/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
db.units.hasMany(db.unit_loadouts, {
as: 'unit_loadouts_unit',
foreignKey: {
name: 'unitId',
},
constraints: false,
});
db.units.hasMany(db.map_markers, {
as: 'map_markers_unit',
foreignKey: {
name: 'unitId',
},
constraints: false,
});
db.units.hasMany(db.orders, {
as: 'orders_unit',
foreignKey: {
name: 'unitId',
},
constraints: false,
});
db.units.hasMany(db.events, {
as: 'events_unit',
foreignKey: {
name: 'unitId',
},
constraints: false,
});
//end loop
db.units.belongsTo(db.scenarios, {
as: 'scenario',
foreignKey: {
name: 'scenarioId',
},
constraints: false,
});
db.units.belongsTo(db.scenario_factions, {
as: 'scenario_faction',
foreignKey: {
name: 'scenario_factionId',
},
constraints: false,
});
db.units.belongsTo(db.unit_types, {
as: 'unit_type',
foreignKey: {
name: 'unit_typeId',
},
constraints: false,
});
db.units.belongsTo(db.users, {
as: 'createdBy',
});
db.units.belongsTo(db.users, {
as: 'updatedBy',
});
};
return units;
};