SpreadQuiz/class/GameMgr.php
2025-09-30 22:44:06 +02:00

144 lines
4.1 KiB
PHP

<?php
require_once "vendor/autoload.php";
require_once "AutoStoring.php";
require_once "Game.php";
class GameMgr
{
private \SleekDB\Store $db; // game database
// --------
function __construct()
{
$this->db = new \SleekDB\Store(GAMEDB, DATADIR, ["timeout" => false]);
}
// Get game by ID.
function getGame(string $gameid): Game|null
{
$game_data_array = $this->db->findById($gameid);
return count($game_data_array) != 0 ? Game::fromArray($this, $game_data_array) : null;
}
// Get public game. FIXME!!!
function getPublicGame(string $public_id): Game|null
{
$game_data_array = $this->db->findBy([["public", "=", "true"], "AND", ["public_id", "=", $public_id]]);
return count($game_data_array) != 0 ? Game::fromArray($this, $game_data_array[0]) : null;
}
// Update game.
function updateGame(Game $game): void
{
$a = $game->toArray();
$this->db->update($a);
}
function addGame(string $name, string $owner, string $description, array $properties = Game::DEFAULT_GAME_PROPERTIES,
array $contributors = [], array $tasks = []): bool
{
$game_data = [
"name" => $name,
"owner" => $owner,
"contributors" => $contributors,
"description" => $description,
"game_file_present" => false,
"properties" => $properties,
"public" => false,
"public_id" => Game::genPublicId(),
"version" => Game::CURRENT_GAME_VERSION
];
$game_data = $this->db->insert($game_data);
// prepare game context
$game = Game::fromArray($this, $game_data);
$current_game_media_dir = $game->getGameDir();
mkdir($current_game_media_dir);
$game->saveTasks();
return true;
}
// Delete game by ID.
function deleteGame(string $gameid): void
{
$this->db->deleteById($gameid);
}
// Get all game data by contributor nickname.
function getAllGameDataByContributor(string $nickname): array {
$games = [];
if ($nickname !== "*") {
$game_data_array = $this->db->findBy([["owner", "=", $nickname], "OR", ["contributors", "CONTAINS", $nickname]]);
} else {
$game_data_array = $this->db->findAll();
}
foreach ($game_data_array as $game_data) {
$games[] = Game::fromArray($this, $game_data);
}
return $games;
}
// Get all games.
function getAllGames() : array {
$gamesa = $this->db->findAll();
$games = [];
foreach ($gamesa as $a) {
$games[] = new Game($this, $a);
}
return $games;
}
// Sanitize games and return game IDs.
function sanitizeGames(array $games) : array {
$sanitized = [];
foreach ($games as $game) {
// explode game identifier
[$name, $id] = explode("#", $game);
// fetch game
if ($id !== null) {
$records = $this->db->findBy([["name", "=", $name], "AND", ["_id", "=", (int)$id]]);
} else {
$records = $this->db->findBy(["name", "=", $name]);
}
// put game ID into sanitized list only if identifier is not ambiguous
if (count($records) === 1) {
$sanitized[] = $records[0]["_id"];
}
}
return $sanitized;
}
// Resolve group IDs to full group identifiers.
function resolveGames(array &$gameIds): void {
$a = $this->db->findBy([["_id", "IN", $gameIds]]); // no caching here...
$gameIds = array_map(fn($r) => $r["name"] . "#" . $r["_id"] ,$a);
}
function upgradeGames(array $ids = []): void
{
$a = [];
if ($ids === []) {
$a = $this->db->findAll();
} else {
$a = $this->db->findBy(["_id", "IN", $ids]);
}
foreach ($a as $g) {
$game = Game::fromArray($this, $g);
$game->loadTasks();
$game->saveTasks();
$game->storeMods();
}
}
// -------
}