Skip to content

Commit

Permalink
feat: scores api, update scores on victory
Browse files Browse the repository at this point in the history
  • Loading branch information
HypedLama committed Mar 8, 2025
1 parent f6b4ec5 commit 0aa7ba6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/http/http.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from "@angular/core";
import { HttpClient } from "./http.client";
import { Auth, Daily, HttpResponse, User, Words } from "./http.types";
import { Auth, Daily, HttpResponse, Score, User, Words } from "./http.types";

@Injectable({
providedIn: "root",
Expand Down Expand Up @@ -28,6 +28,14 @@ export class HttpService {
return this.client.get("words/daily", { language: "en" });
}

getScoresDaily(): HttpResponse<Score[]> {
return this.client.get("/users/@me/scores/daily", {});
}

getScoresPractice(): HttpResponse<Score[]> {
return this.client.get("/users/@me/scores/practice", {});
}

getWords(): HttpResponse<Words> {
return this.client.get("words", { language: "en" });
}
Expand All @@ -36,6 +44,15 @@ export class HttpService {
localStorage.removeItem("token");
sessionStorage.removeItem("token");
}
setScore(score: Score): HttpResponse<Score> {
const data: any = {
attempts: score.attempts.toString(),
time: score.time.toString(),
...(score.date && { date: score.date.toString() })
};

return this.client.post("/users/@me/scores", data);
}

setToken(token: string, remember: boolean): void {
this.removeToken();
Expand Down
6 changes: 6 additions & 0 deletions src/http/http.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ export interface Words {
guessable: string[];
solutions: string[];
}

export interface Score {
attempts: number;
date?: string;
time: number;
}
26 changes: 25 additions & 1 deletion src/words/words.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from "@angular/core";
import { HttpService } from "../http/http.service";
import { Daily, Words } from "../http/http.types";
import { Daily, Words, Score } from "../http/http.types";
import { Mode, Progress, State } from "./words.types";

@Injectable({
Expand Down Expand Up @@ -62,6 +62,30 @@ export class WordService {

progress.guesses.unshift("");
this.setProgress(progress);
this.applyScore()
}

applyScore(): void {
if (this.progress.state !== State.Victory) {
return;
}

if (this.progress.time.start === undefined || this.progress.time.end === undefined) {
throw new Error("start or end time is undefined");
return;
}

const score = {
attempts: this.progress.guesses.length-1,
time: this.progress.time.end - this.progress.time.start,
} as Score;

if (this.mode === Mode.Daily) {
score.date = this.daily?.date;
this.http.setScore(score);
} else {
this.http.setScore(score);
}
}

async load(): Promise<void> {
Expand Down

0 comments on commit 0aa7ba6

Please sign in to comment.