api.violets-purgatory.dev/index.js

153 lines
3.9 KiB
JavaScript
Raw Normal View History

2024-01-20 22:14:18 -06:00
const express = require('express'),
2024-02-07 21:49:28 -06:00
path = require("path"),
fs = require("fs"),
WebSocket = require("ws")
2024-01-19 18:39:24 -06:00
const PORT = process.env.PORT || 8080
var app = express()
2024-02-07 21:49:28 -06:00
var lanyardData = "Nothing Fetched yet!"
var sockets = []
2024-03-07 10:54:28 -06:00
var pfpSeason = "default"
2024-04-20 15:54:05 -05:00
var violetSpins = 0
2024-01-20 22:14:18 -06:00
function randomFileInDir(directory) {
var dir = fs.readdirSync(directory)
return path.join(directory, dir[Math.floor(Math.random() * dir.length)])
}
2024-01-19 18:44:51 -06:00
app.get("/", (req, res) => {
2024-02-07 21:49:28 -06:00
var docs = fs.readFileSync(path.join(__dirname, "basicDocs.json"))
docs = JSON.parse(docs)
res.send(docs)
2024-01-19 18:44:51 -06:00
})
2024-01-19 18:39:24 -06:00
2024-01-20 22:14:18 -06:00
app.get("/v1/pfp", (req, res) => {
2024-03-07 10:54:28 -06:00
var time = new Date(Date.now())
if ([11, 12, 1].includes(time.getMonth() + 1)) {
pfpSeason = "christmas"
}
var pfpConfig = JSON.parse(fs.readFileSync(path.join(__dirname, "PFPs.json")))[pfpSeason]
2024-05-19 08:49:56 -05:00
var characterNumb = time.getDate() % pfpConfig.length
2024-03-07 10:54:28 -06:00
var char = pfpConfig[characterNumb]
var pfps = char.pfps
try {
2024-06-11 23:00:16 -05:00
var pfpPool = pfps
2024-03-07 10:54:28 -06:00
var rand = Math.floor(Math.random() * pfpPool.length)
var pfp = path.join(__dirname, char.pfpRoot, pfpPool[rand])
res.sendFile(pfp)
} catch (error) {
var pfp = path.join(__dirname, char.fallback)
res.sendFile(pfp)
}
2024-01-20 22:14:18 -06:00
})
2024-02-07 21:49:28 -06:00
app.get("/v1/lanyard", (req, res) => {
res.send(lanyardData)
})
2024-04-20 16:03:51 -05:00
app.get("/v1/spins", (req, res) => {
2024-04-21 13:12:50 -05:00
res.send(violetSpins.toString())
2024-04-20 16:03:51 -05:00
})
2024-02-07 21:58:04 -06:00
// app.listen(PORT, () => {
// console.log("API is now listening on port 8080!")
// })
2024-02-07 21:49:28 -06:00
2024-02-19 10:57:50 -06:00
function socketeer() {
var lanyard = new WebSocket('wss://api.lanyard.rest/socket')
2024-02-07 21:49:28 -06:00
2024-02-19 10:57:50 -06:00
function beat(dur) {
2024-02-07 21:49:28 -06:00
lanyard.send(JSON.stringify({
2024-02-19 10:57:50 -06:00
op: 3
2024-02-07 21:49:28 -06:00
}))
2024-02-19 10:57:50 -06:00
setTimeout(() => {
beat(dur)
}, dur);
}
2024-02-07 21:49:28 -06:00
2024-02-19 10:57:50 -06:00
lanyard.addEventListener("message", async (res) => {
var data = JSON.parse(res.data)
if (data.op == 1) {
beat(data.d.heartbeat_interval)
lanyard.send(JSON.stringify({
op: 2,
d: {
subscribe_to_id: "534132311781015564"
}
}))
} else if (data.op == 0) {
lanyardData = data
2024-02-07 21:49:28 -06:00
2024-02-19 10:57:50 -06:00
for (let index = 0; index < sockets.length; index++) {
var socketData = sockets[index]
var socket = socketData.socket;
socket.send(JSON.stringify(lanyardData))
if ((Date.now() - socketData.lastPing) > 120000) {
sockets.splice(index, 1)
}
2024-02-07 21:49:28 -06:00
}
}
2024-02-19 10:57:50 -06:00
})
2024-02-07 21:49:28 -06:00
2024-02-19 10:57:50 -06:00
lanyard.on('close', () => {
console.log("Disconnected")
setTimeout(() => {
socketeer()
}, 60000)
})
}
2024-02-18 06:34:43 -06:00
2024-02-19 11:01:40 -06:00
socketeer()
2024-02-07 21:58:04 -06:00
// wsServer = new WebSocket.WebSocketServer({ port: 8090 })
2024-02-19 10:57:50 -06:00
2024-02-07 21:58:04 -06:00
wsServer = WebSocket.Server;
let server = require('http').createServer()
wsServer = new wsServer({
server: server,
perMessageDeflate: false
})
server.on('request', app)
2024-02-07 21:49:28 -06:00
wsServer.on("connection", function connection(socket) {
socket.on('message', function message(data) {
data = JSON.parse(data)
if (data.op == 3) {
for (let index = 0; index < sockets.length; index++) {
const socketData = sockets[index];
if (socketData.socket == socket) {
sockets[index].lastPing = Date.now()
}
}
2024-02-19 10:57:50 -06:00
2024-02-07 21:49:28 -06:00
socket.send(`{"op": 3}`)
2024-04-20 16:03:51 -05:00
} else if (data.op == 4) {
violetSpins += 1
2024-04-20 16:10:27 -05:00
for (var i = 0; i < sockets.length; i++) {
2024-04-20 16:14:23 -05:00
var soc = sockets[i].socket
soc.send(`{"op": 4, "spins": ${violetSpins}}`)
2024-04-20 16:10:27 -05:00
}
2024-04-20 15:54:05 -05:00
} else {
console.log(data)
2024-02-19 10:57:50 -06:00
}
2024-02-07 21:49:28 -06:00
})
2024-02-19 10:57:50 -06:00
2024-02-07 21:49:28 -06:00
socket.send(JSON.stringify(lanyardData))
socket.send(`{ "op": 1 }`)
2024-04-21 13:11:57 -05:00
socket.send(`{"op": 4, "spins": ${violetSpins}}`)
2024-02-07 21:49:28 -06:00
2024-02-19 10:57:50 -06:00
sockets.push({ socket, lastPing: Date.now() })
2024-02-07 21:58:04 -06:00
})
2024-02-19 10:57:50 -06:00
server.listen(PORT, function () {
2024-02-07 21:58:04 -06:00
console.log("API & Websocket now on port: " + PORT)
2024-01-19 18:39:24 -06:00
})