38201-vm/backend/src/db/api/procedural_generation_profiles.js
2026-02-05 02:29:20 +00:00

763 lines
21 KiB
JavaScript

const db = require('../models');
const FileDBApi = require('./file');
const crypto = require('crypto');
const Utils = require('../utils');
const Sequelize = db.Sequelize;
const Op = Sequelize.Op;
module.exports = class Procedural_generation_profilesDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const procedural_generation_profiles = await db.procedural_generation_profiles.create(
{
id: data.id || undefined,
name: data.name
||
null
,
target_mode: data.target_mode
||
null
,
anchor_spacing_min: data.anchor_spacing_min
||
null
,
anchor_spacing_max: data.anchor_spacing_max
||
null
,
verticality_factor: data.verticality_factor
||
null
,
obstacle_density: data.obstacle_density
||
null
,
moving_obstacle_ratio: data.moving_obstacle_ratio
||
null
,
rotating_obstacle_ratio: data.rotating_obstacle_ratio
||
null
,
booster_ratio: data.booster_ratio
||
null
,
bounce_platform_ratio: data.bounce_platform_ratio
||
null
,
gravity_shift_ratio: data.gravity_shift_ratio
||
null
,
timing_window_scale: data.timing_window_scale
||
null
,
is_enabled: data.is_enabled
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
return procedural_generation_profiles;
}
static async bulkImport(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
// Prepare data - wrapping individual data transformations in a map() method
const procedural_generation_profilesData = data.map((item, index) => ({
id: item.id || undefined,
name: item.name
||
null
,
target_mode: item.target_mode
||
null
,
anchor_spacing_min: item.anchor_spacing_min
||
null
,
anchor_spacing_max: item.anchor_spacing_max
||
null
,
verticality_factor: item.verticality_factor
||
null
,
obstacle_density: item.obstacle_density
||
null
,
moving_obstacle_ratio: item.moving_obstacle_ratio
||
null
,
rotating_obstacle_ratio: item.rotating_obstacle_ratio
||
null
,
booster_ratio: item.booster_ratio
||
null
,
bounce_platform_ratio: item.bounce_platform_ratio
||
null
,
gravity_shift_ratio: item.gravity_shift_ratio
||
null
,
timing_window_scale: item.timing_window_scale
||
null
,
is_enabled: item.is_enabled
||
false
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const procedural_generation_profiles = await db.procedural_generation_profiles.bulkCreate(procedural_generation_profilesData, { transaction });
// For each item created, replace relation files
return procedural_generation_profiles;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const procedural_generation_profiles = await db.procedural_generation_profiles.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.name !== undefined) updatePayload.name = data.name;
if (data.target_mode !== undefined) updatePayload.target_mode = data.target_mode;
if (data.anchor_spacing_min !== undefined) updatePayload.anchor_spacing_min = data.anchor_spacing_min;
if (data.anchor_spacing_max !== undefined) updatePayload.anchor_spacing_max = data.anchor_spacing_max;
if (data.verticality_factor !== undefined) updatePayload.verticality_factor = data.verticality_factor;
if (data.obstacle_density !== undefined) updatePayload.obstacle_density = data.obstacle_density;
if (data.moving_obstacle_ratio !== undefined) updatePayload.moving_obstacle_ratio = data.moving_obstacle_ratio;
if (data.rotating_obstacle_ratio !== undefined) updatePayload.rotating_obstacle_ratio = data.rotating_obstacle_ratio;
if (data.booster_ratio !== undefined) updatePayload.booster_ratio = data.booster_ratio;
if (data.bounce_platform_ratio !== undefined) updatePayload.bounce_platform_ratio = data.bounce_platform_ratio;
if (data.gravity_shift_ratio !== undefined) updatePayload.gravity_shift_ratio = data.gravity_shift_ratio;
if (data.timing_window_scale !== undefined) updatePayload.timing_window_scale = data.timing_window_scale;
if (data.is_enabled !== undefined) updatePayload.is_enabled = data.is_enabled;
updatePayload.updatedById = currentUser.id;
await procedural_generation_profiles.update(updatePayload, {transaction});
return procedural_generation_profiles;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const procedural_generation_profiles = await db.procedural_generation_profiles.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of procedural_generation_profiles) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of procedural_generation_profiles) {
await record.destroy({transaction});
}
});
return procedural_generation_profiles;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const procedural_generation_profiles = await db.procedural_generation_profiles.findByPk(id, options);
await procedural_generation_profiles.update({
deletedBy: currentUser.id
}, {
transaction,
});
await procedural_generation_profiles.destroy({
transaction
});
return procedural_generation_profiles;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const procedural_generation_profiles = await db.procedural_generation_profiles.findOne(
{ where },
{ transaction },
);
if (!procedural_generation_profiles) {
return procedural_generation_profiles;
}
const output = procedural_generation_profiles.get({plain: true});
output.endless_mode_configs_generation_profile = await procedural_generation_profiles.getEndless_mode_configs_generation_profile({
transaction
});
return output;
}
static async findAll(
filter,
options
) {
const limit = filter.limit || 0;
let offset = 0;
let where = {};
const currentPage = +filter.page;
offset = currentPage * limit;
const orderBy = null;
const transaction = (options && options.transaction) || undefined;
let include = [
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.name) {
where = {
...where,
[Op.and]: Utils.ilike(
'procedural_generation_profiles',
'name',
filter.name,
),
};
}
if (filter.anchor_spacing_minRange) {
const [start, end] = filter.anchor_spacing_minRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
anchor_spacing_min: {
...where.anchor_spacing_min,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
anchor_spacing_min: {
...where.anchor_spacing_min,
[Op.lte]: end,
},
};
}
}
if (filter.anchor_spacing_maxRange) {
const [start, end] = filter.anchor_spacing_maxRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
anchor_spacing_max: {
...where.anchor_spacing_max,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
anchor_spacing_max: {
...where.anchor_spacing_max,
[Op.lte]: end,
},
};
}
}
if (filter.verticality_factorRange) {
const [start, end] = filter.verticality_factorRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
verticality_factor: {
...where.verticality_factor,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
verticality_factor: {
...where.verticality_factor,
[Op.lte]: end,
},
};
}
}
if (filter.obstacle_densityRange) {
const [start, end] = filter.obstacle_densityRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
obstacle_density: {
...where.obstacle_density,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
obstacle_density: {
...where.obstacle_density,
[Op.lte]: end,
},
};
}
}
if (filter.moving_obstacle_ratioRange) {
const [start, end] = filter.moving_obstacle_ratioRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
moving_obstacle_ratio: {
...where.moving_obstacle_ratio,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
moving_obstacle_ratio: {
...where.moving_obstacle_ratio,
[Op.lte]: end,
},
};
}
}
if (filter.rotating_obstacle_ratioRange) {
const [start, end] = filter.rotating_obstacle_ratioRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
rotating_obstacle_ratio: {
...where.rotating_obstacle_ratio,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
rotating_obstacle_ratio: {
...where.rotating_obstacle_ratio,
[Op.lte]: end,
},
};
}
}
if (filter.booster_ratioRange) {
const [start, end] = filter.booster_ratioRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
booster_ratio: {
...where.booster_ratio,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
booster_ratio: {
...where.booster_ratio,
[Op.lte]: end,
},
};
}
}
if (filter.bounce_platform_ratioRange) {
const [start, end] = filter.bounce_platform_ratioRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
bounce_platform_ratio: {
...where.bounce_platform_ratio,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
bounce_platform_ratio: {
...where.bounce_platform_ratio,
[Op.lte]: end,
},
};
}
}
if (filter.gravity_shift_ratioRange) {
const [start, end] = filter.gravity_shift_ratioRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
gravity_shift_ratio: {
...where.gravity_shift_ratio,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
gravity_shift_ratio: {
...where.gravity_shift_ratio,
[Op.lte]: end,
},
};
}
}
if (filter.timing_window_scaleRange) {
const [start, end] = filter.timing_window_scaleRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
timing_window_scale: {
...where.timing_window_scale,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
timing_window_scale: {
...where.timing_window_scale,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.target_mode) {
where = {
...where,
target_mode: filter.target_mode,
};
}
if (filter.is_enabled) {
where = {
...where,
is_enabled: filter.is_enabled,
};
}
if (filter.createdAtRange) {
const [start, end] = filter.createdAtRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
['createdAt']: {
...where.createdAt,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
['createdAt']: {
...where.createdAt,
[Op.lte]: end,
},
};
}
}
}
const queryOptions = {
where,
include,
distinct: true,
order: filter.field && filter.sort
? [[filter.field, filter.sort]]
: [['createdAt', 'desc']],
transaction: options?.transaction,
logging: console.log
};
if (!options?.countOnly) {
queryOptions.limit = limit ? Number(limit) : undefined;
queryOptions.offset = offset ? Number(offset) : undefined;
}
try {
const { rows, count } = await db.procedural_generation_profiles.findAndCountAll(queryOptions);
return {
rows: options?.countOnly ? [] : rows,
count: count
};
} catch (error) {
console.error('Error executing query:', error);
throw error;
}
}
static async findAllAutocomplete(query, limit, offset, ) {
let where = {};
if (query) {
where = {
[Op.or]: [
{ ['id']: Utils.uuid(query) },
Utils.ilike(
'procedural_generation_profiles',
'name',
query,
),
],
};
}
const records = await db.procedural_generation_profiles.findAll({
attributes: [ 'id', 'name' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['name', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.name,
}));
}
};