173 lines
5.5 KiB
JavaScript
173 lines
5.5 KiB
JavaScript
import { chapterMetadata } from "./tutorials.js";
|
|
import { mountLocalization } from "./localization/index.js";
|
|
import { mountLesson } from "./tutorial-mount.js";
|
|
import "./style.css";
|
|
import * as engine from "../engine/index.js";
|
|
import { mountGame } from "./ui.js";
|
|
const params = new URLSearchParams(location.search);
|
|
const localization = mountLocalization(
|
|
document.body,
|
|
params.get("locale") ?? "en",
|
|
);
|
|
await localization.ready;
|
|
if (params.has("lesson")) {
|
|
const names = chapterMetadata.map((chapter) => chapter.id);
|
|
const chapter = params.get("lesson");
|
|
const next = names[names.indexOf(chapter) + 1];
|
|
await mountLesson(document.querySelector("#game"), {
|
|
locale: params.get("locale") ?? "en",
|
|
chapter,
|
|
nextChapter: next
|
|
? {
|
|
title: chapterMetadata.find((chapter) => chapter.id === next).title,
|
|
open: () => {
|
|
location.search =
|
|
"?lesson=" + next + "&locale=" + (params.get("locale") ?? "en");
|
|
},
|
|
}
|
|
: undefined,
|
|
});
|
|
} else {
|
|
const count = Math.max(2, Math.min(5, Number(params.get("players") ?? 3)));
|
|
let state,
|
|
hotseat = params.has("hotseat"),
|
|
timer;
|
|
const storageKey = "vanuatu-practice-v1";
|
|
try {
|
|
if (!params.has("new"))
|
|
state = JSON.parse(localStorage.getItem(storageKey));
|
|
} catch {}
|
|
if (!state?.version)
|
|
state = engine.init(
|
|
count,
|
|
params.has("water") ? ["rising-waters"] : [],
|
|
{ practice: true },
|
|
params.get("seed") ?? "island-breeze",
|
|
);
|
|
state.achievementMode = "practice";
|
|
state.players.forEach(
|
|
(p, i) => (p.name = ["You", "Maya", "Noa", "Leo", "Nina"][i]),
|
|
);
|
|
const control = document.createElement("div");
|
|
control.className = "preview-controls";
|
|
control.innerHTML =
|
|
'<span>Local practice</span><button data-new>New game</button><label><input type="checkbox" data-hotseat> Every seat</label><button data-step>Bot move</button><button data-auto>Autoplay</button><label>Players <select data-count><option>2</option><option selected>3</option><option>4</option><option>5</option></select></label><label><input type="checkbox" data-water> Rising Waters</label>';
|
|
document.body.prepend(control);
|
|
control.querySelector("[data-count]").value = state.players.length;
|
|
control.querySelector("[data-water]").checked = state.options.risingWaters;
|
|
control.querySelector("[data-hotseat]").checked = hotseat;
|
|
let undoRequests = 0;
|
|
const ui = mountGame(document.querySelector("#game"), {
|
|
onUndo: () => (undoRequests += 1),
|
|
onMove: async (m) => {
|
|
state = engine.move(state, m, hotseat ? state.actor : 0);
|
|
save();
|
|
show();
|
|
await ui.whenIdle();
|
|
scheduleBots();
|
|
},
|
|
onSetting: (name, value) => {
|
|
const p = hotseat ? state.actor : 0;
|
|
state = engine.setPlayerSettings(state, p, { [name]: value });
|
|
save();
|
|
show();
|
|
},
|
|
onPreference: async (name, value) => {
|
|
preferences[name] = value;
|
|
localStorage.setItem("vanuatu-preferences", JSON.stringify(preferences));
|
|
await localization.setLocale(preferences.language);
|
|
ui.setPreferences(preferences);
|
|
},
|
|
});
|
|
let preferences = {};
|
|
try {
|
|
preferences = JSON.parse(localStorage.getItem("vanuatu-preferences")) ?? {};
|
|
} catch {}
|
|
if (params.has("locale")) preferences.language = params.get("locale");
|
|
await localization.setLocale(preferences.language ?? "en");
|
|
ui.setPreferences(preferences);
|
|
function save() {
|
|
try {
|
|
localStorage.setItem(storageKey, JSON.stringify(state));
|
|
} catch {}
|
|
}
|
|
function show() {
|
|
localization.setState(state);
|
|
const p = hotseat ? state.actor : 0;
|
|
ui.setPlayer(p);
|
|
ui.setSettings(
|
|
Number.isInteger(p) ? engine.playerSettings(state, p) : null,
|
|
);
|
|
ui.render(engine.stripSecret(state, p));
|
|
}
|
|
function scheduleBots() {
|
|
clearTimeout(timer);
|
|
if (!hotseat && !state.finished && state.actor !== 0)
|
|
timer = setTimeout(async () => {
|
|
state = engine.moveAI(state, state.actor);
|
|
save();
|
|
show();
|
|
await ui.whenIdle();
|
|
scheduleBots();
|
|
}, 450);
|
|
}
|
|
function bot() {
|
|
if (state.finished) return;
|
|
state = engine.moveAI(state, state.actor);
|
|
save();
|
|
show();
|
|
scheduleBots();
|
|
}
|
|
control.querySelector("[data-new]").onclick = () => {
|
|
clearTimeout(timer);
|
|
state = engine.init(
|
|
Number(control.querySelector("[data-count]").value),
|
|
control.querySelector("[data-water]").checked ? ["rising-waters"] : [],
|
|
{ practice: true },
|
|
crypto.randomUUID(),
|
|
);
|
|
state.players.forEach(
|
|
(p, i) => (p.name = ["You", "Maya", "Noa", "Leo", "Nina"][i]),
|
|
);
|
|
save();
|
|
show();
|
|
scheduleBots();
|
|
};
|
|
control.querySelector("[data-hotseat]").onchange = (e) => {
|
|
hotseat = e.target.checked;
|
|
show();
|
|
scheduleBots();
|
|
};
|
|
control.querySelector("[data-step]").onclick = bot;
|
|
let autoplay = false;
|
|
control.querySelector("[data-auto]").onclick = async (e) => {
|
|
autoplay = !autoplay;
|
|
e.target.textContent = autoplay ? "Stop" : "Autoplay";
|
|
while (autoplay && !state.finished) {
|
|
clearTimeout(timer);
|
|
state = engine.moveAI(state, state.actor);
|
|
save();
|
|
show();
|
|
await ui.whenIdle();
|
|
await new Promise((r) => setTimeout(r, 160));
|
|
}
|
|
autoplay = false;
|
|
e.target.textContent = "Autoplay";
|
|
};
|
|
window.vanuatuDemo = {
|
|
get state() {
|
|
return state;
|
|
},
|
|
setState(s) {
|
|
state = s;
|
|
show();
|
|
},
|
|
engine,
|
|
ui,
|
|
get undoRequests() {
|
|
return undoRequests;
|
|
},
|
|
};
|
|
show();
|
|
scheduleBots();
|
|
}
|