aboutsummaryrefslogtreecommitdiff
path: root/web/pw-server/src/lib/log_parser.ts
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2022-09-17 11:55:17 +0200
committerIlion Beyst <ilion.beyst@gmail.com>2022-09-17 11:55:17 +0200
commitf5fe1c4f2918561d70a56d196aaf1b13c97a2bf1 (patch)
tree6700d2139eda39d87223b991fb4e9adeb89d1e02 /web/pw-server/src/lib/log_parser.ts
parent518ad1d811e87872fcd6d769435f485d0ed31768 (diff)
downloadplanetwars.dev-f5fe1c4f2918561d70a56d196aaf1b13c97a2bf1.tar.xz
planetwars.dev-f5fe1c4f2918561d70a56d196aaf1b13c97a2bf1.zip
initial version of structured log display
Diffstat (limited to 'web/pw-server/src/lib/log_parser.ts')
-rw-r--r--web/pw-server/src/lib/log_parser.ts55
1 files changed, 55 insertions, 0 deletions
diff --git a/web/pw-server/src/lib/log_parser.ts b/web/pw-server/src/lib/log_parser.ts
new file mode 100644
index 0000000..77c459f
--- /dev/null
+++ b/web/pw-server/src/lib/log_parser.ts
@@ -0,0 +1,55 @@
+export type PlayerLog = PlayerLogTurn[];
+
+export type PlayerLogTurn = {
+ action?: PlayerAction;
+ stderr: string[];
+};
+
+type PlayerAction = BadCommand;
+
+type BadCommand = {
+ type: "bad_command";
+ command: string;
+ error: string;
+};
+
+function createEmptyLogTurn(): PlayerLogTurn {
+ return {
+ stderr: [],
+ };
+}
+
+export function parsePlayerLog(playerId: number, logText: string): PlayerLog {
+ const logLines = logText.split("\n").slice(0, -1);
+
+ const playerLog: PlayerLog = [];
+
+ let turn = null;
+
+ logLines.forEach((logLine) => {
+ const logMessage = JSON.parse(logLine);
+
+ if (logMessage["type"] === "gamestate") {
+ if (turn) {
+ playerLog.push(turn);
+ turn = createEmptyLogTurn();
+ }
+ } else if (logMessage["player_id"] === playerId) {
+ if (!turn) {
+ // older match logs don't have an initial game state due to a bug.
+ turn = createEmptyLogTurn();
+ }
+ switch (logMessage["type"]) {
+ case "stderr": {
+ let msg = logMessage["message"];
+ turn.stderr.push(msg);
+ }
+ case "bad_command": {
+ turn.action = logMessage;
+ }
+ }
+ }
+ });
+
+ return playerLog;
+}