mirror of
https://github.com/sp-tarkov/server.git
synced 2025-02-13 09:50:43 -05:00
Co-authored-by: Refringe <me@refringe.com> Co-authored-by: Dev <dev@dev.sp-tarkov.com> Co-authored-by: Terkoiz <terkoiz@spt.dev> Co-authored-by: Refringe <refringe@noreply.dev.sp-tarkov.com> Co-authored-by: DrakiaXYZ <565558+TheDgtl@users.noreply.github.com> Reviewed-on: SPT-AKI/Server#289
65 lines
2.1 KiB
TypeScript
65 lines
2.1 KiB
TypeScript
import { injectable } from "tsyringe";
|
|
|
|
@injectable()
|
|
export class ProfileActivityService
|
|
{
|
|
protected profileActivityTimestamps: Record<string, number> = {};
|
|
|
|
/**
|
|
* Was the requested profile active in the last requested minutes
|
|
* @param sessionId Profile to check
|
|
* @param minutes Minutes to check for activity in
|
|
* @returns True when profile was active within past x minutes
|
|
*/
|
|
public activeWithinLastMinutes(sessionId: string, minutes: number): boolean
|
|
{
|
|
const currentTimestamp = new Date().getTime() / 1000;
|
|
const storedActivityTimestamp = this.profileActivityTimestamps[sessionId];
|
|
if (!storedActivityTimestamp)
|
|
{
|
|
// No value, no assumed activity (server offline?)
|
|
return false;
|
|
}
|
|
|
|
// True if difference since last timestamp to now is below desired amount
|
|
return (currentTimestamp - storedActivityTimestamp) < (minutes * 60); // convert minutes to seconds to compare
|
|
}
|
|
|
|
/**
|
|
* Get an array of profile ids that were active in the last x minutes
|
|
* @param minutes How many minutes from now to search for profiles
|
|
* @returns String array of profile ids
|
|
*/
|
|
public getActiveProfileIdsWithinMinutes(minutes: number): string[]
|
|
{
|
|
const currentTimestamp = new Date().getTime() / 1000;
|
|
const result: string[] = [];
|
|
|
|
for (const id of Object.keys(this.profileActivityTimestamps ?? {}))
|
|
{
|
|
const lastActiveTimestamp = this.profileActivityTimestamps[id];
|
|
if (!lastActiveTimestamp)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
// Profile was active in last x minutes, add to return list
|
|
if ((currentTimestamp - lastActiveTimestamp) < (minutes * 60))
|
|
{
|
|
result.push(id);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Update the timestamp a profile was last observed active
|
|
* @param sessionId Profile to update
|
|
*/
|
|
public setActivityTimestamp(sessionId: string): void
|
|
{
|
|
this.profileActivityTimestamps[sessionId] = new Date().getTime() / 1000;
|
|
}
|
|
}
|