670 lines
17 KiB
JavaScript
670 lines
17 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 Game_rulesetsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const game_rulesets = await db.game_rulesets.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
name: data.name
|
|
||
|
|
null
|
|
,
|
|
|
|
mode: data.mode
|
|
||
|
|
null
|
|
,
|
|
|
|
round_duration_seconds: data.round_duration_seconds
|
|
||
|
|
null
|
|
,
|
|
|
|
max_players: data.max_players
|
|
||
|
|
null
|
|
,
|
|
|
|
tap_damage: data.tap_damage
|
|
||
|
|
null
|
|
,
|
|
|
|
gift_damage_multiplier: data.gift_damage_multiplier
|
|
||
|
|
null
|
|
,
|
|
|
|
join_bonus_health: data.join_bonus_health
|
|
||
|
|
null
|
|
,
|
|
|
|
initial_health: data.initial_health
|
|
||
|
|
null
|
|
,
|
|
|
|
vip_only: data.vip_only
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
notes: data.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await game_rulesets.setCreated_by_user( data.created_by_user || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return game_rulesets;
|
|
}
|
|
|
|
|
|
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 game_rulesetsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
name: item.name
|
|
||
|
|
null
|
|
,
|
|
|
|
mode: item.mode
|
|
||
|
|
null
|
|
,
|
|
|
|
round_duration_seconds: item.round_duration_seconds
|
|
||
|
|
null
|
|
,
|
|
|
|
max_players: item.max_players
|
|
||
|
|
null
|
|
,
|
|
|
|
tap_damage: item.tap_damage
|
|
||
|
|
null
|
|
,
|
|
|
|
gift_damage_multiplier: item.gift_damage_multiplier
|
|
||
|
|
null
|
|
,
|
|
|
|
join_bonus_health: item.join_bonus_health
|
|
||
|
|
null
|
|
,
|
|
|
|
initial_health: item.initial_health
|
|
||
|
|
null
|
|
,
|
|
|
|
vip_only: item.vip_only
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
notes: item.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const game_rulesets = await db.game_rulesets.bulkCreate(game_rulesetsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return game_rulesets;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const game_rulesets = await db.game_rulesets.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.name !== undefined) updatePayload.name = data.name;
|
|
|
|
|
|
if (data.mode !== undefined) updatePayload.mode = data.mode;
|
|
|
|
|
|
if (data.round_duration_seconds !== undefined) updatePayload.round_duration_seconds = data.round_duration_seconds;
|
|
|
|
|
|
if (data.max_players !== undefined) updatePayload.max_players = data.max_players;
|
|
|
|
|
|
if (data.tap_damage !== undefined) updatePayload.tap_damage = data.tap_damage;
|
|
|
|
|
|
if (data.gift_damage_multiplier !== undefined) updatePayload.gift_damage_multiplier = data.gift_damage_multiplier;
|
|
|
|
|
|
if (data.join_bonus_health !== undefined) updatePayload.join_bonus_health = data.join_bonus_health;
|
|
|
|
|
|
if (data.initial_health !== undefined) updatePayload.initial_health = data.initial_health;
|
|
|
|
|
|
if (data.vip_only !== undefined) updatePayload.vip_only = data.vip_only;
|
|
|
|
|
|
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await game_rulesets.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.created_by_user !== undefined) {
|
|
await game_rulesets.setCreated_by_user(
|
|
|
|
data.created_by_user,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return game_rulesets;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const game_rulesets = await db.game_rulesets.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of game_rulesets) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of game_rulesets) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return game_rulesets;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const game_rulesets = await db.game_rulesets.findByPk(id, options);
|
|
|
|
await game_rulesets.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await game_rulesets.destroy({
|
|
transaction
|
|
});
|
|
|
|
return game_rulesets;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const game_rulesets = await db.game_rulesets.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!game_rulesets) {
|
|
return game_rulesets;
|
|
}
|
|
|
|
const output = game_rulesets.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.arena_sessions_ruleset = await game_rulesets.getArena_sessions_ruleset({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.created_by_user = await game_rulesets.getCreated_by_user({
|
|
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 = [
|
|
|
|
{
|
|
model: db.users,
|
|
as: 'created_by_user',
|
|
|
|
where: filter.created_by_user ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.created_by_user.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.created_by_user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'game_rulesets',
|
|
'name',
|
|
filter.name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.notes) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'game_rulesets',
|
|
'notes',
|
|
filter.notes,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.round_duration_secondsRange) {
|
|
const [start, end] = filter.round_duration_secondsRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
round_duration_seconds: {
|
|
...where.round_duration_seconds,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
round_duration_seconds: {
|
|
...where.round_duration_seconds,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.max_playersRange) {
|
|
const [start, end] = filter.max_playersRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
max_players: {
|
|
...where.max_players,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
max_players: {
|
|
...where.max_players,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.tap_damageRange) {
|
|
const [start, end] = filter.tap_damageRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
tap_damage: {
|
|
...where.tap_damage,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
tap_damage: {
|
|
...where.tap_damage,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.gift_damage_multiplierRange) {
|
|
const [start, end] = filter.gift_damage_multiplierRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
gift_damage_multiplier: {
|
|
...where.gift_damage_multiplier,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
gift_damage_multiplier: {
|
|
...where.gift_damage_multiplier,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.join_bonus_healthRange) {
|
|
const [start, end] = filter.join_bonus_healthRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
join_bonus_health: {
|
|
...where.join_bonus_health,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
join_bonus_health: {
|
|
...where.join_bonus_health,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.initial_healthRange) {
|
|
const [start, end] = filter.initial_healthRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
initial_health: {
|
|
...where.initial_health,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
initial_health: {
|
|
...where.initial_health,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.mode) {
|
|
where = {
|
|
...where,
|
|
mode: filter.mode,
|
|
};
|
|
}
|
|
|
|
if (filter.vip_only) {
|
|
where = {
|
|
...where,
|
|
vip_only: filter.vip_only,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.game_rulesets.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(
|
|
'game_rulesets',
|
|
'name',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.game_rulesets.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,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|