From a31cf5e3ffb11d7b0d917bc2666c09ccc66d0192 Mon Sep 17 00:00:00 2001 From: Gnieark Date: Thu, 7 Jul 2016 08:02:29 +0200 Subject: [PATCH] curl multi --- countBattles.txt | 2 +- src/arenas/tron/act.php | 9 +++-- src/arenas/tron/class.TronGame.php | 4 +++ src/arenas/tron/functions.php | 3 +- src/functions.php | 58 +++++++++++++++++++++++++++++- 5 files changed, 67 insertions(+), 9 deletions(-) diff --git a/countBattles.txt b/countBattles.txt index ce4798b..6634f71 100644 --- a/countBattles.txt +++ b/countBattles.txt @@ -1 +1 @@ -1483 \ No newline at end of file +1486 \ No newline at end of file diff --git a/src/arenas/tron/act.php b/src/arenas/tron/act.php index 1bb4b4c..5081d38 100644 --- a/src/arenas/tron/act.php +++ b/src/arenas/tron/act.php @@ -11,7 +11,8 @@ # -- END LICENSE BLOCK ----------------------------------------- require_once(__DIR__."/functions.php"); - +require_once ("class.TronGame.php"); +require_once ("class.TronPlayer.php"); switch ($_POST['act']){ case "initGame": @@ -41,21 +42,19 @@ switch ($_POST['act']){ break; case "play": $logs = ""; - //check for correct game ID - if(!isset($_SESSION['game'])){ echo '{"status":"error"}'; die; } - $game= unserialize($_SESSION['game']); + $game = unserialize($_SESSION['game']); if($game->getGameId() <> $_POST['gameId']){ //sometimes if an ajax callback is applied after init an other game echo '{"status":"error"}'; die; } - $game()->newLap(); + $game->new_lap(); //make the board array for ($botCount = 0; $botCount < count($bots); $botCount ++){ diff --git a/src/arenas/tron/class.TronGame.php b/src/arenas/tron/class.TronGame.php index b756986..818a025 100644 --- a/src/arenas/tron/class.TronGame.php +++ b/src/arenas/tron/class.TronGame.php @@ -30,6 +30,10 @@ class TronGame{ }else{ return false; } + } + public function new_lap(){ + + } public function init_game(){ //send init messages to bots diff --git a/src/arenas/tron/functions.php b/src/arenas/tron/functions.php index d36c912..9285281 100644 --- a/src/arenas/tron/functions.php +++ b/src/arenas/tron/functions.php @@ -1,6 +1,5 @@ $output, 'responseArr' => $arr ); -} \ No newline at end of file +} +function get_multi_IAS_Responses($iasUrls, $postParams){ + //same as the previous function + // but more than one bot requested parallely + + $cmh = curl_multi_init(); + for ($i = 0; $i < count($iasUrls); $i++){ + $data_string = json_encode($postParams[$i]); + + $ch[$i] = curl_init($iaUrl); + curl_setopt($ch[$i], CURLOPT_CUSTOMREQUEST, "POST"); + curl_setopt($ch[$i], CURLOPT_SSL_VERIFYHOST, false); + curl_setopt($ch[$i], CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch[$i], CURLOPT_POSTFIELDS, $data_string); + curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch[$i], CURLOPT_HTTPHEADER, array( + 'Content-Type: application/json', + 'Content-Length: ' . strlen($data_string)) + ); + curl_multi_add_handle($cmh,$ch[$i]); + } + //send the requests + do { + $returnVal = curl_multi_exec($cmh, $runningHandles); + } while ($returnVal == CURLM_CALL_MULTI_PERFORM); + // Loop and continue processing the request + while ($runningHandles && $returnVal== CURLM_OK) { + // Wait forever for network + $numberReady = curl_multi_select($cmh); + if ($numberReady != -1) { + // Pull in any new data, or at least handle timeouts + do { + $returnVal = curl_multi_exec($cmh, $runningHandles); + } while ($returnVal == CURLM_CALL_MULTI_PERFORM); + } + } + + //Get results + for ($i = 0; $i < count($iasUrls); $i++) + { + // Check for errors + $curlError = curl_error($ch[$i]); + if($curlError == "") { + $res[$i] = curl_multi_getcontent($ch[$i]); + } else { + $res[$i] = false + } + //close + curl_multi_remove_handle($cmh, $ch[$i]); + curl_close($ch[$i]); + } + // Clean up the curl_multi handle + curl_multi_close($mh); + return $res; +} + +