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

125 lines
3.2 KiB
JavaScript
Raw Normal View History

2024-04-18 23:50:10 -05:00
var catsOnMars = new Audio("/snds/cats on mars.mp3")
var whipLash = new Audio("/snds/johnny-test-whip-crack.mp3")
catsOnMars.loop = true
2024-04-28 12:30:40 -05:00
catsOnMars.volume = 0.25
whipLash.volume = 0.25
2024-04-18 23:50:10 -05:00
2024-04-20 16:46:12 -05:00
var sock
2024-04-19 09:31:26 -05:00
var spins = 1
2024-04-20 12:56:37 -05:00
var globalSpins = 0
2024-04-19 09:31:26 -05:00
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
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
var pfp = document.querySelector(".pfp")
if (!catsOnMars.paused) {
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-04-20 12:24:46 -05:00
spins += 0.5
2024-04-20 16:46:12 -05:00
if (Math.round(spins) == spins && sock && sock.OPEN) {
2024-05-19 09:56:03 -05:00
document.querySelector(".pfp").src = "https://api.violets-purgatory.dev/v1/pfp?" + new Date().getTime()
2024-04-20 16:46:12 -05:00
sock.send(`{"op": 4}`)
console.log("Spin Sent!")
}
2024-04-20 12:24:46 -05:00
spinLoop()
}
2024-04-19 09:31:26 -05:00
}, 1000);
}
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
var pfp = document.querySelector(".pfp")
pfp.addEventListener("mousedown", () => {
2024-04-20 12:24:46 -05:00
if (!spinWaiting) {
spinLoop();
}
2024-04-18 23:50:10 -05:00
catsOnMars.play()
pfp.style.animationName = "spinny"
2024-04-20 12:56:37 -05:00
pfp.style.scale = "1.1"
2024-04-18 23:50:10 -05:00
})
document.body.onmouseup = () => {
if (catsOnMars.currentTime != 0) {
catsOnMars.currentTime = 0
catsOnMars.pause()
whipLash.currentTime = 0
whipLash.play()
2024-04-20 16:46:12 -05:00
2024-04-18 23:50:10 -05:00
pfp.style.animationName = "unset"
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) => {
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 discFetch = await (await fetch("/discHTML")).text()
2024-04-22 09:46:29 -05:00
document.querySelector("#activityHtml").innerHTML = discFetch
2024-04-20 17:00:58 -05:00
}else if (data.op == 3) {
lastPong = Date.now()
2024-04-20 16:46:12 -05:00
} else {
console.log(data)
}
2024-04-20 15:39:25 -05:00
})
}