botsArena/src/arenas/Battleship/act.php

98 lines
2.4 KiB
PHP
Raw Normal View History

2015-12-11 13:59:14 +01:00
<?php
2015-12-12 11:06:26 +01:00
require_once(__DIR__."/functions.php");
2015-12-11 17:22:43 +01:00
$bots=get_Bots_Array('Battleship');
2015-12-12 11:05:54 +01:00
2015-12-11 13:59:14 +01:00
switch ($_POST['act']){
case "initGame":
2015-12-11 17:22:43 +01:00
//verifier parametres POST
$postParamsWanted=array(
// key,min,max
array('gridWidth',1,100),
array('gridHeight',1,100),
2015-12-12 11:00:35 +01:00
array('nbShip1',0,10),
array('nbShip2',0,10),
array('nbShip3',0,10),
array('nbShip4',0,10),
array('nbShip5',0,10),
array('nbShip6',0,10)
2015-12-11 17:22:43 +01:00
);
foreach($postParamsWanted as $p){
if(!isset($_POST[$p[0]])){
2015-12-12 10:52:14 +01:00
error (500,'missing parameter 1');
2015-12-11 17:22:43 +01:00
die;
}else{
$value=$_POST[$p[0]];
}
if (
(!is_numeric($value))
2015-12-12 11:01:10 +01:00
OR ($value < $p[1])
OR ($value > $p[2])
2015-12-11 17:22:43 +01:00
)
{
2015-12-12 11:00:35 +01:00
error(500,'wrong parameters '.$p[0]);
2015-12-11 17:22:43 +01:00
die;
}
$postValues[$p[0]]=$value;
}
//check if bots exists
$bot1Exists = false;
$bot2Exists = false;
foreach($bots as $bot){
if($bot['id'] == $_POST['bot1']){
2015-12-12 10:50:44 +01:00
2015-12-11 17:22:43 +01:00
$bot1 = $bot;
$bot1Exists =true;
}
if($bot['id'] == $_POST['bot2']){
$bot2 = $bot;
$bot2Exists =true;
}
if ($bot1Exists && $bot2Exists){
break;
}
}
if ((!$bot1Exists) OR (!$bot2Exists)){
2015-12-12 10:52:14 +01:00
error (500,"missing parameter 2");
2015-12-11 17:22:43 +01:00
}
//vars checked, lets init the initGame
$_SESSION['matchId']=get_unique_id();
2015-12-12 10:25:40 +01:00
// get_IA_Response($iaUrl,$postParams)
//array à envoyer au bot 1
$bot1ParamsToSend=array(
'game' => 'Battleship',
'match_id' => $_SESSION['matchId']."-1",
2015-12-12 13:29:37 +01:00
'act' => 'init',
2015-12-12 10:25:40 +01:00
'opponent' => $bot2['name'],
'width' => $postValues['gridWidth'],
2015-12-12 11:22:04 +01:00
'height' => $postValues['gridHeight'],
2015-12-12 11:03:46 +01:00
'ship1' => $postValues['nbShip1'],
'ship2' => $postValues['nbShip2'],
'ship3' => $postValues['nbShip3'],
'ship4' => $postValues['nbShip4'],
'ship5' => $postValues['nbShip5'],
'ship6' => $postValues['nbShip6']
2015-12-12 10:25:40 +01:00
);
2015-12-14 19:55:07 +01:00
$anwserPlayer1=get_IA_Response($bot1['url'],$bot1ParamsToSend);
2015-12-14 20:25:22 +01:00
$boatsPlayer1 = json_decode( html_entity_decode($anwserPlayer1));
2015-12-14 20:17:58 +01:00
if(!$boatsPlayer1){
2015-12-14 20:06:22 +01:00
echo $bot1['name']." a fait une réponse non conforme, il perd.".$anwserPlayer1;
2015-12-14 19:53:24 +01:00
save_battle('Battleship',$bot1['name'],$bot2['name'],2);
}
2015-12-14 21:06:01 +01:00
//vérifier si'il y a le bon nombre de bateaux
print_r($boatsPlayer1);
//echo $anwserPlayer1; die;
2015-12-11 17:22:43 +01:00
2015-12-11 13:59:14 +01:00
break;
default:
break;
}