From 734e4d9a61c06eb494e273f8bcd80ebfc7ec8b21 Mon Sep 17 00:00:00 2001 From: Gnieark Date: Fri, 15 Jul 2016 07:46:44 +0200 Subject: [PATCH] . --- src/arenas/tron/{trail.php => Trail.php} | 4 + src/arenas/tron/TronGame.php | 383 +++++++++++++---------- src/arenas/tron/TronPlayer.php | 16 +- 3 files changed, 226 insertions(+), 177 deletions(-) rename src/arenas/tron/{trail.php => Trail.php} (92%) diff --git a/src/arenas/tron/trail.php b/src/arenas/tron/Trail.php similarity index 92% rename from src/arenas/tron/trail.php rename to src/arenas/tron/Trail.php index 874bb45..888366f 100644 --- a/src/arenas/tron/trail.php +++ b/src/arenas/tron/Trail.php @@ -12,6 +12,10 @@ class Trail { return $this->trail->top(); } + public function emptyTrail(){ + $this->trail = new SplStack(); + } + public function add($value) { if(!$this->trail->isEmpty()) { if(Trail::kind($this->trail->bottom()) !== Trail::kind($value)) { diff --git a/src/arenas/tron/TronGame.php b/src/arenas/tron/TronGame.php index bb1e7f7..58ed29f 100644 --- a/src/arenas/tron/TronGame.php +++ b/src/arenas/tron/TronGame.php @@ -4,147 +4,7 @@ class TronGame private $bots; //array of bots private $gameId; private $status; //false => Game ended or not initialised - - - -} -/* -class TronGame -{ - private $bots; - private $gameId; - public function getBotsPositions(){ - $nbeBots = count($this->bots); - $arr = array(); - for ($botCount = 0; $botCount < $nbeBots; $botCount++){ - $arr[$botCount] = array( - "name" => $this->bots[$botCount]->getName(), - "tail" => $this->bots[$botCount]->getTail() - - ); - } - return $arr; - } - - public function getGameId(){ - return $this->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 save_draw_bots($arr){ - * - * Recursive function who save all combionaisons of draw matches - * - - if(count($arr) < 2){ - return; - }else{ - $a = $arr[0]; - array_shift($arr); - foreach($arr as $bot){ - save_battle('tron',$a,$bot,0,'id'); - } - $this->save_draw_bots($arr); - } - } - - private function save_losers_winers($arrLoosers,$arrWiners){ - foreach($arrWiners as $winner){ - foreach($arrLoosers as $loser){ - save_battle('tron',$winer,$loser,1,'id'); - } - } - - } - - 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; - foreach($this->bots as $bot){ - if( $bot->getStatus() == true){ - $count++; - } - } - if($count > 1){ - return true; - }else{ - return false; - } - } - public function new_lap(){ // for all alive bots $logs = ""; @@ -163,12 +23,14 @@ class TronGame 'game' => 'tron', 'board' => $board, 'player-index' => $botCount, // To do: verifier que ça restera le même à chaque tour - 'players' => $nbeBots + 'players' => $nbeBots ); } } $responses = $this->get_multi_IAS_Responses($urls,$paramsToSend); + + //print_r($responses); $targetsList = array(); $busyCells = $this->getBusyCells(); @@ -237,9 +99,219 @@ class TronGame } } - return $arrRapport; + return $arrRapport; } + + + 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 init_game(){ + //send init messages to bots + $logs = ""; + $fullLogs = ""; + $nbeBots = count($this->bots); + for ($botCount = 0; $botCount < $nbeBots; $botCount++){ + + $messageArr = array( + 'game-id' => "".$this->gameId, + 'action' => 'init', + 'game' => 'tron', + 'board' => '', + 'players' => $nbeBots, + 'player-index' => $botCount + ); + + $resp = get_IA_Response($this->bots[$botCount]->getURL(),$messageArr); + $fullLogs .= 'Arena send to '.$bots[$botCount]->getName().''.htmlentities($resp['messageSend']).'
+ HTTP status: '.htmlentities($resp['httpStatus']).'
+ Bot anwser: '.htmlentities($resp['response']).'
'; + $logs.="Init message send to ".$this->bots[$botCount]->getName()."
"; + } + return array($logs,$fullLogs); + } + + + + public function __construct($botsInfos){ + /* + * $botsInfo like: + * $botsInfo = array( + array( + 'id' => + 'name' => + 'url' => + ), + array( + 'id' => + 'name' => + 'url' => + ) + ) + */ + + + $this->gameId = get_unique_id(); + $this->bots = array(); + $positions = array(); + $botCount = 0; + $err = ""; + foreach($botsInfo as $bot){ + //find a random start position + do{ + $x = rand(1,999); + $y = rand(1,999); + }while(in_array($x.",".$y,$positions)); + + $positions[] = $x.",".$y; + $startCoord = new Coords($x,$y); + + $this->bots[$botCount] = new TronPlayer($bot['id'],$startCoord,$bot['name'],$bot['url']); + + if ($this->bots[$botCount]->getStatus() === false){ + $err .= "Something went wrong for ".$this->bots[$botCount]->getName()."
"; + }else{ + $botCount++; + } + } + return $err; + } + +} +/* +class TronGame +{ + private $bots; + private $gameId; + public function getBotsPositions(){ + $nbeBots = count($this->bots); + $arr = array(); + for ($botCount = 0; $botCount < $nbeBots; $botCount++){ + $arr[$botCount] = array( + "name" => $this->bots[$botCount]->getName(), + "tail" => $this->bots[$botCount]->getTail() + + ); + } + return $arr; + } + + public function getGameId(){ + return $this->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 save_draw_bots($arr){ + * + * Recursive function who save all combionaisons of draw matches + * + + if(count($arr) < 2){ + return; + }else{ + $a = $arr[0]; + array_shift($arr); + foreach($arr as $bot){ + save_battle('tron',$a,$bot,0,'id'); + } + $this->save_draw_bots($arr); + } + } + + private function save_losers_winers($arrLoosers,$arrWiners){ + foreach($arrWiners as $winner){ + foreach($arrLoosers as $loser){ + save_battle('tron',$winer,$loser,1,'id'); + } + } + + } + + public function get_continue(){ + //count bots alive. if less than 1, game is ended + $count = 0; + foreach($this->bots as $bot){ + if( $bot->getStatus() == true){ + $count++; + } + } + if($count > 1){ + return true; + }else{ + return false; + } + } + public function init_game(){ //send init messages to bots @@ -277,31 +349,6 @@ class TronGame return $arr; } - public function __construct($botsIds){ - - $this->gameId = get_unique_id(); - $this->bots = array(); - $positions = array(); - $botCount = 0; - $err = ""; - foreach($botsIds as $botId){ - //find a random start position - do{ - $x = rand(1,999); - $y = rand(1,999); - }while(in_array($x.",".$y,$positions)); - - $positions[] = $x.",".$y; - $this->bots[$botCount] = new TronPlayer($botId,$x,$y,'y+'); - - if ($this->bots[$botCount]->getStatus() === false){ - - $err = "Something went wrong for ".$this->bots[$botCount]->getName()."
"; - }else{ - $botCount++; - } - } - return $err; - } + } */ \ No newline at end of file diff --git a/src/arenas/tron/TronPlayer.php b/src/arenas/tron/TronPlayer.php index 8bee942..22cdab4 100644 --- a/src/arenas/tron/TronPlayer.php +++ b/src/arenas/tron/TronPlayer.php @@ -10,25 +10,24 @@ class TronPlayer{ public function grow(Direction $dir){ - //$targetCell = $this->getTargetCell($dir); - //$this->tail[] = $targetCell; - //return $targetCell; - $this->trail->grow($dir); + $this->trail->add($this->trail->last()->addDirection($dir)); } public function loose(){ $this->isAlive = false; - $this->tail->empty_tail(); + $this->trail->emptyTrail(); return false; } public function __make($botId, Coords $initialsCoords,$name,$url){ $this->id = $botId; - $this->tail = Trail::make($initialsCoords); + $this->trail = new Trail; + $this->trail->add($initialsCoords); $this->name = $name; $this->url = $url; + $this->state = true; } public function __construct(){ - $this->state = true; + $this->state = false; } /* public function __construct($id,$initialX,$initialY,$initialDirection){ @@ -48,5 +47,4 @@ class TronPlayer{ } } */ -} -?> \ No newline at end of file +} \ No newline at end of file