871 lines
22 KiB
JavaScript
871 lines
22 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 ScoresDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const scores = await db.scores.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
osu_score_number: data.osu_score_number
|
|
||
|
|
null
|
|
,
|
|
|
|
scoring_type: data.scoring_type
|
|
||
|
|
null
|
|
,
|
|
|
|
accuracy: data.accuracy
|
|
||
|
|
null
|
|
,
|
|
|
|
max_combo: data.max_combo
|
|
||
|
|
null
|
|
,
|
|
|
|
grade: data.grade
|
|
||
|
|
null
|
|
,
|
|
|
|
count_300: data.count_300
|
|
||
|
|
null
|
|
,
|
|
|
|
count_200: data.count_200
|
|
||
|
|
null
|
|
,
|
|
|
|
count_100: data.count_100
|
|
||
|
|
null
|
|
,
|
|
|
|
count_50: data.count_50
|
|
||
|
|
null
|
|
,
|
|
|
|
count_miss: data.count_miss
|
|
||
|
|
null
|
|
,
|
|
|
|
is_full_combo: data.is_full_combo
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
score_weight: data.score_weight
|
|
||
|
|
null
|
|
,
|
|
|
|
played_at: data.played_at
|
|
||
|
|
null
|
|
,
|
|
|
|
ingested_at: data.ingested_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await scores.setPlayer( data.player || null, {
|
|
transaction,
|
|
});
|
|
|
|
await scores.setBeatmap( data.beatmap || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return scores;
|
|
}
|
|
|
|
|
|
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 scoresData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
osu_score_number: item.osu_score_number
|
|
||
|
|
null
|
|
,
|
|
|
|
scoring_type: item.scoring_type
|
|
||
|
|
null
|
|
,
|
|
|
|
accuracy: item.accuracy
|
|
||
|
|
null
|
|
,
|
|
|
|
max_combo: item.max_combo
|
|
||
|
|
null
|
|
,
|
|
|
|
grade: item.grade
|
|
||
|
|
null
|
|
,
|
|
|
|
count_300: item.count_300
|
|
||
|
|
null
|
|
,
|
|
|
|
count_200: item.count_200
|
|
||
|
|
null
|
|
,
|
|
|
|
count_100: item.count_100
|
|
||
|
|
null
|
|
,
|
|
|
|
count_50: item.count_50
|
|
||
|
|
null
|
|
,
|
|
|
|
count_miss: item.count_miss
|
|
||
|
|
null
|
|
,
|
|
|
|
is_full_combo: item.is_full_combo
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
score_weight: item.score_weight
|
|
||
|
|
null
|
|
,
|
|
|
|
played_at: item.played_at
|
|
||
|
|
null
|
|
,
|
|
|
|
ingested_at: item.ingested_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const scores = await db.scores.bulkCreate(scoresData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return scores;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const scores = await db.scores.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.osu_score_number !== undefined) updatePayload.osu_score_number = data.osu_score_number;
|
|
|
|
|
|
if (data.scoring_type !== undefined) updatePayload.scoring_type = data.scoring_type;
|
|
|
|
|
|
if (data.accuracy !== undefined) updatePayload.accuracy = data.accuracy;
|
|
|
|
|
|
if (data.max_combo !== undefined) updatePayload.max_combo = data.max_combo;
|
|
|
|
|
|
if (data.grade !== undefined) updatePayload.grade = data.grade;
|
|
|
|
|
|
if (data.count_300 !== undefined) updatePayload.count_300 = data.count_300;
|
|
|
|
|
|
if (data.count_200 !== undefined) updatePayload.count_200 = data.count_200;
|
|
|
|
|
|
if (data.count_100 !== undefined) updatePayload.count_100 = data.count_100;
|
|
|
|
|
|
if (data.count_50 !== undefined) updatePayload.count_50 = data.count_50;
|
|
|
|
|
|
if (data.count_miss !== undefined) updatePayload.count_miss = data.count_miss;
|
|
|
|
|
|
if (data.is_full_combo !== undefined) updatePayload.is_full_combo = data.is_full_combo;
|
|
|
|
|
|
if (data.score_weight !== undefined) updatePayload.score_weight = data.score_weight;
|
|
|
|
|
|
if (data.played_at !== undefined) updatePayload.played_at = data.played_at;
|
|
|
|
|
|
if (data.ingested_at !== undefined) updatePayload.ingested_at = data.ingested_at;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await scores.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.player !== undefined) {
|
|
await scores.setPlayer(
|
|
|
|
data.player,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.beatmap !== undefined) {
|
|
await scores.setBeatmap(
|
|
|
|
data.beatmap,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return scores;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const scores = await db.scores.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of scores) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of scores) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return scores;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const scores = await db.scores.findByPk(id, options);
|
|
|
|
await scores.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await scores.destroy({
|
|
transaction
|
|
});
|
|
|
|
return scores;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const scores = await db.scores.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!scores) {
|
|
return scores;
|
|
}
|
|
|
|
const output = scores.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.score_skill_contributions_score = await scores.getScore_skill_contributions_score({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.player_top_plays_score = await scores.getPlayer_top_plays_score({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.player = await scores.getPlayer({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.beatmap = await scores.getBeatmap({
|
|
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.players,
|
|
as: 'player',
|
|
|
|
where: filter.player ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.player.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
username: {
|
|
[Op.or]: filter.player.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.beatmaps,
|
|
as: 'beatmap',
|
|
|
|
where: filter.beatmap ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.beatmap.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
difficulty_name: {
|
|
[Op.or]: filter.beatmap.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.osu_score_numberRange) {
|
|
const [start, end] = filter.osu_score_numberRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
osu_score_number: {
|
|
...where.osu_score_number,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
osu_score_number: {
|
|
...where.osu_score_number,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.accuracyRange) {
|
|
const [start, end] = filter.accuracyRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
accuracy: {
|
|
...where.accuracy,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
accuracy: {
|
|
...where.accuracy,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.max_comboRange) {
|
|
const [start, end] = filter.max_comboRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
max_combo: {
|
|
...where.max_combo,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
max_combo: {
|
|
...where.max_combo,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.count_300Range) {
|
|
const [start, end] = filter.count_300Range;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
count_300: {
|
|
...where.count_300,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
count_300: {
|
|
...where.count_300,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.count_200Range) {
|
|
const [start, end] = filter.count_200Range;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
count_200: {
|
|
...where.count_200,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
count_200: {
|
|
...where.count_200,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.count_100Range) {
|
|
const [start, end] = filter.count_100Range;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
count_100: {
|
|
...where.count_100,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
count_100: {
|
|
...where.count_100,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.count_50Range) {
|
|
const [start, end] = filter.count_50Range;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
count_50: {
|
|
...where.count_50,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
count_50: {
|
|
...where.count_50,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.count_missRange) {
|
|
const [start, end] = filter.count_missRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
count_miss: {
|
|
...where.count_miss,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
count_miss: {
|
|
...where.count_miss,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.score_weightRange) {
|
|
const [start, end] = filter.score_weightRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
score_weight: {
|
|
...where.score_weight,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
score_weight: {
|
|
...where.score_weight,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.played_atRange) {
|
|
const [start, end] = filter.played_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
played_at: {
|
|
...where.played_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
played_at: {
|
|
...where.played_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.ingested_atRange) {
|
|
const [start, end] = filter.ingested_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
ingested_at: {
|
|
...where.ingested_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
ingested_at: {
|
|
...where.ingested_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.scoring_type) {
|
|
where = {
|
|
...where,
|
|
scoring_type: filter.scoring_type,
|
|
};
|
|
}
|
|
|
|
if (filter.grade) {
|
|
where = {
|
|
...where,
|
|
grade: filter.grade,
|
|
};
|
|
}
|
|
|
|
if (filter.is_full_combo) {
|
|
where = {
|
|
...where,
|
|
is_full_combo: filter.is_full_combo,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.scores.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(
|
|
'scores',
|
|
'scoring_type',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.scores.findAll({
|
|
attributes: [ 'id', 'scoring_type' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['scoring_type', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.scoring_type,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|