Merge pull request #86 from gnieark/dev

Dev
This commit is contained in:
Gnieark 2016-06-12 22:46:46 +02:00 committed by GitHub
commit e0a6f7df63
7 changed files with 501 additions and 450 deletions

View File

@ -4,318 +4,333 @@ require_once(__DIR__."/functions.php");
$bots=get_Bots_Array('connectFou');
$new=false;
switch ($_POST['act']){
case "newFight":
$new=true;
//remove $_SESSION less xd_check
$xd=$_SESSION['xd_check'];
session_unset();
$_SESSION['xd_check']=$xd;
//init map
$_SESSION['map']=array(
array("","","","","","",""),
array("","","","","","",""),
array("","","","","","",""),
array("","","","","","",""),
array("","","","","","",""),
array("","","","","","",""),
);
//check if bots exists
$bot1Exists = false;
$bot2Exists = false;
foreach($bots as $bot){
if($bot['id'] == $_POST['bot1']){
$bot1 = $bot;
$_SESSION['bot1']=$bot;
$bot1Exists =true;
}
if($bot['id'] == $_POST['bot2']){
$bot2 = $bot;
$_SESSION['bot2']=$bot;
$bot2Exists =true;
}
if ($bot1Exists && $bot2Exists){
break;
}
}
if ((!$bot1Exists) OR (!$bot2Exists)){
error (500,"missing parameter 2");
}
//get a match id
$_SESSION['matchId']=get_unique_id();
$_SESSION['game']="connectFou";
//send init message
for($player = 0; $player < 2; $player++){
$params[$player]=array(
'game-id' => $_SESSION['matchId'],
'action' => 'init',
'game' => 'connectfour',
'players' => 2,
'board' => '',
'player-index' => $player
);
}
get_IA_Response($_SESSION['bot1']['url'],$params[0]); //don't care about result
get_IA_Response($_SESSION['bot2']['url'],$params[1]); //don't care about result
case "fight":
if($_SESSION['game'] <> "connectFou"){
error(500,"game non found");
die;
}
if((!$new) && ($_POST['gameId'] <> $_SESSION['matchId'])){
error (512, "not correct gameId");
}
//What player has to play?
if(!isset($_SESSION['currentPlayer'])){
$_SESSION['currentPlayer']=1;
$you="X";
$currentBotName=$_SESSION['bot1']['name'];
$opponentName=$_SESSION['bot2']['name'];
$botUrl=$_SESSION['bot1']['url'];
}else{
if($_SESSION['currentPlayer']==1){
$_SESSION['currentPlayer']=2;
$you="O";
$opponentName=$_SESSION['bot1']['name'];
$currentBotName=$_SESSION['bot2']['name'];
$botUrl=$_SESSION['bot2']['url'];
}else{
$_SESSION['currentPlayer']=1;
$opponentName=$_SESSION['bot2']['name'];
$currentBotName=$_SESSION['bot1']['name'];
$botUrl=$_SESSION['bot1']['url'];
$you="X";
}
}
/*
game-id string identifiant la partie.
action string identifiant la phase, init tout de suite, sera play-turn dans le châpitre suivant.
game string identifiant le jeu. Ici, ce sera forcément tictactoe. ça peut servir si vous donnez une seule URL pour plusieurs bots.
players Int indiquant le nombre de joueurs dans la partie, toujours 2 au morpion.
board Vide à cette étape, voir chapitre suivant.
player-index int, L'ordre de votre bot dans les tours de jeu. Le premier joueur a la valeur 0, le deuxième 1.
*/
//make post datas to send
$postDatas=array(
'game-id' => "".$_SESSION['matchId'],
'action' => 'play-turn',
'game' => 'connectfour',
'players' => 2,
'opponent' => $opponentName,
'you' => $you,
'board' => $_SESSION['map'],
'player-index' => $_SESSION['currentPlayer'] - 1
);
//send query
$tempPlayer = get_IA_Response($botUrl,$postDatas);
$anwserPlayer = $tempPlayer['play'];
//vérifier la validité de la réponse
if((isset($_SESSION['map'][5][$anwserPlayer])) && ($_SESSION['map'][5][$anwserPlayer] == "")){
//reponse conforme
for($y = 0; $_SESSION['map'][$y][$anwserPlayer] <> ""; $y++){
}
$_SESSION['map'][$y][$anwserPlayer]=$you;
$strikeX=$anwserPlayer;
$strikeY=$y;
//does he win?
$wins=false;
//diagonale \
$count=1;
$x=$strikeX;
$y=$strikeY;
$cellsWin=array();
$cellsWin[]=array($x,$y);
while(($x > 0) && ($y < 5) && ($_SESSION['map'][$y + 1][$x - 1] == $you)){
$x--;
$y++;
$count++;
$cellsWin[]=array($x,$y);
}
case "newFight":
$new=true;
//remove $_SESSION less xd_check
$xd=$_SESSION['xd_check'];
session_unset();
$_SESSION['xd_check']=$xd;
$x=$strikeX;
$y=$strikeY;
while(($x < 6) && ($y > 0) && ($_SESSION['map'][$y - 1][$x + 1] == $you)){
$x++;
$y--;
$count++;
$cellsWin[]=array($x,$y);
}
if($count>3){
$wins=true;
}
//diagonale /
if(!$wins){
$count=1;
$x=$strikeX;
$y=$strikeY;
$cellsWin =array();
$cellsWin[]=array($x ,$y);
while(($x < 6) && ($y < 5) && ($_SESSION['map'][$y + 1][$x + 1 ] == $you)){
$x++;
$y++;
$count++;
$cellsWin[]=array($x,$y);
//init map
$_SESSION['map']=array(
array("","","","","","",""),
array("","","","","","",""),
array("","","","","","",""),
array("","","","","","",""),
array("","","","","","",""),
array("","","","","","",""),
);
//check if bots exists
$bot1Exists = false;
$bot2Exists = false;
foreach($bots as $bot){
if($bot['id'] == $_POST['bot1']){
$bot1 = $bot;
$_SESSION['bot1']=$bot;
$bot1Exists =true;
}
if($bot['id'] == $_POST['bot2']){
$bot2 = $bot;
$_SESSION['bot2']=$bot;
$bot2Exists =true;
}
if ($bot1Exists && $bot2Exists){
break;
}
}
$x=$strikeX;
$y=$strikeY;
while(($x > 0) && ($y > 0) && ($_SESSION['map'][$y - 1][$x - 1 ] == $you)){
$x--;
$y--;
$count++;
$cellsWin[]=array($x,$y);
if ((!$bot1Exists) OR (!$bot2Exists)){
error (500,"missing parameter 2");
}
if($count>3){
$wins=true;
//get a match id
$_SESSION['matchId']=get_unique_id();
$_SESSION['game']="connectFou";
//send init message
for($player = 0; $player < 2; $player++){
$params[$player]=array(
'game-id' => $_SESSION['matchId'],
'action' => 'init',
'game' => 'connectfour',
'players' => 2,
'board' => '',
'player-index' => $player
);
}
}
//horizontale
if(!$wins){
$count=1;
$x=$strikeX;
$y=$strikeY;
$cellsWin =array();
$cellsWin[]=array($x ,$y);
while(($x < 6) && ($_SESSION['map'][$y][$x + 1 ] == $you)){
$x++;
$count++;
$cellsWin[]=array($x,$y);
/*
* 'messageSend' => $data_string,
* 'httpStatus' => $curl_getinfo($ch)['http_code'],
* 'response' => $output,
* 'responseArr' => $arr
*/
get_IA_Response($_SESSION['bot1']['url'],$params[0]); //don't care about result
get_IA_Response($_SESSION['bot2']['url'],$params[1]); //don't care about result
case "fight":
}
$x=$strikeX;
while(($x >0) && ($_SESSION['map'][$y][$x - 1 ] == $you)){
$count++;
$x--;
$cellsWin[]=array($x,$y);
}
if($count>3){
$wins=true;
}
}
//verticale
if(!$wins){
$count=1;
$x=$strikeX;
$y=$strikeY;
$cellsWin =array();
$cellsWin[]=array($x ,$y);
while(($y < 5) && ($_SESSION['map'][$y + 1 ][$x] == $you)){
$y++;
$count++;
$cellsWin[]=array($x,$y);
}
$y=$strikeY;
while(($y >0) && ($_SESSION['map'][$y - 1][$x] == $you)){
$count++;
$y--;
$cellsWin[]=array($x,$y);
}
if($count>3){
$wins=true;
}
}
if($wins){
$anwserToJS=array(
'continue' => 0,
'strikeX' => $strikeX,
'strikeY' => $strikeY,
'strikeSymbol'=> $you,
'log' => $you." ".$currentBotName." joue colonne ". $anwserPlayer." et a gagné",
'cellsWin' => json_encode($cellsWin),
'gameId' => $_SESSION['matchId']
);
if($_SESSION['currentPlayer']==1){
save_battle('connectFou',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],1);
}else{
save_battle('connectFou',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],2);
}
}else{
//Was it the last cell?
$full=true;
foreach ($_SESSION['map'][5] as $cell) {
if ($cell == ""){
$full=false;
break;
}
}
if($full){
save_battle('connectFou',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],0);
$anwserToJS=array(
'continue' => 0,
'strikeX' => $strikeX,
'strikeY' => $strikeY,
'strikeSymbol'=> $you,
'log' => $you." ".$currentBotName." joue colonne ". $anwserPlayer." match nul",
'gameId' => $_SESSION['matchId']
);
}else{
$anwserToJS=array(
'continue' => 1,
'strikeX' => $strikeX,
'strikeY' => $strikeY,
'strikeSymbol'=> $you,
'log' => $you." ".$currentBotName." joue colonne ". $anwserPlayer,
'gameId' => $_SESSION['matchId']
);
}
}
}else{
//reponse non conforme
$anwserToJS=array(
'continue' =>0,
'strikeX' => -1,
'strikeY' => -1,
'log' => $you." ".$currentBotName." a fait une réponse non conforme, il perd".json_encode($tempPlayer),
'gameId' => $_SESSION['matchId']
);
if($_SESSION['currentPlayer']==1){
save_battle('connectFou',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],2);
}else{
save_battle('connectFou',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],1);
}
}
echo json_encode($anwserToJS);
die;
break;
default:
break;
if($_SESSION['game'] <> "connectFou"){
error(500,"game non found");
die;
}
if((!$new) && ($_POST['gameId'] <> $_SESSION['matchId'])){
error (512, "not correct gameId");
}
//What player has to play?
if(!isset($_SESSION['currentPlayer'])){
$_SESSION['currentPlayer']=1;
$you="X";
$currentBotName=$_SESSION['bot1']['name'];
$opponentName=$_SESSION['bot2']['name'];
$botUrl=$_SESSION['bot1']['url'];
}else{
if($_SESSION['currentPlayer']==1){
$_SESSION['currentPlayer']=2;
$you="O";
$opponentName=$_SESSION['bot1']['name'];
$currentBotName=$_SESSION['bot2']['name'];
$botUrl=$_SESSION['bot2']['url'];
}else{
$_SESSION['currentPlayer']=1;
$opponentName=$_SESSION['bot2']['name'];
$currentBotName=$_SESSION['bot1']['name'];
$botUrl=$_SESSION['bot1']['url'];
$you="X";
}
}
/*
*
*
* game-id string identifiant la partie.
* action string identifiant la phase, init tout de suite, sera play-turn dans le châpitre suivant.
* game string identifiant le jeu. Ici, ce sera forcément tictactoe. ça peut servir si vous donnez une seule URL pour plusieurs bots.
* players Int indiquant le nombre de joueurs dans la partie, toujours 2 au morpion.
* board Vide à cette étape, voir chapitre suivant.
* player-index int, L'ordre de votre bot dans les tours de jeu. Le premier joueur a la valeur 0, le deuxième 1.
*/
//make post datas to send
$postDatas=array(
'game-id' => "".$_SESSION['matchId'],
'action' => 'play-turn',
'game' => 'connectfour',
'players' => 2,
'opponent' => $opponentName,
'you' => $you,
'board' => $_SESSION['map'],
'player-index' => $_SESSION['currentPlayer'] - 1
);
//send query
$tempPlayer = get_IA_Response($botUrl,$postDatas);
if(isset($tempPlayer['responseArr']['play'])){
$anwserPlayer = $tempPlayer['responseArr']['play'];
}else{
$anwserPlayer = -1;
}
//vérifier la validité de la réponse
if((isset($_SESSION['map'][5][$anwserPlayer])) && ($_SESSION['map'][5][$anwserPlayer] == "")){
//reponse conforme
for($y = 0; $_SESSION['map'][$y][$anwserPlayer] <> ""; $y++){
}
$_SESSION['map'][$y][$anwserPlayer]=$you;
$strikeX=$anwserPlayer;
$strikeY=$y;
//does he win?
$wins=false;
//diagonale \
$count=1;
$x=$strikeX;
$y=$strikeY;
$cellsWin=array();
$cellsWin[]=array($x,$y);
while(($x > 0) && ($y < 5) && ($_SESSION['map'][$y + 1][$x - 1] == $you)){
$x--;
$y++;
$count++;
$cellsWin[]=array($x,$y);
}
$x=$strikeX;
$y=$strikeY;
while(($x < 6) && ($y > 0) && ($_SESSION['map'][$y - 1][$x + 1] == $you)){
$x++;
$y--;
$count++;
$cellsWin[]=array($x,$y);
}
if($count>3){
$wins=true;
}
//diagonale /
if(!$wins){
$count=1;
$x=$strikeX;
$y=$strikeY;
$cellsWin =array();
$cellsWin[]=array($x ,$y);
while(($x < 6) && ($y < 5) && ($_SESSION['map'][$y + 1][$x + 1 ] == $you)){
$x++;
$y++;
$count++;
$cellsWin[]=array($x,$y);
}
$x=$strikeX;
$y=$strikeY;
while(($x > 0) && ($y > 0) && ($_SESSION['map'][$y - 1][$x - 1 ] == $you)){
$x--;
$y--;
$count++;
$cellsWin[]=array($x,$y);
}
if($count>3){
$wins=true;
}
}
//horizontale
if(!$wins){
$count=1;
$x=$strikeX;
$y=$strikeY;
$cellsWin =array();
$cellsWin[]=array($x ,$y);
while(($x < 6) && ($_SESSION['map'][$y][$x + 1 ] == $you)){
$x++;
$count++;
$cellsWin[]=array($x,$y);
}
$x=$strikeX;
while(($x >0) && ($_SESSION['map'][$y][$x - 1 ] == $you)){
$count++;
$x--;
$cellsWin[]=array($x,$y);
}
if($count>3){
$wins=true;
}
}
//verticale
if(!$wins){
$count=1;
$x=$strikeX;
$y=$strikeY;
$cellsWin =array();
$cellsWin[]=array($x ,$y);
while(($y < 5) && ($_SESSION['map'][$y + 1 ][$x] == $you)){
$y++;
$count++;
$cellsWin[]=array($x,$y);
}
$y=$strikeY;
while(($y >0) && ($_SESSION['map'][$y - 1][$x] == $you)){
$count++;
$y--;
$cellsWin[]=array($x,$y);
}
if($count>3){
$wins=true;
}
}
if($wins){
$anwserToJS=array(
'continue' => 0,
'strikeX' => $strikeX,
'strikeY' => $strikeY,
'strikeSymbol'=> $you,
'log' => $you." ".$currentBotName." joue colonne ". $anwserPlayer." et a gagné",
'cellsWin' => json_encode($cellsWin),
'gameId' => $_SESSION['matchId']
);
if($_SESSION['currentPlayer']==1){
save_battle('connectFou',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],1);
}else{
save_battle('connectFou',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],2);
}
}else{
//Was it the last cell?
$full=true;
foreach ($_SESSION['map'][5] as $cell) {
if ($cell == ""){
$full=false;
break;
}
}
if($full){
save_battle('connectFou',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],0);
$anwserToJS=array(
'continue' => 0,
'strikeX' => $strikeX,
'strikeY' => $strikeY,
'strikeSymbol'=> $you,
'log' => $you." ".$currentBotName." joue colonne ". $anwserPlayer." match nul",
'gameId' => $_SESSION['matchId']
);
}else{
$anwserToJS=array(
'continue' => 1,
'strikeX' => $strikeX,
'strikeY' => $strikeY,
'strikeSymbol'=> $you,
'log' => $you." ".$currentBotName." joue colonne ". $anwserPlayer,
'gameId' => $_SESSION['matchId']
);
}
}
}else{
//reponse non conforme
$anwserToJS=array(
'continue' =>0,
'strikeX' => -1,
'strikeY' => -1,
'log' => $you." ".$currentBotName." made a non conform anwser: <br/>
Bots Arena sent:<em>".$tempPlayer['messageSend']."</em><br/>
".$currentBotName." HTTP STATUS:<em> ".$tempPlayer['httpStatus']."</em><br/>
His response: <em>".htmlentities($tempPlayer['response'])."</em>",
'gameId' => $_SESSION['matchId']
);
if($_SESSION['currentPlayer']==1){
save_battle('connectFou',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],2);
}else{
save_battle('connectFou',$_SESSION['bot1']['name'],$_SESSION['bot2']['name'],1);
}
}
echo json_encode($anwserToJS);
die;
break;
default:
break;
}

View File

@ -12,6 +12,11 @@ function Ajx(){
}
return request;
}
function addLog(message){
var p=createElem('p',{});
p.innerHTML=message;
document.getElementById('logs').appendChild(p);
}
function createElem(type,attributes){
var elem=document.createElement(type);
for (var i in attributes)
@ -21,10 +26,14 @@ function createElem(type,attributes){
function connectFour(bot1,bot2,xd_check, gameId, newGame){
if (newGame === undefined){
newGame = true;
gameId=0;
}
if (newGame){
gameId=0;
//don't touch the button while game is not ended
document.getElementById('fightButton').disabled=true;
//empty
while (document.getElementById('fightResult').firstChild) {
document.getElementById('fightResult').removeChild(document.getElementById('fightResult').firstChild);
@ -43,7 +52,7 @@ function connectFour(bot1,bot2,xd_check, gameId, newGame){
document.getElementById('fightResult').appendChild(table);
var divLogs=createElem("div",{"id":"logs"});
document.getElementById('fightResult').appendChild(divLogs);
}
}
//send request
var xhr = Ajx();
xhr.onreadystatechange = function(){if(xhr.readyState == 4){
@ -54,17 +63,18 @@ function connectFour(bot1,bot2,xd_check, gameId, newGame){
try{
var reponse = JSON.parse(xhr.responseText);
}catch(e){
document.getElementById('logs').innerHTML += 'erreur' +xhr.responseText;
addLog('erreur' +xhr.responseText);
return;
}
//log
document.getElementById('logs').innerHTML += reponse['log'] + '<br/>';
addLog(reponse['log']);
//fill the grid
if( reponse['strikeX'] > -1){
document.getElementById('td' + reponse['strikeX'] + '_' + reponse['strikeY']).innerHTML = reponse['strikeSymbol'];
}
//colorise cells that win
if(reponse['cellsWin'] === undefined){
}else{
@ -79,12 +89,15 @@ function connectFour(bot1,bot2,xd_check, gameId, newGame){
//if game isn't finished, continue
if(reponse['continue'] == 1){
connectFour(bot1,bot2,xd_check,reponse['gameId'], false);
}else{
document.getElementById('fightButton').disabled=false;
}
}else if(xhr.status == 512){
//just forget
return;
}else{
alert ('error ' + xhr.status);
document.getElementById('fightButton').disabled=false;
return;
}
}};

View File

@ -23,5 +23,5 @@ $bots=get_Bots_Array('connectFou');
?>
</select>
</p>
<p><input type="button" value="<?php echo $lang['FIGHT']; ?>" onclick="connectFour(document.getElementById('bot1').value,document.getElementById('bot2').value,'<?php echo xd_check_input(2); ?>',0);"></p>
<p><input id="fightButton" type="button" value="<?php echo $lang['FIGHT']; ?>" onclick="connectFour(document.getElementById('bot1').value,document.getElementById('bot2').value,'<?php echo xd_check_input(2); ?>',0);"></p>
<div id="fightResult"></div>

View File

@ -2,6 +2,7 @@
.tabledoc tr td,.tabledoc tr th{border: 1px solid green; padding-left: 5px;}
#fightResult{display:table;}
#logs{display:block;padding-left:10px; height: 200px; overflow-y: scroll;}
#logs p em {color: grey; font-size: 70%; test-transform:italic;}
.battleGrid{display:table-cell; padding-left:10px; border-collapse:collapse; margin: 20px 20px 20px 20px;}
.battleGrid tr{}
.battleGrid tr td{border: 1px dashed green; text-align: center; font-weight: bold;min-width:20px; height:20px;}

View File

@ -2,29 +2,29 @@
require_once(__DIR__."/functions.php");
switch ($_POST['act']){
case "fight":
$bots=get_Bots_Array('tictactoe');
//clean $_POST vars
$postParams=get_Post_Params(count($bots));
if(!$postParams){
error(400,"wrong parameters send");
die;
}else{
$bot1=$postParams['bot1'];
$bot2=$postParams['bot2'];
}
//init map
$map=array(
'0-0' => '','0-1' => '','0-2' => '',
'1-0' => '','1-1' => '','1-2' => '',
'2-0' => '','2-1' => '','2-2' => '');
$end=false;
//send init message to bots
$game_id = "".get_unique_id();
for($player = 0; $player < 2; $player++){
case "fight":
$bots=get_Bots_Array('tictactoe');
//clean $_POST vars
$postParams=get_Post_Params(count($bots));
if(!$postParams){
error(400,"wrong parameters send");
die;
}else{
$bot1=$postParams['bot1'];
$bot2=$postParams['bot2'];
}
//init map
$map=array(
'0-0' => '','0-1' => '','0-2' => '',
'1-0' => '','1-1' => '','1-2' => '',
'2-0' => '','2-1' => '','2-2' => '');
$end=false;
//send init message to bots
$game_id = "".get_unique_id();
for($player = 0; $player < 2; $player++){
$params[$player]=array(
'game-id' => $game_id,
'action' => 'init',
@ -32,106 +32,117 @@ switch ($_POST['act']){
'players' => 2,
'board' => '',
'player-index' => $player
);
}
get_IA_Response($bots[$bot1]['url'],$params[0]); //don't care about result
get_IA_Response($bots[$bot2]['url'],$params[1]); //don't care about result
$playerPlayingNow=1;
while($end==false){
switch($playerPlayingNow){
case 1:
$playerURL=$bots[$bot1]['url'];
$playerCHAR='X';
$playerName=$bots[$bot1]['name'];
$playerIndex=0;
break;
case 2:
$playerURL=$bots[$bot2]['url'];
$playerCHAR='O';
$playerName=$bots[$bot2]['name'];
$playerIndex=1;
break;
default:
error(500,"oups");
die;
}
$paramsToSend=array(
'game-id' => $game_id,
'action' => 'play-turn',
'game' => 'tictactoe',
'players' => 2,
'board' => $map,
'you' => $playerCHAR,
'player-index' =>$playerIndex
);
$playerResponse=get_IA_Response($playerURL,$paramsToSend)['play'];
//tester la validité de la réponse
if((isset($map[$playerResponse])) && ($map[$playerResponse]=="")){
//reponse conforme
echo "<p>".$playerName." joue en ".$playerResponse." la nouvelle grille est <br/>";
$map[$playerResponse]=$playerCHAR;
echo "<table>";
for($j=0;$j<3;$j++){
echo "<tr>";
for($i=0;$i<3;$i++){
echo '<td class="cellj'.$j.' celli'.$i.'">'.$map[$j.'-'.$i].'</td>';
}
echo "</tr>";
}
echo "</table>";
//tester si trois caracteres allignés
if(
(($map['0-0']==$map['0-1'])&&($map['0-1']==$map['0-2'])&&($map['0-2']!==""))
OR (($map['1-0']==$map['1-1'])&&($map['1-1']==$map['1-2'])&&($map['1-2']!==""))
OR (($map['2-0']==$map['2-1'])&&($map['2-1']==$map['2-2'])&&($map['2-2']!==""))
OR (($map['0-0']==$map['1-0'])&&($map['1-0']==$map['2-0'])&&($map['2-0']!==""))
OR (($map['0-1']==$map['1-1'])&&($map['1-1']==$map['2-1'])&&($map['2-1']!==""))
OR (($map['0-2']==$map['1-2'])&&($map['1-2']==$map['2-2'])&&($map['2-2']!==""))
OR (($map['0-0']==$map['1-1'])&&($map['1-1']==$map['2-2'])&&($map['2-2']!==""))
OR (($map['0-2']==$map['1-1'])&&($map['1-1']==$map['2-0'])&&($map['2-0']!==""))
){
echo "<p>".$playerName." ".$playerCHAR." a gagné.</p>";
save_battle('tictactoe',$bots[$bot1]['name'],$bots[$bot2]['name'],$playerPlayingNow);
$end=true;
break;
}
//tester si toutes les cases ne seraient pas prises
$full=true;
foreach($map as $char){
if($char==""){
$full=false;
break;
}
}
if($full){
echo "<p>Match nul</p>";
save_battle('tictactoe',$bots[$bot1]['name'],$bots[$bot2]['name'],0);
$end=true;
break;
}
//on change de joueur
if($playerPlayingNow==1){
$playerPlayingNow=2;
}else{
$playerPlayingNow=1;
}
}else{
echo "<p>".$playerName." a fait une réponse non conforme. Il perd.</p>";
break;
}
}
die;
break;
default:
break;
);
}
get_IA_Response($bots[$bot1]['url'],$params[0]); //don't care about result
get_IA_Response($bots[$bot2]['url'],$params[1]); //don't care about result
$playerPlayingNow=1;
while($end==false){
switch($playerPlayingNow){
case 1:
$playerURL=$bots[$bot1]['url'];
$playerCHAR='X';
$playerName=$bots[$bot1]['name'];
$playerIndex=0;
break;
case 2:
$playerURL=$bots[$bot2]['url'];
$playerCHAR='O';
$playerName=$bots[$bot2]['name'];
$playerIndex=1;
break;
default:
error(500,"oups");
die;
}
$paramsToSend=array(
'game-id' => $game_id,
'action' => 'play-turn',
'game' => 'tictactoe',
'players' => 2,
'board' => $map,
'you' => $playerCHAR,
'player-index' =>$playerIndex
);
$tempPlayer = get_IA_Response($playerURL,$paramsToSend);
if(isset($tempPlayer['responseArr']['play'])){
$playerResponse = $tempPlayer['responseArr']['play'];
}else{
$playerResponse = -1;
}
//tester la validité de la réponse
if((isset($map[$playerResponse])) && ($map[$playerResponse]=="")){
//reponse conforme
echo "<p>".$playerName." joue en ".$playerResponse." la nouvelle grille est <br/>";
$map[$playerResponse]=$playerCHAR;
echo "<table>";
for($j=0;$j<3;$j++){
echo "<tr>";
for($i=0;$i<3;$i++){
echo '<td class="cellj'.$j.' celli'.$i.'">'.$map[$j.'-'.$i].'</td>';
}
echo "</tr>";
}
echo "</table>";
//tester si trois caracteres allignés
if(
(($map['0-0']==$map['0-1'])&&($map['0-1']==$map['0-2'])&&($map['0-2']!==""))
OR (($map['1-0']==$map['1-1'])&&($map['1-1']==$map['1-2'])&&($map['1-2']!==""))
OR (($map['2-0']==$map['2-1'])&&($map['2-1']==$map['2-2'])&&($map['2-2']!==""))
OR (($map['0-0']==$map['1-0'])&&($map['1-0']==$map['2-0'])&&($map['2-0']!==""))
OR (($map['0-1']==$map['1-1'])&&($map['1-1']==$map['2-1'])&&($map['2-1']!==""))
OR (($map['0-2']==$map['1-2'])&&($map['1-2']==$map['2-2'])&&($map['2-2']!==""))
OR (($map['0-0']==$map['1-1'])&&($map['1-1']==$map['2-2'])&&($map['2-2']!==""))
OR (($map['0-2']==$map['1-1'])&&($map['1-1']==$map['2-0'])&&($map['2-0']!==""))
){
echo "<p>".$playerName." ".$playerCHAR." a gagné.</p>";
save_battle('tictactoe',$bots[$bot1]['name'],$bots[$bot2]['name'],$playerPlayingNow);
$end=true;
break;
}
//tester si toutes les cases ne seraient pas prises
$full=true;
foreach($map as $char){
if($char==""){
$full=false;
break;
}
}
if($full){
echo "<p>Match nul</p>";
save_battle('tictactoe',$bots[$bot1]['name'],$bots[$bot2]['name'],0);
$end=true;
break;
}
//on change de joueur
if($playerPlayingNow==1){
$playerPlayingNow=2;
}else{
$playerPlayingNow=1;
}
}else{
echo "<p>".$playerName." made a non conform anwser, he lost: <br/>
Bots Arena sent:<em>".$tempPlayer['messageSend']."</em><br/>
".$playerName." HTTP STATUS:<em> ".$tempPlayer['httpStatus']."</em><br/>
His response: <em>".htmlentities($tempPlayer['response'])."</em></p>";
break;
}
}
die;
break;
default:
break;
}

View File

@ -6,4 +6,5 @@ td {min-width: 10px; height: 10px;}
.arrenaMessage{color: green;}
.arrenaMessage::before{content: "arena message:"; color: grey; font-size: 70%; test-transform:italic;}
.botResponse{color: red;}
.botResponse::before{content: "bot response:";color: grey; font-size: 70%; test-transform:italic;}
.botResponse::before{content: "bot response:";color: grey; font-size: 70%; test-transform:italic;}
#fightResult p em {color: grey; font-size: 70%; test-transform:italic;}

View File

@ -280,42 +280,41 @@ function save_battle($game,$bot1,$bot2,$resultat){
}
function get_unique_id(){
//increment the number
$fp = fopen(__DIR__.'/../countBattles.txt', 'c+');
flock($fp, LOCK_EX);
$count = (int)fread($fp, filesize(__DIR__.'/../countBattles.txt'));
ftruncate($fp, 0);
fseek($fp, 0);
fwrite($fp, $count + 1);
flock($fp, LOCK_UN);
fclose($fp);
return $count;
}
function get_default_aside_content($currentArena){
global $lang;
$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>
<h2>Scores</h2>';
$podium=ELO_get_podium($currentArena);
$count=0;
$asideSectionContent.='<ul class="podium">';
$asideSectionContent.= '<ul class="podium">';
foreach($podium as $sc){
$count++;
switch($count){
case 1:
$img='<img src="/imgs/Gold_Medal.svg" alt="Gold_Medal.svg"/>';
$img = '<img src="/imgs/Gold_Medal.svg" alt="Gold_Medal.svg"/>';
break;
case 2:
$img='<img src="/imgs/Silver_Medal.svg" alt="Silver_Medal.svg"/>';
$img = '<img src="/imgs/Silver_Medal.svg" alt="Silver_Medal.svg"/>';
break;
case 3:
$img='<img src="/imgs/Bronze_Medal.svg" alt="Bronze_Medal.svg"/>';
$img = '<img src="/imgs/Bronze_Medal.svg" alt="Bronze_Medal.svg"/>';
break;
default:
$img='<img src="/imgs/Emoji_u1f4a9.svg" alt="caca"/>';
$img = '<img src="/imgs/Emoji_u1f4a9.svg" alt="caca"/>';
break;
}
@ -334,7 +333,8 @@ function does_arena_exist($string,$arenasArr){
}
function get_IA_Response($iaUrl,$postParams){
//send params JSON as body
// send params JSON as body
// return query detail on an array
$data_string = json_encode($postParams);
@ -348,8 +348,18 @@ function get_IA_Response($iaUrl,$postParams){
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$output= curl_exec($ch);
$httpCode = curl_getinfo($ch)['http_code'];
curl_close($ch);
//echo $iaUrl." ".$data_string." ".$output.'<br/>';
return json_decode($output,TRUE);
if(! $arr = json_decode($output,TRUE)){
$arr=array();
}
return array(
'messageSend' => $data_string,
'httpStatus' => $httpCode,
'response' => $output,
'responseArr' => $arr
);
}