Skip to content

Commit

Permalink
Bugfix configuration (#28)
Browse files Browse the repository at this point in the history
The configuration from the lecturer interface is now used correctly,
however the lecturer interface does not restrict the selection of regex
structures, so that illegal combinations are possible. This will be
fixed soon in the lecturer interface itself
  • Loading branch information
Fa8Bit authored Jul 18, 2024
1 parent 396c183 commit a308f31
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const apiPath: string = "api/v1/"

async function fetchGameConfigration(id: string): Promise<GameConfiguration> {
async function fetchGameConfiguration(id: string): Promise<GameConfiguration> {
let fetchResponse = await fetch(apiPath + "configurations/" + id)
if (fetchResponse.status != 200) throw fetchResponse.status
let json = await fetchResponse.text()
Expand All @@ -26,6 +26,7 @@ async function getGameConfigurationBySearchQuery(): Promise<GameConfiguration> {
if (gameConfigurationBySearchQuery) return gameConfigurationBySearchQuery
let id = new URLSearchParams(document.location.search).get("id")
if (!id) throw new Error("no id specified")
gameConfigurationBySearchQuery = await fetchGameConfigration(id)
gameConfigurationBySearchQuery = await fetchGameConfiguration(id)
console.log("getGameConfig",gameConfigurationBySearchQuery)
return gameConfigurationBySearchQuery
}
23 changes: 17 additions & 6 deletions src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ var gameStartTimestamp: Date
*/
var countdownTimer: number

let noTimer: boolean = false

/**
* reward from playing the game (coins)
*/
Expand Down Expand Up @@ -70,8 +72,15 @@ async function displayRiddle(riddle: Riddle) {
gameProgressBar.innerHTML = progress + "%"
}
await gameBoxHeightTransitionEnd()
timeoutProgressBar.style.setProperty("--countdown", getRemainingTime() + "s")
timeoutProgressBar.classList.add("countdown")

if(getRemainingTime() == Number.POSITIVE_INFINITY) {
noTimer = true
// FIXME make bar be full green when infinity, and display Infinity instead of infinitys
} else {
noTimer = false
timeoutProgressBar.style.setProperty("--countdown", getRemainingTime() + "s")
timeoutProgressBar.classList.add("countdown")
}
}

function getRemainingTime() {
Expand All @@ -86,9 +95,11 @@ function calculateCompletionPercentage() {
async function nextRiddle() {
round++
try {
await displayRiddle(generateRiddle(round))
if (countdownTimer > 0) clearTimeout(countdownTimer)
countdownTimer = setTimeout(gameEnd, getRemainingTime() * 1000)
await displayRiddle(generateRiddle(round, gameConfigurationBySearchQuery))
if(!noTimer) {
if (countdownTimer > 0) clearTimeout(countdownTimer)
countdownTimer = setTimeout(gameEnd, getRemainingTime() * 1000)
}
} catch (e) {
console.error("error creating next riddle", e)
//TODO: notify user?
Expand Down Expand Up @@ -125,7 +136,7 @@ function onAnswerSelected(button: HTMLButtonElement, answer: string) {
let correctAnswer: boolean

if (!currentRiddle) throw new Error("current riddle undefined")
if (countdownTimer > 0) clearTimeout(countdownTimer)
if (countdownTimer > 0 && !noTimer) clearTimeout(countdownTimer)
if (answersContainer.querySelector("button.selected")) {
correctAnswer = answersContainer.querySelector("button.selected.correct") != undefined
if (correctAnswer && nextAnswerTimeout !== undefined) {
Expand Down
2 changes: 2 additions & 0 deletions src/generators/riddlegenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ const defaultConfiguration: GameConfiguration = new GameConfiguration("", new Se
*/
function generateRiddle(round?: number, configuration?: GameConfiguration): Riddle {
if (configuration === undefined) configuration = defaultConfiguration

let complexity = configuration.complexity
if (round != undefined) complexity += RegexComplexity.calculateRoundComplexityFactor(round)
let regex = RegexGenerator.generateRegex(configuration.allowedRegexStructures, complexity)
let answers = generateRegexAnswers(regex, configuration.answerCount)

return new Riddle(regex, answers)
}
8 changes: 7 additions & 1 deletion src/model/GameConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@ class GameConfiguration {
if (!parsedJSON.allowedRegexStructures) throw new Error("missing allowed regex structures")
if (!(parsedJSON.allowedRegexStructures instanceof Array)) throw new Error("regex structures invalid. Expected array but got " + parsedJSON.allowedRegexStructures)
let allowedRegexStructures = new Set<RegexStructure>();
(parsedJSON.allowedRegexStructures as Array<string>).forEach(regexStructureString => {
(parsedJSON.allowedRegexStructures as Array<string>).forEach((regexStructureString) => {
allowedRegexStructures.add(getRegexStructureFromString(regexStructureString))
})
let gameconfiguration: GameConfiguration = new GameConfiguration(parsedJSON.id, allowedRegexStructures)
if (parsedJSON.complexity) gameconfiguration.complexity = parsedJSON.complexity
if (parsedJSON.increaseComplexity) gameconfiguration.increaseComplexity = parsedJSON.increaseComplexity
if (parsedJSON.answerCount) gameconfiguration.answerCount = parsedJSON.answerCount
if(parsedJSON.minimumCompletedRounds) gameconfiguration.minimumCompletedRounds = parsedJSON.minimumCompletedRounds
if(parsedJSON.riddleTimeoutSeconds) {
gameconfiguration.riddleTimeoutSeconds = parsedJSON.riddleTimeoutSeconds
} else {
gameconfiguration.riddleTimeoutSeconds = Number.POSITIVE_INFINITY;
}
return gameconfiguration
}
}

0 comments on commit a308f31

Please sign in to comment.