Skip to content

Commit

Permalink
feat(Kotobade Asobou): add presence (PreMiD#8306)
Browse files Browse the repository at this point in the history
  • Loading branch information
theusaf authored Apr 28, 2024
1 parent e0d9100 commit d39c4c0
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
45 changes: 45 additions & 0 deletions websites/K/Kotobade Asobou/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"$schema": "https://schemas.premid.app/metadata/1.10",
"author": {
"id": "193714715631812608",
"name": "nihongojouzu"
},
"service": "Kotobade Asobou",
"altnames": [
"言葉で遊ぼう"
],
"description": {
"en": "Japanese Wordle - daily word puzzle game.",
"ja_JP": "日本語版 Wordle - 日替わり単語パズルゲーム。"
},
"url": "taximanli.github.io",
"matches": [
"*://taximanli.github.io/*"
],
"version": "1.0.0",
"logo": "https://taximanli.github.io/kotobade-asobou/kotobade-asobou_logo_512x512.png",
"thumbnail": "https://taximanli.github.io/kotobade-asobou/kotobade-asobou_card_600x314.png",
"color": "#22c55e",
"category": "games",
"tags": [
"wordle",
"kotoba",
"japanese",
"game",
"puzzle",
"language",
"word"
],
"settings": [
{
"id": "displayLanguage",
"icon": "fas fa-language",
"value": 0,
"values": [
"English",
"にほんご"
],
"title": "Display Language"
}
]
}
73 changes: 73 additions & 0 deletions websites/K/Kotobade Asobou/presence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import languageStrings from "./strings";

const presence = new Presence({
clientId: "1233147201559990292",
}),
browsingTimestamp = Math.floor(Date.now() / 1000);

const enum PresenceAssets {
Logo = "https://taximanli.github.io/kotobade-asobou/kotobade-asobou_logo_512x512.png",
}

presence.on("UpdateData", async () => {
const presenceData: PresenceData = {
largeImageKey: PresenceAssets.Logo,
startTimestamp: browsingTimestamp,
},
language = await presence.getSetting<number>("displayLanguage"),
strings = languageStrings[["en", "ja"][language] as "en" | "ja"],
rows = [...document.querySelectorAll<HTMLDivElement>(".grid > div")],
nextIndex = rows.findIndex(row =>
[...row.children].every(
tile =>
!tile.querySelector<HTMLDivElement>(".letter-container").textContent
)
),
lastRow: HTMLDivElement = rows[nextIndex - 1],
isComplete = !!(
(lastRow?.children.length &&
[...lastRow.children].every(tile =>
tile.classList.contains("correct")
)) ||
nextIndex === -1
);

if (isComplete) {
const finalScore = nextIndex === -1 ? 12 : nextIndex;
let outcome = "";
for (let i = 0; i < finalScore; i++) {
outcome += getRowEmojis(rows[i]);
outcome += " | ";
}
presenceData.details = nextIndex === -1 ? strings.failure : strings.victory;
presenceData.state = `${finalScore}/12 - ${getRowEmojis(
rows[finalScore - 1]
)}`;
presenceData.smallImageKey = Assets.Question;
presenceData.smallImageText = outcome;
presenceData.buttons = [
{ url: document.location.href, label: strings.play },
];
} else {
presenceData.details = strings.guessing;
presenceData.state = `${nextIndex}/12 - ${getRowEmojis(lastRow)}`;
}

presence.setActivity(presenceData);
});

function getRowEmojis(row: HTMLDivElement): string {
const darkMode = document.documentElement.classList.contains("dark");
if (!row) return darkMode ? "⬛".repeat(4) : "⬜".repeat(4);
let emojis = "";
for (let i = 0; i < row.children.length; i++) {
const tile = row.children[i];
if (tile.classList.contains("correct")) emojis += "🟩";
else if (tile.classList.contains("present")) emojis += "🟨";
else if (tile.classList.contains("vowel")) emojis += "↔️";
else if (tile.classList.contains("consonant")) emojis += "↕️";
else if (tile.classList.contains("close")) emojis += "🟢";
else emojis += darkMode ? "⬛" : "⬜";
}
return emojis;
}
14 changes: 14 additions & 0 deletions websites/K/Kotobade Asobou/strings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const en = {
victory: "Victory",
failure: "Failure",
guessing: "Guessing",
play: "Play Game",
},
ja: Record<keyof typeof en, string> = {
victory: "しょうり",
failure: "しっぱい",
guessing: "すいそく",
play: "あそぼう",
};

export default { en, ja };

0 comments on commit d39c4c0

Please sign in to comment.