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 Arena_playersDBApi { static async create(data, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const arena_players = await db.arena_players.create( { id: data.id || undefined, nickname: data.nickname || null , team: data.team || null , state: data.state || null , spawn_order: data.spawn_order || null , health: data.health || null , max_health: data.max_health || null , total_damage_dealt: data.total_damage_dealt || null , total_damage_taken: data.total_damage_taken || null , tap_count: data.tap_count || null , gift_count: data.gift_count || null , gift_value_total: data.gift_value_total || null , spawned_at: data.spawned_at || null , eliminated_at: data.eliminated_at || null , importHash: data.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, }, { transaction }, ); await arena_players.setSession( data.session || null, { transaction, }); await arena_players.setViewer( data.viewer || null, { transaction, }); return arena_players; } 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 arena_playersData = data.map((item, index) => ({ id: item.id || undefined, nickname: item.nickname || null , team: item.team || null , state: item.state || null , spawn_order: item.spawn_order || null , health: item.health || null , max_health: item.max_health || null , total_damage_dealt: item.total_damage_dealt || null , total_damage_taken: item.total_damage_taken || null , tap_count: item.tap_count || null , gift_count: item.gift_count || null , gift_value_total: item.gift_value_total || null , spawned_at: item.spawned_at || null , eliminated_at: item.eliminated_at || null , importHash: item.importHash || null, createdById: currentUser.id, updatedById: currentUser.id, createdAt: new Date(Date.now() + index * 1000), })); // Bulk create items const arena_players = await db.arena_players.bulkCreate(arena_playersData, { transaction }); // For each item created, replace relation files return arena_players; } static async update(id, data, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const arena_players = await db.arena_players.findByPk(id, {}, {transaction}); const updatePayload = {}; if (data.nickname !== undefined) updatePayload.nickname = data.nickname; if (data.team !== undefined) updatePayload.team = data.team; if (data.state !== undefined) updatePayload.state = data.state; if (data.spawn_order !== undefined) updatePayload.spawn_order = data.spawn_order; if (data.health !== undefined) updatePayload.health = data.health; if (data.max_health !== undefined) updatePayload.max_health = data.max_health; if (data.total_damage_dealt !== undefined) updatePayload.total_damage_dealt = data.total_damage_dealt; if (data.total_damage_taken !== undefined) updatePayload.total_damage_taken = data.total_damage_taken; if (data.tap_count !== undefined) updatePayload.tap_count = data.tap_count; if (data.gift_count !== undefined) updatePayload.gift_count = data.gift_count; if (data.gift_value_total !== undefined) updatePayload.gift_value_total = data.gift_value_total; if (data.spawned_at !== undefined) updatePayload.spawned_at = data.spawned_at; if (data.eliminated_at !== undefined) updatePayload.eliminated_at = data.eliminated_at; updatePayload.updatedById = currentUser.id; await arena_players.update(updatePayload, {transaction}); if (data.session !== undefined) { await arena_players.setSession( data.session, { transaction } ); } if (data.viewer !== undefined) { await arena_players.setViewer( data.viewer, { transaction } ); } return arena_players; } static async deleteByIds(ids, options) { const currentUser = (options && options.currentUser) || { id: null }; const transaction = (options && options.transaction) || undefined; const arena_players = await db.arena_players.findAll({ where: { id: { [Op.in]: ids, }, }, transaction, }); await db.sequelize.transaction(async (transaction) => { for (const record of arena_players) { await record.update( {deletedBy: currentUser.id}, {transaction} ); } for (const record of arena_players) { await record.destroy({transaction}); } }); return arena_players; } static async remove(id, options) { const currentUser = (options && options.currentUser) || {id: null}; const transaction = (options && options.transaction) || undefined; const arena_players = await db.arena_players.findByPk(id, options); await arena_players.update({ deletedBy: currentUser.id }, { transaction, }); await arena_players.destroy({ transaction }); return arena_players; } static async findBy(where, options) { const transaction = (options && options.transaction) || undefined; const arena_players = await db.arena_players.findOne( { where }, { transaction }, ); if (!arena_players) { return arena_players; } const output = arena_players.get({plain: true}); output.match_participants_arena_player = await arena_players.getMatch_participants_arena_player({ transaction }); output.session = await arena_players.getSession({ transaction }); output.viewer = await arena_players.getViewer({ 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.arena_sessions, as: 'session', where: filter.session ? { [Op.or]: [ { id: { [Op.in]: filter.session.split('|').map(term => Utils.uuid(term)) } }, { name: { [Op.or]: filter.session.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, { model: db.tiktok_viewers, as: 'viewer', where: filter.viewer ? { [Op.or]: [ { id: { [Op.in]: filter.viewer.split('|').map(term => Utils.uuid(term)) } }, { display_name: { [Op.or]: filter.viewer.split('|').map(term => ({ [Op.iLike]: `%${term}%` })) } }, ] } : {}, }, ]; if (filter) { if (filter.id) { where = { ...where, ['id']: Utils.uuid(filter.id), }; } if (filter.nickname) { where = { ...where, [Op.and]: Utils.ilike( 'arena_players', 'nickname', filter.nickname, ), }; } if (filter.spawn_orderRange) { const [start, end] = filter.spawn_orderRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, spawn_order: { ...where.spawn_order, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, spawn_order: { ...where.spawn_order, [Op.lte]: end, }, }; } } if (filter.healthRange) { const [start, end] = filter.healthRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, health: { ...where.health, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, health: { ...where.health, [Op.lte]: end, }, }; } } if (filter.max_healthRange) { const [start, end] = filter.max_healthRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, max_health: { ...where.max_health, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, max_health: { ...where.max_health, [Op.lte]: end, }, }; } } if (filter.total_damage_dealtRange) { const [start, end] = filter.total_damage_dealtRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, total_damage_dealt: { ...where.total_damage_dealt, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, total_damage_dealt: { ...where.total_damage_dealt, [Op.lte]: end, }, }; } } if (filter.total_damage_takenRange) { const [start, end] = filter.total_damage_takenRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, total_damage_taken: { ...where.total_damage_taken, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, total_damage_taken: { ...where.total_damage_taken, [Op.lte]: end, }, }; } } if (filter.tap_countRange) { const [start, end] = filter.tap_countRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, tap_count: { ...where.tap_count, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, tap_count: { ...where.tap_count, [Op.lte]: end, }, }; } } if (filter.gift_countRange) { const [start, end] = filter.gift_countRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, gift_count: { ...where.gift_count, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, gift_count: { ...where.gift_count, [Op.lte]: end, }, }; } } if (filter.gift_value_totalRange) { const [start, end] = filter.gift_value_totalRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, gift_value_total: { ...where.gift_value_total, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, gift_value_total: { ...where.gift_value_total, [Op.lte]: end, }, }; } } if (filter.spawned_atRange) { const [start, end] = filter.spawned_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, spawned_at: { ...where.spawned_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, spawned_at: { ...where.spawned_at, [Op.lte]: end, }, }; } } if (filter.eliminated_atRange) { const [start, end] = filter.eliminated_atRange; if (start !== undefined && start !== null && start !== '') { where = { ...where, eliminated_at: { ...where.eliminated_at, [Op.gte]: start, }, }; } if (end !== undefined && end !== null && end !== '') { where = { ...where, eliminated_at: { ...where.eliminated_at, [Op.lte]: end, }, }; } } if (filter.active !== undefined) { where = { ...where, active: filter.active === true || filter.active === 'true' }; } if (filter.team) { where = { ...where, team: filter.team, }; } if (filter.state) { where = { ...where, state: filter.state, }; } 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.arena_players.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( 'arena_players', 'nickname', query, ), ], }; } const records = await db.arena_players.findAll({ attributes: [ 'id', 'nickname' ], where, limit: limit ? Number(limit) : undefined, offset: offset ? Number(offset) : undefined, orderBy: [['nickname', 'ASC']], }); return records.map((record) => ({ id: record.id, label: record.nickname, })); } };