ça avance

pull/96/head
Gnieark 8 years ago
parent 3c0cba6fb3
commit b6060449e7

@ -1 +1 @@
1515
1549

@ -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);
}

@ -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().'<em>'.htmlentities($resp['messageSend']).'</em><br/>
$resp = get_IA_Response($this->bots[$botCount]->url,$messageArr);
$fullLogs .= 'Arena send to '.$this->bots[$botCount]->name.'<em>'.htmlentities($resp['messageSend']).'</em><br/>
HTTP status: <em>'.htmlentities($resp['httpStatus']).'</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);
}
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()."<br/>";
}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().'<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;
}
}
*/

@ -1,14 +1,13 @@
<?php
class TronPlayer{
private $url;
public $url;
public $id;
public $name;
public $tail;
public $trail;
private $direction;
public $isAlive = true;
public function grow(Direction $dir){
$this->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;
}
}
*/
}

@ -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);

@ -26,10 +26,7 @@ foreach($bots as $bot){
var botsAvailable = <?php echo json_encode($botsArr); ?>;
show_bot_panel(0);
</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>
<div id="fightResult"></div>
</article>
Loading…
Cancel
Save