This commit is contained in:
Gnieark 2016-06-20 15:51:37 +02:00
parent 3bad116750
commit 0ea6c418c5
2 changed files with 131 additions and 131 deletions

View File

@ -240,133 +240,133 @@ switch ($_POST['act']){
die; die;
break; break;
case "fight": case "fight":
if($_POST['fullLogs'] == 'true'){ if($_POST['fullLogs'] == 'true'){
$fullLogs = true; $fullLogs = true;
}else{ }else{
$fullLogs = false; $fullLogs = false;
} }
if(count($_SESSION['strikes'][1]) == count($_SESSION['strikes'][2])){ if(count($_SESSION['strikes'][1]) == count($_SESSION['strikes'][2])){
//player 1 has to fight //player 1 has to fight
$currentPlayer=1; $currentPlayer=1;
$currentBot=$_SESSION['bot1']; $currentBot=$_SESSION['bot1'];
$opponent=2; $opponent=2;
$opponentName=$_SESSION['bot2']['name']; $opponentName=$_SESSION['bot2']['name'];
}else{ }else{
//it's player2 //it's player2
$currentPlayer=2; $currentPlayer=2;
$currentBot=$_SESSION['bot2']; $currentBot=$_SESSION['bot2'];
$opponentName=$_SESSION['bot1']['name']; $opponentName=$_SESSION['bot1']['name'];
$opponent=1; $opponent=1;
} }
$botParamsToSend=array( $botParamsToSend=array(
'game' => 'Battleship', 'game' => 'Battleship',
'game-id' => $_SESSION['matchId'], 'game-id' => $_SESSION['matchId'],
'action' => 'play-turn', 'action' => 'play-turn',
'player-index' => $currentPlayer - 1, 'player-index' => $currentPlayer - 1,
'board' => array( 'board' => array(
'opponent' => $opponentName, 'opponent' => $opponentName,
'width' => $_SESSION['width'], 'width' => $_SESSION['width'],
'height' => $_SESSION['height'], 'height' => $_SESSION['height'],
'ship1' => $_SESSION['ship1'], 'ship1' => $_SESSION['ship1'],
'ship2' => $_SESSION['ship2'], 'ship2' => $_SESSION['ship2'],
'ship3' => $_SESSION['ship3'], 'ship3' => $_SESSION['ship3'],
'ship4' => $_SESSION['ship4'], 'ship4' => $_SESSION['ship4'],
'ship5' => $_SESSION['ship5'], 'ship5' => $_SESSION['ship5'],
'ship6' => $_SESSION['ship6'], 'ship6' => $_SESSION['ship6'],
'your_strikes' => $_SESSION['strikes'][$currentPlayer], 'your_strikes' => $_SESSION['strikes'][$currentPlayer],
'his_strikes' => $_SESSION['strikes'][$opponent] 'his_strikes' => $_SESSION['strikes'][$opponent]
) )
); );
$anwserPlayer=get_IA_Response($currentBot['url'],$botParamsToSend); $anwserPlayer=get_IA_Response($currentBot['url'],$botParamsToSend);
$anwserPlayerJson=$anwserPlayer['responseArr']; $anwserPlayerJson=$anwserPlayer['responseArr'];
if($fullLogs){ if($fullLogs){
$fullLogs='Arena send to '.$currentBot['name'].'<em>'.htmlentities($anwserPlayer['messageSend']).'</em><br /> $fullLogs='Arena send to '.$currentBot['name'].'<em>'.htmlentities($anwserPlayer['messageSend']).'</em><br />
HTTP status: <em>'.htmlentities($anwserPlayer['httpStatus']).'</em><br /> HTTP status: <em>'.htmlentities($anwserPlayer['httpStatus']).'</em><br />
Bot anwser: <em>'.htmlentities($anwserPlayer['response']).'</em><br />'; Bot anwser: <em>'.htmlentities($anwserPlayer['response']).'</em><br />';
}else{ }else{
$fullLogs=""; $fullLogs="";
} }
if ((!isset($anwserPlayerJson['play'])) if ((!isset($anwserPlayerJson['play']))
OR (!preg_match('/^[0-9]+,[0-9]+$/',$anwserPlayerJson['play']))){ OR (!preg_match('/^[0-9]+,[0-9]+$/',$anwserPlayerJson['play']))){
echo json_encode(array( echo json_encode(array(
'target' => '', 'target' => '',
'log' => $fullLogs.$currentBot['name']." a fait une réponse non conforme, il perd.")); 'log' => $fullLogs.$currentBot['name']." a fait une réponse non conforme, il perd."));
save_battle('Battleship',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],$opponent); save_battle('Battleship',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],$opponent);
die; die;
} }
list($x,$y)=explode(",",$anwserPlayerJson['play']); list($x,$y)=explode(",",$anwserPlayerJson['play']);
//check if shot is under map's limits //check if shot is under map's limits
if(($x >= $_SESSION['width']) OR ($y >= $_SESSION['height'])){ if(($x >= $_SESSION['width']) OR ($y >= $_SESSION['height'])){
echo json_encode(array( echo json_encode(array(
'target' => '', 'target' => '',
'log' => $fullLogs.$currentBot['name']." a fait un tir en dehors des limites de la carte. ".$x.",".$y." C'est interdit par les conventions de Geneve. Il perd" 'log' => $fullLogs.$currentBot['name']." a fait un tir en dehors des limites de la carte. ".$x.",".$y." C'est interdit par les conventions de Geneve. Il perd"
)); ));
save_battle('Battleship',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],$opponent); save_battle('Battleship',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],$opponent);
die; die;
} }
//put previous strikes in a one dimmension array ; //put previous strikes in a one dimmension array ;
$previousStrikes=array(); $previousStrikes=array();
foreach( $_SESSION['strikes'][$currentPlayer] as $strikes){ foreach( $_SESSION['strikes'][$currentPlayer] as $strikes){
$previousStrikes[]=$strikes['target']; $previousStrikes[]=$strikes['target'];
} }
//do this strike hit a boat? //do this strike hit a boat?
$continue=1; $continue=1;
$result=''; $result='';
for( $shipIndex = 0; $shipIndex < count( $_SESSION['ships'][$opponent]); $shipIndex ++){ for( $shipIndex = 0; $shipIndex < count( $_SESSION['ships'][$opponent]); $shipIndex ++){
$ennemyBoat = $_SESSION['ships'][$opponent][$shipIndex]; $ennemyBoat = $_SESSION['ships'][$opponent][$shipIndex];
if(in_array($x.",".$y, $ennemyBoat)){ if(in_array($x.",".$y, $ennemyBoat)){
$result='hit'; $result='hit';
//sunk? //sunk?
$sunk=true; $sunk=true;
foreach($ennemyBoat as $boatCase){ foreach($ennemyBoat as $boatCase){
if((!in_array($boatCase,$previousStrikes)) && ($boatCase <> $x.",".$y)) { if((!in_array($boatCase,$previousStrikes)) && ($boatCase <> $x.",".$y)) {
$sunk=false; $sunk=false;
break; break;
} }
} }
if($sunk){ if($sunk){
$result="hit and sunk"; $result="hit and sunk";
//remove the ship //remove the ship
unset($_SESSION['ships'][$opponent][$shipIndex]); unset($_SESSION['ships'][$opponent][$shipIndex]);
$_SESSION['ships'][$opponent] = array_values($_SESSION['ships'][$opponent]); $_SESSION['ships'][$opponent] = array_values($_SESSION['ships'][$opponent]);
//var_dump($_SESSION['ships'][$opponent]); //var_dump($_SESSION['ships'][$opponent]);
//win the game? //win the game?
if(count($_SESSION['ships'][$opponent]) == 0){ if(count($_SESSION['ships'][$opponent]) == 0){
$result="hit sunk and win"; $result="hit sunk and win";
$continue=0; $continue=0;
save_battle('Battleship',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],$currentPlayer); save_battle('Battleship',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],$currentPlayer);
} }
} }
break; break;
} }
} }
//remember this shot //remember this shot
$_SESSION['strikes'][$currentPlayer][]=array( $_SESSION['strikes'][$currentPlayer][]=array(
'target' => $x.",".$y, 'target' => $x.",".$y,
'result' => $result 'result' => $result
); );
//send message for the arena's ajax web page //send message for the arena's ajax web page
echo json_encode(array( echo json_encode(array(
'opponent'=> $opponent, 'opponent'=> $opponent,
'target' => $x.",".$y, 'target' => $x.",".$y,
'log' => $fullLogs.$currentBot['name']." tire en ".$x.",".$y." ".$result, 'log' => $fullLogs.$currentBot['name']." tire en ".$x.",".$y." ".$result,
'continue' => $continue 'continue' => $continue
)); ));
die; die;
break; break;
default: default:
break; break;
} }

View File

@ -131,9 +131,9 @@ function battleship(bot1,bot2,gridWidth,gridHeight,nbShip1,nbShip2,nbShip3,nbShi
} }
} }
} }
for each (var lo in grid['logs']){ for (var i = 1; i < grid['logs'].lenght; i++) {
var p=createElem("p"); var p=createElem("p");
p.innerHTML='o; p.innerHTML=grid['logs'][i];
document.getElementById('logs').appendChild(p); document.getElementById('logs').appendChild(p);
} }
fight(xd_check,fullLogs); fight(xd_check,fullLogs);