This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
Files
scoresaber-reloadedv3/src/common/schema/player-schema.ts

170 lines
4.1 KiB
TypeScript
Raw Normal View History

2024-09-28 05:57:35 +01:00
import mongoose, { Document, Schema } from "mongoose";
import { PlayerHistory } from "@/common/player/player-history";
2024-09-30 07:18:13 +01:00
import {
formatDateMinimal,
getDaysAgo,
getMidnightAlignedDate,
} from "@/common/time-utils";
2024-09-28 05:57:35 +01:00
import ScoreSaberPlayer from "@/common/model/player/impl/scoresaber-player";
2024-09-30 07:18:13 +01:00
import { sortPlayerHistory } from "@/common/player-utils";
2024-09-28 05:57:35 +01:00
// Interface for Player Document
export interface IPlayer extends Document {
/**
* The player's id
*/
id: string;
/**
* The player's statistic history
*/
statisticHistory: Map<string, PlayerHistory>;
/**
* The last time the player was tracked
*/
2024-09-28 16:18:49 +01:00
lastTracked: Date;
2024-09-28 05:57:35 +01:00
/**
* The raw player data.
*/
rawPlayer: ScoreSaberPlayer;
2024-09-28 14:43:21 +01:00
/**
* The first time the player was tracked
*/
trackedSince: Date;
2024-09-28 05:57:35 +01:00
/**
* Gets when this player was last tracked.
2024-09-30 07:18:13 +01:00
*
* @returns the date when the player was last tracked
2024-09-28 05:57:35 +01:00
*/
2024-09-28 16:18:49 +01:00
getLastTracked(): Date;
2024-09-28 05:57:35 +01:00
/**
* Gets the history for the given date
*
* @param date
2024-09-30 07:18:13 +01:00
* @returns the player history
*/
getHistoryByDate(date: Date): PlayerHistory;
/**
* Gets the history for the previous X days
*
* @param amount the amount of days
* @returns the player history
2024-09-28 05:57:35 +01:00
*/
2024-09-30 07:18:13 +01:00
getHistoryPrevious(amount: number): { [key: string]: PlayerHistory };
2024-09-28 05:57:35 +01:00
/**
* Gets all the statistic history
2024-09-30 07:18:13 +01:00
*
* @returns the statistic history
2024-09-28 05:57:35 +01:00
*/
getStatisticHistory(): Map<string, PlayerHistory>;
/**
* Sets the statistic history for the given date
*
* @param date the date to set it on
* @param data the data to set
*/
setStatisticHistory(date: Date, data: PlayerHistory): void;
/**
* Sorts the statistic history
2024-09-30 07:18:13 +01:00
*
* @returns the sorted statistic history
2024-09-28 05:57:35 +01:00
*/
sortStatisticHistory(): Map<string, PlayerHistory>;
}
// Mongoose Schema definition for Player
const PlayerSchema = new Schema<IPlayer>({
_id: { type: String, required: true },
2024-09-28 16:18:49 +01:00
lastTracked: { type: Date, default: new Date(), required: false },
2024-09-28 05:57:35 +01:00
rawPlayer: { type: Object, required: false },
statisticHistory: { type: Map, default: () => new Map(), required: false },
2024-09-28 14:43:21 +01:00
trackedSince: { type: Date, default: new Date(), required: false },
2024-09-28 05:57:35 +01:00
});
2024-09-28 16:18:49 +01:00
PlayerSchema.methods.getLastTracked = function (): Date {
2024-09-28 15:19:51 +01:00
return this.ked || new Date();
2024-09-28 05:57:35 +01:00
};
2024-09-30 07:18:13 +01:00
PlayerSchema.methods.getHistoryByDate = function (date: Date): PlayerHistory {
2024-09-28 05:57:35 +01:00
return (
2024-09-28 06:37:10 +01:00
this.statisticHistory.get(
formatDateMinimal(getMidnightAlignedDate(date)),
) || {}
2024-09-28 05:57:35 +01:00
);
};
2024-09-30 07:18:13 +01:00
PlayerSchema.methods.getHistoryPrevious = function (amount: number): {
[key: string]: PlayerHistory;
} {
const toReturn: { [key: string]: PlayerHistory } = {};
const history = sortPlayerHistory(this.getStatisticHistory());
for (let [date, stat] of history) {
const parsedDate = new Date(date);
if (getDaysAgo(parsedDate) + 1 <= amount) {
toReturn[date] = stat;
}
}
return toReturn;
};
2024-09-28 05:57:35 +01:00
PlayerSchema.methods.getStatisticHistory = function (): Map<
Date,
PlayerHistory
> {
if (!this.statisticHistory) {
this.statisticHistory = new Map();
}
return this.statisticHistory;
};
PlayerSchema.methods.setStatisticHistory = function (
date: Date,
data: PlayerHistory,
): void {
if (!this.statisticHistory) {
this.statisticHistory = new Map();
}
2024-09-28 06:31:25 +01:00
return this.statisticHistory.set(
2024-09-28 06:37:10 +01:00
formatDateMinimal(getMidnightAlignedDate(date)),
2024-09-28 06:31:25 +01:00
data,
);
2024-09-28 05:57:35 +01:00
};
PlayerSchema.methods.sortStatisticHistory = function (): Map<
Date,
PlayerHistory
> {
if (!this.statisticHistory) {
this.statisticHistory = new Map();
}
// Sort the player's history
this.statisticHistory = new Map(
Array.from(this.statisticHistory.entries() as [string, PlayerHistory][])
.sort(
(a: [string, PlayerHistory], b: [string, PlayerHistory]) =>
Date.parse(b[0]) - Date.parse(a[0]),
)
// Convert the date strings back to Date objects for the resulting Map
2024-09-28 06:43:27 +01:00
.map(([date, history]) => [formatDateMinimal(new Date(date)), history]),
2024-09-28 05:57:35 +01:00
);
return this.statisticHistory;
};
// Mongoose Model for Player
const PlayerModel =
mongoose.models.Player || mongoose.model<IPlayer>("Player", PlayerSchema);
export { PlayerModel };