From defd075b7dd56e1ded6fa966961d43cd72d47e0b Mon Sep 17 00:00:00 2001 From: Gnieark Date: Thu, 7 Jul 2016 20:18:05 +0200 Subject: [PATCH] curl multi --- countBattles.txt | 2 +- src/arenas/tron/act.php | 4 +- src/arenas/tron/class.TronGame.php | 108 ++++++++++++++++++++++++++++- src/functions.php | 54 --------------- 4 files changed, 109 insertions(+), 59 deletions(-) diff --git a/countBattles.txt b/countBattles.txt index 6634f71..bcddd5d 100644 --- a/countBattles.txt +++ b/countBattles.txt @@ -1 +1 @@ -1486 \ No newline at end of file +1494 \ No newline at end of file diff --git a/src/arenas/tron/act.php b/src/arenas/tron/act.php index 5081d38..82c1037 100644 --- a/src/arenas/tron/act.php +++ b/src/arenas/tron/act.php @@ -55,7 +55,7 @@ switch ($_POST['act']){ die; } $game->new_lap(); - + /* //make the board array for ($botCount = 0; $botCount < count($bots); $botCount ++){ $board[$botCount] = $bots[$botCount]->getTail(); @@ -110,7 +110,7 @@ switch ($_POST['act']){ print_r($targets); - + */ die; break; default: diff --git a/src/arenas/tron/class.TronGame.php b/src/arenas/tron/class.TronGame.php index 818a025..c62b215 100644 --- a/src/arenas/tron/class.TronGame.php +++ b/src/arenas/tron/class.TronGame.php @@ -1,5 +1,6 @@ gameId; } + + private function getBoard(){ + $board = array(); + $nbeBots = count($this->bots); + for ($botCount = 0; $botCount < $nbeBots; $botCount++){ + $board[] = $this->bots[$botCount]->getTail(); + } + return $board; + } + + private function get_multi_IAS_Responses($iasUrls, $postParams){ + //same as the get_IAS_Responses 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($iasUrls[$i]); + 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 == "") { + $response = curl_multi_getcontent($ch[$i]); + if(! $arr = json_decode($response,TRUE)){ + $arr=array(); + } + $res[$i] = array( + 'messageSend' => json_encode($postParams[$i]), + 'response' => $response, + 'httpStatus' => curl_getinfo($ch[$i])['http_code'], + 'responseArr' => $arr + ); + + }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($cmh); + return $res; + } + public function get_continue(){ //count bots alive. if less than 1, game is ended $count = 0; @@ -31,10 +107,36 @@ class TronGame{ return false; } } + public function new_lap(){ + // for all alive bots + $logs = ""; + $nbeBots = count($this->bots); + $urls = array(); + $paramToSend = array(); + + + for ($botCount = 0; $botCount < $nbeBots; $botCount++){ + if ($this->bots[$botCount]->getStatus()){ + $urls[$botCount] = $this->bots[$botCount]->getURL(); + $paramsToSend[$botCount] = array( + 'game-id' => "".$this->gameId, + 'action' => 'play-turn', + 'game' => 'tron', + 'board' => $this->getBoard(), + 'player-index' => $botCount, // To do: verifier que ça restera le même à chaque tour + 'players' => $nbeBots + ); + + } + } + + $responses = $this->get_multi_IAS_Responses($urls,$paramsToSend); + print_r($responses); + - } + public function init_game(){ //send init messages to bots $logs = ""; @@ -62,6 +164,7 @@ class TronGame{ return $logs; } + private function getBusyCells(){ $arr=array(); foreach($this->bots as $bot){ @@ -69,6 +172,7 @@ class TronGame{ } return $arr; } + public function __construct($botsIds){ $this->gameId = get_unique_id(); diff --git a/src/functions.php b/src/functions.php index 04c13bb..3e9e3b1 100644 --- a/src/functions.php +++ b/src/functions.php @@ -382,57 +382,3 @@ function get_IA_Response($iaUrl,$postParams){ 'responseArr' => $arr ); } -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; -} \ No newline at end of file