Skip to content

Commit

Permalink
feat: create PlaytimeService
Browse files Browse the repository at this point in the history
  • Loading branch information
R-unic committed Mar 14, 2024
1 parent 6b21054 commit 9e4591c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/server/classes/firebase.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ function FirebaseService:fetch(name, database)
end
end

print("fetched firebase")
return Firebase;
end

Expand Down
2 changes: 1 addition & 1 deletion src/server/services/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class DatabaseService implements OnInit, LogStart {
this.set(player, directory, value);
}

private getDirectoryForPlayer(player: Player, directory: string) {
private getDirectoryForPlayer(player: Player, directory: string): string {
return `${player.UserId}/${directory}`;
}
}
31 changes: 31 additions & 0 deletions src/server/services/playtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Service, type OnInit } from "@flamework/core";
import { Players } from "@rbxts/services";

import type { LogStart } from "shared/hooks";
import type { OnPlayerJoin, OnPlayerLeave } from "server/hooks";
import type { DatabaseService } from "./database";

@Service()
export class PlaytimeService implements OnInit, OnPlayerJoin, OnPlayerLeave, LogStart {
private readonly joinTimestamps: Record<number, number> = {};

public constructor(
private readonly db: DatabaseService
) {}

public onInit(): void {
game.BindToClose(() => {
for (const player of Players.GetPlayers())
this.onPlayerLeave(player);
});
}

public onPlayerJoin(player: Player): void {
this.joinTimestamps[player.UserId] = os.clock();
}

public onPlayerLeave(player: Player): void {
const timeSpent = os.clock() - this.joinTimestamps[player.UserId];
this.db.increment(player, "playtime", timeSpent);
}
}

0 comments on commit 9e4591c

Please sign in to comment.