-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Kotobade Asobou): add presence (#8306)
- Loading branch information
Showing
3 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |