@textadventures/quest-viva-wasmplayer 6.1.0 → 6.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/wasm-player.js CHANGED
@@ -776,6 +776,7 @@ function wireTranscriptsButton() {
776
776
  // The WebPlayer object — the host API that the shared playercore.js and
777
777
  // player.js call into. The name predates WasmPlayer and is kept as-is.
778
778
  window.WebPlayer = {
779
+ whenTurnOver,
779
780
  gameId: null,
780
781
  chromeStrings: null,
781
782
  transcript,
@@ -822,6 +823,7 @@ window.WebPlayer = {
822
823
  } finally {
823
824
  turnsInFlight--;
824
825
  scheduleInterfaceChanges();
826
+ notifyIfTurnOver();
825
827
  }
826
828
  },
827
829
  async uiEndWait() { await playerTurn(() => Bridge.FinishWait()); },
@@ -855,6 +857,7 @@ async function playerTurn(run) {
855
857
  turnsInFlight--;
856
858
  window.notifyQuestUITurn?.(false);
857
859
  scheduleInterfaceChanges();
860
+ notifyIfTurnOver();
858
861
  }
859
862
  }
860
863
 
@@ -864,6 +867,22 @@ async function playerTurn(run) {
864
867
  let turnsInFlight = 0;
865
868
  let turnPending = false;
866
869
 
870
+ // Whoever is waiting for the turn to be over (whenTurnOver): a save a game's script asks for waits
871
+ // until then, so it has the whole turn rather than however much had run when it was asked for.
872
+ let turnOverWaiters = [];
873
+
874
+ function whenTurnOver() {
875
+ if (turnsInFlight === 0 && !turnPending) return Promise.resolve();
876
+ return new Promise(resolve => turnOverWaiters.push(resolve));
877
+ }
878
+
879
+ function notifyIfTurnOver() {
880
+ if (turnsInFlight > 0 || turnPending || !turnOverWaiters.length) return;
881
+ const waiters = turnOverWaiters;
882
+ turnOverWaiters = [];
883
+ for (const resolve of waiters) resolve();
884
+ }
885
+
867
886
  // Exported bridge reference — set once WASM is loaded.
868
887
  var Bridge;
869
888
 
@@ -1232,6 +1251,7 @@ async function initWasmPlayer(gameBytes, filename, bc = null, saveBytes = null,
1232
1251
  if (!pending) scheduleInterfaceChanges();
1233
1252
  },
1234
1253
  beginPause: (ms) => whenClockRuns(() => playerUi.beginPause(ms)),
1254
+ dismissPrompt: () => playerUi.dismissPrompt(),
1235
1255
  updateLocation: (loc) => playerUi.updateLocation(loc),
1236
1256
  setGameName: (name) => {
1237
1257
  currentGameName = name;
@@ -1482,6 +1502,8 @@ let restartInProgress = false;
1482
1502
  async function restartGame(saveBytes) {
1483
1503
  if (restartInProgress) return;
1484
1504
  restartInProgress = true;
1505
+ // A save asked for before the restart is of the game being thrown away.
1506
+ turnOverWaiters = [];
1485
1507
  try {
1486
1508
  await restartGameCore(saveBytes);
1487
1509
  } finally {
@@ -1957,7 +1979,7 @@ function downloadSaveBytes(bytes) {
1957
1979
  }
1958
1980
 
1959
1981
  async function downloadSaveToFile() {
1960
- const bytes = await WebPlayer.uiSaveGame(playerUi.getOutputHtml());
1982
+ const bytes = await WebPlayer.uiSaveGame(GameSaver.outputHtmlForSave());
1961
1983
  downloadSaveBytes(bytes);
1962
1984
  playerUi.clearUnsavedProgress();
1963
1985
  stepsSinceSave = 0;