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 BattlesDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const battles = await db.battles.create( { id: data.id || undefined, battle_code: data.battle_code || null , status: data.status || null , started_on: data.started_on || null , ended_on: data.ended_on || null , animations_skipped: data.animations_skipped || false , attacker_total_health_start: data.attacker_total_health_start || null , attacker_total_attack_start: data.attacker_total_attack_start || null , attacker_total_defense_start: data.attacker_total_defense_start || null , defender_total_health_start: data.defender_total_health_start || null , defender_total_attack_start: data.defender_total_attack_start || null , defender_total_defense_start: data.defender_total_defense_start || null , attacker_total_damage_dealt: data.attacker_total_damage_dealt || null , defender_total_damage_dealt: data.defender_total_damage_dealt || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await battles.setAttacker_farm( data.attacker_farm || null, { transaction, }); await battles.setDefender_farm( data.defender_farm || null, { transaction, }); await battles.setWinner_farm( data.winner_farm || null, { transaction, }); return battles; } 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 battlesData = data.map((item, index) => ({ id: item.id || undefined, battle_code: item.battle_code || null , status: item.status || null , started_on: item.started_on || null , ended_on: item.ended_on || null , animations_skipped: item.animations_skipped || false , attacker_total_health_start: item.attacker_total_health_start || null , attacker_total_attack_start: item.attacker_total_attack_start || null , attacker_total_defense_start: item.attacker_total_defense_start || null , defender_total_health_start: item.defender_total_health_start || null , defender_total_attack_start: item.defender_total_attack_start || null , defender_total_defense_start: item.defender_total_defense_start || null , attacker_total_damage_dealt: item.attacker_total_damage_dealt || null , defender_total_damage_dealt: item.defender_total_damage_dealt || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const battles = await db.battles.bulkCreate(battlesData, { transaction }); // For each item created, replace relation files return battles; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const battles = await db.battles.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.battle_code !== undefined) updatePayload.battle_code = data.battle_code; if (data.status !== undefined) updatePayload.status = data.status; if (data.started_on !== undefined) updatePayload.started_on = data.started_on; if (data.ended_on !== undefined) updatePayload.ended_on = data.ended_on; if (data.animations_skipped !== undefined) updatePayload.animations_skipped = data.animations_skipped; if (data.attacker_total_health_start !== undefined) updatePayload.attacker_total_health_start = data.attacker_total_health_start; if (data.attacker_total_attack_start !== undefined) updatePayload.attacker_total_attack_start = data.attacker_total_attack_start; if (data.attacker_total_defense_start !== undefined) updatePayload.attacker_total_defense_start = data.attacker_total_defense_start; if (data.defender_total_health_start !== undefined) updatePayload.defender_total_health_start = data.defender_total_health_start; if (data.defender_total_attack_start !== undefined) updatePayload.defender_total_attack_start = data.defender_total_attack_start; if (data.defender_total_defense_start !== undefined) updatePayload.defender_total_defense_start = data.defender_total_defense_start; if (data.attacker_total_damage_dealt !== undefined) updatePayload.attacker_total_damage_dealt = data.attacker_total_damage_dealt; if (data.defender_total_damage_dealt !== undefined) updatePayload.defender_total_damage_dealt = data.defender_total_damage_dealt; updatePayload.updatedById = currentUser.id; await battles.update(updatePayload, {transaction}); if (data.attacker_farm !== undefined) { await battles.setAttacker_farm( data.attacker_farm, { transaction } ); } if (data.defender_farm !== undefined) { await battles.setDefender_farm( data.defender_farm, { transaction } ); } if (data.winner_farm !== undefined) { await battles.setWinner_farm( data.winner_farm, { transaction } ); } return battles; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const battles = await db.battles.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of battles) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of battles) { await record.destroy({transaction}); } }); return battles; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const battles = await db.battles.findByPk(id, options); await battles.update({ deletedBy: currentUser.id }, { transaction, }); await battles.destroy({ transaction }); return battles; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const battles = await db.battles.findOne( { where }, { transaction }, ); if (!battles) { return battles; } const output = battles.get({plain: true}); output.battle_turns_battle = await battles.getBattle_turns_battle({ transaction }); output.attacker_farm = await battles.getAttacker_farm({ transaction }); output.defender_farm = await battles.getDefender_farm({ transaction }); output.winner_farm = await battles.getWinner_farm({ 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.farms, as: 'attacker_farm', where: filter.attacker_farm ? { [Op.or]: [ { id: { [Op.in]: filter.attacker_farm.split('|').map(term => Utils.uuid(term)) } }, { farm_name: { [Op.or]: filter.attacker_farm.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.farms, as: 'defender_farm', where: filter.defender_farm ? { [Op.or]: [ { id: { [Op.in]: filter.defender_farm.split('|').map(term => Utils.uuid(term)) } }, { farm_name: { [Op.or]: filter.defender_farm.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.farms, as: 'winner_farm', where: filter.winner_farm ? { [Op.or]: [ { id: { [Op.in]: filter.winner_farm.split('|').map(term => Utils.uuid(term)) } }, { farm_name: { [Op.or]: filter.winner_farm.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.battle_code) { where = { ...where, [Op.and]: Utils.ilike( 'battles', 'battle_code', filter.battle_code, ), }; } if (filter.calendarStart && filter.calendarEnd) { where = { ...where, [Op.or]: [ { started_on: { [Op.between]: [filter.calendarStart, filter.calendarEnd], }, }, { ended_on: { [Op.between]: [filter.calendarStart, filter.calendarEnd], }, }, ], }; } if (filter.started_onRange) { const [start, end] = filter.started_onRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, started_on: { ...where.started_on, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, started_on: { ...where.started_on, [Op.lte]: end, }, }; } } if (filter.ended_onRange) { const [start, end] = filter.ended_onRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, ended_on: { ...where.ended_on, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, ended_on: { ...where.ended_on, [Op.lte]: end, }, }; } } if (filter.attacker_total_health_startRange) { const [start, end] = filter.attacker_total_health_startRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, attacker_total_health_start: { ...where.attacker_total_health_start, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, attacker_total_health_start: { ...where.attacker_total_health_start, [Op.lte]: end, }, }; } } if (filter.attacker_total_attack_startRange) { const [start, end] = filter.attacker_total_attack_startRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, attacker_total_attack_start: { ...where.attacker_total_attack_start, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, attacker_total_attack_start: { ...where.attacker_total_attack_start, [Op.lte]: end, }, }; } } if (filter.attacker_total_defense_startRange) { const [start, end] = filter.attacker_total_defense_startRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, attacker_total_defense_start: { ...where.attacker_total_defense_start, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, attacker_total_defense_start: { ...where.attacker_total_defense_start, [Op.lte]: end, }, }; } } if (filter.defender_total_health_startRange) { const [start, end] = filter.defender_total_health_startRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, defender_total_health_start: { ...where.defender_total_health_start, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, defender_total_health_start: { ...where.defender_total_health_start, [Op.lte]: end, }, }; } } if (filter.defender_total_attack_startRange) { const [start, end] = filter.defender_total_attack_startRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, defender_total_attack_start: { ...where.defender_total_attack_start, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, defender_total_attack_start: { ...where.defender_total_attack_start, [Op.lte]: end, }, }; } } if (filter.defender_total_defense_startRange) { const [start, end] = filter.defender_total_defense_startRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, defender_total_defense_start: { ...where.defender_total_defense_start, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, defender_total_defense_start: { ...where.defender_total_defense_start, [Op.lte]: end, }, }; } } if (filter.attacker_total_damage_dealtRange) { const [start, end] = filter.attacker_total_damage_dealtRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, attacker_total_damage_dealt: { ...where.attacker_total_damage_dealt, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, attacker_total_damage_dealt: { ...where.attacker_total_damage_dealt, [Op.lte]: end, }, }; } } if (filter.defender_total_damage_dealtRange) { const [start, end] = filter.defender_total_damage_dealtRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, defender_total_damage_dealt: { ...where.defender_total_damage_dealt, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, defender_total_damage_dealt: { ...where.defender_total_damage_dealt, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.status) { where = { ...where, status: filter.status, }; } if (filter.animations_skipped) { where = { ...where, animations_skipped: filter.animations_skipped, }; } 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.battles.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( 'battles', 'battle_code', query, ), ], }; } const records = await db.battles.findAll({ attributes: [ 'id', 'battle_code' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['battle_code', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.battle_code, })); } };