Repositories | Polyback | Log in to see private repositories


polyback-website / db9a7ac7345e94204336a2a121c1338c5462f5f6 / api/gameserver/players.php

1,486 bytes | parent directory

<?php require $_SERVER['DOCUMENT_ROOT'].'/api/private/core.php';
// The game reports its player count: {"jobId":"...","playerCount":3}, and optionally who
// is playing: "userIds":[1,2,3] (then the game's player list is exactly those).
// Without userIds the list can only be cleared when the game is empty; players also drop
// off it when they join another game or the game ends.
use pizzaboxer\ProjectPolygon\Database;
use pizzaboxer\ProjectPolygon\Gameservers;

Gameservers::RequireAuthorized();
$body = Gameservers::Body();
$jobId = substr((string) ($body["jobId"] ?? ""), 0, 36);
$count = max(0, (int) ($body["playerCount"] ?? 0));

Database::singleton()->run(
	"UPDATE GameJobs SET PlayerCount = :c, LastUpdated = UNIX_TIMESTAMP() WHERE JobID = :j AND Status = 'Ready'",
	[":c" => $count, ":j" => $jobId]
);

if (isset($body["userIds"]) && is_array($body["userIds"]))
{
	$ids = array_values(array_unique(array_filter(array_map("intval", array_slice($body["userIds"], 0, 200)), fn($id) => $id > 0)));
	if (count($ids))
	{
		$in = implode(",", $ids); // integers only
		Database::singleton()->run("UPDATE GameJobPlayers SET Active = (UserID IN ($in)) WHERE JobID = :j", [":j" => $jobId]);
	}
	else
	{
		Database::singleton()->run("UPDATE GameJobPlayers SET Active = 0 WHERE JobID = :j", [":j" => $jobId]);
	}
}
else if ($count == 0)
{
	Database::singleton()->run("UPDATE GameJobPlayers SET Active = 0 WHERE JobID = :j", [":j" => $jobId]);
}

die(json_encode(["success" => true]));