show dispatches and timeouts in outputpane

This commit is contained in:
Ilion Beyst 2022-09-18 13:16:57 +02:00
parent 3be0cfa0ea
commit 3138eca6d0
2 changed files with 90 additions and 31 deletions

View file

@ -6,6 +6,19 @@
let showRawStderr = false; let showRawStderr = false;
const PLURAL_MAP = {
dispatch: "dispatches",
ship: "ships",
};
function pluralize(num: number, word: string): string {
if (num == 1) {
return `1 ${word}`;
} else {
return `${num} ${PLURAL_MAP[word]}`;
}
}
$: if (matchLog) { $: if (matchLog) {
playerLog = parsePlayerLog(1, matchLog); playerLog = parsePlayerLog(1, matchLog);
} else { } else {
@ -16,36 +29,53 @@
<div class="output"> <div class="output">
<h3 class="output-header">Player log</h3> <h3 class="output-header">Player log</h3>
{#if showRawStderr} {#if showRawStderr}
<div class="output-text stderr-text"> <div class="output-text stderr-text">
{playerLog.flatMap((turn) => turn.stderr).join("\n")} {playerLog.flatMap((turn) => turn.stderr).join("\n")}
</div> </div>
{:else} {:else}
<div class="output-text"> <div class="output-text">
{#each playerLog as logTurn, i} {#each playerLog as logTurn, i}
<div class="turn"> <div class="turn">
<div class="turn-header"> <div class="turn-header">
<span class="turn-header-text">Turn {i}</span> <span class="turn-header-text">Turn {i}</span>
{#if logTurn.action?.type === "bad_command"} {#if logTurn.action?.type === "dispatches"}
<span class="turn-error">invalid command</span> {pluralize(logTurn.action.dispatches.length, "dispatch")}
{/if} {:else if logTurn.action?.type === "timeout"}
</div> <span class="turn-error">timeout</span>
{#if logTurn.action?.type === "bad_command"} {:else if logTurn.action?.type === "bad_command"}
<div class="bad-command-container"> <span class="turn-error">invalid command</span>
<div class="bad-command-text">{logTurn.action.command}</div> {/if}
<div class="bad-command-error">Parse error: {logTurn.action.error}</div> </div>
</div> {#if logTurn.action?.type === "dispatches"}
{/if} <div class="dispatches-container">
{#if logTurn.stderr.length > 0} {#each logTurn.action.dispatches as dispatch}
<div class="stderr-header">stderr</div> <div class="dispatch">
<div class="stderr-text-box"> <div class="dispatch-text">
{#each logTurn.stderr as stdErrMsg} {pluralize(dispatch.ship_count, "ship")} from {dispatch.origin} to {dispatch.destination}
<div class="stderr-text">{stdErrMsg}</div> </div>
{/each} {#if dispatch.error}
</div> <span class="dispatch-error">{dispatch.error}</span>
{/if} {/if}
</div>
{/each}
</div>
{:else if logTurn.action?.type === "bad_command"}
<div class="bad-command-container">
<div class="bad-command-text">{logTurn.action.command}</div>
<div class="bad-command-error">Parse error: {logTurn.action.error}</div>
</div>
{/if}
{#if logTurn.stderr.length > 0}
<div class="stderr-header">stderr</div>
<div class="stderr-text-box">
{#each logTurn.stderr as stdErrMsg}
<div class="stderr-text">{stdErrMsg}</div>
{/each}
</div>
{/if}
</div>
{/each}
</div> </div>
{/each}
</div>
{/if} {/if}
</div> </div>
@ -81,6 +111,15 @@
color: red; color: red;
} }
.dispatch {
display: flex;
justify-content: space-between;
}
.dispatch-error {
color: red;
}
.bad-command-container { .bad-command-container {
border-left: 1px solid red; border-left: 1px solid red;
margin-left: 4px; margin-left: 4px;

View file

@ -5,7 +5,11 @@ export type PlayerLogTurn = {
stderr: string[]; stderr: string[];
}; };
type PlayerAction = BadCommand; type PlayerAction = Timeout | BadCommand | Dispatches;
type Timeout = {
type: "timeout";
};
type BadCommand = { type BadCommand = {
type: "bad_command"; type: "bad_command";
@ -13,6 +17,18 @@ type BadCommand = {
error: string; error: string;
}; };
type Dispatches = {
type: "dispatches";
dispatches: Dispatch[];
};
type Dispatch = {
origin: string;
destination: string;
ship_count: number;
error?: string;
};
function createEmptyLogTurn(): PlayerLogTurn { function createEmptyLogTurn(): PlayerLogTurn {
return { return {
stderr: [], stderr: [],
@ -43,9 +59,13 @@ export function parsePlayerLog(playerId: number, logText: string): PlayerLog {
case "stderr": { case "stderr": {
let msg = logMessage["message"]; let msg = logMessage["message"];
turn.stderr.push(msg); turn.stderr.push(msg);
break;
} }
case "bad_command": { case "timeout":
case "bad_command":
case "dispatches": {
turn.action = logMessage; turn.action = logMessage;
break;
} }
} }
} }