Adds stats for the truth tree quiz

This commit is contained in:
2024-12-02 18:54:45 -05:00
parent 7ea891f3b3
commit cdbeec0af3
7 changed files with 113 additions and 103 deletions

18
html/api/logic.php Executable file
View File

@@ -0,0 +1,18 @@
<?php
require_once "api.php";
require_once "../include/logic.php";
if (array_key_exists("solved", $_GET)) {
$solved = GetLogicSolved();
$complexity = GetLogicTotalComplexity();
$complexity += $_GET["solved"];
$solved++;
dbCommand("DELETE FROM information WHERE pointer='logicSolved'");
dbCommand("DELETE FROM information WHERE pointer='logicComplexity'");
dbCommand("INSERT INTO information VALUES ('logicComplexity', ?)", [$complexity]);
dbCommand("INSERT INTO information VALUES ('logicSolved', ?)", [$solved]);
echo json_encode([
"solved" => $solved,
"average" => GetLogicAverageComplexity()
]);
}

57
html/include/logic.php Normal file
View File

@@ -0,0 +1,57 @@
<?php
/**
* Retrieves the number of logic problems solved from the database.
*
* Queries the 'information' table for the entry with the pointer 'logicSolved'.
* If no entry is found, returns 0. Otherwise, returns the first element of the
* result, which represents the number of logic problems solved.
*
* @return int The number of logic problems solved.
*/
function GetLogicSolved()
{
$result = dbRequest2("SELECT data FROM information WHERE pointer='logicSolved'", "data");
if (!$result) {
$result = 0;
} else {
$result = $result[0];
}
return $result;
}
/**
* Retrieves the total complexity of all logic problems solved from the database.
*
* Queries the 'information' table for the entry with the pointer 'logicComplexity'.
* If no entry is found, returns 0. Otherwise, returns the first element of the
* result, which represents the total complexity of all logic problems solved.
*
* @return int The total complexity of all logic problems solved.
*/
function GetLogicTotalComplexity()
{
$total = dbRequest2("SELECT data FROM information WHERE pointer='logicComplexity'", "data");
if (!$total) {
$total = 0;
} else {
$total = $total[0];
}
return $total;
}
/**
* Retrieves the average complexity of all logic problems solved from the database.
*
* Calculates the average complexity of all logic problems solved by dividing the
* total complexity of all logic problems solved by the number of logic problems
* solved.
*
* @return float The average complexity of all logic problems solved.
*/
function GetLogicAverageComplexity()
{
$solved = GetLogicSolved();
if ($solved == 0) {
return 0;
}
return GetLogicTotalComplexity() / $solved;
}

View File

@@ -93,14 +93,39 @@ function firstOperation() {
complexity = 1000;
}
localStorage.firstOperationComplexity = complexity;
if (localStorage.solvedTruthTree == undefined) {
localStorage.solvedTruthTree = 0;
}
if (localStorage.complexityTruthTree == undefined) {
localStorage.complexityTruthTree = 0;
}
// Checks if the game has already been played and if this was a completed game sends the event.
if (firstOperationAttempts != 0) {
_paq.push(
["trackEvent", "Truth Tree", `Complexity : ${complexity}`],
`Attempts : ${firstOperationAttempts}`
);
_paq.push([
"trackEvent",
"Truth Tree",
`Complexity : ${complexity}`,
`Attempts : ${firstOperationAttempts}`,
]);
firstOperationAttempts = 0;
localStorage.solvedTruthTree++;
localStorage.complexityTruthTree =
parseInt(localStorage.complexityTruthTree) + complexity;
fetch(`/api/logic.php?solved=${complexity}`)
.then((r) => r.json())
.then((r) => {
$("#solvedTotal").text(r.solved);
$("#averageTotal").text(r.average);
});
}
$("#solved").text(parseInt(localStorage.solvedTruthTree));
$("#average").text(
parseInt(localStorage.solvedTruthTree) === 0
? 0
: parseInt(localStorage.complexityTruthTree) /
parseInt(localStorage.solvedTruthTree)
);
// Will generate the text for the truth sentence
function generateSentenceText(sentence, first) {
if (sentence.length == 2) {

View File

@@ -9,6 +9,7 @@
<?php
$DESCRIPTION = "This just contains a simple quiz that helps you decompose a truth tree.";
require_once '../include/all.php';
require_once "../include/logic.php";
?>
</head>
@@ -17,8 +18,10 @@
include '../include/menu.php';
echo "<div class='main'>";
?>
<script type='text/javascript' src='index.js?v=1.0.3'></script>
<script type='text/javascript' src='index.js?v=1.1.0'></script>
<h1>Truth Tree Quiz</h1>
<p>Everyone solved <c id='solvedTotal'><?php echo GetLogicSolved(); ?></c> question(s) with an average complexity of <c id='averageTotal'><?php echo GetLogicAverageComplexity(); ?></c>.</p>
<p>You solved <c id='solved'>0</c> question(s) with an average complexity of <c id='average'>0</c>.</p>
<p>Notes for all the logic symbols:</p>
<ul>
<li>Negation: ~</li>
@@ -28,10 +31,10 @@
<li>Biconditional(if and only if): ↔</li>
</ul>
<h3>Click on the first logical operation that should be decomposed in a truth tree.</h3>
<label for="firstOperationComplexity">Complexity of Sentence: </label><input id='firstOperationComplexity' value='4' type='number'>
<label for="firstOperationComplexity">Complexity of Sentence: </label><input id='firstOperationComplexity' value='4' type='number' onchange="firstOperation()">
<button id='firstOperationGenerate' onClick='firstOperation()'>Generate Sentence</button>
<p id='firstOperation'></p>
</div>
</body>
</html>
</html>

0
html/minecraft.php Normal file → Executable file
View File