js reduce try catch

This commit is contained in:
gnieark 2015-12-17 22:27:17 +01:00
parent 5cf7dd2615
commit c4fa2974cf
2 changed files with 28 additions and 26 deletions

View File

@ -126,7 +126,7 @@ switch ($_POST['act']){
$nbBoatsIwant[$long]-=1;
$grid[$player]=place_ship_on_map($xStart,$yStart,$xEnd,$yEnd,$grid[$player]);
if(!$grid[$player]){
echo $currentBot['name']." n'a pas placé correctement ses bateaux. Certains se chevauchent. Il perd<pre>".$anwserPlayer." ".$xStart.$yStart.$xEnd.$yEnd."</pre>";
echo $currentBot['name']." n'a pas placé correctement ses bateaux. Certains se chevauchent. Il perd.";
if($player==1){
save_battle('Battleship',$bot1['name'],$bot2['name'],2);
}else{
@ -137,7 +137,7 @@ switch ($_POST['act']){
}
foreach($nbBoatsIwant as $nb){
if($nb <> 0){
echo $currentBot['name']." n'a pas placé correctement ses bateaux. Il perd. sa réponse: <pre>".$anwserPlayer."</pre>";
echo $currentBot['name']." n'a pas placé le bon nombre de bateaux. Il perd.";
if($player==1){
save_battle('Battleship',$bot1['name'],$bot2['name'],2);
}else{
@ -148,7 +148,7 @@ switch ($_POST['act']){
}
}
$_SESSION['grids']=$grid;
//$_SESSION['grids']=$grid;
echo json_encode($grid); die;
die;

View File

@ -82,30 +82,32 @@ function battleship(bot1,bot2,gridWidth,gridHeight,nbShip1,nbShip2,nbShip3,nbShi
var xhr = Ajx();
xhr.onreadystatechange = function(){if(xhr.readyState == 4){
if(xhr.status == 200) {
//debug
//alert(xhr.responseText);
try{
var grids = JSON.parse(xhr.responseText);
for( var player=1; player <= 2 ; player ++){
var p=createElem("p");
p.innerHTML='Reponse joueurs:' + xhr.responseText;
document.getElementById('logs').appendChild(p);
if(xhr.status == 200) {
//debug
//alert(xhr.responseText);
try{
var grids = JSON.parse(xhr.responseText);
catch(e){
document.getElementById('logs').innerHTML = xhr.responseText;
return;
}
for( var player=1; player <= 2 ; player ++){
var p=createElem("p");
p.innerHTML='Reponse joueurs:' + xhr.responseText;
document.getElementById('logs').appendChild(p);
for (var y=0; y < grids[player].length ; y++){
for (var x=0; x < grids[player][y].length ; x++){
if (grids[player][y][x] == 1){
document.getElementById( 'bot' + player + '-' + y + '-' + x).className="shipOn";
}
}
}
}
}
catch(e){
document.getElementById('logs').innerHTML = xhr.responseText;
}
for (var y=0; y < grids[player].length ; y++){
for (var x=0; x < grids[player][y].length ; x++){
if (grids[player][y][x] == 1){
document.getElementById( 'bot' + player + '-' + y + '-' + x).className="shipOn";
}
}
}
}
}
}
}};
xhr.open("POST", '/Battleship', true);