Skip to content

Stats logger #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as Presets from './presets';
import { RegisterRoutes } from './routes';
import * as Storage from './storage';
import * as WebSocket from './webSocket';
import * as StatsLogger from './statsLogger';

export const TMT_LOG_ADDRESS: string | null = (() => {
if (!process.env['TMT_LOG_ADDRESS']) {
Expand Down Expand Up @@ -126,6 +127,7 @@ const main = async () => {
console.info(`App dir: ${APP_DIR}, frontend dir: ${FRONTEND_DIR}`);
await Storage.setup();
await ManagedGameServers.setup();
await StatsLogger.setup();
await Auth.setup();
await WebSocket.setup(httpServer);
await Presets.setup();
Expand Down
105 changes: 105 additions & 0 deletions backend/src/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { Rcon } from './rcon-client';
import { Settings } from './settings';
import * as Storage from './storage';
import * as Team from './team';
import * as StatsLogger from './statsLogger';

const STORAGE_LOGS_PREFIX = 'logs_';
const STORAGE_LOGS_SUFFIX = '.jsonl';
Expand All @@ -53,6 +54,7 @@ export const createFromData = async (data: IMatch, logMessage?: string) => {
log: () => {},
warnAboutWrongTeam: true,
};
await StatsLogger.onNewMatch(data);
match.data = addChangeListener(data, createOnDataChangeHandler(match));
match.log = createLogger(match);
if (logMessage) {
Expand Down Expand Up @@ -226,6 +228,7 @@ const setup = async (match: Match) => {
await setTeamNames(match);

await execRcon(match, 'log on');
await execRcon(match, 'mp_logdetail 1');
await execRcon(match, 'mp_warmuptime 600');
await execRcon(match, 'mp_warmup_pausetimer 1');
await execRcon(match, 'mp_autokick 0');
Expand Down Expand Up @@ -596,6 +599,49 @@ const onPlayerLogLine = async (
player = match.data.players.find((p) => p.steamId64 === steamId64);
if (!player) {
player = Player.create(match, steamId, name);
const playerExists =
(
(await Storage.queryDB(
`SELECT * FROM ${StatsLogger.PLAYERS_TABLE} WHERE steamId = '${player.steamId64}'`
)) as any[]
).length > 0;
if (!playerExists) {
await Storage.insertDB(
StatsLogger.PLAYERS_TABLE,
new Map<string, string | number>([
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

diff, hspct and adr should be removed here as well

['steamId', player.steamId64],
['name', player.name],
['tKills', 0],
['tDeaths', 0],
['tAssists', 0],
['tDiff', 0],
['tHits', 0],
['tHeadshots', 0],
['tHsPct', 0],
['tRounds', 0],
['tDamages', 0],
['tAdr', 0],
])
);
}
await Storage.insertDB(
StatsLogger.PLAYERS_TABLE,
new Map<string, string | number>([
['steamId', player.steamId64],
['matchId', match.data.id],
['map', match.data.matchMaps[match.data.currentMap]?.name ?? ''],
['kills', 0],
['deaths', 0],
['assists', 0],
['diff', 0],
['hits', 0],
['headshots', 0],
['hsPct', 0],
['rounds', 0],
['damages', 0],
['adr', 0],
])
);
match.log(`Player ${player.steamId64} (${name}) created`);
match.data.players.push(player);
player = match.data.players[match.data.players.length - 1]!; // re-assign to work nicely with changeListener (ProxyHandler)
Expand Down Expand Up @@ -669,6 +715,65 @@ const onPlayerLogLine = async (
await onPlayerSay(match, player, message, isTeamChat, teamString);
return;
}

//attacked "PlayerName<1><U:1:12345678><CT>" [2397 2079 133] with "glock" (damage "117") (damage_armor "0") (health "0") (armor "0") (hitgroup "head")
const damageMatch = remainingLine.match(
/^attacked ".+<\d+><[\[\]\w:]+><(?:TERRORIST|CT)>" \[-?\d+ -?\d+ -?\d+\] with "\w+" \(damage "(\d+)"\) \(damage_armor "(\d+)"\) \(health "(\d+)"\) \(armor "(\d+)"\) \(hitgroup "([\w ]+)"\)$/
);
if (damageMatch) {
const damage = Number(damageMatch[1]);
const damageArmor = Number(damageMatch[2]);
const headshot = damageMatch[3] === 'head';
await StatsLogger.onDamage(
match.data.id,
match.data.matchMaps[match.data.currentMap]?.name ?? '',
steamId,
damage,
damageArmor,
headshot
);
return;
}

//killed "PlayerName<2><STEAM_1:1:12345678><TERRORIST>" [-100 150 60] with "ak47" (headshot)
const killMatch = remainingLine.match(
/^killed ".+<\d+><([\[\]\w:]+)><(?:|Unassigned|TERRORIST|CT)>" \[-?\d+ -?\d+ -?\d+\] with "\w+" ?\(?(headshot|penetrated|headshot penetrated)?\)?$/
);
if (killMatch) {
const victimId = killMatch[1]!;
await StatsLogger.onKill(
match.data.id,
match.data.matchMaps[match.data.currentMap]?.name ?? '',
steamId,
victimId
);
return;
}

//assisted killing "PlayerName2<3><STEAM_1:1:87654321><CT>"
const assistMatch = remainingLine.match(/^assisted killing/);
if (assistMatch) {
await StatsLogger.onAssist(
match.data.id,
match.data.matchMaps[match.data.currentMap]?.name ?? '',
steamId
);
return;
}

//committed suicide with "world"
//was killed by the bomb
const otherDeathMatch = remainingLine.match(
/^(?:was killed by the bomb|committed suicide with)/
);
if (otherDeathMatch) {
await StatsLogger.onOtherDeath(
match.data.id,
match.data.matchMaps[match.data.currentMap]?.name ?? '',
steamId
);
return;
}
};

const onPlayerSay = async (
Expand Down
2 changes: 2 additions & 0 deletions backend/src/matchMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as Events from './events';
import { colors, formatMapName } from './gameServer';
import * as Match from './match';
import * as MatchService from './matchService';
import * as StatsLogger from './statsLogger';

export const create = (
map: string,
Expand Down Expand Up @@ -163,6 +164,7 @@ export const onRoundEnd = async (
match.log('Switching sides');
await Match.say(match, 'SWITCHING SIDES');
}
await StatsLogger.updateRoundCount(match.data, matchMap);
return;
}
};
Expand Down
Loading