ça avance

This commit is contained in:
Gnieark 2016-07-22 18:19:48 +02:00
parent 3c0cba6fb3
commit b6060449e7
6 changed files with 64 additions and 193 deletions

View File

@ -1 +1 @@
1515 1549

View File

@ -34,8 +34,11 @@ class Trail {
$this->trail->push($value); $this->trail->push($value);
} }
public function __toString(){ public function __toString(){
$str = ""; $arr = "";
return implode( foreach($this->trail as $value) {
$arr[] = $value;
}
return json_encode($arr);
} }

View File

@ -2,11 +2,25 @@
class TronGame class TronGame
{ {
private $bots; //array of bots private $bots; //array of bots
private $gameId; public $gameId;
private $status; //false => Game ended or not initialised 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 //save draws
if( count($loosersArr) > 1 ){ if( count($loosersArr) > 1 ){
$loosersById = array(); $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(){ public function new_lap(){
// for all alive bots // for all alive bots
$logs = ""; $logs = "";
@ -82,9 +104,8 @@ class TronGame
} }
$responses = $this->get_multi_IAS_Responses($urls,$paramsToSend); $responses = $this->get_multi_IAS_Responses($urls,$paramsToSend);
//$responses[$botCount]['responseArr']['play']
//grow bots'tails
//grow bots'tails
for ($botCount = 0; $botCount < $nbeBots; $botCount++){ for ($botCount = 0; $botCount < $nbeBots; $botCount++){
if ($this->bots[$botCount]->getStatus()){ if ($this->bots[$botCount]->getStatus()){
$lastsCells[$botCount] = $this->bots[$botCount]->grow($responses[$botCount]['responseArr']['play']); $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 'player-index' => $botCount
); );
$resp = get_IA_Response($this->bots[$botCount]->getURL(),$messageArr); $resp = get_IA_Response($this->bots[$botCount]->url,$messageArr);
$fullLogs .= 'Arena send to '.$bots[$botCount]->getName().'<em>'.htmlentities($resp['messageSend']).'</em><br/> $fullLogs .= 'Arena send to '.$this->bots[$botCount]->name.'<em>'.htmlentities($resp['messageSend']).'</em><br/>
HTTP status: <em>'.htmlentities($resp['httpStatus']).'</em><br/> HTTP status: <em>'.htmlentities($resp['httpStatus']).'</em><br/>
Bot anwser: <em>'.htmlentities($resp['response']).'</em><br/>'; Bot anwser: <em>'.htmlentities($resp['response']).'</em><br/>';
$logs.="Init message send to ".$this->bots[$botCount]->getName()."<br/>"; $logs.="Init message send to ".$this->bots[$botCount]->name."<br/>";
} }
return array($logs,$fullLogs); 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->gameId = get_unique_id();
$this->bots = array(); $this->bots = array();
$positions = array(); $positions = array();
$botCount = 0; $botCount = 0;
$err = ""; $err = "";
//print_r($botsInfos);
foreach($botsInfos as $bot){ foreach($botsInfos as $bot){
//find a random start position //find a random start position
do{ do{
@ -231,10 +236,11 @@ class TronGame
$positions[] = $x.",".$y; $positions[] = $x.",".$y;
$startCoord = new Coords($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()."<br/>"; $err .= "Something went wrong for ".$this->bots[$botCount]->getName()."<br/>";
}else{ }else{
$botCount++; $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().'<em>'.htmlentities($resp['messageSend']).'</em><br/>
HTTP status: <em>'.htmlentities($resp['httpStatus']).'</em><br/>
Bot anwser: <em>'.htmlentities($resp['response']).'</em><br/>';
}else{
$logs.="Init message send to ".$this->bots[$botCount]->getName()."<br/>";
}
}
return $logs;
}
private function getBusyCells(){
$arr=array();
foreach($this->bots as $bot){
$arr = array_merge($arr,$bot->getTail());
}
return $arr;
}
}
*/

View File

@ -1,14 +1,13 @@
<?php <?php
class TronPlayer{ class TronPlayer{
private $url; public $url;
public $id; public $id;
public $name; public $name;
public $tail; public $trail;
private $direction; private $direction;
public $isAlive = true; public $isAlive = true;
public function grow(Direction $dir){ public function grow(Direction $dir){
$this->trail->add($this->trail->last()->addDirection($dir)); $this->trail->add($this->trail->last()->addDirection($dir));
return $this->trail->last(); return $this->trail->last();
@ -19,7 +18,7 @@ class TronPlayer{
$this->trail->emptyTrail(); $this->trail->emptyTrail();
return false; return false;
} }
public function __make($botId, Coords $initialsCoords,$name,$url){ public function make($botId, Coords $initialsCoords,$name,$url){
$this->id = $botId; $this->id = $botId;
$this->trail = new Trail; $this->trail = new Trail;
$this->trail->add($initialsCoords); $this->trail->add($initialsCoords);
@ -30,22 +29,5 @@ class TronPlayer{
public function __construct(){ public function __construct(){
$this->state = false; $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;
}
}
*/
} }

View File

@ -10,7 +10,6 @@
# #
# -- END LICENSE BLOCK ----------------------------------------- # -- END LICENSE BLOCK -----------------------------------------
require_once(__DIR__."/functions.php");
require_once ("TronGame.php"); require_once ("TronGame.php");
require_once ("TronPlayer.php"); require_once ("TronPlayer.php");
require_once ("Direction.php"); require_once ("Direction.php");
@ -20,36 +19,32 @@ require_once ("Coords.php");
switch ($_POST['act']){ switch ($_POST['act']){
case "initGame": 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)){ 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']); $botsArrayTemp = json_decode($_POST['bots']);
$botsIds = array(); $botsInfos = array();
//dont take non selected bots (value=0)
$queries = ""; foreach($botsArrayTemp as $id){
foreach($botsArrayTemp as $bot){ //tester si le bot existe dans la bdd
if($bot > 0){ if(isset($botsFullArr[$id])){
$botsIds[] = $bot; $botsInfos[] = $botsFullArr[$id];
} }
} }
//************
$game = new TronGame($botsInfos);
$game = new TronGame($botsIds);
$logs = $game->init_game(); $logs = $game->init_game();
echo json_encode(array( echo json_encode(array(
'status' => $game->get_continue(), 'status' => $game->get_continue(),
'logs' => $logs, 'logs' => $logs,
'gameId' => $game->getGameId(), 'gameId' => $game->gameId,
'botsPosition' => $game->getBotsPositions() 'botsPosition' => $game->get_trails()
)); ));
$_SESSION['game'] = serialize($game); $_SESSION['game'] = serialize($game);

View File

@ -26,10 +26,7 @@ foreach($bots as $bot){
var botsAvailable = <?php echo json_encode($botsArr); ?>; var botsAvailable = <?php echo json_encode($botsArr); ?>;
show_bot_panel(0); show_bot_panel(0);
</script> </script>
<p><input type="checkbox" id="fullLogs"/><label for="fullLogs">view the full logs</label></p>
<p><input type="checkbox" id="fullLogs"/><label for="fullLogs">view the full logs</label></p>
<p><input id="fightButton" disabled="disabled" type="button" value="<?php echo $lang['FIGHT']; ?>" onclick="tron('<?php echo xd_check_input(2); ?>');"></p> <p><input id="fightButton" disabled="disabled" type="button" value="<?php echo $lang['FIGHT']; ?>" onclick="tron('<?php echo xd_check_input(2); ?>');"></p>
<div id="fightResult"></div> <div id="fightResult"></div>
</article> </article>