allowing to load challenges from command line, keeping track of which challenge is active

This commit is contained in:
Jack 2021-08-22 17:29:40 +01:00
parent 7b9a9af3fa
commit 28044334c1
5 changed files with 59 additions and 1 deletions

View file

@ -6,6 +6,15 @@ import * as CustomText from "./custom-text";
import * as TestLogic from "./test-logic";
import * as Funbox from "./funbox";
export let active = null;
export function clearActive() {
if (active) {
Notifications.add("Challenge cleared", 0);
active = null;
}
}
export async function setup(challengeName) {
let list = await Misc.getChallengeList();
let challenge = list.filter((c) => c.name === challengeName)[0];
@ -92,10 +101,11 @@ export async function setup(challengeName) {
$(".page.pageTest").removeClass("hidden");
if (notitext === undefined) {
Notifications.add(`Challenge '${challengeName}' loaded.`, 0);
Notifications.add(`Challenge '${challenge.display}' loaded.`, 0);
} else {
Notifications.add("Challenge loaded. " + notitext, 0);
}
active = challenge;
} catch (e) {
Notifications.add("Something went wrong: " + e, -1);
}

View file

@ -17,6 +17,7 @@ import * as PresetController from "./preset-controller";
import * as Commandline from "./commandline";
import * as CustomText from "./custom-text";
import * as Settings from "./settings";
import * as ChallengeController from "./challenge-controller";
export let current = [];
@ -2151,6 +2152,24 @@ Misc.getThemesList().then((themes) => {
});
});
export let commandsChallenges = {
title: "Load challenge...",
list: [],
};
Misc.getChallengeList().then((challenges) => {
challenges.forEach((challenge) => {
commandsChallenges.list.push({
id: "loadChallenge" + Misc.capitalizeFirstLetter(challenge.name),
noIcon: true,
display: challenge.display,
exec: () => {
ChallengeController.setup(challenge.name);
},
});
});
});
// export function showFavouriteThemesAtTheTop() {
export function updateThemeCommands() {
if (Config.favThemes.length > 0) {
@ -2790,6 +2809,12 @@ export let defaultCommands = {
return canBailOut();
},
},
{
id: "loadChallenge",
display: "Load challenge...",
icon: "fa-award",
subgroup: commandsChallenges,
},
{
id: "joinDiscord",
display: "Join the Discord server",

View file

@ -18,6 +18,7 @@ import * as UI from "./ui";
import * as CommandlineLists from "./commandline-lists";
import * as BackgroundFilter from "./custom-background-filter";
import LayoutList from "./layouts";
import * as ChallengeContoller from "./challenge-controller";
export let localStorageConfig = null;
export let dbConfigLoaded = false;
@ -170,6 +171,7 @@ export function setNumbers(numb, nosave) {
} else {
$("#top .config .numbersMode .text-button").addClass("active");
}
ChallengeContoller.clearActive();
if (!nosave) saveToLocalStorage();
}
@ -197,6 +199,7 @@ export function setPunctuation(punc, nosave) {
} else {
$("#top .config .punctuationMode .text-button").addClass("active");
}
ChallengeContoller.clearActive();
if (!nosave) saveToLocalStorage();
}
@ -284,6 +287,7 @@ export function setMode(mode, nosave) {
}
// setPaceCaret("off", true);
}
ChallengeContoller.clearActive();
if (!nosave) saveToLocalStorage();
}
@ -335,6 +339,7 @@ export function setFavThemes(themes, nosave) {
export function setFunbox(funbox, nosave) {
config.funbox = funbox ? funbox : "none";
ChallengeContoller.clearActive();
if (!nosave) {
saveToLocalStorage();
}
@ -507,6 +512,7 @@ export function setPaceCaret(val, nosave) {
// val = "off";
// }
config.paceCaret = val;
ChallengeContoller.clearActive();
TestUI.updateModesNotice();
PaceCaret.init(nosave);
if (!nosave) saveToLocalStorage();
@ -633,6 +639,7 @@ export function setShowAllLines(sal, nosave) {
sal = false;
}
config.showAllLines = sal;
ChallengeContoller.clearActive();
if (!nosave) {
saveToLocalStorage();
TestLogic.restart();
@ -837,6 +844,7 @@ export function setShowLiveWpm(live, nosave) {
} else {
LiveWpm.hide();
}
ChallengeContoller.clearActive();
if (!nosave) saveToLocalStorage();
}
@ -912,6 +920,7 @@ export function setHighlightMode(mode, nosave) {
}
config.highlightMode = mode;
// if(TestLogic.active){
ChallengeContoller.clearActive();
try {
if (!nosave) TestUI.updateWordElement(config.blindMode);
} catch {}
@ -1025,6 +1034,7 @@ export function setTimeConfig(time, nosave) {
$("#top .config .time .text-button[timeConfig='" + time + "']").addClass(
"active"
);
ChallengeContoller.clearActive();
if (!nosave) saveToLocalStorage();
}
@ -1075,6 +1085,7 @@ export function setWordCount(wordCount, nosave) {
$(
"#top .config .wordCount .text-button[wordCount='" + wordCount + "']"
).addClass("active");
ChallengeContoller.clearActive();
if (!nosave) saveToLocalStorage();
}
@ -1349,6 +1360,7 @@ export function setKeymapMode(mode, nosave) {
$(".active-key").removeClass("active-key");
$(".keymap-key").attr("style", "");
config.keymapMode = mode;
ChallengeContoller.clearActive();
if (!nosave) TestLogic.restart(false, nosave);
if (!nosave) saveToLocalStorage();
}
@ -1397,6 +1409,7 @@ export function setKeymapLayout(layout, nosave) {
layout = "qwerty";
}
config.keymapLayout = layout;
ChallengeContoller.clearActive();
Keymap.refreshKeys(layout, setKeymapLayout);
if (!nosave) saveToLocalStorage();
}
@ -1406,6 +1419,7 @@ export function setLayout(layout, nosave) {
layout = "qwerty";
}
config.layout = layout;
ChallengeContoller.clearActive();
TestUI.updateModesNotice();
if (config.keymapLayout === "overrideSync") {
Keymap.refreshKeys(config.keymapLayout, setKeymapLayout);

View file

@ -4,6 +4,7 @@ import * as Misc from "./misc";
import * as Notifications from "./notifications";
import * as TestLogic from "./test-logic";
import * as WordFilterPopup from "./word-filter-popup";
import * as ChallengeController from "./challenge-controller";
let wrapper = "#customTextPopupWrapper";
let popup = "#customTextPopup";
@ -151,6 +152,7 @@ $("#customTextPopup .apply").click(() => {
);
}
ChallengeController.clearActive();
ManualRestart.set();
TestLogic.restart();
hide();

View file

@ -17,6 +17,7 @@ import * as Replay from "./replay";
import * as TestStats from "./test-stats";
import * as Misc from "./misc";
import * as TestUI from "./test-ui";
import * as ChallengeController from "./challenge-controller";
export let currentWordElementIndex = 0;
export let resultVisible = false;
@ -475,6 +476,12 @@ export function updateModesNotice() {
);
}
if (ChallengeController.active) {
$(".pageTest #testModesNotice").append(
`<div class="text-button" commands="commandsChallenges"><i class="fas fa-award"></i>${ChallengeController.active.display}</div>`
);
}
if (Config.mode === "zen") {
$(".pageTest #testModesNotice").append(
`<div class="text-button"><i class="fas fa-poll"></i>shift + enter to finish zen </div>`