update how countries and hmds are stored
All checks were successful
deploy / deploy (push) Successful in 39s

This commit is contained in:
Lee 2023-10-30 15:27:10 +00:00
parent 4be7a46a90
commit b348969089
3 changed files with 44 additions and 12 deletions

@ -4,6 +4,14 @@ const { Schema } = mongoose;
const leaderboardSchema = new Schema({ const leaderboardSchema = new Schema({
_id: String, _id: String,
totalPlays: Number, totalPlays: Number,
countries: {
type: Map,
of: Number,
},
headsets: {
type: Map,
of: Number,
},
}); });
export const LeaderboardSchema = export const LeaderboardSchema =

@ -5,7 +5,6 @@ const scoreSchema = new Schema({
_id: String, _id: String,
player: { player: {
id: String, id: String,
hmd: String,
}, },
acc: Number, acc: Number,
pp: Number, pp: Number,

@ -26,6 +26,8 @@ async function connectWebsocket() {
await LeaderboardSchema.create({ _id: "scoresaber", totalPlays: 0 }); await LeaderboardSchema.create({ _id: "scoresaber", totalPlays: 0 });
} }
let totalScores = leaderboard?.totalPlays || 0; let totalScores = leaderboard?.totalPlays || 0;
let totalCountries = leaderboard?.countries || {};
let totalHeadsets = leaderboard?.headsets || {};
const socket = new WebsocketClient("wss://scoresaber.com/ws"); const socket = new WebsocketClient("wss://scoresaber.com/ws");
socket.onopen = () => { socket.onopen = () => {
@ -55,10 +57,6 @@ async function connectWebsocket() {
if (commandName == "score") { if (commandName == "score") {
const { score, leaderboard } = commandData; const { score, leaderboard } = commandData;
totalScores++; totalScores++;
LeaderboardSchema.updateOne(
{ _id: "scoresaber" },
{ totalPlays: totalScores }
).exec();
const { const {
id, id,
@ -71,6 +69,7 @@ async function connectWebsocket() {
pp, pp,
} = score; } = score;
const { maxScore, stars, id: leaderboardId } = leaderboard; const { maxScore, stars, id: leaderboardId } = leaderboard;
await Score.findByIdAndDelete(id).exec(); // Remove any existing score with the same id
const data: any = { const data: any = {
_id: id, _id: id,
@ -94,15 +93,41 @@ async function connectWebsocket() {
data.stars = stars; data.stars = stars;
} }
await Score.findByIdAndDelete(id).exec();
Score.create(data); Score.create(data);
const countryId = country?.toLowerCase() || "unknown";
totalCountries[countryId] = (totalCountries[countryId] || 0) + 1;
totalHeadsets[hmd] = (totalHeadsets[hmd] || 0) + 1;
await LeaderboardSchema.updateOne(
{ _id: "scoresaber" },
{
$set: {
totalPlays: totalScores,
countries: totalCountries,
headsets: totalHeadsets,
},
}
).exec();
const point = new Point("scoresaber") InfluxWriteAPI.writePoint(
.tag("type", "score_count") new Point("scoresaber")
.intField("value", totalScores) .tag("type", "score_count")
.stringField("country", country) .intField("value", totalScores)
.timestamp(new Date()); .timestamp(new Date())
InfluxWriteAPI.writePoint(point); );
InfluxWriteAPI.writePoint(
new Point("scoresaber")
.tag("type", "headsets")
.tag("hmd", hmd)
.intField("value", totalHeadsets[hmd])
.timestamp(new Date())
);
InfluxWriteAPI.writePoint(
new Point("scoresaber")
.tag("type", "countries")
.tag("country", countryId)
.intField("value", totalCountries[countryId])
.timestamp(new Date())
);
} }
}; };
} }