2023-10-27 15:54:11 +01:00
|
|
|
import { Point } from "@influxdata/influxdb-client";
|
2023-10-27 16:30:30 +01:00
|
|
|
import axios from "axios";
|
2023-10-27 15:54:11 +01:00
|
|
|
import { w3cwebsocket as WebsocketClient } from "websocket";
|
|
|
|
import { InfluxWriteAPI } from "..";
|
|
|
|
import { connectMongo } from "../db/mongo";
|
|
|
|
import { LeaderboardSchema } from "../db/schemas/leaderboard";
|
2023-10-30 15:45:35 +00:00
|
|
|
import { Headsets } from "../headsets";
|
|
|
|
import { normalizedRegionName } from "../utils/regionUtils";
|
2023-10-27 15:54:11 +01:00
|
|
|
|
|
|
|
async function update() {
|
2023-10-27 16:30:30 +01:00
|
|
|
const response = await axios.get("https://scoresaber.com/api/players/count");
|
|
|
|
const count = response.data;
|
2023-10-27 15:54:11 +01:00
|
|
|
|
|
|
|
const point = new Point("scoresaber")
|
|
|
|
.tag("type", "player_count")
|
|
|
|
.intField("value", parseInt(count))
|
|
|
|
.timestamp(new Date());
|
|
|
|
InfluxWriteAPI.writePoint(point);
|
|
|
|
|
|
|
|
console.log(`Updated player count to ${count}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function connectWebsocket() {
|
|
|
|
await connectMongo();
|
|
|
|
const leaderboard = await LeaderboardSchema.findOne({ _id: "scoresaber" });
|
|
|
|
if (!leaderboard) {
|
|
|
|
await LeaderboardSchema.create({ _id: "scoresaber", totalPlays: 0 });
|
|
|
|
}
|
|
|
|
let totalScores = leaderboard?.totalPlays || 0;
|
2023-10-30 15:27:10 +00:00
|
|
|
let totalCountries = leaderboard?.countries || {};
|
|
|
|
let totalHeadsets = leaderboard?.headsets || {};
|
2023-10-27 15:54:11 +01:00
|
|
|
|
|
|
|
const socket = new WebsocketClient("wss://scoresaber.com/ws");
|
|
|
|
socket.onopen = () => {
|
|
|
|
console.log("Connected to websocket");
|
|
|
|
};
|
|
|
|
|
|
|
|
socket.onclose = () => {
|
|
|
|
console.log("Disconnected from websocket");
|
|
|
|
setTimeout(connectWebsocket, 5000);
|
|
|
|
};
|
|
|
|
|
|
|
|
socket.onerror = (error) => {
|
|
|
|
console.error("Websocket error:", error);
|
|
|
|
};
|
|
|
|
|
2023-10-30 15:01:08 +00:00
|
|
|
socket.onmessage = async (message) => {
|
2023-10-27 15:54:11 +01:00
|
|
|
const data = message.data;
|
|
|
|
let json;
|
|
|
|
try {
|
|
|
|
json = JSON.parse(data.toString());
|
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const commandName = json.commandName;
|
|
|
|
const commandData = json.commandData;
|
|
|
|
if (commandName == "score") {
|
2023-10-30 15:01:08 +00:00
|
|
|
const { score, leaderboard } = commandData;
|
2023-10-27 15:54:11 +01:00
|
|
|
totalScores++;
|
2023-10-30 15:01:08 +00:00
|
|
|
|
|
|
|
const {
|
|
|
|
id,
|
|
|
|
hmd,
|
|
|
|
baseScore,
|
|
|
|
missedNotes,
|
|
|
|
badCuts,
|
2023-10-30 20:14:50 +00:00
|
|
|
rank,
|
|
|
|
maxCombo,
|
2023-10-30 15:01:08 +00:00
|
|
|
leaderboardPlayerInfo: player,
|
|
|
|
pp,
|
|
|
|
} = score;
|
|
|
|
const { maxScore, stars, id: leaderboardId } = leaderboard;
|
2023-10-30 16:08:47 +00:00
|
|
|
|
2023-10-30 21:06:27 +00:00
|
|
|
const hmdName = Headsets[hmd] || "Unknown";
|
2023-10-30 16:08:47 +00:00
|
|
|
const countryId = normalizedRegionName(player.country) || "Unknown";
|
2023-10-30 15:27:10 +00:00
|
|
|
totalCountries[countryId] = (totalCountries[countryId] || 0) + 1;
|
2023-10-30 16:08:47 +00:00
|
|
|
totalHeadsets[hmdName] = (totalHeadsets[hmdName] || 0) + 1;
|
|
|
|
|
2023-10-30 15:49:06 +00:00
|
|
|
// Update the leaderboard data in the database
|
2023-10-30 15:27:10 +00:00
|
|
|
await LeaderboardSchema.updateOne(
|
|
|
|
{ _id: "scoresaber" },
|
|
|
|
{
|
|
|
|
$set: {
|
|
|
|
totalPlays: totalScores,
|
|
|
|
countries: totalCountries,
|
|
|
|
headsets: totalHeadsets,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
).exec();
|
2023-10-30 15:01:08 +00:00
|
|
|
|
2023-10-30 20:14:50 +00:00
|
|
|
const scorePoint = new Point("scoresaber")
|
|
|
|
.tag("type", "score")
|
2023-10-27 15:54:11 +01:00
|
|
|
|
2023-10-30 20:14:50 +00:00
|
|
|
.intField("id", id)
|
|
|
|
.timestamp(new Date());
|
|
|
|
|
|
|
|
if (baseScore && maxScore) {
|
2023-10-31 13:54:33 +00:00
|
|
|
scorePoint.floatField("acc", ((baseScore / maxScore) * 100).toFixed(2));
|
2023-10-30 15:01:08 +00:00
|
|
|
}
|
2023-10-30 20:14:50 +00:00
|
|
|
if (stars) {
|
|
|
|
scorePoint.floatField("stars", stars);
|
2023-10-30 15:01:08 +00:00
|
|
|
}
|
2023-10-30 20:14:50 +00:00
|
|
|
if (pp) {
|
2023-10-31 13:54:33 +00:00
|
|
|
scorePoint.floatField("pp", pp.toFixed(2));
|
2023-10-30 15:01:08 +00:00
|
|
|
}
|
2023-10-30 20:14:50 +00:00
|
|
|
if (missedNotes) {
|
|
|
|
scorePoint.intField("missed_notes", missedNotes);
|
2023-10-30 15:01:08 +00:00
|
|
|
}
|
2023-10-30 20:14:50 +00:00
|
|
|
if (badCuts) {
|
|
|
|
scorePoint.intField("bad_cuts", badCuts);
|
2023-10-30 15:01:08 +00:00
|
|
|
}
|
2023-10-30 20:14:50 +00:00
|
|
|
if (rank) {
|
|
|
|
scorePoint.intField("rank", rank);
|
2023-10-30 15:01:08 +00:00
|
|
|
}
|
2023-10-31 13:54:33 +00:00
|
|
|
// if (leaderboardId) {
|
|
|
|
// scorePoint.intField("leaderboard_id", leaderboardId);
|
|
|
|
// }
|
2023-10-30 20:14:50 +00:00
|
|
|
if (hmdName) {
|
|
|
|
scorePoint.stringField("hmd", hmdName);
|
|
|
|
}
|
|
|
|
if (countryId) {
|
|
|
|
scorePoint.stringField("country", countryId);
|
|
|
|
}
|
|
|
|
if (maxCombo) {
|
|
|
|
scorePoint.intField("max_combo", maxCombo);
|
|
|
|
}
|
2023-10-31 13:54:33 +00:00
|
|
|
//scorePoint.intField("player_id", player.id);
|
2023-10-30 20:14:50 +00:00
|
|
|
|
|
|
|
// Write the data to influx
|
|
|
|
InfluxWriteAPI.writePoint(scorePoint);
|
|
|
|
InfluxWriteAPI.writePoint(
|
|
|
|
new Point("scoresaber")
|
|
|
|
.tag("type", "score_count")
|
|
|
|
.intField("value", totalScores)
|
|
|
|
.timestamp(new Date())
|
2023-10-30 15:05:28 +00:00
|
|
|
);
|
2023-10-30 15:03:29 +00:00
|
|
|
}
|
2023-10-30 20:14:50 +00:00
|
|
|
};
|
2023-10-30 15:01:08 +00:00
|
|
|
}
|
|
|
|
|
2023-10-27 15:54:11 +01:00
|
|
|
update();
|
|
|
|
connectWebsocket();
|
|
|
|
setInterval(update, 60_000); // 1 minute
|