Skip to content

Commit

Permalink
update to schema
Browse files Browse the repository at this point in the history
  • Loading branch information
bananahampster committed Apr 4, 2024
1 parent 4248b74 commit 854dd65
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 45 deletions.
101 changes: 69 additions & 32 deletions db-schema.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
EVENT table (table is new)

| name | type | values | notes |
|-----------------|----------|---------|----------------------------|
| eventId | | | auto-increment, NOT NULL |
| logId | | | log table id ref, NOT NULL |
| isFirstLog | bool | | default true |
| eventType | enum | 0-71 | NOT NULL |
| lineNumber | number | | NOT NULL |
| timestamp | datetime | | NOT NULL |
| gameTime | number | seconds | NOT NULL |
| extraData | string | | (prefer json format?) |
| playerFrom | | | player table id ref |
| playerFromClass | short | 0-9 | |
| playerTo | | | player table id ref |
| playerToClass | short | 0-9 | |
| withWeapon | short | 0-39 | |
| playerFromFlag | bool | | default false |
| playerToFlag | bool | | default false |
| name | type | values | notes |
|-----------------|----------|---------|------------------------------|
| eventId | | | auto-increment, NOT NULL |
| roundId | | | round table id ref, NOT NULL |
| eventType | enum | 0-71 | NOT NULL |
| lineNumber | number | | NOT NULL |
| timestamp | datetime | | NOT NULL |
| gameTime | number | seconds | NOT NULL |
| extraData | string | | (prefer json format?) |
| playerFrom | | | player table id ref |
| playerFromClass | short | 0-9 | |
| playerTo | | | player table id ref |
| playerToClass | short | 0-9 | |
| withWeapon | short | 0-39 | |
| playerFromFlag | bool | | default false |
| playerToFlag | bool | | default false |

CREATE TABLE event (
id serial,
logId integer NOT NULL,
isFirstLog boolean NOT NULL DEFAULT TRUE,
roundId integer NOT NULL,
eventType smallint NOT NULL,
lineNumber smallint NOT NULL,
timestamp timestamp without time zone NOT NULL,
Expand All @@ -35,9 +33,9 @@ CREATE TABLE event (
playerFromFlag boolean DEFAULT false,
playerToFlag boolean DEFAULT false,
PRIMARY KEY(id),
CONSTRAINT fk_log
FOREIGN KEY(logId)
REFERENCES logs(id)
CONSTRAINT fk_round
FOREIGN KEY(roundId)
REFERENCES round(id)
ON DELETE NO ACTION,
CONSTRAINT fk_playerFrom
FOREIGN KEY(playerFrom)
Expand All @@ -49,11 +47,34 @@ CREATE TABLE event (
ON DELETE NO ACTION
);

CREATE INDEX idx_event_logid on event (logid)
CREATE INDEX idx_event_roundid on event (roundId);
CREATE INDEX idx_event_playerfrom on event(playerFrom);
CREATE INDEX idx_event_playerto on event(playerTo);
CREATE INDEX idx_event_eventtype on event(eventType);


ROUND table (table is new)

| name | type | desc | notes |
|-----------------|----------|---------|----------------------------|
| id | | | auto-increment, NOT NULL |
| logId | number | | logs table id ref |
| isFirst | boolean | | default TRUE |

CREATE TABLE round (
id serial,
logId integer NOT NULL,
isFirst boolean NOT NULL DEFAULT TRUE,
PRIMARY KEY(id),
CONSTRAINT fk_log
FOREIGN KEY(logId)
REFERENCES logs(id)
ON DELETE NO ACTION
);

CREATE INDEX idx_round_logid on round(logid);


PLAYER table (table is new)

| name | type | desc | notes |
Expand All @@ -75,25 +96,26 @@ MATCH table (table is new)

| name | type | desc | notes |
|-----------------|----------|---------|----------------------------|
| logid | int | | log table id ref |
| playerid | int | | player table id ref |
| roundId | int | | round table id ref |
| playerId | int | | player table id ref |
| team | smallint | 0-4 | NOT NULL (players are 1-2) |

CREATE TABLE match (
logid integer NOT NULL,
playerid integer NOT NULL,
roundId integer NOT NULL,
playerId integer NOT NULL,
team smallint NOT NULL,
CONSTRAINT fk_log
FOREIGN KEY(logId)
REFERENCES logs(id)
CONSTRAINT fk_round
FOREIGN KEY(roundId)
REFERENCES round(id)
ON DELETE NO ACTION,
CONSTRAINT fk_player
FOREIGN KEY(playerid)
FOREIGN KEY(playerId)
REFERENCES player(id)
ON DELETE NO ACTION
);

CREATE INDEX idx_match_logid ON match (logid)
CREATE INDEX idx_match_roundid ON match (roundId);
CREATE INDEX idx_match_playerid ON match (playerId);


LOGS table (* are new columns)
Expand All @@ -117,6 +139,21 @@ alter table logs add column "is_valid" boolean not null default true;
alter table logs add column "score_team1" integer default 0;
alter table logs add column "score_team2" integer default 0;

CREATE TABLE logs (
id integer NOT NULL,
parsedlog character varying(255) NOT NULL,
log_file1 character varying(255) NOT NULL,
log_file2 character varying(255),
date_parsed timestamp without time zone NOT NULL,
date_match timestamp without time zone NOT NULL,
map character varying(50),
server character varying(255),
num_players smallint
is_valid boolean NOT NULL DEFAULT TRUE,
score_team1 integer DEFAULT 0,
score_team2 integer DEFAULT 0,
);


PARSEDLOGS table (table is new)

Expand Down
77 changes: 64 additions & 13 deletions src/database.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pg from 'pg';
import { OutputPlayer, ParsingError, TeamComposition } from './constants.js';
import { OutputPlayer, ParsingError, TeamColor, TeamComposition } from './constants.js';
import { Event, ParsedStats } from './parser.js';
import PlayerList from './playerList.js';
import Player from './player.js';
Expand Down Expand Up @@ -49,6 +49,7 @@ export class DB {

await this.query('TRUNCATE TABLE match');
await this.query('TRUNCATE TABLE event RESTART IDENTITY');
await this.query('TRUNCATE TABLE round RESTART IDENTITY CASCADE');
// await this.query('TRUNCATE TABLE player RESTART IDENTITY CASCADE'); // no need to truncate this

return logs;
Expand Down Expand Up @@ -150,15 +151,18 @@ export class DB {
if (logId == null)
logId = await this.recordLog(matchMeta, client);

const numRounds = events.length;

const playerMapping = await this.ensurePlayers(client, players);
await this.saveTeams(client, parsedStats.players, logId, playerMapping);
const roundIds = await this.saveRoundTeams(client, parsedStats.players, logId, numRounds, playerMapping);
await this.saveScore(client, matchMeta.score, logId);

for (let roundNum = 0; roundNum < events.length; roundNum++) {
const round = events[roundNum];
const isFirstRound = roundNum === 0;
const roundId = roundIds[roundNum];

for (const event of round) {
await this.addEvent(client, logId, event, isFirstRound);
await this.addEvent(client, roundId, event);
}
}

Expand Down Expand Up @@ -271,24 +275,72 @@ export class DB {
return playerToId;
}

private async saveTeams(client: pg.PoolClient, players: TeamComposition<OutputPlayer>, logId: number, playerMapping: Record<string, number>): Promise<void> {
private async saveRoundTeams(
client: pg.PoolClient,
players: TeamComposition<OutputPlayer>,
logId: number,
numRounds: number,
playerMapping: Record<string, number>): Promise<number[]> {

const secondRoundOppositeTeam: Record<TeamColor, TeamColor> = {
0: 0,
1: 2, // swap
2: 1, // swap
3: 3,
4: 4,
5: 5,
}
const roundIds: number[] = [];

for (let i = 0; i < numRounds; i++) {
const newRoundResult = await client.query<{ id: number }>(
`INSERT INTO round(logId, isFirst) VALUES ($1, $2) RETURNING id`,
[
logId,
i === 0,
]
);

const roundId = newRoundResult.rows?.[0].id;
if (roundId == null)
throw new ParsingError({
name: "LOGIC_FAILURE",
message: 'unable to add new round to DB',
});

roundIds.push(roundId);
}

for (const team in players) {
const teamPlayers = players[team] as OutputPlayer[];
if (teamPlayers != null) {
for (const player of teamPlayers) {
const playerId = playerMapping[player.steamID];

client.query(
`INSERT INTO match(logid, playerid, team) VALUES ($1, $2, $3)`,
await client.query(
`INSERT INTO match(roundid, playerid, team) VALUES ($1, $2, $3)`,
[
logId,
roundIds[0],
playerId,
+team
]
);

if (roundIds[1] != null) {
await client.query(
`INSERT INTO match(roundid, playerid, team) VALUES ($1, $2, $3)`,
[
roundIds[1],
playerId,
secondRoundOppositeTeam[+team],
]
);
}
}
}
}

return roundIds;
}

private async saveScore(client: pg.PoolClient, score: TeamScore, logId: number): Promise<void> {
Expand All @@ -304,14 +356,13 @@ export class DB {
);
}

private async addEvent(client: pg.PoolClient, logId: number, event: Event, isFirstRound: boolean): Promise<void> {
private async addEvent(client: pg.PoolClient, roundId: number, event: Event): Promise<void> {
client.query(
`INSERT INTO
event(logId, isFirstLog, eventType, lineNumber, timestamp, gameTime, extraData, playerFrom, playerFromClass, playerTo, playerToClass, withWeapon, playerFromFlag, playerToFlag)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)`,
event(roundId, eventType, lineNumber, timestamp, gameTime, extraData, playerFrom, playerFromClass, playerTo, playerToClass, withWeapon, playerFromFlag, playerToFlag)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)`,
[
logId,
isFirstRound,
roundId,
event.eventType,
event.lineNumber,
event.timestamp,
Expand Down

0 comments on commit 854dd65

Please sign in to comment.