Game impl update

This commit is contained in:
Astrologies 2021-12-21 18:39:26 -05:00
parent 4f59856286
commit 763d7d1a8b
4 changed files with 61 additions and 72 deletions

View File

@ -254,5 +254,62 @@ namespace Alphaland\Games {
return "Banned";
}
}
public static function RemovePlayerFromQueue(int $userid)
{
$removeQueue = $GLOBALS['pdo']->prepare("DELETE FROM game_launch_queue WHERE userid = :uid");
$removeQueue->bindParam(":uid", $userid, PDO::PARAM_INT);
$removeQueue->execute();
if ($removeQueue->rowCount() > 0) {
return true;
}
return false;
}
public static function IsPlayerInQueue(int $placeid, string $jobid, int $userid)
{
$playerinqueue = $GLOBALS['pdo']->prepare("SELECT COUNT(*) FROM game_launch_queue WHERE placeid = :pid AND jobid = :jid AND userid = :uid");
$playerinqueue->bindParam(":pid", $placeid, PDO::PARAM_INT);
$playerinqueue->bindParam(":jid", $jobid, PDO::PARAM_STR);
$playerinqueue->bindParam(":uid", $userid, PDO::PARAM_INT);
$playerinqueue->execute();
if ($playerinqueue->fetchColumn() > 0) {
return true;
}
return false;
}
public static function AddPlayerToQueue(int $placeid, string $jobid, int $userid)
{
if (!Game::IsPlayerInQueue($placeid, $jobid, $userid)) {
Game::RemovePlayerFromQueue($userid); //if any queue leftover
$newQueue = $GLOBALS['pdo']->prepare("INSERT INTO game_launch_queue(placeid, jobid, userid, queuePing, whenQueued) VALUES (:pid, :jid, :uid, UNIX_TIMESTAMP(), UNIX_TIMESTAMP())");
$newQueue->bindParam(":pid", $placeid, PDO::PARAM_INT);
$newQueue->bindParam(":jid", $jobid, PDO::PARAM_STR);
$newQueue->bindParam(":uid", $userid, PDO::PARAM_INT);
$newQueue->execute();
} else { //ping
$updateQueue = $GLOBALS['pdo']->prepare("UPDATE game_launch_queue SET queuePing = UNIX_TIMESTAMP() WHERE placeid = :pid AND jobid = :jid AND userid = :uid");
$updateQueue->bindParam(":pid", $placeid, PDO::PARAM_INT);
$updateQueue->bindParam(":jid", $jobid, PDO::PARAM_STR);
$updateQueue->bindParam(":uid", $userid, PDO::PARAM_INT);
$updateQueue->execute();
}
}
public static function IsNextInQueue($placeid, $jobid, $userid)
{
$queue = $GLOBALS['pdo']->prepare("SELECT * FROM game_launch_queue WHERE placeid = :pid AND jobid = :jid ORDER BY whenQueued DESC LIMIT 1");
$queue->bindParam(":pid", $placeid, PDO::PARAM_INT);
$queue->bindParam(":jid", $jobid, PDO::PARAM_STR);
$queue->execute();
$queue = $queue->fetch(PDO::FETCH_OBJ);
if ((int)$queue->queuePing + 10 < time()) { //hasnt pinged in 10 seconds, assume they left queue
Game::RemovePlayerFromQueue($queue->userid);
} else if ($queue->userid == $userid) {
return true;
}
return false;
}
}
}

View File

@ -200,75 +200,6 @@ function genTicket()
//TODO: Render Queue?
//place launcher queue system
function removePlayerFromQueue($userid)
{
$removeQueue = $GLOBALS['pdo']->prepare("DELETE FROM game_launch_queue WHERE userid = :uid");
$removeQueue->bindParam(":uid", $userid, PDO::PARAM_INT);
$removeQueue->execute();
if ($removeQueue->rowCount() > 0)
{
return true;
}
return false;
}
function playerInQueue($placeid, $jobid, $userid)
{
$playerinqueue = $GLOBALS['pdo']->prepare("SELECT * FROM game_launch_queue WHERE placeid = :pid AND jobid = :jid AND userid = :uid");
$playerinqueue->bindParam(":pid", $placeid, PDO::PARAM_INT);
$playerinqueue->bindParam(":jid", $jobid, PDO::PARAM_STR);
$playerinqueue->bindParam(":uid", $userid, PDO::PARAM_INT);
$playerinqueue->execute();
if ($playerinqueue->rowCount() > 0)
{
return true;
}
return false;
}
function addPlayerToQueue($placeid, $jobid, $userid)
{
if (!playerInQueue($placeid, $jobid, $userid))
{
removePlayerFromQueue($userid); //if any queue leftover
$newQueue = $GLOBALS['pdo']->prepare("INSERT INTO game_launch_queue(placeid, jobid, userid, queuePing, whenQueued) VALUES (:pid, :jid, :uid, UNIX_TIMESTAMP(), UNIX_TIMESTAMP())");
$newQueue->bindParam(":pid", $placeid, PDO::PARAM_INT);
$newQueue->bindParam(":jid", $jobid, PDO::PARAM_STR);
$newQueue->bindParam(":uid", $userid, PDO::PARAM_INT);
$newQueue->execute();
}
else //ping
{
$updateQueue = $GLOBALS['pdo']->prepare("UPDATE game_launch_queue SET queuePing = UNIX_TIMESTAMP() WHERE placeid = :pid AND jobid = :jid AND userid = :uid");
$updateQueue->bindParam(":pid", $placeid, PDO::PARAM_INT);
$updateQueue->bindParam(":jid", $jobid, PDO::PARAM_STR);
$updateQueue->bindParam(":uid", $userid, PDO::PARAM_INT);
$updateQueue->execute();
}
}
function isNextInQueue($placeid, $jobid, $userid)
{
$queue = $GLOBALS['pdo']->prepare("SELECT * FROM game_launch_queue WHERE placeid = :pid AND jobid = :jid ORDER BY whenQueued DESC LIMIT 1");
$queue->bindParam(":pid", $placeid, PDO::PARAM_INT);
$queue->bindParam(":jid", $jobid, PDO::PARAM_STR);
$queue->execute();
$queue = $queue->fetch(PDO::FETCH_OBJ);
if ((int)$queue->queuePing + 10 < time()) //hasnt pinged in 10 seconds, assume they left queue
{
removePlayerFromQueue($queue->userid);
}
else if ($queue->userid == $userid)
{
return true;
}
return false;
}
// ...
//filter shit
function getWordList()

View File

@ -2,6 +2,7 @@
//the design choice here was to tie in clientpresence with recently played and visits and make it fully server-sided besides the client pings
use Alphaland\Games\Game;
use Alphaland\Web\WebContextManager;
if (!WebContextManager::VerifyAccessKeyHeader())
@ -70,7 +71,7 @@ else if ($action == "connect")
// ...
//remove them from queue (once presence is created above, placelauncher will detect playercount correctly. should be very rare for two people to get in a single slot)
removePlayerFromQueue($userid);
Game::RemovePlayerFromQueue($userid);
//update or create player recently played (TODO: restrict to 4 database entries to save space)
$checkforrecent = $pdo->prepare("SELECT * FROM game_recents WHERE uid = :i AND gid = :g");

View File

@ -154,8 +154,8 @@ if ($requesttype == "RequestGame") //start new server or join existing one
}
elseif($sInfo->status == 1) //game is open, check if its joinable (player count, queue, etc)
{
addPlayerToQueue($gameID, $sInfo->jobid, $user->id); //add player to queue (if they are in it, this updates ping)
if (isNextInQueue($gameID, $sInfo->jobid, $user->id)) //player next in queue
Game::AddPlayerToQueue($gameID, $sInfo->jobid, $user->id); //add player to queue (if they are in it, this updates ping)
if (Game::IsNextInQueue($gameID, $sInfo->jobid, $user->id)) //player next in queue
{
if (Game::JobPlayerCount($gameID, $sInfo->jobid) >= $gInfo->MaxPlayers)
{