diff --git a/countBattles.txt b/countBattles.txt index fc0a244..d10f97d 100644 --- a/countBattles.txt +++ b/countBattles.txt @@ -1 +1 @@ -1515 \ No newline at end of file +1549 \ No newline at end of file diff --git a/src/arenas/tron/Trail.php b/src/arenas/tron/Trail.php index de6dd7d..c740b36 100644 --- a/src/arenas/tron/Trail.php +++ b/src/arenas/tron/Trail.php @@ -34,8 +34,11 @@ class Trail { $this->trail->push($value); } public function __toString(){ - $str = ""; - return implode( + $arr = ""; + foreach($this->trail as $value) { + $arr[] = $value; + } + return json_encode($arr); } diff --git a/src/arenas/tron/TronGame.php b/src/arenas/tron/TronGame.php index e71d3eb..57f5de8 100644 --- a/src/arenas/tron/TronGame.php +++ b/src/arenas/tron/TronGame.php @@ -2,11 +2,25 @@ class TronGame { private $bots; //array of bots - private $gameId; + public $gameId; private $status; //false => Game ended or not initialised - private function apply_looses($loosersArr){ - + public function get_continue(){ + //count bots alive. if less than 1, game is ended + $count = 0; + foreach($this->bots as $bot){ + if( $bot->isAlive == true){ + $count++; + } + } + if($count > 1){ + return true; + }else{ + return false; + } + } + + private function apply_looses($loosersArr){ //save draws if( count($loosersArr) > 1 ){ $loosersById = array(); @@ -55,6 +69,14 @@ class TronGame } + public function get_trails(){ + //return all trails for draw svg + $trailsArr = array(); + foreach($this->bots as $bot){ + $trailsArr[] = $bot->trail; + } + return json_encode($trailsArr); + } public function new_lap(){ // for all alive bots $logs = ""; @@ -82,9 +104,8 @@ class TronGame } $responses = $this->get_multi_IAS_Responses($urls,$paramsToSend); - //$responses[$botCount]['responseArr']['play'] - - //grow bots'tails + + //grow bots'tails for ($botCount = 0; $botCount < $nbeBots; $botCount++){ if ($this->bots[$botCount]->getStatus()){ $lastsCells[$botCount] = $this->bots[$botCount]->grow($responses[$botCount]['responseArr']['play']); @@ -103,7 +124,7 @@ class TronGame } } - //return all trails for draw svg + return $this->get_trails(); } @@ -188,40 +209,24 @@ class TronGame 'player-index' => $botCount ); - $resp = get_IA_Response($this->bots[$botCount]->getURL(),$messageArr); - $fullLogs .= 'Arena send to '.$bots[$botCount]->getName().''.htmlentities($resp['messageSend']).'
+ $resp = get_IA_Response($this->bots[$botCount]->url,$messageArr); + $fullLogs .= 'Arena send to '.$this->bots[$botCount]->name.''.htmlentities($resp['messageSend']).'
HTTP status: '.htmlentities($resp['httpStatus']).'
Bot anwser: '.htmlentities($resp['response']).'
'; - $logs.="Init message send to ".$this->bots[$botCount]->getName()."
"; + $logs.="Init message send to ".$this->bots[$botCount]->name."
"; } return array($logs,$fullLogs); } - - - - public function __construct($botsInfos){ - /* - * $botsInfo like: - * $botsInfo = array( - array( - 'id' => - 'name' => - 'url' => - ), - array( - 'id' => - 'name' => - 'url' => - ) - ) - */ - + public function __construct($botsInfos){ $this->gameId = get_unique_id(); $this->bots = array(); $positions = array(); $botCount = 0; $err = ""; + + //print_r($botsInfos); + foreach($botsInfos as $bot){ //find a random start position do{ @@ -231,10 +236,11 @@ class TronGame $positions[] = $x.",".$y; $startCoord = new Coords($x,$y); - - $this->bots[$botCount] = new TronPlayer($bot['id'],$startCoord,$bot['name'],$bot['url']); + + $this->bots[$botCount] = new TronPlayer(); + $this->bots[$botCount]->make($bot['id'],$startCoord,$bot['name'],$bot['url']); - if ($this->bots[$botCount]->getStatus() === false){ + if ($this->bots[$botCount]->isAlive === false){ $err .= "Something went wrong for ".$this->bots[$botCount]->getName()."
"; }else{ $botCount++; @@ -244,115 +250,3 @@ class TronGame } } -/* -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 - $logs = ""; - $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); - - if($_POST['fullLogs'] == "true"){ - $logs.='Arena send to '.$bots[$botCount]->getName().''.htmlentities($resp['messageSend']).'
- HTTP status: '.htmlentities($resp['httpStatus']).'
- Bot anwser: '.htmlentities($resp['response']).'
'; - }else{ - $logs.="Init message send to ".$this->bots[$botCount]->getName()."
"; - } - } - - return $logs; - } - - private function getBusyCells(){ - $arr=array(); - foreach($this->bots as $bot){ - $arr = array_merge($arr,$bot->getTail()); - } - return $arr; - } - - -} -*/ \ No newline at end of file diff --git a/src/arenas/tron/TronPlayer.php b/src/arenas/tron/TronPlayer.php index 2a2fed6..ba8c357 100644 --- a/src/arenas/tron/TronPlayer.php +++ b/src/arenas/tron/TronPlayer.php @@ -1,14 +1,13 @@ trail->add($this->trail->last()->addDirection($dir)); return $this->trail->last(); @@ -19,7 +18,7 @@ class TronPlayer{ $this->trail->emptyTrail(); return false; } - public function __make($botId, Coords $initialsCoords,$name,$url){ + public function make($botId, Coords $initialsCoords,$name,$url){ $this->id = $botId; $this->trail = new Trail; $this->trail->add($initialsCoords); @@ -30,22 +29,5 @@ class TronPlayer{ public function __construct(){ $this->state = false; } - /* - public function __construct($id,$initialX,$initialY,$initialDirection){ - $lnBdd = conn_bdd(); - $rs = mysqli_query($lnBdd, - "SELECT name,url FROM bots WHERE game='tron' AND id='".mysqli_real_escape_string($lnBdd, $id)."';" - ); - if(($r=mysqli_fetch_row($rs)) && in_array($initialDirection,array('x-','x+','y-','y+'))){ - $this->id = $id; - $this->name = $r[0]; - $this->url = $r[1]; - $this->tail = array(array($initialX,$initialY)); - $this->direction = $initialDirection; - $this->state= true; - }else{ - $this->state = false; - } - } - */ + } \ No newline at end of file diff --git a/src/arenas/tron/act.php b/src/arenas/tron/act.php index 6adbdeb..2bec0ac 100644 --- a/src/arenas/tron/act.php +++ b/src/arenas/tron/act.php @@ -10,7 +10,6 @@ # # -- END LICENSE BLOCK ----------------------------------------- -require_once(__DIR__."/functions.php"); require_once ("TronGame.php"); require_once ("TronPlayer.php"); require_once ("Direction.php"); @@ -20,36 +19,32 @@ require_once ("Coords.php"); switch ($_POST['act']){ case "initGame": - $rs = mysqli_query($lnBdd,"SELECT id,name,url FROM bots WHERE game='tron';"); + $rs = mysqli_query($lnMysql,"SELECT id,name,url FROM bots WHERE game='tron';"); while($r = mysqli_fetch_row($rs)){ - $botsFullArr[$r[0]] = array('name' => $r[1], 'url' => $r[2]); + $botsFullArr[$r[0]] = array('id' => $r[0], 'name' => $r[1], 'url' => $r[2]); } + $botsArrayTemp = json_decode($_POST['bots']); - $botsIds = array(); - //dont take non selected bots (value=0) - $queries = ""; - foreach($botsArrayTemp as $bot){ - if($bot > 0){ - $botsIds[] = $bot; + $botsInfos = array(); + + foreach($botsArrayTemp as $id){ + //tester si le bot existe dans la bdd + if(isset($botsFullArr[$id])){ + $botsInfos[] = $botsFullArr[$id]; } } - - - - $game = new TronGame($botsIds); - - - - + //************ + $game = new TronGame($botsInfos); + $logs = $game->init_game(); echo json_encode(array( 'status' => $game->get_continue(), 'logs' => $logs, - 'gameId' => $game->getGameId(), - 'botsPosition' => $game->getBotsPositions() + 'gameId' => $game->gameId, + 'botsPosition' => $game->get_trails() )); $_SESSION['game'] = serialize($game); diff --git a/src/arenas/tron/public.php b/src/arenas/tron/public.php index 550e2dd..7479ade 100644 --- a/src/arenas/tron/public.php +++ b/src/arenas/tron/public.php @@ -26,10 +26,7 @@ foreach($bots as $bot){ var botsAvailable = ; show_bot_panel(0); - - -

+

- \ No newline at end of file