182 lines
5.5 KiB
PHP
182 lines
5.5 KiB
PHP
<?php
|
|
|
|
require_once "globals.php";
|
|
require_once "common_func.php";
|
|
|
|
require_once "gamemgr.php";
|
|
require_once "usermgr.php";
|
|
|
|
$testdb = new \SleekDB\Store(TESTDB, DATADIR, ["timeout" => false]);
|
|
|
|
const TEST_ONGOING = "ongoing";
|
|
const TEST_CONCLUDED = "concluded";
|
|
|
|
function create_or_continue_test(string $gameid, string $nickname): string
|
|
{
|
|
global $testdb;
|
|
|
|
// get game and user data
|
|
$game_data = get_game($gameid);
|
|
$user_data = get_user($nickname);
|
|
if ((count($game_data) === 0) || (count($user_data) === 0)) {
|
|
return "";
|
|
}
|
|
|
|
// check if this user has permission to take this test
|
|
// if the intersection of user's groups and game's assigned groups is zero, then the user has no access to this game
|
|
if (count(array_intersect($game_data["groups"], $user_data["groups"])) === 0) {
|
|
return "";
|
|
}
|
|
|
|
// check if the user had taken this test before
|
|
$fetch_criteria = [["gameid", "=", (int)$gameid], "AND", ["nickname", "=", $nickname]];
|
|
$previous_tests = $testdb->findBy($fetch_criteria);
|
|
if (count($previous_tests) > 0) { // if there are previous attempts, then...
|
|
// update timed tests to see if they had expired
|
|
update_timed_tests($previous_tests);
|
|
|
|
// re-fetch tests, look only for ongoing
|
|
$ongoing_tests = $testdb->findBy([$fetch_criteria, "AND", ["state", "=", TEST_ONGOING]]);
|
|
if (count($ongoing_tests) !== 0) { // if there's an ongoing test
|
|
return $ongoing_tests[0]["_id"];
|
|
} else { // there's no ongoing test
|
|
if ($game_data["properties"]["repeatable"]) { // test is repeatable...
|
|
return create_test($game_data, $user_data);
|
|
} else { // test is non-repeatable, cannot be attempted more times
|
|
return "";
|
|
}
|
|
}
|
|
} else { // there were no previous attempts
|
|
return create_test($game_data, $user_data);
|
|
}
|
|
}
|
|
|
|
function create_test(array $game_data, array $user_data) : string
|
|
{
|
|
global $testdb;
|
|
$gameid = $game_data["_id"];
|
|
$game_properties = $game_data["properties"];
|
|
|
|
// fill basic data
|
|
$test_data = [
|
|
"gameid" => $gameid,
|
|
"nickname" => $user_data["nickname"],
|
|
"gamename" => $game_data["name"]
|
|
];
|
|
|
|
// fill challenges
|
|
$challenges = load_challenges($gameid);
|
|
|
|
// shuffle answers
|
|
foreach ($challenges as &$ch) {
|
|
shuffle($ch["answers"]);
|
|
$ch["correct_answer"] = "";
|
|
$ch["player_answer"] = "";
|
|
}
|
|
|
|
// involve properties
|
|
$now = time();
|
|
$properties = [
|
|
"state" => TEST_ONGOING,
|
|
"time_limited" => (($game_properties["time_limit"] ?: -1) > -1),
|
|
"start_time" => $now,
|
|
"repeatable" => $game_properties["repeatable"] ?: false
|
|
];
|
|
if ($properties["time_limited"]) {
|
|
$properties["end_limit_time"] = $now + $game_properties["time_limit"];
|
|
}
|
|
|
|
// merge properties and test data
|
|
$test_data = array_merge($test_data, $properties);
|
|
|
|
// add challenges
|
|
$test_data["challenges"] = $challenges;
|
|
|
|
// store game
|
|
$test_data = $testdb->insert($test_data);
|
|
|
|
$testid = $test_data["_id"];
|
|
return $testid;
|
|
}
|
|
|
|
function update_timed_tests(array $test_data_array)
|
|
{
|
|
$now = time();
|
|
foreach ($test_data_array as $test_data) {
|
|
// look for unprocessed expired tests
|
|
if (($test_data["state"] === TEST_ONGOING) && ($test_data["time_limited"]) && ($test_data["end_limit_time"] < $now)) {
|
|
conclude_test($test_data["_id"]);
|
|
}
|
|
}
|
|
}
|
|
|
|
function update_test(array $test_data)
|
|
{
|
|
global $testdb;
|
|
$testdb->update($test_data);
|
|
}
|
|
|
|
function get_test(string $testid): array
|
|
{
|
|
global $testdb;
|
|
return $testdb->findById($testid);
|
|
}
|
|
|
|
function save_answer(string $testid, $chidx, $ansidx)
|
|
{
|
|
$test_data = get_test($testid);
|
|
$chidx = (int)$chidx;
|
|
if ((count($test_data) > 0) && ($test_data["state"] === TEST_ONGOING)) {
|
|
if ($chidx < count($test_data["challenges"])) {
|
|
$test_data["challenges"][$chidx]["player_answer"] = $ansidx;
|
|
update_test($test_data);
|
|
}
|
|
}
|
|
}
|
|
|
|
function get_concluded_tests(string $gameid, string $nickname) {
|
|
global $testdb;
|
|
$fetch_criteria = [["gameid", "=", (int)$gameid], "AND", ["nickname", "=", $nickname], "AND", ["state", "=", TEST_CONCLUDED]];
|
|
$test_data_array = $testdb->findBy($fetch_criteria);
|
|
return $test_data_array;
|
|
}
|
|
|
|
function conclude_test(string $testid) {
|
|
$test_data = get_test($testid);
|
|
if (count($test_data) === 0) {
|
|
return;
|
|
}
|
|
|
|
// load game data
|
|
//$game_data = get_game($test_data["gameid"]);
|
|
$game_challenges = load_challenges($test_data["gameid"]);
|
|
|
|
// check the answers
|
|
$challenge_n = count($test_data["challenges"]); // number of challenges
|
|
$cans_n = 0; // number of correct answers
|
|
for ($chidx = 0; $chidx < $challenge_n; $chidx++) {
|
|
// get challenge
|
|
$tch = &$test_data["challenges"][$chidx];
|
|
$gch = $game_challenges[$chidx];
|
|
|
|
// translate correct answer into an index by the shuffled answer order
|
|
$cans_idx = array_search($gch["correct_answer"], $tch["answers"]);
|
|
$tch["correct_answer"] = $cans_idx;
|
|
|
|
// check the player's answer
|
|
$player_answer = trim($tch["player_answer"]);
|
|
if (($player_answer !== "") && ($cans_idx === (int)$player_answer)) {
|
|
$cans_n++;
|
|
}
|
|
}
|
|
|
|
// set state and fill summary
|
|
$test_data["state"] = TEST_CONCLUDED;
|
|
$test_data["end_time"] = time();
|
|
$test_data["summary"] = [
|
|
"challenge_n" => $challenge_n,
|
|
"correct_answer_n" => $cans_n
|
|
];
|
|
|
|
update_test($test_data);
|
|
} |