Repositories | Polyback | Log in to see private repositories


polyback-website / db9a7ac7345e94204336a2a121c1338c5462f5f6 / api/games/shutdown.php

2,058 bytes | parent directory

<?php require $_SERVER['DOCUMENT_ROOT'].'/api/private/core.php';

use pizzaboxer\ProjectPolygon\Database;
use pizzaboxer\ProjectPolygon\Gameservers;
use pizzaboxer\ProjectPolygon\Games;
use pizzaboxer\ProjectPolygon\Users;
use pizzaboxer\ProjectPolygon\API;

API::initialize(["method" => "GET", "logged_in" => true, "secure" => true]);

$JobID = API::GetParameter("GET", "jobId", "string");
$JobInfo = Database::singleton()->run("SELECT * FROM GameJobs WHERE JobID = :JobID", [":JobID" => $JobID])->fetch(\PDO::FETCH_OBJ);

if (!$JobInfo) API::respond(200, false, "The requested game does not exist");

$ServerInfo = Database::singleton()->run("SELECT * FROM GameServers WHERE ServerID = :ServerID", [":ServerID" => $JobInfo->ServerID])->fetch(\PDO::FETCH_OBJ);
$PlaceInfo = Database::singleton()->run("SELECT * FROM assets WHERE id = :PlaceID", [":PlaceID" => $JobInfo->PlaceID])->fetch(\PDO::FETCH_OBJ);

// the place's creator, an administrator, or (a private server) the player who started it
$IsPrivateOwner = $JobInfo->Private && Games::GetJobOwner($JobInfo) == SESSION["user"]["id"];
if ($PlaceInfo->creator != SESSION["user"]["id"] && !$IsPrivateOwner && !Users::IsAdmin(Users::STAFF_ADMINISTRATOR)) API::respond(200, false, "The requested game cannot be shut down");
if ($JobInfo->Status == "Closed" || $JobInfo->Status == "Crashed") API::respond(200, false, "The requested game has already been shut down");
if ($JobInfo->Status != "Ready") API::respond(200, false, "The requested game cannot be shut down");

// polygon-gameserver stops the engine and reports the job ended; close it here too
if (!$ServerInfo || !Gameservers::StopJob($ServerInfo, $JobID))
{
	// the machine no longer knows the game (it already ended): just mark it closed
}
Database::singleton()->run("UPDATE GameJobs SET Status = 'Closed', LastUpdated = UNIX_TIMESTAMP() WHERE JobID = :j", [":j" => $JobID]);
Database::singleton()->run("UPDATE GameJobPlayers SET Active = 0 WHERE JobID = :j", [":j" => $JobID]);

API::respond(200, true, "The requested game has been shut down");