add daily scores set tracking
This commit is contained in:
@ -18,6 +18,7 @@ import { cron } from "@elysiajs/cron";
|
||||
import { PlayerDocument, PlayerModel } from "./model/player";
|
||||
import { scoresaberService } from "@ssr/common/service/impl/scoresaber";
|
||||
import { delay } from "@ssr/common/utils/utils";
|
||||
import { connectScoreSaberWebSocket } from "@ssr/common/websocket/scoresaber-websocket";
|
||||
|
||||
// Load .env file
|
||||
dotenv.config({
|
||||
@ -28,8 +29,14 @@ dotenv.config({
|
||||
|
||||
await mongoose.connect(Config.mongoUri!); // Connect to MongoDB
|
||||
setLogLevel("DEBUG");
|
||||
export const app = new Elysia();
|
||||
|
||||
connectScoreSaberWebSocket({
|
||||
onScore: async score => {
|
||||
await PlayerService.trackScore(score);
|
||||
},
|
||||
});
|
||||
|
||||
export const app = new Elysia();
|
||||
app.use(
|
||||
cron({
|
||||
name: "player-statistics-tracker-cron",
|
||||
|
@ -62,13 +62,13 @@ export class Player {
|
||||
public getHistoryPreviousDays(days: number): Record<string, PlayerHistory> {
|
||||
const statisticHistory = this.getStatisticHistory();
|
||||
const history: Record<string, PlayerHistory> = {};
|
||||
|
||||
for (let i = 0; i < days; i++) {
|
||||
const date = formatDateMinimal(getMidnightAlignedDate(getDaysAgoDate(i)));
|
||||
const playerHistory = statisticHistory[date];
|
||||
if (playerHistory === undefined || Object.keys(playerHistory).length === 0) {
|
||||
continue;
|
||||
if (playerHistory !== undefined && Object.keys(playerHistory).length > 0) {
|
||||
history[date] = playerHistory;
|
||||
}
|
||||
history[date] = playerHistory;
|
||||
}
|
||||
return history;
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import { getDaysAgoDate, getMidnightAlignedDate } from "@ssr/common/utils/time-u
|
||||
import { scoresaberService } from "@ssr/common/service/impl/scoresaber";
|
||||
import ScoreSaberPlayerToken from "@ssr/common/types/token/scoresaber/score-saber-player-token";
|
||||
import { InternalServerError } from "../error/internal-server-error";
|
||||
import ScoreSaberPlayerScoreToken from "@ssr/common/types/token/scoresaber/score-saber-player-score-token";
|
||||
|
||||
export class PlayerService {
|
||||
/**
|
||||
@ -113,4 +114,44 @@ export class PlayerService {
|
||||
|
||||
console.log(`Tracked player "${foundPlayer.id}"!`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Track player score.
|
||||
*
|
||||
* @param score the score to track
|
||||
* @param leaderboard the leaderboard to track
|
||||
*/
|
||||
public static async trackScore({ score, leaderboard }: ScoreSaberPlayerScoreToken) {
|
||||
const playerId = score.leaderboardPlayerInfo.id;
|
||||
const player: PlayerDocument | null = await PlayerModel.findById(playerId);
|
||||
// Player is not tracked, so ignore the score.
|
||||
if (player == undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
const today = new Date();
|
||||
let history = player.getHistoryByDate(today);
|
||||
if (history == undefined || Object.keys(history).length === 0) {
|
||||
history = { scores: { rankedScores: 0, unrankedScores: 0 } }; // Ensure initialization
|
||||
}
|
||||
|
||||
const scores = history.scores || {};
|
||||
if (leaderboard.stars > 0) {
|
||||
scores.rankedScores!++;
|
||||
} else {
|
||||
scores.unrankedScores!++;
|
||||
}
|
||||
|
||||
history.scores = scores;
|
||||
player.setStatisticHistory(today, history);
|
||||
player.sortStatisticHistory();
|
||||
|
||||
// Save the changes
|
||||
player.markModified("statisticHistory");
|
||||
await player.save();
|
||||
|
||||
console.log(
|
||||
`Updated scores set statistic for "${player.id}", scores today: ${scores.rankedScores} ranked, ${scores.unrankedScores} unranked`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user