Skip to content

Commit 37cff11

Browse files
author
Ecnama
committed
Log hits and calculated stats
1 parent 4c8f2cd commit 37cff11

File tree

2 files changed

+164
-25
lines changed

2 files changed

+164
-25
lines changed

backend/src/match.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ const onPlayerLogLine = async (
628628
new Map<string, string | number>([
629629
['steamId', player.steamId64],
630630
['matchId', match.data.id],
631+
['map', match.data.matchMaps[match.data.currentMap]?.name ?? ''],
631632
['kills', 0],
632633
['deaths', 0],
633634
['assists', 0],
@@ -716,14 +717,20 @@ const onPlayerLogLine = async (
716717

717718
//attacked "PlayerName<1><U:1:12345678><CT>" [2397 2079 133] with "glock" (damage "117") (damage_armor "0") (health "0") (armor "0") (hitgroup "head")
718719
const damageMatch = remainingLine.match(
719-
/^attacked ".+<\d+><([\[\]\w:]+)><(?:TERRORIST|CT)>" \[-?\d+ -?\d+ -?\d+\] with "\w+" \(damage "(\d+)"\) \(damage_armor "(\d+)"\) \(health "(\d+)"\) \(armor "(\d+)"\) \(hitgroup "([\w ]+)"\)$/
720+
/^attacked ".+<\d+><[\[\]\w:]+><(?:TERRORIST|CT)>" \[-?\d+ -?\d+ -?\d+\] with "\w+" \(damage "(\d+)"\) \(damage_armor "(\d+)"\) \(health "(\d+)"\) \(armor "(\d+)"\) \(hitgroup "([\w ]+)"\)$/
720721
);
721722
if (damageMatch) {
722-
const victimId = damageMatch[1]!;
723-
const damage = Number(damageMatch[2]);
724-
const damageArmor = Number(damageMatch[3]);
725-
const headshot = damageMatch[4] == 'head';
726-
await StatsLogger.onDamage(match.data.id, steamId, victimId, damage, damageArmor, headshot);
723+
const damage = Number(damageMatch[1]);
724+
const damageArmor = Number(damageMatch[2]);
725+
const headshot = damageMatch[3] === 'head';
726+
await StatsLogger.onDamage(
727+
match.data.id,
728+
match.data.matchMaps[match.data.currentMap]?.name ?? '',
729+
steamId,
730+
damage,
731+
damageArmor,
732+
headshot
733+
);
727734
return;
728735
}
729736

@@ -733,22 +740,37 @@ const onPlayerLogLine = async (
733740
);
734741
if (killMatch) {
735742
const victimId = killMatch[1]!;
736-
await StatsLogger.onKill(match.data.id, steamId, victimId);
743+
await StatsLogger.onKill(
744+
match.data.id,
745+
match.data.matchMaps[match.data.currentMap]?.name ?? '',
746+
steamId,
747+
victimId
748+
);
737749
return;
738750
}
739751

740752
//assisted killing "PlayerName2<3><STEAM_1:1:87654321><CT>"
741753
const assistMatch = remainingLine.match(/^assisted killing/);
742754
if (assistMatch) {
743-
await StatsLogger.onAssist(match.data.id, steamId);
755+
await StatsLogger.onAssist(
756+
match.data.id,
757+
match.data.matchMaps[match.data.currentMap]?.name ?? '',
758+
steamId
759+
);
744760
return;
745761
}
746762

747763
//committed suicide with "world"
748764
//was killed by the bomb
749-
const bombKillMatch = remainingLine.match(/^(?:was killed by the bomb|committed suicide with)/);
750-
if (bombKillMatch) {
751-
await StatsLogger.onOtherDeath(match.data.id, steamId);
765+
const otherDeathMatch = remainingLine.match(
766+
/^(?:was killed by the bomb|committed suicide with)/
767+
);
768+
if (otherDeathMatch) {
769+
await StatsLogger.onOtherDeath(
770+
match.data.id,
771+
match.data.matchMaps[match.data.currentMap]?.name ?? '',
772+
steamId
773+
);
752774
return;
753775
}
754776
};

backend/src/statsLogger.ts

Lines changed: 131 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { SqlAttribute, TableSchema } from './tableSchema';
22
import { createTableDB, queryDB, updateDB } from './storage';
3+
import { update } from './match';
34

45
export const PLAYERS_TABLE = 'players';
56
export const MATCHES_TABLE = 'matches';
@@ -31,14 +32,17 @@ export const setup = async () => {
3132
// Create matches table
3233
const matchesAttributes = [
3334
{ name: 'matchId', type: 'TEXT' },
35+
{ name: 'map', type: 'TEXT' },
3436
{ name: 'teamA', type: 'TEXT' },
3537
{ name: 'teamAScore', type: 'TEXT' },
3638
{ name: 'teamB', type: 'TEXT' },
3739
{ name: 'teamBScore', type: 'TEXT' },
38-
{ name: 'map', type: 'TEXT' },
3940
{ name: 'winner', type: 'TEXT' },
4041
] as SqlAttribute[];
41-
const matchesTableSchema = new TableSchema(MATCHES_TABLE, matchesAttributes, ['matchId']);
42+
const matchesTableSchema = new TableSchema(MATCHES_TABLE, matchesAttributes, [
43+
'matchId',
44+
'map',
45+
]);
4246
await createTableDB(matchesTableSchema);
4347

4448
// Create player match stats table
@@ -53,6 +57,11 @@ export const setup = async () => {
5357
type: 'TEXT',
5458
constraints: `REFERENCES ${MATCHES_TABLE}(matchId)`,
5559
},
60+
{
61+
name: 'map',
62+
type: 'TEXT',
63+
constraints: `REFERENCES ${MATCHES_TABLE}(map)`,
64+
},
5665
{ name: 'kills', type: 'INTEGER' },
5766
{ name: 'deaths', type: 'INTEGER' },
5867
{ name: 'assists', type: 'INTEGER' },
@@ -71,35 +80,65 @@ export const setup = async () => {
7180
const playerMatchStatsTableSchema = new TableSchema(
7281
PLAYER_MATCH_STATS_TABLE,
7382
playerMatchStatsAttributes,
74-
['steamId', 'matchId']
83+
['steamId', 'matchId', 'map']
7584
);
7685
await createTableDB(playerMatchStatsTableSchema);
7786
};
7887

7988
export const onDamage = async (
8089
matchId: string,
90+
map: string,
8191
attackerId: string,
82-
victimId: string,
8392
damage: number,
8493
damageArmor: number,
8594
headshot: boolean
8695
) => {
87-
// TODO
96+
const currentAttackerMatchStats = (await queryDB(
97+
`SELECT hits,headshots,damages FROM ${PLAYER_MATCH_STATS_TABLE} WHERE steamId = '${attackerId}' AND matchId = '${matchId}'`
98+
)) as number[] | undefined;
99+
const currentAttackerGlobalStats = (await queryDB(
100+
`SELECT tHits,tHeadshots,tDamages FROM ${PLAYERS_TABLE} WHERE steamId = '${attackerId}'`
101+
)) as number[] | undefined;
102+
103+
if (currentAttackerMatchStats && currentAttackerGlobalStats) {
104+
await updateDB(
105+
PLAYER_MATCH_STATS_TABLE,
106+
new Map<string, number>([
107+
['hits', (currentAttackerMatchStats[0] ?? 0) + 1],
108+
['headshots', (currentAttackerMatchStats[1] ?? 0) + (headshot ? 1 : 0)],
109+
['damages', (currentAttackerMatchStats[2] ?? 0) + damage + damageArmor],
110+
]),
111+
`steamId = '${attackerId}' AND matchId = '${matchId}'`
112+
);
113+
await updateDB(
114+
PLAYERS_TABLE,
115+
new Map<string, number>([
116+
['tHits', (currentAttackerGlobalStats[0] ?? 0) + 1],
117+
['tHeadshots', (currentAttackerGlobalStats[1] ?? 0) + (headshot ? 1 : 0)],
118+
['tDamages', (currentAttackerGlobalStats[2] ?? 0) + damage + damageArmor],
119+
]),
120+
`steamId = '${attackerId}'`
121+
);
122+
}
123+
124+
await updateAdr(matchId, map, attackerId);
125+
await updateHsPct(matchId, map, attackerId);
88126
};
89127

90-
export const onKill = async (matchId: string, killerId: string, victimId: string) => {
128+
export const onKill = async (matchId: string, map: string, killerId: string, victimId: string) => {
91129
const currentKillerMatchStats = (await queryDB(
92-
`SELECT kills FROM ${PLAYER_MATCH_STATS_TABLE} WHERE steamId = '${killerId}' AND matchId = '${matchId}'`
130+
`SELECT kills FROM ${PLAYER_MATCH_STATS_TABLE} WHERE steamId = '${killerId}' AND matchId = '${matchId}' AND map = '${map}'`
93131
)) as number;
94132
const currentVictimMatchStats = (await queryDB(
95-
`SELECT deaths FROM ${PLAYER_MATCH_STATS_TABLE} WHERE steamId = '${victimId}' AND matchId = '${matchId}'`
133+
`SELECT deaths FROM ${PLAYER_MATCH_STATS_TABLE} WHERE steamId = '${victimId}' AND matchId = '${matchId}' AND map = '${map}'`
96134
)) as number;
97135
const currentKillerGlobalStats = (await queryDB(
98136
`SELECT tKills FROM ${PLAYERS_TABLE} WHERE steamId = '${killerId}'`
99137
)) as number;
100138
const currentVictimGlobalStats = (await queryDB(
101139
`SELECT tDeaths FROM ${PLAYERS_TABLE} WHERE steamId = '${victimId}'`
102140
)) as number;
141+
103142
await updateDB(
104143
PLAYER_MATCH_STATS_TABLE,
105144
new Map<string, number>([['kills', currentKillerMatchStats + 1]]),
@@ -120,15 +159,18 @@ export const onKill = async (matchId: string, killerId: string, victimId: string
120159
new Map<string, number>([['tDeaths', currentVictimGlobalStats + 1]]),
121160
`steamId = '${victimId}'`
122161
);
162+
163+
await updateDiff(matchId, map, killerId);
123164
};
124165

125-
export const onAssist = async (matchId: string, attackerId: string) => {
166+
export const onAssist = async (matchId: string, map: string, attackerId: string) => {
126167
const currentAttackerMatchStats = (await queryDB(
127-
`SELECT assists FROM ${PLAYER_MATCH_STATS_TABLE} WHERE steamId = '${attackerId}' AND matchId = '${matchId}'`
168+
`SELECT assists FROM ${PLAYER_MATCH_STATS_TABLE} WHERE steamId = '${attackerId}' AND matchId = '${matchId}' AND map = '${map}'`
128169
)) as number;
129170
const currentAttackerGlobalStats = (await queryDB(
130171
`SELECT tAssists FROM ${PLAYERS_TABLE} WHERE steamId = '${attackerId}'`
131172
)) as number;
173+
132174
await updateDB(
133175
PLAYER_MATCH_STATS_TABLE,
134176
new Map<string, number>([['assists', currentAttackerMatchStats + 1]]),
@@ -141,13 +183,14 @@ export const onAssist = async (matchId: string, attackerId: string) => {
141183
);
142184
};
143185

144-
export const onOtherDeath = async (matchId: string, victimId: string) => {
186+
export const onOtherDeath = async (matchId: string, map: string, victimId: string) => {
145187
const currentVictimMatchStats = (await queryDB(
146-
`SELECT deaths FROM ${PLAYER_MATCH_STATS_TABLE} WHERE steamId = '${victimId}' AND matchId = '${matchId}'`
188+
`SELECT deaths FROM ${PLAYER_MATCH_STATS_TABLE} WHERE steamId = '${victimId}' AND matchId = '${matchId}' AND map = '${map}'`
147189
)) as number;
148190
const currentVictimGlobalStats = (await queryDB(
149191
`SELECT tDeaths FROM ${PLAYERS_TABLE} WHERE steamId = '${victimId}'`
150192
)) as number;
193+
151194
await updateDB(
152195
PLAYER_MATCH_STATS_TABLE,
153196
new Map<string, number>([['deaths', currentVictimMatchStats + 1]]),
@@ -158,8 +201,82 @@ export const onOtherDeath = async (matchId: string, victimId: string) => {
158201
new Map<string, number>([['tDeaths', currentVictimGlobalStats + 1]]),
159202
`steamId = '${victimId}'`
160203
);
204+
205+
await updateDiff(matchId, map, victimId);
206+
};
207+
208+
export const updateRoundCount = async (matchId: string, map: string) => {
209+
// TODO
210+
};
211+
212+
const updateDiff = async (matchId: string, map: string, steamId: string) => {
213+
const matchStats = (await queryDB(
214+
`SELECT kills,deaths FROM ${PLAYER_MATCH_STATS_TABLE} WHERE steamId = '${steamId}' AND matchId = '${matchId}' AND map = '${map}'`
215+
)) as number[] | undefined;
216+
const globalStats = (await queryDB(
217+
`SELECT tKills,tDeaths FROM ${PLAYERS_TABLE} WHERE steamId = '${steamId}'`
218+
)) as number[] | undefined;
219+
220+
if (matchStats && globalStats) {
221+
let diff = (matchStats[1] ?? 0) - (matchStats[0] ?? 0);
222+
await updateDB(
223+
PLAYER_MATCH_STATS_TABLE,
224+
new Map<string, number>([['diff', diff]]),
225+
`steamId = '${steamId}' AND matchId = '${matchId}' AND map = '${map}'`
226+
);
227+
diff = (globalStats[1] ?? 0) - (globalStats[0] ?? 0);
228+
await updateDB(
229+
PLAYERS_TABLE,
230+
new Map<string, number>([['diff', diff]]),
231+
`steamId = '${steamId}'`
232+
);
233+
}
234+
};
235+
236+
const updateHsPct = async (matchId: string, map: string, steamId: string) => {
237+
const matchStats = (await queryDB(
238+
`SELECT hits,headshots FROM ${PLAYER_MATCH_STATS_TABLE} WHERE steamId = '${steamId}' AND matchId = '${matchId}' AND map = '${map}'`
239+
)) as number[] | undefined;
240+
const globalStats = (await queryDB(
241+
`SELECT tHits,tHeadshots FROM ${PLAYERS_TABLE} WHERE steamId = '${steamId}'`
242+
)) as number[] | undefined;
243+
244+
if (matchStats && globalStats) {
245+
let hsPct = (100 * (matchStats[1] ?? 0)) / (matchStats[0] ?? 1);
246+
await updateDB(
247+
PLAYER_MATCH_STATS_TABLE,
248+
new Map<string, number>([['hsPct', hsPct]]),
249+
`steamId = '${steamId}' AND matchId = '${matchId}' AND map = '${map}'`
250+
);
251+
hsPct = (100 * (globalStats[1] ?? 0)) / (globalStats[0] ?? 1);
252+
await updateDB(
253+
PLAYERS_TABLE,
254+
new Map<string, number>([['tHsPct', hsPct]]),
255+
`steamId = '${steamId}'`
256+
);
257+
}
161258
};
162259

163-
export const updateRoundCount = async (matchId: string) => {
164-
//TODO
260+
const updateAdr = async (matchId: string, map: string, steamId: string) => {
261+
const matchStats = (await queryDB(
262+
`SELECT rounds,damages FROM ${PLAYER_MATCH_STATS_TABLE} WHERE steamId = '${steamId}' AND matchId = '${matchId}' AND map = '${map}'`
263+
)) as number[] | undefined;
264+
const globalStats = (await queryDB(
265+
`SELECT tRounds,tDamages FROM ${PLAYERS_TABLE} WHERE steamId = '${steamId}'`
266+
)) as number[] | undefined;
267+
268+
if (matchStats && globalStats) {
269+
let adr = (matchStats[1] ?? 0) / (matchStats[0] ?? 1);
270+
await updateDB(
271+
PLAYER_MATCH_STATS_TABLE,
272+
new Map<string, number>([['adr', adr]]),
273+
`steamId = '${steamId}' AND matchId = '${matchId}' AND map = '${map}'`
274+
);
275+
adr = (globalStats[1] ?? 0) / (globalStats[0] ?? 1);
276+
await updateDB(
277+
PLAYERS_TABLE,
278+
new Map<string, number>([['tAdr', adr]]),
279+
`steamId = '${steamId}'`
280+
);
281+
}
165282
};

0 commit comments

Comments
 (0)