From 376e767449f060341cec38f4574c93149611be5c Mon Sep 17 00:00:00 2001 From: gnieark Date: Fri, 18 Dec 2015 13:18:14 +0100 Subject: [PATCH] log --- src/arenas/Battleship/act.php | 37 ++++++++++++++++++++++++++++++++++- src/arenas/Battleship/js.js | 19 +++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/arenas/Battleship/act.php b/src/arenas/Battleship/act.php index 49a266b..ece3efc 100644 --- a/src/arenas/Battleship/act.php +++ b/src/arenas/Battleship/act.php @@ -43,10 +43,12 @@ switch ($_POST['act']){ if($bot['id'] == $_POST['bot1']){ $bot1 = $bot; + $_SESSION['bot1']=$bot; $bot1Exists =true; } if($bot['id'] == $_POST['bot2']){ $bot2 = $bot; + $_SESSION['bot2']=$bot; $bot2Exists =true; } if ($bot1Exists && $bot2Exists){ @@ -134,6 +136,33 @@ switch ($_POST['act']){ } die; } + //remember each cases of each boats + $boatListOfCases=array(); + if($xStart == $xEnd){ + if($yStart <= $yEnd ){ + $start=$yStart; + $end=$yEnd; + }else{ + $start=$yEnd; + $end=$yStart; + } + for($i = $start; $i <= $end; $i++){ + $boatListOfCases[]=$xStart.",".$i; + } + }else{ + if($xStart <= $xEnd ){ + $start=$xStart; + $end=$xEnd; + }else{ + $start=$xEnd; + $end=$xStart; + } + for($i = $start; $i <= $end; $i++){ + $boatListOfCases[]=$i.",".$yStart; + } + + } + $_SESSION['ships'][$player][]=$boatListOfCases; } foreach($nbBoatsIwant as $nb){ if($nb <> 0){ @@ -148,11 +177,17 @@ switch ($_POST['act']){ } } - //$_SESSION['grids']=$grid; + $_SESSION['shots'][1]=array(); + $_SESSION['shots'][2]=array(); + $_SESSION['width']=$postValues['gridWidth']; + $_SESSION['height']=$postValues['gridHeight']; echo json_encode($grid); die; die; + break; + case "fight"; + print_r($_SESSION); break; default: break; diff --git a/src/arenas/Battleship/js.js b/src/arenas/Battleship/js.js index fd7d108..bc2b6f9 100644 --- a/src/arenas/Battleship/js.js +++ b/src/arenas/Battleship/js.js @@ -19,6 +19,23 @@ function createElem(type,attributes) {elem.setAttribute(i,attributes[i]);} return elem; } +function fight(xd_check){ + var xhr = Ajx(); + xhr.onreadystatechange = function(){if(xhr.readyState == 4){ + if(xhr.status == 200) { + var p=createElem("p"); + p.innerHTML=xhr.responseText; + document.getElementById('logs').appendChild(p); + fight(xd_check); + + } + + }}; + xhr.open("POST", '/Battleship', true); + xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); + xhr.send('act=fight&xd_check=' + xd_check); + +} function battleship(bot1,bot2,gridWidth,gridHeight,nbShip1,nbShip2,nbShip3,nbShip4,nbShip5,nbShip6,xd_check){ @@ -104,7 +121,7 @@ function battleship(bot1,bot2,gridWidth,gridHeight,nbShip1,nbShip2,nbShip3,nbShi var p=createElem("p"); p.innerHTML='players placed theirs ships'; document.getElementById('logs').appendChild(p); - + fight(xd_check); } }};