api.violets-purgatory.dev/static/main.js

193 lines
5 KiB
JavaScript
Raw Normal View History

const discStatuses = {
"online": {
"text": "Online",
"color": "rgb(100, 255, 100)"
},
"dnd": {
"text": "DND",
"color": "rgb(255, 100, 100)"
},
"idle": {
"text": "Idle",
"color": "rgb(255, 255, 75)"
},
"offline": {
"text": "",
"color": "rgb(175, 175, 200)"
}
}
2024-06-07 22:02:19 -05:00
const spinSpeed = 30
var pfp
2024-06-03 01:01:48 -05:00
var music = new Audio("/snds/Lotus Waters.ogg")
2024-06-07 22:02:19 -05:00
music.preservesPitch = false
2024-06-03 01:01:48 -05:00
music.loop = true
2024-06-07 22:02:19 -05:00
music.playbackRate = 0
// var whipLash = new Audio("/snds/johnny-test-whip-crack.mp3")
// whipLash.volume = 0.25
2024-04-18 23:50:10 -05:00
2024-04-20 16:46:12 -05:00
var sock
2024-06-07 22:02:19 -05:00
var spins = 0
var lastSent = 0
2024-04-20 12:56:37 -05:00
var globalSpins = 0
2024-04-19 09:31:26 -05:00
2024-06-07 22:02:19 -05:00
var spinning = false
2024-04-22 07:56:44 -05:00
var firsttimeDebounce = true
2024-04-20 12:24:46 -05:00
var spinWaiting = false
2024-04-19 09:31:26 -05:00
function resetPFP() {
pfp.src = "https://api.violets-purgatory.dev/v1/pfp?" + new Date().getTime()
}
2024-06-07 22:02:19 -05:00
function lerp(a, b, t) {
return a * (1 - t) + b * t
}
2024-04-20 12:24:46 -05:00
function spinLoop() {
spinWaiting = true
2024-04-19 09:31:26 -05:00
setTimeout(() => {
2024-04-20 12:24:46 -05:00
spinWaiting = false
2024-06-07 22:02:19 -05:00
if (spinning) {
music.volume = 0.5
music.playbackRate = lerp(music.playbackRate, 1, 1/spinSpeed)
2024-04-20 12:56:37 -05:00
if (spins > 1) {
document.querySelector(".spinnyCount").style.display = "block"
2024-04-20 16:46:12 -05:00
document.querySelector(".localSpins").innerHTML = Math.ceil(spins - 1);
2024-04-20 12:56:37 -05:00
}
2024-06-07 22:02:19 -05:00
spins += 1/spinSpeed / 3
document.querySelector(".pfp").style.rotate = (spins * 360) + "deg"
if (Math.floor(spins) != lastSent && sock && sock.OPEN) {
document.querySelector(".globalSpins").innerHTML = globalSpins + 1
lastSent = Math.floor(spins)
2024-06-03 01:01:48 -05:00
// resetPFP()
2024-04-20 16:46:12 -05:00
sock.send(`{"op": 4}`)
console.log("Spin Sent!")
}
2024-06-07 22:02:19 -05:00
} else {
music.playbackRate = lerp(music.playbackRate, 0.5, 1/spinSpeed)
music.volume = lerp(music.volume, 0, 1/spinSpeed * 4)
2024-04-20 12:24:46 -05:00
}
2024-06-07 22:02:19 -05:00
spinLoop()
}, 1/spinSpeed * 1000);
2024-04-19 09:31:26 -05:00
}
2024-04-13 00:08:16 -05:00
window.onbeforeunload = function () {
window.scrollTo(0, 0);
2024-04-12 23:37:21 -05:00
}
2024-04-13 00:08:16 -05:00
window.onload = function () {
window.scrollTo(0, 0);
2024-04-18 23:50:10 -05:00
pfp = document.querySelector(".pfp")
2024-04-18 23:50:10 -05:00
2024-06-07 22:02:19 -05:00
spinLoop()
2024-04-18 23:50:10 -05:00
pfp.addEventListener("mousedown", () => {
2024-06-07 22:02:19 -05:00
// if (!spinWaiting) {
// spinLoop();
// }
2024-06-03 01:01:48 -05:00
music.play()
2024-04-18 23:50:10 -05:00
2024-06-07 22:02:19 -05:00
spinning = true
pfp.style.transition = ""
2024-04-18 23:50:10 -05:00
2024-04-20 12:56:37 -05:00
pfp.style.scale = "1.1"
2024-04-18 23:50:10 -05:00
})
document.body.onmouseup = () => {
2024-06-03 01:01:48 -05:00
if (music.currentTime != 0) {
2024-06-07 22:02:19 -05:00
// music.currentTime = 0
// music.pause()
// whipLash.currentTime = 0
// whipLash.play()
spinning = false
2024-04-18 23:50:10 -05:00
2024-06-07 22:02:19 -05:00
pfp.style.transition = "all 3s cubic-bezier(0.39, 0.575, 0.565, 1)"
2024-04-20 16:46:12 -05:00
2024-04-18 23:50:10 -05:00
pfp.style.scale = "1"
}
}
2024-04-20 15:39:25 -05:00
socketeer()
2024-04-13 00:08:16 -05:00
}
2024-04-20 13:03:54 -05:00
var lastPong = Date.now()
2024-04-20 17:00:58 -05:00
function ping(dur) {
sock.send(JSON.stringify({
op: 3
}))
setTimeout(() => {
ping(dur)
if (Date.now() - lastPong > 120000) {
sock.close()
console.log("Max duration since last pong exceeded- Closing socket.")
}
}, dur);
}
2024-04-13 00:08:16 -05:00
function socketeer() {
2024-04-20 16:46:12 -05:00
sock = new WebSocket('wss://api.violets-purgatory.dev')
2024-04-13 00:08:16 -05:00
2024-04-20 16:46:12 -05:00
sock.addEventListener("open", () => {
ping(30000)
})
sock.addEventListener("error", (error) => {
console.log(error)
})
sock.addEventListener("close", () => {
console.log("Connection Closed. Attempting Reconnect in 30 seconds.")
setTimeout(() => {
socketeer()
}, 30000);
})
sock.addEventListener("message", async (data) => {
2024-04-20 16:46:12 -05:00
data = data.data
data = JSON.parse(data)
if (data.op == 4) {
globalSpins = data.spins
2024-04-22 07:56:44 -05:00
if (firsttimeDebounce == true) {
firsttimeDebounce = false
document.querySelector(".globalSpins").innerHTML = globalSpins + 1;
} else {
document.querySelector(".globalSpins").innerHTML = globalSpins;
}
2024-04-20 16:46:12 -05:00
} else if (data.op == 0) {
var lanyard = data.d
var statusInfo = discStatuses[lanyard.discord_status]
var lastStatus = document.querySelector(".statusColor")
if (lastStatus.innerHTML != statusInfo.text) {
lastStatus.innerHTML = statusInfo.text
lastStatus.style.color = statusInfo.color
pfp.style.borderColor = lastStatus.style.color
resetPFP()
}
2024-06-08 17:04:53 -05:00
// if (lanyard.activities[0] && lanyard.activities[0].type == 4) {
// document.querySelector(".customStatus").innerHTML = `<hr><img src=""><p>${lanyard.activities[0].state}</p>`
// } else {
// document.querySelector(".customStatus").innerHTML = ""
// }
2024-04-20 16:46:12 -05:00
var discFetch = await (await fetch("/discHTML")).text()
2024-04-22 09:46:29 -05:00
document.querySelector("#activityHtml").innerHTML = discFetch
} else if (data.op == 3) {
2024-04-20 17:00:58 -05:00
lastPong = Date.now()
2024-04-20 16:46:12 -05:00
} else {
console.log(data)
}
2024-04-20 15:39:25 -05:00
})
}