commit
2334285999
2
html/StupidIABattleship.php
Normal file
2
html/StupidIABattleship.php
Normal file
|
@ -0,0 +1,2 @@
|
|||
<?php
|
||||
print_r($_POST);
|
|
@ -1,4 +1,5 @@
|
|||
<?php
|
||||
require_once(__DIR__."/functions.php");
|
||||
$bots=get_Bots_Array('Battleship');
|
||||
|
||||
switch ($_POST['act']){
|
||||
|
@ -6,21 +7,19 @@ switch ($_POST['act']){
|
|||
//verifier parametres POST
|
||||
$postParamsWanted=array(
|
||||
// key,min,max
|
||||
array('bot1',1,999),
|
||||
array('bot2',1,999),
|
||||
array('gridWidth',1,100),
|
||||
array('gridHeight',1,100),
|
||||
array('ship1',1,10),
|
||||
array('ship2',1,10),
|
||||
array('ship3',1,10),
|
||||
array('ship4',1,10),
|
||||
array('ship5',1,10),
|
||||
array('ship6',1,10)
|
||||
array('nbShip1',0,10),
|
||||
array('nbShip2',0,10),
|
||||
array('nbShip3',0,10),
|
||||
array('nbShip4',0,10),
|
||||
array('nbShip5',0,10),
|
||||
array('nbShip6',0,10)
|
||||
);
|
||||
|
||||
foreach($postParamsWanted as $p){
|
||||
if(!isset($_POST[$p[0]])){
|
||||
error (500,'missing parameter');
|
||||
error (500,'missing parameter 1');
|
||||
die;
|
||||
}else{
|
||||
$value=$_POST[$p[0]];
|
||||
|
@ -31,7 +30,7 @@ switch ($_POST['act']){
|
|||
OR ($value > $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,7 +54,7 @@ switch ($_POST['act']){
|
|||
}
|
||||
}
|
||||
if ((!$bot1Exists) OR (!$bot2Exists)){
|
||||
error (500,"missing parameter";
|
||||
error (500,"missing parameter 2");
|
||||
}
|
||||
|
||||
//vars checked, lets init the initGame
|
||||
|
@ -62,6 +62,29 @@ switch ($_POST['act']){
|
|||
$_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:
|
||||
break;
|
||||
|
|
|
@ -42,3 +42,14 @@ function generate_numeric_select($start,$end,$selected,$name,$id){
|
|||
return $out."</select>";
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -31,7 +31,7 @@ if(!$postParams){
|
|||
else
|
||||
$selected='';
|
||||
|
||||
echo '<option value="'.$i."-".$bots[$i]['name'].'" '.$selected.'>'.$bots[$i]['name'].'</option>';
|
||||
echo '<option value="'.$bots[$i]['id']."-".htmlentities($bots[$i]['name']).'" '.$selected.'>'.htmlentities($bots[$i]['name']).'</option>';
|
||||
}
|
||||
?>
|
||||
</select>
|
||||
|
@ -44,7 +44,7 @@ if(!$postParams){
|
|||
else
|
||||
$selected='';
|
||||
|
||||
echo '<option value="'.$i."-".$bots[$i]['name'].'" '.$selected.'>'.$bots[$i]['name'].'</option>';
|
||||
echo '<option value="'.$bots[$i]['id']."-".htmlentities($bots[$i]['name']).'" '.$selected.'>'.htmlentities($bots[$i]['name']).'</option>';
|
||||
}
|
||||
?>
|
||||
</select></em>
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue
Block a user