Skip to content
This repository has been archived by the owner on Jul 31, 2024. It is now read-only.

Commit

Permalink
feat: completed tests get stored into the db now
Browse files Browse the repository at this point in the history
  • Loading branch information
vycdev committed Jul 16, 2020
1 parent b62953a commit f823fdc
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
69 changes: 67 additions & 2 deletions packages/web/src/components/common/typingBox/typingBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { TypingBoxProps, TypedArrayInterface } from "./helpers/interfaces";
import { PreviousScoresType } from "../../statisticsPage/helpers/interfaces";
import { DataBox } from "./components/testChart";

import { apiUrl } from "../../../utils/constants";

import {
Wrapper,
Container,
Expand Down Expand Up @@ -61,8 +63,53 @@ export const TypingBox = (props: TypingBoxProps) => {
updateText();

// eslint-disable-next-line @typescript-eslint/no-use-before-define
updateBestScore();
}, [time >= 60]);

const getUserBestScore = async (id: string) => {
if (id === "0") return [];
const result = await (
await fetch(`${apiUrl}/users/userpbs/${id}`, {
method: "GET",
credentials: "include",
headers: {
"Content-Type": "application/json"
}
})
).json();

return (await result.message) ? 0 : result[result.length - 1]?.wpm;
};

const updateBestScore = async () => {
setBestwpm(await getUserBestScore(localStorage.getItem("userid")));
};

const addGameToDb = async (
wpm: number,
rawwpm: number,
accuracy: number,
difficulty: number,
textid: number
) => {
const newGame = await fetch(`${apiUrl}/games/newGame`, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
wpm,
rawwpm,
accuracy,
difficulty,
textid
})
});

console.log(await newGame.json());
};

const updateText = async (forceUpdate = false) => {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
const gotTextInfo =
Expand Down Expand Up @@ -127,6 +174,26 @@ export const TypingBox = (props: TypingBoxProps) => {
accuracy: getAccuracy(typed)
});

addGameToDb(
props.mode === "easy"
? cpm / 5
: Math.floor(
// eslint-disable-next-line @typescript-eslint/no-use-before-define
(((cpm === 0 ? 0 : cpm / getAccuracy(typed)) * 100) /
5) *
100
) / 100,
Math.floor(
// eslint-disable-next-line @typescript-eslint/no-use-before-define
(((cpm === 0 ? 0 : cpm / getAccuracy(typed)) * 100) / 5) *
100
) / 100,
// eslint-disable-next-line @typescript-eslint/no-use-before-define
getAccuracy(typed),
parseInt(textInfo.difficulty),
parseInt(textInfo.id)
);

localStorage.setItem(
"previousScores",
JSON.stringify(previousScores)
Expand Down Expand Up @@ -377,8 +444,6 @@ export const TypingBox = (props: TypingBoxProps) => {
)
setWasTypedWrong(false);

console.log(wasTypedWrong);

// typed array is an array of objects that contans info about every second of the test
const typedArray: Array<TypedArrayInterface> =
e.target.value[e.target.value.length - 1] === " " &&
Expand Down
2 changes: 2 additions & 0 deletions packages/web/src/components/profilePage/helpers/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ export interface UserGame {
rawwpm: number;
userid: number;
wpm: number;
difficulty: number;
textid: number;
}

0 comments on commit f823fdc

Please sign in to comment.