SpreadQuiz/gamemgr.php
2024-03-05 16:07:47 +01:00

186 lines
4.9 KiB
PHP

<?php
require_once "globals.php";
require_once "common_func.php";
$gamedb = new \SleekDB\Store(GAMEDB, DATADIR, ["timeout" => false]);
const DEFAULT_GAME_PROPERTIES = [
"forward_only" => false, // player may traverse back and forth between challenges
"time_limit" => -1, // no time limit; otherwise, this field indicates time limit in seconds
"repeatable" => false // this test can be taken multiple times
];
function create_game(string $name, string $owner, string $description = "", array $properties = DEFAULT_GAME_PROPERTIES, array $contributors = [], array $challenges = []): bool
{
global $testdb;
$game_data = [
"name" => $name,
"owner" => $owner,
"contributors" => $contributors,
"description" => $description,
"game_file_present" => false,
"properties" => $properties,
"groups" => [],
];
$game_data = $testdb->insert($game_data);
// prepare game context
$id = $game_data["_id"];
$current_game_media_dir = GAMEMEDIA_DIR . DIRECTORY_SEPARATOR . $id;
mkdir($current_game_media_dir);
save_challenges($id, []);
return true;
}
function get_game(string $gameid): array
{
global $testdb;
return $testdb->findById($gameid);
}
function update_game(array $game_data)
{
global $testdb;
$testdb->update($game_data);
}
function delete_game(string $gameid)
{
global $testdb;
$game_data = get_game($gameid);
if (count($game_data) != 0) {
foreach ($game_data["groups"] as $groupid) {
change_group_game_assignments($groupid, null, $gameid);
}
$testdb->deleteById($gameid);
}
}
function change_game_group_assignments(string $gid, $groupname_add, $groupname_remove)
{
$game_data = get_game($gid);
if (count($game_data) != 0) {
alter_array_contents($game_data["groups"], $groupname_add, $groupname_remove);
update_game($game_data); // update user
}
}
function get_all_games()
{
global $testdb;
return $testdb->findAll();
}
function get_all_game_data_by_contributor_nickname(string $nickname): array
{
global $testdb;
$game_headers = [];
if ($nickname !== "*") {
$game_data_array = $testdb->findBy([["owner", "=", $nickname], "OR", ["contributors", "CONTAINS", $nickname]]);
} else {
$game_data_array = $testdb->findAll();
}
foreach ($game_data_array as $game_data) {
$game_headers[] = $game_data;
}
return $game_headers;
}
function import_challenges_from_csv(string $csv_path, string $gameid)
{
$game_data = get_game($gameid);
if (count($game_data) === []) {
return;
}
$challenges = [];
// load filled CSV file
$f = fopen($csv_path, "r");
if (!$f) { // failed to open file
return;
}
while ($csvline = fgetcsv($f)) {
if (count($csvline) >= 3) {
$ch = [
"question" => $csvline[0],
"image_url" => $csvline[1],
"correct_answer" => $csvline[2],
"answers" => array_filter(array_slice($csvline, 2), function ($v) {
return trim($v) !== "";
})
];
$challenges[] = $ch;
}
}
fclose($f);
// save challenges
save_challenges($gameid, $challenges);
// update game with game file present
$game_data["game_file_present"] = true;
update_game($game_data);
}
function is_user_contributor_to_game(string $gameid, string $nickname): bool
{
$game_data = get_game($gameid);
if (count($game_data) === 0) {
return false;
}
return in_array($nickname, $game_data["contributors"]) || ($game_data["owner"] === $nickname);
}
function is_user_owner_of_the_game(string $gameid, string $nickname): bool
{
$game_data = get_game($gameid);
if (count($game_data) === 0) {
return false;
}
return $game_data["owner"] === $nickname;
}
function save_challenges(string $gameid, array $challenges)
{
file_put_contents(get_game_file_by_gameid($gameid), json_encode($challenges)); // store challenges in JSON-format
}
function load_challenges(string $gameid)
{
return json_decode(file_get_contents(get_game_file_by_gameid($gameid)), true);
}
function export_challenges_to_csv($f, string $gameid)
{
$game_data = get_game($gameid);
if ((count($game_data) === []) || (!$game_data["game_file_present"])) {
return;
}
// load challenges
$challenges = load_challenges($gameid);
// populate CSV file
foreach ($challenges as $ch) {
$csvline = [
$ch["question"],
$ch["image_url"],
];
$csvline = array_merge($csvline, $ch["answers"]);
fputcsv($f, $csvline);
}
}
function get_contributors(string $gameid): array
{
$game_data = get_game($gameid);
return $game_data["contributors"];
}
function get_game_file_by_gameid(string $gameid)
{
return GAMEMEDIA_DIR . DIRECTORY_SEPARATOR . $gameid . DIRECTORY_SEPARATOR . GAME_FILE;
}