diff --git a/src/client/client.ts b/src/client/client.ts index 512dad5..0c8cdab 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -73,20 +73,23 @@ router.get( allData .filter((e) => filters(e, req.query)) .forEach((e) => { - if (!data[e[index]]) - data[e[index]] = { + if (!e.cause_of_death || !e.attacker_id || !e.map || !e.servername || !e.game_mode) return + const requestIndex = e[index] + if (!requestIndex) return + if (!data[requestIndex]) + data[requestIndex] = { deaths: 0, kills: 0, max_distance: 0, total_distance: 0 } - if (index === 'attacker_id') data[e[index]].username = e.attacker_name - if (index === 'servername') data[e[index]].host = e.host - data[e[index]].deaths += Number(e.deaths) - data[e[index]].kills += Number(e.kills) - data[e[index]].total_distance += Number(e.total_distance) - data[e[index]].max_distance = Math.max( - data[e[index]].max_distance, + if (index === 'attacker_id') data[requestIndex].username = e.attacker_name + if (index === 'servername') data[requestIndex].host = e.host + data[requestIndex].deaths += Number(e.deaths) + data[requestIndex].kills += Number(e.kills) + data[requestIndex].total_distance += Number(e.total_distance) + data[requestIndex].max_distance = Math.max( + data[requestIndex].max_distance, Number(e.max_distance) ) }) diff --git a/src/process/onKill.ts b/src/process/onKill.ts index 0accb23..df9944d 100644 --- a/src/process/onKill.ts +++ b/src/process/onKill.ts @@ -69,7 +69,7 @@ export default async function listenKills() { killEntry.kills++ killEntry.total_distance += payload.distance - killEntry.max_distance = Math.max(killEntry.max_distance, payload.distance) + killEntry.max_distance = Math.max(killEntry.max_distance || 0, payload.distance) deathEntry.deaths++ }) return pgClient diff --git a/src/process/process.ts b/src/process/process.ts index fe4a606..e9ed7c7 100644 --- a/src/process/process.ts +++ b/src/process/process.ts @@ -62,7 +62,8 @@ async function processGlobalStats() { .select([ count('id').as('deaths'), 'victim_id', - 'victim_current_weapon', + sql`last(victim_name)`.as('victim_name'), + 'cause_of_death', 'map', 'game_mode', 'servername', @@ -70,7 +71,7 @@ async function processGlobalStats() { ]) .groupBy([ 'victim_id', - 'victim_current_weapon', + 'cause_of_death', 'map', 'servername', 'host', @@ -78,17 +79,29 @@ async function processGlobalStats() { ]) ) .selectFrom('kills') - .leftJoin('deaths', (join) => + .fullJoin('deaths', (join) => join .onRef('deaths.victim_id', '=', 'kills.attacker_id') - .onRef('deaths.victim_current_weapon', '=', 'kills.cause_of_death') + .onRef('deaths.cause_of_death', '=', 'kills.cause_of_death') .onRef('deaths.servername', '=', 'kills.servername') .onRef('deaths.host', '=', 'kills.host') .onRef('deaths.game_mode', '=', 'kills.game_mode') .onRef('deaths.map', '=', 'kills.map') ) - .selectAll('kills') - .select(sql`COALESCE(deaths.deaths, 0)`.as('deaths')) + //.selectAll('kills') + .select([ + sql`COALESCE(kills.attacker_name, deaths.victim_name)`.as('attacker_name'), + sql`COALESCE(kills.attacker_id, deaths.victim_id)`.as('attacker_id'), + sql`COALESCE(kills.kills, 0)`.as('kills'), + sql`COALESCE(kills.total_distance, 0)`.as('total_distance'), + sql`COALESCE(kills.max_distance, 0)`.as('max_distance'), + sql`COALESCE(deaths.deaths, 0)`.as('deaths'), + sql`COALESCE(kills.servername, deaths.servername)`.as('servername'), + sql`COALESCE(kills.host, deaths.host)`.as('host'), + sql`COALESCE(kills.cause_of_death, deaths.cause_of_death)`.as('cause_of_death'), + sql`COALESCE(kills.map, deaths.map)`.as('map'), + sql`COALESCE(kills.game_mode, deaths.game_mode)`.as('game_mode'), + ]) .execute() } export default processAll