diff --git a/html/StupidIABattleship.php b/html/StupidIABattleship.php new file mode 100644 index 0000000..25e4a71 --- /dev/null +++ b/html/StupidIABattleship.php @@ -0,0 +1,2 @@ + $p[2]) ) { - error(500,'wrong parameters'); + error(500,'wrong parameters '.$p[0]); die; } $postValues[$p[0]]=$value; @@ -42,6 +41,7 @@ switch ($_POST['act']){ $bot2Exists = false; foreach($bots as $bot){ if($bot['id'] == $_POST['bot1']){ + $bot1 = $bot; $bot1Exists =true; } @@ -54,13 +54,36 @@ switch ($_POST['act']){ } } if ((!$bot1Exists) OR (!$bot2Exists)){ - error (500,"missing parameter"; + error (500,"missing parameter 2"); } //vars checked, lets init the initGame $_SESSION['matchId']=get_unique_id(); - + + + + // get_IA_Response($iaUrl,$postParams) + //array à envoyer au bot 1 + + $bot1ParamsToSend=array( + 'game' => 'Battleship', + 'act' => 'init', + 'match_id' => $_SESSION['matchId']."-1", + 'opponent' => $bot2['name'], + 'width' => $postValues['gridWidth'], + 'height' => $postValues['gridHeight'], + 'ship1' => $postValues['nbShip1'], + 'ship2' => $postValues['nbShip2'], + 'ship3' => $postValues['nbShip3'], + 'ship4' => $postValues['nbShip4'], + 'ship5' => $postValues['nbShip5'], + 'ship6' => $postValues['nbShip6'] + + ); + + $anwserPlayer1 = get_IA_Response($bot1['url'],$bot1ParamsToSend); + echo $anwserPlayer1; die; break; default: diff --git a/src/arenas/Battleship/functions.php b/src/arenas/Battleship/functions.php index f57cd56..e570ee1 100644 --- a/src/arenas/Battleship/functions.php +++ b/src/arenas/Battleship/functions.php @@ -41,4 +41,15 @@ function generate_numeric_select($start,$end,$selected,$name,$id){ } return $out.""; +} + +function get_IA_Response($iaUrl,$postParams){ + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $iaUrl); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, $postParams); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + $output = curl_exec($ch); + curl_close($ch); + return htmlentities($output); } \ No newline at end of file diff --git a/src/arenas/Battleship/js.js b/src/arenas/Battleship/js.js index e5d752b..1d07041 100644 --- a/src/arenas/Battleship/js.js +++ b/src/arenas/Battleship/js.js @@ -61,18 +61,19 @@ function battleship(bot1,bot2,gridWidth,gridHeight,nbShip1,nbShip2,nbShip3,nbShi var xhr = Ajx(); xhr.onreadystatechange = function(){if(xhr.readyState == 4){ if(xhr.status == 200) { - //document.getElementById('fightResult').innerHTML = xhr.responseText; + //debug + document.getElementById('logs').innerHTML = xhr.responseText; } }}; xhr.open("POST", '/Battleship', true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.send( - 'act=initGame&bot1=' + bot1 - + '&bot2=' + bot2 + 'act=initGame&bot1=' + bot1IdName[0] + + '&bot2=' + bot2IdName[0] + '&gridWidth=' + gridWidth + '&gridHeight=' + gridHeight + '&nbShip1=' + nbShip1 - + '&nbShip1=' + nbShip2 + + '&nbShip2=' + nbShip2 + '&nbShip3=' + nbShip3 + '&nbShip4=' + nbShip4 + '&nbShip5=' + nbShip5 diff --git a/src/arenas/Battleship/public.php b/src/arenas/Battleship/public.php index f62943a..7914490 100644 --- a/src/arenas/Battleship/public.php +++ b/src/arenas/Battleship/public.php @@ -31,7 +31,7 @@ if(!$postParams){ else $selected=''; - echo ''; + echo ''; } ?> @@ -44,7 +44,7 @@ if(!$postParams){ else $selected=''; - echo ''; + echo ''; } ?> diff --git a/src/functions.php b/src/functions.php index fb776da..3d55215 100644 --- a/src/functions.php +++ b/src/functions.php @@ -214,10 +214,11 @@ function save_battle($game,$bot1,$bot2,$resultat){ ON DUPLICATE KEY UPDATE ".$field." = ".$field." + 1;"); } function get_unique_id(){ - $fp = fopen(__DIR__.'countBattles.txt', 'c+'); + + $fp = fopen(__DIR__.'/../countBattles.txt', 'c+'); flock($fp, LOCK_EX); - $count = (int)fread($fp, filesize('count.txt')); + $count = (int)fread($fp, filesize(__DIR__.'/../countBattles.txt')); ftruncate($fp, 0); fseek($fp, 0); fwrite($fp, $count + 1);