Repository navigation
Implementing Playtime Tracking #506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
58a61e0
feat(api): add playtime endpoints server side
Visirt 2524413
feat(frontend): displaying playtime in game library
Visirt f7830e2
feat(desktop): display Playtime counter on desktop app
Visirt 0cf4f6f
feat(desktop): sending recorded playtime
Visirt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
server/server/api/v1/client/playtime/[gameid]/index.get.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { ClientCapabilities } from "~/prisma/client/enums"; | ||
| import { defineClientEventHandler } from "~/server/internal/clients/event-handler"; | ||
| import prisma from "~/server/internal/db/database"; | ||
|
|
||
| export default defineClientEventHandler( | ||
| async (h3, { fetchClient, fetchUser }) => { | ||
| const client = await fetchClient(); | ||
| if (!client.capabilities.includes(ClientCapabilities.TrackPlaytime)) | ||
| throw createError({ | ||
| statusCode: 403, | ||
| statusMessage: "Capability not allowed.", | ||
| }); | ||
| const user = await fetchUser(); | ||
| const gameId = getRouterParam(h3, "gameid"); | ||
| if (!gameId) | ||
| throw createError({ | ||
| statusCode: 400, | ||
| statusMessage: "No gameID in route params", | ||
| }); | ||
|
|
||
| const game = await prisma.game.findUnique({ | ||
| where: { id: gameId }, | ||
| select: { id: true }, | ||
| }); | ||
| if (!game) | ||
| throw createError({ statusCode: 400, statusMessage: "Invalid game ID" }); | ||
|
|
||
| const playtime = await prisma.playtime.findUnique({ | ||
| where: { | ||
| gameId_userId: { | ||
| gameId, | ||
| userId: user.id, | ||
| }, | ||
| }, | ||
| }); | ||
| if (!playtime) | ||
| throw createError({ | ||
| statusCode: 404, | ||
| statusMessage: "Playtime not found", | ||
| }); | ||
|
|
||
| return playtime; | ||
| }, | ||
| ); |
60 changes: 60 additions & 0 deletions
60
server/server/api/v1/client/playtime/[gameid]/index.post.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { type } from "arktype"; | ||
| import { ClientCapabilities } from "~/prisma/client/enums"; | ||
| import { readDropValidatedBody, throwingArktype } from "~/server/arktype"; | ||
| import { defineClientEventHandler } from "~/server/internal/clients/event-handler"; | ||
| import prisma from "~/server/internal/db/database"; | ||
|
|
||
| const maxElapsedSeconds = 31536000; | ||
| const PlaytimeBody = type({ | ||
| seconds: "number.integer", | ||
| }).configure(throwingArktype); | ||
|
|
||
| export default defineClientEventHandler( | ||
| async (h3, { fetchClient, fetchUser }) => { | ||
| const client = await fetchClient(); | ||
| if (!client.capabilities.includes(ClientCapabilities.TrackPlaytime)) | ||
| throw createError({ | ||
| statusCode: 403, | ||
| statusMessage: "Capability not allowed.", | ||
| }); | ||
| const user = await fetchUser(); | ||
| const gameId = getRouterParam(h3, "gameid"); | ||
| if (!gameId) | ||
| throw createError({ | ||
| statusCode: 400, | ||
| statusMessage: "No gameID in route params", | ||
| }); | ||
|
|
||
| const game = await prisma.game.findUnique({ | ||
| where: { id: gameId }, | ||
| select: { id: true }, | ||
| }); | ||
| if (!game) | ||
| throw createError({ statusCode: 400, statusMessage: "Invalid game ID" }); | ||
|
|
||
| const body = await readDropValidatedBody(h3, PlaytimeBody); | ||
| const elapsedSeconds = body.seconds; | ||
| if (elapsedSeconds <= 0 || elapsedSeconds > maxElapsedSeconds) | ||
| throw createError({ | ||
| statusCode: 400, | ||
| statusMessage: `seconds must be between 1 and ${maxElapsedSeconds}.`, | ||
| }); | ||
|
|
||
| return prisma.playtime.upsert({ | ||
| where: { | ||
| gameId_userId: { | ||
| gameId, | ||
| userId: user.id, | ||
| }, | ||
| }, | ||
| create: { | ||
| gameId, | ||
| userId: user.id, | ||
| seconds: elapsedSeconds, | ||
| }, | ||
| update: { | ||
| seconds: { increment: elapsedSeconds }, | ||
| }, | ||
| }); | ||
| }, | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { ClientCapabilities } from "~/prisma/client/enums"; | ||
| import { defineClientEventHandler } from "~/server/internal/clients/event-handler"; | ||
| import prisma from "~/server/internal/db/database"; | ||
|
|
||
| export default defineClientEventHandler( | ||
| async (h3, { fetchClient, fetchUser }) => { | ||
| const client = await fetchClient(); | ||
| if (!client.capabilities.includes(ClientCapabilities.TrackPlaytime)) | ||
| throw createError({ | ||
| statusCode: 403, | ||
| statusMessage: "Capability not allowed.", | ||
| }); | ||
| const user = await fetchUser(); | ||
|
|
||
| const playtimes = await prisma.playtime.findMany({ | ||
| where: { | ||
| userId: user.id, | ||
| }, | ||
| }); | ||
|
|
||
| return playtimes; | ||
| }, | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.