Code:
setInfos(9, "BuzzQuizTest", "It could be painful to get these questions wrong.", "NIFOC99",
"v0.1 test", 0x222222, "en", ["game", "formale", "forfemale", "estim"])
String SCRIPTSDIR = "scripts/BuzzQuiz"
String SOUNDSDIR = "ShockYourself/"
List ESTIMFILENAMES = [
"sawtooth_100hz_10sec_level",
"sine_100hz_10sec_level",
"square_100hz_10sec_level",
]
String EXT = ".wav"
List PAINLEVELRANGELIMIT = [1, 20]
List SHOCKTIMERANGELIMIT = [0.1, 10.0]
List estimFileNames = ESTIMFILENAMES // TODO options
def getProgressBar = { def value, def colRange = [[255, 255, 255], [255, 255, 255]], def colBG = [80, 80, 80], int charLen = 20, int charWidth = 1 ->
String bt = ""
String blockChar = "▇" * charWidth
List colRangeRed = [colRange[0][0], colRange[1][0]]
List colRangeGreen = [colRange[0][1], colRange[1][1]]
List colRangeBlue = [colRange[0][2], colRange[1][2]]
List colGradientRed = []
List colGradientGreen = []
List colGradientBlue = []
float deltaR = (colRangeRed[1] - colRangeRed[0]) / (charLen - 1)
float deltaG = (colRangeGreen[1] - colRangeGreen[0]) / (charLen - 1)
float deltaB = (colRangeBlue[1] - colRangeBlue[0]) / (charLen - 1)
for (int i = 0; i < charLen; i++) {
colGradientRed.add((int) (colRangeRed[0] + i * deltaR))
colGradientGreen.add((int) (colRangeGreen[0] + i * deltaG))
colGradientBlue.add((int) (colRangeBlue[0] + i * deltaB))
}
float val = (value * charLen) / 100
for (int i = 0; i < charLen; i++) {
if (i < val) bt += "<span style='color: rgb(" +
colGradientRed[i] + "," +
colGradientGreen[i] + "," +
colGradientBlue[i] + ");'>" + blockChar + "</span>"
else bt += "<span style='color: rgb(" + colBG.toString().replaceAll("\\[|\\]", "") + ");'>" + blockChar + "</span>"
}
return bt
}
def getRecordBar = { List records ->
String bulletChar = "⬤"
def colBG = [80, 80, 80]
def colRed = [220, 0, 0]
def colGreen = [0, 220, 0]
String bt = ""
records.each { record ->
record.each { r ->
if (r == true) bt += "<span style='color: rgb(" + colGreen.toString().replaceAll("\\[|\\]", "") + ");'>" + bulletChar + "</span>"
else if (r == false) bt += "<span style='color: rgb(" + colRed.toString().replaceAll("\\[|\\]", "") + ");'>" + bulletChar + "</span>"
else bt += "<span style='color: rgb(" + colBG.toString().replaceAll("\\[|\\]", "") + ");'>" + bulletChar + "</span>"
}
bt += " "
}
return bt
}
def getQuestions = {
def getQuizzFiles = {
def files = []
def dir = new File(SCRIPTSDIR)
dir.traverse(type: groovy.io.FileType.FILES, maxDepth: 0) { file -> files.add(file.absolutePath) }
return files
}
List questions = []
List quizzFiles = getQuizzFiles()
quizzFiles.each { fName ->
File txtFile = new File(fName)
List lines = txtFile.readLines()
lines.each { strLine -> questions.add(strLine.split('\\|').collect()) }
}
return questions
}
def getRandomEstimFileName = { return estimFileNames[getRandom(estimFileNames.size())] }
def shock = { stime, plvl ->
String sound = SOUNDSDIR + getRandomEstimFileName() + plvl + EXT
try {
playBackgroundSound(sound)
wait(stime)
playBackgroundSound(null)
return true
}
catch (Exception e) {
playBackgroundSound(null)
openCdTrays()
show("Error! Sound file missing?")
showButton("Quit")
exit()
}
}
def playBuzzQuizz = {
int score = 0 // TODO score?
int rounds = 4
int questionsPerRound = 5
// int totalQuestions = rounds * questionsPerRound
List questions = getQuestions()
Collections.shuffle(questions)
int qNum = 0
int round = 1
int question = 1
List records = []
for (int i = 0; i < rounds; i++) {
records.add([null] * questionsPerRound)
}
int painLevel = 1
float shockTime = 1.0
int painLevelPlus = 1
float shockTimePlus = 0.25
def getDisplayText = { String txt = "" ->
return "\n" + getRecordBar(records) +
"\nRound " + round + " - Question " + question + " - Score " + score +
"\nPain Level " + painLevel + getProgressBar((painLevel / PAINLEVELRANGELIMIT.max()) * 100, [[220, 220, 0], [220, 0, 0]]) +
"\nShock Time " + shockTime + getProgressBar((shockTime / SHOCKTIMERANGELIMIT.max()) * 100, [[220, 220, 0], [220, 0, 0]]) +
"\n\n<p style='font-size:16px;'>" + txt + "</p>"
}
for (int r = 0; r < rounds; r++) {
for (int q = 0; q < questionsPerRound; q++) {
round = r + 1
question = q + 1
String correctAnswer = questions[qNum][2]
List wrongAnswers = questions[qNum][3..<questions[qNum].size()]
List allAnswers = [correctAnswer] + wrongAnswers
Collections.shuffle(allAnswers)
int timeStart = new Date().getTime()
int selectedAnswer = getSelectedValue(getDisplayText(questions[qNum][1]), allAnswers)
if (correctAnswer == allAnswers[selectedAnswer]) {
int duration = new Date().getTime() - timeStart
records[r][q] = true
String txt = "Correct!"
if (duration <= 5000) {
int points = 5000 - duration
score += points
txt += " " + points + " Points!"
} else txt += " But too slow! No Points!"
show(getDisplayText(txt))
wait(2)
} else {
records[r][q] = false
show(getDisplayText("Wrong!"))
shock(shockTime, painLevel)
int newPLvl = painLevel + painLevelPlus
float newSTime = shockTime + shockTimePlus
painLevel = (newPLvl <= PAINLEVELRANGELIMIT.max()) ? newPLvl : PAINLEVELRANGELIMIT.max()
shockTime = (newSTime <= SHOCKTIMERANGELIMIT.max()) ? newSTime : SHOCKTIMERANGELIMIT.max()
}
qNum++
}
if (round != rounds) {
if (!getBoolean(getDisplayText("End of Round " + round + ". Continue?"))) {
break
}
}
}
show(getDisplayText("Game Over\n\nYour final score is " + score))
showButton("End Game")
}
while (true) {
playBackgroundSound(null)
show(null)
setImage(null)
show("Test for BuzzQuiz\n\n<b>For testing only!</b>\n\nNeeds 'BuzzQuiz' and 'Shock Yourself' installed!")
showButton("Start Quiz")
playBuzzQuizz()
}