Skip to content

Commit

Permalink
feat(Rhythm+): add activity (#9275)
Browse files Browse the repository at this point in the history
Co-authored-by: Florian Metz <[email protected]>
  • Loading branch information
joerkig and Timeraa authored Feb 20, 2025
1 parent f3468dc commit 604af65
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 0 deletions.
23 changes: 23 additions & 0 deletions websites/R/Rhythm+/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$schema": "https://schemas.premid.app/metadata/1.13",
"apiVersion": 1,
"author": {
"id": "205984221859151873",
"name": "joerkig"
},
"service": "Rhythm+",
"description": {
"en": "Play, create and share your favorite songs right in your browser anywhere, anytime. A community-based online music game."
},
"url": "rhythm-plus.com",
"version": "1.0.0",
"logo": "https://i.imgur.com/9UpfGFw.png",
"thumbnail": "https://i.imgur.com/f6LdzsO.png",
"color": "#07b39b",
"category": "games",
"tags": [
"rhythm",
"game",
"rhythmplus"
]
}
136 changes: 136 additions & 0 deletions websites/R/Rhythm+/presence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { Assets } from 'premid'

const script = document.createElement('script')
script.textContent = `
setInterval(() => {
window.postMessage({ type: "pmd-receive-info", songInfo: document.querySelector(".game")?.__vue__?.currentSong, sheetInfo: document.querySelector(".game")?.__vue__?.currentSheet }, "*");
}, 100);
`
document.head.appendChild(script)

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

let songInfo: SongInfo, sheetInfo: SheetInfo

window.addEventListener('message', (e) => {
if (e.data.type === 'pmd-receive-info')
({ songInfo, sheetInfo } = e.data)
})

enum ActivityAssets {
Logo = 'https://i.imgur.com/9UpfGFw.png',
}

interface SheetInfo {
difficulty: number
length: number
keys: number
noteCount: number
}

interface SongInfo {
title: string
subtitle: string
image: string
}

presence.on('UpdateData', async () => {
const presenceData: PresenceData = {
largeImageKey: ActivityAssets.Logo,
startTimestamp: browsingTimestamp,
}

if (document.querySelector('.profile_card')) {
presenceData.smallImageKey
= document.querySelector<HTMLImageElement>('.profile_card img')?.src
?? Assets.Question
presenceData.smallImageText
= document
.querySelector('.profile_card .detail')
?.firstChild
?.textContent
?.trim() ?? 'Unknown'
}
switch (document.location.pathname.split('/')[1]) {
case '': {
presenceData.details = 'Viewing homepage'
break
}
case 'menu': {
presenceData.details = 'Browsing songs'
presenceData.state = document.querySelector('.cat_tab.active')
if (!presenceData.state)
return
break
}
case 'tutorial': {
presenceData.details = 'Playing the tutorial'
break
}
case 'game': {
if (!songInfo || !sheetInfo)
return
presenceData.details = `${songInfo?.title} ${
songInfo?.subtitle ? ` (${songInfo?.subtitle})` : ''
}`
presenceData.state = `Diff ${sheetInfo?.difficulty} | ${
Math.floor(sheetInfo?.length / 60).toString().length === 1 ? '0' : ''
}${Math.floor(sheetInfo?.length / 60)}:${
Math.floor(sheetInfo?.length % 60).toString().length === 1 ? '0' : ''
}${Math.floor(sheetInfo?.length % 60)} | ${sheetInfo?.keys} Keys | ${
sheetInfo?.noteCount
} Notes`
presenceData.largeImageKey = songInfo?.image ?? ActivityAssets.Logo
presenceData.buttons = [
{
label: 'Play Song',
url: document.location.href,
},
]
if (presenceData.details === 'Game')
return
break
}
case 'result': {
presenceData.details = 'Viewing results'
presenceData.state = `${
document.querySelector('.score')?.textContent
} | ${document.querySelector('.title')?.textContent}`
presenceData.buttons = [
{
label: 'View Results',
url: document.location.href,
},
]
if (!document.querySelector('.title'))
return
break
}
case 'game-over': {
presenceData.details = 'Game Over'
break
}
case 'studio': {
presenceData.details = 'Viewing their studio'
break
}
case 'editor': {
presenceData.details = 'Editing a song'
break
}
case 'account': {
presenceData.details = 'Viewing settings'
break
}
default: {
presenceData.details = document.location.pathname
break
}
}

if (presenceData.details)
presence.setActivity(presenceData)
})

0 comments on commit 604af65

Please sign in to comment.