diff --git a/wikiweaver-web/index.html b/wikiweaver-web/index.html index 7b20c62..3668f8f 100644 --- a/wikiweaver-web/index.html +++ b/wikiweaver-web/index.html @@ -45,7 +45,12 @@
Start a race from
to
- +
+ + +
+
@@ -87,4 +92,4 @@
- \ No newline at end of file + diff --git a/wikiweaver-web/js/index.js b/wikiweaver-web/js/index.js index 6e87803..5d033c1 100644 --- a/wikiweaver-web/js/index.js +++ b/wikiweaver-web/js/index.js @@ -21,6 +21,11 @@ async function init() { await JoinLobby(code); } +async function ToggleGoalSummary() { + const elem = document.getElementById("goal-page-summary"); + elem.hidden = !elem.hidden; +} + async function HandleStartGameClicked() { if (!isHost) return; diff --git a/wikiweaver-web/js/networking.js b/wikiweaver-web/js/networking.js index 97dc2ff..63456c7 100644 --- a/wikiweaver-web/js/networking.js +++ b/wikiweaver-web/js/networking.js @@ -62,7 +62,7 @@ function HandleMessageLobby(msg) { SetCode(msg.Code); } -function HandleMessageStart(msg) { +async function HandleMessageStart(msg) { if (!msg.Success) return; const elements = { @@ -76,6 +76,7 @@ function HandleMessageStart(msg) { }; EnableElements(elements); + document.getElementById("goal-page-summary").innerText = await GetArticleSummary(msg.GoalPage); document.getElementById("start-page-input").value = msg.StartPage; document.getElementById("goal-page-input").value = msg.GoalPage; StartGame(msg.StartPage, msg.GoalPage); @@ -83,6 +84,12 @@ function HandleMessageStart(msg) { StartCountdownTimer(msg.StartTime, msg.Countdown); } +async function GetArticleSummary(page) { + // TODO: we could limit the number of sentences as a room option for difficulty setting + let data = await fetch("https://en.wikipedia.org/w/api.php?origin=*&action=query&prop=extracts&exsectionformat=plain&format=json&explaintext&exintro&exsentences=2&titles=" + encodeURIComponent(page)).then((response) => (response.json())); + return Object.values(data.query.pages)[0].extract +} + function HandleMessagePage(msg) { AddNewPage(msg.Username, msg.Previous, msg.Page, msg.Backmove); UpdateLeaderboardEntry(msg.Username, msg.Clicks, msg.Pages, msg.FinishTime); @@ -169,7 +176,7 @@ async function JoinLobby(code) { ShowElements(elements); }); - globalThis.socket.addEventListener("message", (event) => { + globalThis.socket.addEventListener("message", async (event) => { const msg = JSON.parse(event.data); console.log("recv message:", msg); @@ -200,7 +207,7 @@ async function JoinLobby(code) { break; case "start": - HandleMessageStart(msg); + await HandleMessageStart(msg); break; default: @@ -222,14 +229,14 @@ async function GetRandomWikipediaArticles(n) { }; url = url + "?origin=*"; - Object.keys(params).forEach(function (key) { + Object.keys(params).forEach(function(key) { url += "&" + key + "=" + params[key]; }); articles = await fetch(url) .then((response) => response.json()) .then((json) => json.query.random) - .catch(function (error) { + .catch(function(error) { console.log(error); return [{ title: "Gingerbread", title: "League of Legends" }]; }); @@ -253,14 +260,14 @@ async function SearchForWikipediaTitle(title) { }; url = url + "?origin=*"; - Object.keys(params).forEach(function (key) { + Object.keys(params).forEach(function(key) { url += "&" + key + "=" + params[key]; }); response = await fetch(url) .then((response) => response.json()) .then((json) => json) - .catch(function (error) { + .catch(function(error) { return { error: error }; }); diff --git a/wikiweaver-web/style.css b/wikiweaver-web/style.css index 1c010e1..1464c56 100644 --- a/wikiweaver-web/style.css +++ b/wikiweaver-web/style.css @@ -97,6 +97,16 @@ img { overflow-y: auto; } +#goal-page-container { + display: flex; + flex-direction: row; + gap: 0.5em; +} + +#goal-page-input { + flex-grow: 1; +} + #maincanvas { background: var(--maincanvas-background); min-width: 0; @@ -230,4 +240,4 @@ img { .button:hover { filter: brightness(85%); -} \ No newline at end of file +}