286 lines
8.1 KiB
PHP
286 lines
8.1 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);
|
|
}
|
|
|
|
function split_criterion(string $crstr): array {
|
|
preg_match("/([<>=!]+|LIKE|NOT LIKE|IN|NOT IN|CONTAINS|NOT CONTAINS|BETWEEN|NOT BETWEEN|EXISTS)/", $crstr, $matches, PREG_OFFSET_CAPTURE);
|
|
|
|
// extract operator
|
|
$op = $matches[0][0];
|
|
$op_pos = $matches[0][1];
|
|
|
|
// extract operands
|
|
$left = trim(substr($crstr, 0, $op_pos));
|
|
$right = trim(substr($crstr, $op_pos + strlen($op), strlen($crstr)));
|
|
|
|
// automatic type conversion
|
|
if (($right[0] !== "\"") && ($right[0] !== "\'")) { // numeric value
|
|
$right = (int) $right;
|
|
} else { // string value
|
|
$right = substr($right, 1, strlen($right) - 2); // strip leading and trailing quotes
|
|
}
|
|
|
|
return [$left, $op, $right];
|
|
}
|
|
|
|
function build_query(string $filter): array
|
|
{
|
|
// skip empty filter processing
|
|
if (trim($filter) === "") {
|
|
return [];
|
|
}
|
|
|
|
// subfilters and operations
|
|
$subfilts = [];
|
|
$operations = [];
|
|
|
|
// buffer and scoring
|
|
$k = 0;
|
|
$k_prev = 0;
|
|
$buffer = "";
|
|
|
|
for ($i = 0; $i < strlen($filter); $i++) {
|
|
$c = $filter[$i];
|
|
|
|
// extract groups surrounded by parantheses
|
|
if ($c === "(") {
|
|
$k++;
|
|
} elseif ($c === ")") {
|
|
$k--;
|
|
} else {
|
|
$buffer .= $c;
|
|
}
|
|
|
|
// if k = 0, then we found a subfilter
|
|
if (($k === 0) && ($k_prev === 1)) {
|
|
$subfilts[] = trim($buffer);
|
|
$buffer = "";
|
|
} elseif (($k === 1) && ($k_prev === 0)) {
|
|
$op = trim($buffer);
|
|
if ($op !== "") {
|
|
$operations[] = $op;
|
|
}
|
|
$buffer = "";
|
|
}
|
|
|
|
// save k to be used next iteration
|
|
$k_prev = $k;
|
|
}
|
|
|
|
// decide, whether further expansion of condition is needed
|
|
$criteria = [];
|
|
for ($i = 0; $i < count($subfilts); $i++) {
|
|
$subfilt = $subfilts[$i];
|
|
|
|
// add subcriterion
|
|
if ($subfilt[0] === "(") {
|
|
$criteria[] = build_query($subfilt);
|
|
} else {
|
|
$criteria[] = split_criterion($subfilt);
|
|
}
|
|
|
|
// add operator
|
|
if (($i + 1) < count($subfilts)) {
|
|
$criteria[] = $operations[$i];
|
|
}
|
|
}
|
|
|
|
|
|
return $criteria;
|
|
}
|
|
|
|
function get_results_by_gameid(string $gameid, string $filter): array
|
|
{
|
|
global $testdb;
|
|
$qb = $testdb->createQueryBuilder();
|
|
$qb = $qb->where(["gameid", "=", (int)$gameid]);
|
|
|
|
if (trim($filter) !== "") {
|
|
$criteria = build_query($filter);
|
|
$qb->where($criteria);
|
|
}
|
|
|
|
$test_data_array = $qb->getQuery()->fetch();
|
|
return $test_data_array;
|
|
} |