aboutsummaryrefslogtreecommitdiff
path: root/web/pw-server/src/lib/log_parser.ts
blob: da1d3c0dcd7f0d96a349a6e53c7693d0dbd5c39d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
export type PlayerLog = PlayerLogTurn[];

export type PlayerLogTurn = {
  action?: PlayerAction;
  stderr: string[];
};

type PlayerAction = Timeout | BadCommand | Dispatches;

type Timeout = {
  type: "timeout";
};

type BadCommand = {
  type: "bad_command";
  command: string;
  error: string;
};

type Dispatches = {
  type: "dispatches";
  dispatches: Dispatch[];
};

type Dispatch = {
  origin: string;
  destination: string;
  ship_count: number;
  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);
          break;
        }
        case "timeout":
        case "bad_command":
        case "dispatches": {
          turn.action = logMessage;
          break;
        }
      }
    }
  });

  return playerLog;
}