aboutsummaryrefslogtreecommitdiff
path: root/web/pw-server/src/lib/components/log_viewer/PlayerLog.svelte
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2022-11-13 10:05:15 +0100
committerIlion Beyst <ilion.beyst@gmail.com>2022-11-13 10:05:15 +0100
commit4aa8ca8303b5d25b745e8cae71e283455945bdd1 (patch)
tree1869fbe29ee1e63cdd2c28e93225561d391f4a71 /web/pw-server/src/lib/components/log_viewer/PlayerLog.svelte
parentb0530be5010494f2db3deded340c16a6dc3cbe56 (diff)
downloadplanetwars.dev-4aa8ca8303b5d25b745e8cae71e283455945bdd1.tar.xz
planetwars.dev-4aa8ca8303b5d25b745e8cae71e283455945bdd1.zip
copy input gamestate from player log
Diffstat (limited to 'web/pw-server/src/lib/components/log_viewer/PlayerLog.svelte')
-rw-r--r--web/pw-server/src/lib/components/log_viewer/PlayerLog.svelte38
1 files changed, 33 insertions, 5 deletions
diff --git a/web/pw-server/src/lib/components/log_viewer/PlayerLog.svelte b/web/pw-server/src/lib/components/log_viewer/PlayerLog.svelte
index 450fdf2..f21a038 100644
--- a/web/pw-server/src/lib/components/log_viewer/PlayerLog.svelte
+++ b/web/pw-server/src/lib/components/log_viewer/PlayerLog.svelte
@@ -3,11 +3,39 @@
import LogTurn from "./LogTurn.svelte";
export let matchLog: string;
+ export let matchData: object;
export let playerId: number;
let playerLog: PlayerLog;
let showRawStderr = false;
+ async function copyTurn(turnNum: number) {
+ // find state for turnNum
+ let gamestate = matchLog
+ .split("\n")
+ .slice(0, -1)
+ .map((line) => JSON.parse(line))
+ .filter((json) => json["type"] == "gamestate")
+ .at(turnNum);
+
+ let numPlayers = matchData["players"].length;
+ let rotatePlayerNum = (playerNum: number | null) => {
+ if (playerNum === null) {
+ return null;
+ }
+ return ((numPlayers + playerNum - playerId) % numPlayers) + 1;
+ };
+
+ gamestate["planets"].forEach((planet) => {
+ planet["owner"] = rotatePlayerNum(planet["owner"]);
+ });
+ gamestate["expeditions"].forEach((expedition) => {
+ expedition["owner"] = rotatePlayerNum(expedition["owner"]);
+ });
+
+ await navigator.clipboard.writeText(JSON.stringify(gamestate));
+ }
+
$: if (matchLog) {
playerLog = parsePlayerLog(playerId, matchLog);
} else {
@@ -23,11 +51,11 @@
{:else}
<!-- The log should be rerendered when playerId changes -->
{#key playerId}
- <div class="log-contents">
- {#each playerLog as logTurn, turnNum}
- <LogTurn {logTurn} {turnNum} />
- {/each}
- </div>
+ <div class="log-contents">
+ {#each playerLog as logTurn, turnNum}
+ <LogTurn {logTurn} {turnNum} copyTurn={() => copyTurn(turnNum)} />
+ {/each}
+ </div>
{/key}
{/if}
</div>