botsArena/src/arenas/Battleship/act.php
2015-12-12 10:57:57 +01:00

90 lines
2.1 KiB
PHP

<?php
$bots=get_Bots_Array('Battleship');
print_r($bots);
switch ($_POST['act']){
case "initGame":
//verifier parametres POST
$postParamsWanted=array(
// key,min,max
array('gridWidth',1,100),
array('gridHeight',1,100),
array('nbShip1',1,10),
array('nbShip2',1,10),
array('nbShip3',1,10),
array('nbShip4',1,10),
array('nbShip5',1,10),
array('nbShip6',1,10)
);
foreach($postParamsWanted as $p){
if(!isset($_POST[$p[0]])){
error (500,'missing parameter 1');
die;
}else{
$value=$_POST[$p[0]];
}
if (
(!is_numeric($value))
OR ( <= $p[1])
OR ($value >= $p[2])
)
{
error(500,'wrong parameters'.$p[0]);
die;
}
$postValues[$p[0]]=$value;
}
//check if bots exists
$bot1Exists = false;
$bot2Exists = false;
foreach($bots as $bot){
if($bot['id'] == $_POST['bot1']){
$bot1 = $bot;
$bot1Exists =true;
}
if($bot['id'] == $_POST['bot2']){
$bot2 = $bot;
$bot2Exists =true;
}
if ($bot1Exists && $bot2Exists){
break;
}
}
if ((!$bot1Exists) OR (!$bot2Exists)){
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['height'],
'ship1' => $postValues['ship1'],
'ship2' => $postValues['ship2'],
'ship3' => $postValues['ship3'],
'ship4' => $postValues['ship4'],
'ship5' => $postValues['ship5'],
'ship6' => $postValues['ship6']
);
$anwserPlayer1 = get_IA_Response($bot1['url'],$bot1ParamsToSend);
echo $anwserPlayer1; die;
break;
default:
break;
}