diff --git a/globals/Dependencies/Games/Game.php b/globals/Dependencies/Games/Game.php index e90ad68..b0aec78 100644 --- a/globals/Dependencies/Games/Game.php +++ b/globals/Dependencies/Games/Game.php @@ -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; + } } } diff --git a/globals/functions.php b/globals/functions.php index 8fc24c6..8464e93 100644 --- a/globals/functions.php +++ b/globals/functions.php @@ -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() diff --git a/html/Game/ClientPresence.php b/html/Game/ClientPresence.php index fc3f12b..80513e6 100644 --- a/html/Game/ClientPresence.php +++ b/html/Game/ClientPresence.php @@ -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"); diff --git a/html/Game/PlaceLauncher.php b/html/Game/PlaceLauncher.php index a9248ec..b542ad8 100644 --- a/html/Game/PlaceLauncher.php +++ b/html/Game/PlaceLauncher.php @@ -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) {