523 lines
13 KiB
JavaScript
523 lines
13 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 Hit_eventsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const hit_events = await db.hit_events.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
time_offset_ms: data.time_offset_ms
|
|
||
|
|
null
|
|
,
|
|
|
|
judgement: data.judgement
|
|
||
|
|
null
|
|
,
|
|
|
|
combo_at_hit: data.combo_at_hit
|
|
||
|
|
null
|
|
,
|
|
|
|
score_delta: data.score_delta
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await hit_events.setSession( data.session || null, {
|
|
transaction,
|
|
});
|
|
|
|
await hit_events.setMove( data.move || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return hit_events;
|
|
}
|
|
|
|
|
|
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 hit_eventsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
time_offset_ms: item.time_offset_ms
|
|
||
|
|
null
|
|
,
|
|
|
|
judgement: item.judgement
|
|
||
|
|
null
|
|
,
|
|
|
|
combo_at_hit: item.combo_at_hit
|
|
||
|
|
null
|
|
,
|
|
|
|
score_delta: item.score_delta
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const hit_events = await db.hit_events.bulkCreate(hit_eventsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return hit_events;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const hit_events = await db.hit_events.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.time_offset_ms !== undefined) updatePayload.time_offset_ms = data.time_offset_ms;
|
|
|
|
|
|
if (data.judgement !== undefined) updatePayload.judgement = data.judgement;
|
|
|
|
|
|
if (data.combo_at_hit !== undefined) updatePayload.combo_at_hit = data.combo_at_hit;
|
|
|
|
|
|
if (data.score_delta !== undefined) updatePayload.score_delta = data.score_delta;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await hit_events.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.session !== undefined) {
|
|
await hit_events.setSession(
|
|
|
|
data.session,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.move !== undefined) {
|
|
await hit_events.setMove(
|
|
|
|
data.move,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return hit_events;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const hit_events = await db.hit_events.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of hit_events) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of hit_events) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return hit_events;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const hit_events = await db.hit_events.findByPk(id, options);
|
|
|
|
await hit_events.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await hit_events.destroy({
|
|
transaction
|
|
});
|
|
|
|
return hit_events;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const hit_events = await db.hit_events.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!hit_events) {
|
|
return hit_events;
|
|
}
|
|
|
|
const output = hit_events.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.session = await hit_events.getSession({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.move = await hit_events.getMove({
|
|
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.game_sessions,
|
|
as: 'session',
|
|
|
|
where: filter.session ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.session.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
device_info: {
|
|
[Op.or]: filter.session.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.routine_moves,
|
|
as: 'move',
|
|
|
|
where: filter.move ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.move.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
move_name: {
|
|
[Op.or]: filter.move.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.time_offset_msRange) {
|
|
const [start, end] = filter.time_offset_msRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
time_offset_ms: {
|
|
...where.time_offset_ms,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
time_offset_ms: {
|
|
...where.time_offset_ms,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.combo_at_hitRange) {
|
|
const [start, end] = filter.combo_at_hitRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
combo_at_hit: {
|
|
...where.combo_at_hit,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
combo_at_hit: {
|
|
...where.combo_at_hit,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.score_deltaRange) {
|
|
const [start, end] = filter.score_deltaRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
score_delta: {
|
|
...where.score_delta,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
score_delta: {
|
|
...where.score_delta,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.judgement) {
|
|
where = {
|
|
...where,
|
|
judgement: filter.judgement,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.hit_events.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(
|
|
'hit_events',
|
|
'judgement',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.hit_events.findAll({
|
|
attributes: [ 'id', 'judgement' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['judgement', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.judgement,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|