Merge pull request #18 from gnieark/dev

begin battleship arena
This commit is contained in:
Gnieark 2015-12-11 17:28:32 +01:00
commit 42f96177b1
13 changed files with 291 additions and 19 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
src/config.php src/config.php
src/countBattles.txt

View File

@ -44,7 +44,7 @@ if(isset($_GET['arena'])){
$hist=get_battles_history($currentArena); $hist=get_battles_history($currentArena);
$siteTitle=$currentArenaArr['title']." - bots Arena"; $siteTitle=$currentArenaArr['title'];
$siteDescription=$currentArenaArr['metaDescription']; $siteDescription=$currentArenaArr['metaDescription'];
$mainSectionScript="../src/arenas/".$currentArena."/public.php"; $mainSectionScript="../src/arenas/".$currentArena."/public.php";
$asideSectionContent='<h2>infos:</h2><p>'.$lang['DEV-YOUR-OWN-BOT'].'<br/> <a href="/'.$currentArena.'/doc">'.$lang['DOC_SPECS_LINKS'].'</a></p> $asideSectionContent='<h2>infos:</h2><p>'.$lang['DEV-YOUR-OWN-BOT'].'<br/> <a href="/'.$currentArena.'/doc">'.$lang['DOC_SPECS_LINKS'].'</a></p>
@ -85,7 +85,7 @@ if(isset($_GET['arena'])){
error(404,"Wrong parameter"); error(404,"Wrong parameter");
die; die;
} }
$siteTitle="Specifications ".$currentArenaArr['title']." - bots Arena"; $siteTitle="Specifications ".$currentArenaArr['title'];
$siteDescription="documentation, faites votre propre bot pour ".$currentArenaArr['metaDescription']; $siteDescription="documentation, faites votre propre bot pour ".$currentArenaArr['metaDescription'];
$mainSectionScript="../src/arenas/".$currentArenaArr['id']."/doc-".$lang['lang'].".html"; $mainSectionScript="../src/arenas/".$currentArenaArr['id']."/doc-".$lang['lang'].".html";
$asideSectionContent=''; //to do $asideSectionContent=''; //to do
@ -104,7 +104,7 @@ if(isset($_GET['arena'])){
$jsAdditionalScript=""; $jsAdditionalScript="";
break; break;
case "About": case "About":
$siteTitle="About - bots Arena"; $siteTitle="About";
$siteDescription="bots arena about page"; $siteDescription="bots arena about page";
$mainSectionScript="../src/about.html"; $mainSectionScript="../src/about.html";
$asideSectionContent=''; //to do or not to do $asideSectionContent=''; //to do or not to do
@ -112,7 +112,7 @@ if(isset($_GET['arena'])){
$jsAdditionalScript=""; $jsAdditionalScript="";
break; break;
case "addBot": case "addBot":
$siteTitle="Valider l'ajout d'une IA - bots Arena"; $siteTitle="Valider l'ajout d'une IA";
$siteDescription="bots arena about page"; $siteDescription="bots arena about page";
$permitIndex=false; $permitIndex=false;
$mainSectionScript="../src/addBot.php"; $mainSectionScript="../src/addBot.php";

View File

@ -1,18 +1,12 @@
html {
height: 100%;
}
body{ body{
width:100%; width:100%;
font-size:100%; font-size:100%;
top: 0px;
line-height:140%; line-height:140%;
word-wrap:break-word; word-wrap:break-word;
text-rendering:optimizelegibility; text-rendering:optimizelegibility;
margin:0 auto; margin:0 auto;
font-family : "lucida grande", "gill sans", arial, sans-serif; font-family : "lucida grande", "gill sans", arial, sans-serif;
left:auto; left:auto;
top:auto;
min-height:100%;
} }
header{ header{
background-color:#A60800; background-color:#A60800;
@ -24,7 +18,6 @@ header h1{
display: block; display: block;
font-size:300%; font-size:300%;
color:#FFF;float: left; color:#FFF;float: left;
width: 45%;
margin-left: 5%;} margin-left: 5%;}
header nav{ header nav{
@ -82,7 +75,6 @@ footer a {
section{ section{
margin: 0 auto; margin: 0 auto;
width: 90%; width: 90%;
padding-bottom: 40px;
} }
article{ article{
float: right; float: right;
@ -91,7 +83,7 @@ article{
aside{ aside{
float:left; float:left;
width: 28%; width: 28%;
border-right: border-right: 1px dashed green;
} }
form textarea, form input, form select {width:40%;} form textarea, form input, form select {width:40%;}

View File

@ -0,0 +1,68 @@
<?php
$bots=get_Bots_Array('Battleship');
switch ($_POST['act']){
case "initGame":
//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)
);
foreach($postParamsWanted as $p){
if(!isset($_POST[$p[0]])){
error (500,'missing parameter');
die;
}else{
$value=$_POST[$p[0]];
}
if (
(!is_numeric($value))
OR ($value < $p[1])
OR ($value > $p[2])
)
{
error(500,'wrong parameters');
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";
}
//vars checked, lets init the initGame
$_SESSION['matchId']=get_unique_id();
break;
default:
break;
}

View File

@ -0,0 +1,7 @@
<h2>Initialisation de la partie</h2>
<p>Pour démarrer la partie, l'arène va envoyer une requette http(s) à votre programme contenant les paramètres POST suivants:</p>
<ul>
<li></li>
<li></li>
<li></li>
</ul>

View File

@ -0,0 +1,44 @@
<?php
function get_Post_Params($botsCount){
$keysBots=array('bot1','bot2');
foreach($keysBots as $botKey){
if(!isset($_POST[$botKey])){
return false;
}
if(!is_numeric(($_POST[$botKey]))){
}
if(($_POST[$botKey] < 0) OR ($_POST[$botKey] > $botsCount)){
error(400,"wrong parameters");
die;
}
}
return array('bot1' => $_POST['bot1'],'bot2' => $_POST['bot2']);
}
function generate_numeric_select($start,$end,$selected,$name,$id){
$out="<select";
if($name !== ""){
$out.=' name="'.$name.'"';
}
if($id !== ""){
$out.=' id="'.$id.'"';
}
$out.=">";
if($selected == -1){
for($i=$start; $i <= $end; $i++ ){
$out.='<option value="'.$i.'">'.$i.'</option>';
}
}else{
for($i=$start; $i < $selected; $i++ ){
$out.='<option value="'.$i.'">'.$i.'</option>';
}
$out.='<option value="'.$selected.'" selected="selected">'.$selected.'</option>';
for($i=$selected + 1; $i <= $end; $i++ ){
$out.='<option value="'.$i.'">'.$i.'</option>';
}
}
return $out."</select>";
}

View File

@ -0,0 +1,83 @@
function Ajx(){
var request = false;
try {request = new ActiveXObject('Msxml2.XMLHTTP');}
catch (err2) {
try {request = new ActiveXObject('Microsoft.XMLHTTP');}
catch (err3) {
try { request = new XMLHttpRequest();}
catch (err1) {
request = false;
}
}
}
return request;
}
function createElem(type,attributes)
{
var elem=document.createElement(type);
for (var i in attributes)
{elem.setAttribute(i,attributes[i]);}
return elem;
}
function battleship(bot1,bot2,gridWidth,gridHeight,nbShip1,nbShip2,nbShip3,nbShip4,nbShip5,nbShip6,xd_check){
var bot1IdName = bot1.split("-");
var bot2IdName = bot2.split("-");
document.getElementById('fightResult').innerHTML = '';
//dessiner les deux grilles
var tableAdv=createElem("table",{"id":"tblAdv","class":"battleshipGrid"});
var tableMe=createElem("table",{"id":"tblAdv","class":"battleshipGrid"});
//ligne de titre
var trTitre1=createElem("tr");
var trTitre2=createElem("tr");
var tdTitre1=createElem("th",{"colspan":gridWidth});
var tdTitre2=createElem("th",{"colspan":gridWidth});
tdTitre1.innerHTML = bot1IdName[1];
tdTitre2.innerHTML = bot2IdName[1];
trTitre1.appendChild(tdTitre1);
tableAdv.appendChild(trTitre1);
trTitre2.appendChild(tdTitre2);
tableMe.appendChild(trTitre2);
for (var i=0; i < gridHeight ; i++){
var trAdv=createElem("tr");
var trMe=createElem("tr");
for (var j=0; j < gridWidth ; j++){
var tdAdv=createElem("td",{"id":"adv" + i +"-" + j,"class": "empty"});
var tdMe=createElem("td",{"id":"me" + i +"-" + j,"class": "empty"});
trAdv.appendChild(tdAdv);
trMe.appendChild(tdMe);
}
tableAdv.appendChild(trAdv);
tableMe.appendChild(trMe);
}
document.getElementById('fightResult').appendChild(tableAdv);
document.getElementById('fightResult').appendChild(tableMe);
var divLogs=createElem("div",{"id":"logs"});
document.getElementById('fightResult').appendChild(divLogs);
var xhr = Ajx();
xhr.onreadystatechange = function(){if(xhr.readyState == 4){
if(xhr.status == 200) {
//document.getElementById('fightResult').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
+ '&gridWidth=' + gridWidth
+ '&gridHeight=' + gridHeight
+ '&nbShip1=' + nbShip1
+ '&nbShip1=' + nbShip2
+ '&nbShip3=' + nbShip3
+ '&nbShip4=' + nbShip4
+ '&nbShip5=' + nbShip5
+ '&nbShip6=' + nbShip6
+ '&xd_check=' + xd_check
);
}

View File

@ -1 +1,55 @@
<h2>Battle ship</h2> <?php
require_once(__DIR__."/functions.php");
$bots=get_Bots_Array('Battleship');
$postParams=get_Post_Params(count($bots));
if(!$postParams){
$bot1="";
$bot2="";
}else{
$bot1=$postParams['bot1'];
$bot2=$postParams['bot2'];
}
?>
<article>
<h2><?php echo $lang['MAKE_DUEL'];?></h2>
<p><label for="width">Largeur de la grille:</label><?php echo generate_numeric_select(1,100,10,'width','width'); ?></p>
<p><label for="height">Hauteur de la grille:</label><?php echo generate_numeric_select(1,100,10,'height','height'); ?></p>
<p><label for="ship1">Nombre de navires de 1 case:</label><?php echo generate_numeric_select(0,10,0,'ship1','ship1'); ?></p>
<p><label for="ship2">Nombre de navires de 2 cases:</label><?php echo generate_numeric_select(0,10,1,'ship2','ship2'); ?></p>
<p><label for="ship3">Nombre de navires de 3 cases:</label><?php echo generate_numeric_select(0,10,2,'ship3','ship3'); ?></p>
<p><label for="ship4">Nombre de navires de 4 cases:</label><?php echo generate_numeric_select(0,10,1,'ship4','ship4'); ?></p>
<p><label for="ship5">Nombre de navires de 5 cases:</label><?php echo generate_numeric_select(0,10,1,'ship5','ship5'); ?></p>
<p><label for="ship6">Nombre de navires de 6 cases:</label><?php echo generate_numeric_select(0,10,0,'ship6','ship6'); ?></p>
<p><label>&nbsp;</label><em>
<select name="bot1" id="bot1">
<?php
for($i=0;$i<count($bots);$i++){
if($i==$bot1)
$selected='selected="selected"';
else
$selected='';
echo '<option value="'.$i."-".$bots[$i]['name'].'" '.$selected.'>'.$bots[$i]['name'].'</option>';
}
?>
</select>
&nbsp;VS&nbsp;
<select name="bot2" id="bot2">
<?php
for($i=0;$i<count($bots);$i++){
if($i==$bot2)
$selected='selected="selected"';
else
$selected='';
echo '<option value="'.$i."-".$bots[$i]['name'].'" '.$selected.'>'.$bots[$i]['name'].'</option>';
}
?>
</select></em>
</p>
<!-- battleship(bot1,bot2,gridWidth,gridHeight,nbShip1,nbShip2,nbShip3,nbShip4,nbShip5,nbShip6,xd_check) -->
<p><label>&nbsp;</label><input type="button" value="<?php echo $lang['FIGHT']; ?>" onclick="battleship(document.getElementById('bot1').value,document.getElementById('bot2').value,getElementById('width').value,getElementById('height').value,getElementById('ship1').value,getElementById('ship2').value,getElementById('ship3').value,getElementById('ship4').value,getElementById('ship5').value,getElementById('ship6').value,'<?php echo xd_check_input(2); ?>');"></p>
<div id="fightResult"></div>
</article>

View File

@ -0,0 +1,8 @@
article p {width: 100%;}
article p label {float:left; text-align:right; width:60%}
article p select {}
td{min-width: 15px; height: 15px;}
.battleshipGrid{float: left; border-collapse:collapse; margin: 20px 20px 20px 20px;}
.battleshipGrid tr{}
.battleshipGrid tr td{border: 1px dashed green;}
.battleshipGrid tr th{text-align: center;}

View File

@ -32,4 +32,4 @@ avec les coordonnées d'une case déjà jouée perd la partie.</p>
<h2>Publier votre programme pour le tester puis le lâcher dans l'arène</h2> <h2>Publier votre programme pour le tester puis le lâcher dans l'arène</h2>
<p>Ce n'est pas encore possible. L'interface est en cours de developpement. Mais vous pouvez toujours me contacter, @gnieark sur twitter, je mettrai à la main votre bot dans l'arène.</p> <p>le formulaire est sur la <a href="/">page d'accueil du site</a></p>

View File

@ -41,5 +41,5 @@ if(!$postParams){
</select> </select>
</p> </p>
<p><input type="button" value="<?php echo $lang['FIGHT']; ?>" onclick="tictactoe(document.getElementById('bot1').value,document.getElementById('bot2').value,'<?php echo xd_check_input(2); ?>');"></p> <p><input type="button" value="<?php echo $lang['FIGHT']; ?>" onclick="tictactoe(document.getElementById('bot1').value,document.getElementById('bot2').value,'<?php echo xd_check_input(2); ?>');"></p>
<div id="fightResult"></div>
</article> </article>
<article id="fightResult"></article>

View File

@ -12,7 +12,9 @@ $arenas=array(
'id' => "Battleship", 'id' => "Battleship",
'url' => "/Battleship", 'url' => "/Battleship",
'title' => "bataille Navale", 'title' => "bataille Navale",
'metaDescription' => 'Affrontements de bots à la battaille navale' 'metaDescription' => 'Affrontements de bots à la battaille navale',
'jsFile'=> "js.js",
'cssFile'=> "style.css"
) )
); );

View File

@ -213,3 +213,16 @@ function save_battle($game,$bot1,$bot2,$resultat){
'1') '1')
ON DUPLICATE KEY UPDATE ".$field." = ".$field." + 1;"); ON DUPLICATE KEY UPDATE ".$field." = ".$field." + 1;");
} }
function get_unique_id(){
$fp = fopen(__DIR__.'countBattles.txt', 'c+');
flock($fp, LOCK_EX);
$count = (int)fread($fp, filesize('count.txt'));
ftruncate($fp, 0);
fseek($fp, 0);
fwrite($fp, $count + 1);
flock($fp, LOCK_UN);
fclose($fp);
return $count;
}