From 251b7234a4177e183927d72c5687fe7035f29f49 Mon Sep 17 00:00:00 2001 From: Miodec Date: Mon, 21 Feb 2022 13:46:00 +0100 Subject: [PATCH] fixed showavg duplication --- .../src/scripts/elements/last-10-average.ts | 25 +++++++++++++++++++ frontend/src/scripts/elements/modes-notice.ts | 18 +++---------- frontend/src/scripts/test/test-logic.js | 2 ++ 3 files changed, 31 insertions(+), 14 deletions(-) create mode 100644 frontend/src/scripts/elements/last-10-average.ts diff --git a/frontend/src/scripts/elements/last-10-average.ts b/frontend/src/scripts/elements/last-10-average.ts new file mode 100644 index 000000000..d1baa81b7 --- /dev/null +++ b/frontend/src/scripts/elements/last-10-average.ts @@ -0,0 +1,25 @@ +import * as DB from "../db"; +import * as Misc from "../misc"; +import Config from "../config"; +import * as TestWords from "../test/test-words"; + +let value = 0; + +export async function update(): Promise { + const mode2 = Misc.getMode2(Config, TestWords.randomQuote); + let wpm = await DB.getUserAverageWpm10( + Config.mode, + mode2 as never, + Config.punctuation, + Config.language, + Config.difficulty, + Config.lazyMode + ); + wpm = Misc.roundTo2(wpm); + if (!Config.alwaysShowDecimalPlaces) wpm = Math.round(wpm); + value = wpm; +} + +export function get(): number { + return value; +} diff --git a/frontend/src/scripts/elements/modes-notice.ts b/frontend/src/scripts/elements/modes-notice.ts index 38cd94951..7bef40cd0 100644 --- a/frontend/src/scripts/elements/modes-notice.ts +++ b/frontend/src/scripts/elements/modes-notice.ts @@ -1,7 +1,7 @@ import * as PaceCaret from "../test/pace-caret"; import * as TestState from "../test/test-state"; import * as DB from "../db"; -import * as Misc from "../misc"; +import * as Last10Average from "../elements/last-10-average"; import Config from "../config"; import * as TestWords from "../test/test-words"; import * as ConfigEvent from "../observables/config-event"; @@ -107,20 +107,10 @@ export async function update(): Promise { } if (Config.showAvg) { - const mode2 = Misc.getMode2(Config, TestWords.randomQuote); - let wpm = await DB.getUserAverageWpm10( - Config.mode, - mode2 as never, - Config.punctuation, - Config.language, - Config.difficulty, - Config.lazyMode - ); - wpm = Math.round(wpm * 100) / 100; - if (!Config.alwaysShowDecimalPlaces) wpm = Math.round(wpm); - if (wpm > 0) { + const val = Last10Average.get(); + if (firebase.auth().currentUser && val > 0) { $(".pageTest #testModesNotice").append( - `
avg: ${wpm}wpm
` + `
avg: ${val}wpm
` ); } } diff --git a/frontend/src/scripts/test/test-logic.js b/frontend/src/scripts/test/test-logic.js index d9b9c66f9..28c4d68a9 100644 --- a/frontend/src/scripts/test/test-logic.js +++ b/frontend/src/scripts/test/test-logic.js @@ -46,6 +46,7 @@ import * as ModesNotice from "../elements/modes-notice"; import * as PageTransition from "../states/page-transition"; import * as ConfigEvent from "../observables/config-event"; import * as TimerEvent from "../observables/timer-event"; +import * as Last10Average from "../elements/last-10-average"; const objecthash = require("node-object-hash")().hash; @@ -372,6 +373,7 @@ export function restart( Replay.pauseReplay(); TestInput.setBailout(false); PaceCaret.reset(); + if (Config.showAvg) Last10Average.update(); $("#showWordHistoryButton").removeClass("loaded"); $("#restartTestButton").blur(); Funbox.resetMemoryTimer();