From f823fdc80e4c248711d34e42c468054bef93797c Mon Sep 17 00:00:00 2001 From: Sandu Victor Date: Thu, 16 Jul 2020 19:37:14 +0300 Subject: [PATCH] feat: completed tests get stored into the db now --- .../components/common/typingBox/typingBox.tsx | 69 ++++++++++++++++++- .../profilePage/helpers/interfaces.ts | 2 + 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/packages/web/src/components/common/typingBox/typingBox.tsx b/packages/web/src/components/common/typingBox/typingBox.tsx index 8d0ded0..9873e36 100644 --- a/packages/web/src/components/common/typingBox/typingBox.tsx +++ b/packages/web/src/components/common/typingBox/typingBox.tsx @@ -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, @@ -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 = @@ -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) @@ -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 = e.target.value[e.target.value.length - 1] === " " && diff --git a/packages/web/src/components/profilePage/helpers/interfaces.ts b/packages/web/src/components/profilePage/helpers/interfaces.ts index e9a9ad5..516faa6 100644 --- a/packages/web/src/components/profilePage/helpers/interfaces.ts +++ b/packages/web/src/components/profilePage/helpers/interfaces.ts @@ -16,4 +16,6 @@ export interface UserGame { rawwpm: number; userid: number; wpm: number; + difficulty: number; + textid: number; }