2015-11-25 22:24:41 +01:00
< ? php
2015-11-26 19:38:09 +01:00
function get_arenas_list (){
2015-11-25 22:24:41 +01:00
include ( __DIR__ . " /arenas_lists.php " );
return $arenas ;
2015-11-26 19:38:09 +01:00
}
2015-11-30 22:30:51 +01:00
function get_Bots_Array ( $arena , $activeOnly = true ){
2015-11-30 22:28:20 +01:00
global $lnMysql ;
//$bots[]=array("name" => $name, "url" =>$url);
if ( $activeOnly ){
$addClause = " AND active='1' " ;
} else {
$addClause = " " ;
}
$rs = mysqli_query ( $lnMysql ,
" SELECT id,name,url,description FROM bots WHERE game=' " . mysqli_real_escape_string ( $lnMysql , $arena ) . " ' " . $addClause );
$bots = array ();
while ( $r = mysqli_fetch_row ( $rs )){
$bots [] = array (
'id' => $r [ 0 ],
'name' => $r [ 1 ],
'url' => $r [ 2 ],
'description' => $r [ 3 ]
2015-11-30 22:31:21 +01:00
);
2015-11-30 22:28:20 +01:00
}
return $bots ;
}
2015-11-27 17:18:12 +01:00
function rand_str ( $length = 32 , $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890' ){
$chars_length = ( strlen ( $chars ) - 1 );
$string = $chars { rand ( 0 , $chars_length )};
for ( $i = 1 ; $i < $length ; $i = strlen ( $string )){
$r = $chars { rand ( 0 , $chars_length )};
if ( $r != $string { $i - 1 }) $string .= $r ;
}
return $string ;
}
2015-11-30 22:28:20 +01:00
2015-11-27 17:18:12 +01:00
function xd_check_input ( $id = 1 ){
/*
* On génére un hash aléatoire qui sera
* ajouté aux formulaires , afin d ' ajouter
* une vérification supplémentaire
* lors du traitement de ce dernier
*/
/*
* le parametre $id permet de selectionner le type de retour
* 0 => un input type hidden sans id
* 1 => un input type hidden avec id
* 2 => juste la valeur
*/
if ( ! isset ( $_SESSION [ 'xd_check' ])){
//le générer
$_SESSION [ 'xd_check' ] = rand_str ( 25 );
}
switch ( $id ){
case 0 :
return " <input type= \" hidden \" name= \" xd_check \" value= \" " . $_SESSION [ 'xd_check' ] . " \" /> " ;
break ;
case 1 :
return " <input type= \" hidden \" name= \" xd_check \" id= \" xd_check \" value= \" " . $_SESSION [ 'xd_check' ] . " \" /> " ;
break ;
case 2 :
return $_SESSION [ 'xd_check' ];
break ;
default :
return " <input type= \" hidden \" name= \" xd_check \" id= \" xd_check \" value= \" " . $_SESSION [ 'xd_check' ] . " \" /> " ;
break ;
}
}
2015-11-26 19:38:09 +01:00
function get_language_array (){
/*
* Choisir la langue de l ' utilisateur
* en priorisant parametre GET , cookie , info du navigateur
* Retourner l ' array contenant les bonnes traductions
*/
$langsAvailable = array ( 'fr' , 'en' );
$language = " " ;
2015-11-28 18:27:41 +01:00
if ( isset ( $_GET [ 'lang' ]) ){
$language = $_GET [ 'lang' ];
2015-11-26 19:38:09 +01:00
setcookie ( 'lang' , $language , time () + 60 * 60 * 24 * 30 );
} elseif ( isset ( $_COOKIE [ 'lang' ]) ){
$language = $_COOKIE [ 'lang' ];
} else {
if ( in_array ( substr ( $_SERVER [ 'HTTP_ACCEPT_LANGUAGE' ], 0 , 2 ), $langsAvailable )){
$language = substr ( $_SERVER [ 'HTTP_ACCEPT_LANGUAGE' ], 0 , 2 );
}
}
if ( ! in_array ( $language , $langsAvailable )){
$language = " en " ;
}
include ( __DIR__ . " /../lang/ " . $language . " .php " );
return $lang ;
2015-11-27 22:25:57 +01:00
}
function error ( $code , $message ){
2015-11-27 22:27:02 +01:00
switch ( $code ){
2015-11-27 22:25:57 +01:00
case 404 :
header ( " HTTP/1.0 404 Not Found " );
2016-06-04 23:45:32 +02:00
echo '<!DOCTYPE html><html lang="fr"><head><meta charset="UTF-8" /><title>Page Not found</title></head><body><p>' . $message . ' </ p >
< pre >
_ _ ___ _ _
| || | / _ \ | || |
| || | _ | | | | || | _
| __ _ | | | | __ _ |
| | | | _ | | | |
| _ | \___ / | _ |
</ pre >< p >< a href = " / " > Go to home page </ a ></ p ></ body ></ html > ' ;
2015-11-27 22:25:57 +01:00
die ;
2015-11-28 14:54:07 +01:00
case 400 :
header ( " HTTP/1.0 400 Bad Request " );
echo '<!DOCTYPE html><html lang="fr"><head><meta charset="UTF-8" /><title>Bad request</title></head><body><p>' . $message . '</p></body></html>' ;
die ;
2015-11-28 15:10:35 +01:00
case 500 :
header ( " HTTP/1.0 500 Internal Server Error " );
2015-11-29 00:11:21 +01:00
echo '<!DOCTYPE html><html lang="fr"><head><meta charset="UTF-8" /><title>Internal Server Error</title></head><body><p>' . $message . '</p></body></html>' ;
2015-11-28 15:10:35 +01:00
die ;
2015-11-27 22:25:57 +01:00
default :
2015-11-28 14:54:07 +01:00
2015-11-27 22:25:57 +01:00
die ;
break ;
}
2015-11-29 00:03:07 +01:00
}
function conn_bdd (){
require ( __DIR__ . " /config.php " );
if ( ! $linkMysql = mysqli_connect ( $mysqlParams [ 'host' ], $mysqlParams [ 'user' ], $mysqlParams [ 'pass' ])) {
error ( 500 , 'database connexion failed' );
die ;
}
2015-11-29 08:19:06 +01:00
mysqli_select_db ( $linkMysql , $mysqlParams [ 'database' ]);
2015-11-29 00:03:07 +01:00
mysqli_set_charset ( $linkMysql , 'utf8' );
return $linkMysql ; //does PHP can do that?
}
2015-11-29 09:42:34 +01:00
function get_battles_history ( $game ){
2015-11-30 00:16:01 +01:00
global $lnMysql ;
2015-11-29 11:25:47 +01:00
$game = substr ( $game , 0 , 8 ); //limit 8 char for limitting mysql index size
2015-11-30 00:16:01 +01:00
2015-11-29 09:42:34 +01:00
$rs = mysqli_query ( $lnMysql ,
" SELECT
player1 . name ,
player2 . name ,
arena_history . player1_winsCount ,
arena_history . player2_winsCount ,
arena_history . nulCount
FROM
bots as player1 ,
bots as player2 ,
arena_history
WHERE
player1 . id = arena_history . player1_id
AND player2 . id = arena_history . player2_id
AND arena_history . game = '".mysqli_real_escape_string($lnMysql,$game)."' ; "
);
$results = array ();
while ( $r = mysqli_fetch_row ( $rs )){
$results [] = array (
'bot1' => $r [ 0 ],
'bot2' => $r [ 1 ],
'player1Wins' => $r [ 2 ],
'player2Wins' => $r [ 3 ],
'draws' => $r [ 4 ]
);
}
return $results ;
}
2016-06-10 00:21:57 +02:00
function ELO_get_podium ( $arena ){
global $lnMysql ;
$podium = array ();
2016-06-10 11:10:04 +02:00
$rs = mysqli_query ( $lnMysql , " SELECT id,name,description,ELO FROM bots WHERE game=' " . substr ( $arena , 0 , 10 ) . " ' AND active='1' ORDER BY ELO DESC, name " );
2016-06-10 00:21:57 +02:00
while ( $r = mysqli_fetch_row ( $rs )){
$podium [] = array (
'id' => $r [ 0 ],
'name' => $r [ 1 ],
'description' => $r [ 2 ],
'ELO' => $r [ 3 ]
);
}
return $podium ;
}
2016-06-09 22:44:58 +02:00
function ELO_get_k ( $elo ){
if ( $elo < 1000 ){
return 80 ;
}
if ( $elo < 2000 ){
return 50 ;
}
if ( $elo <= 2400 ){
return 30 ;
}
return 20 ;
}
function ELO_get_new_ranks ( $elo1 , $elo2 , $score ){
/*
* return an array containing new ELO scores after a battle
* $score : 0 player 2 won
* 0.5 draws
* 1 player 1 won
*/
//good luck for understanding it
//(see https://blog.antoine-augusti.fr/2012/06/maths-et-code-le-classement-elo/)
return array (
2016-06-09 22:50:40 +02:00
$elo1 + ELO_get_k ( $elo1 ) * ( $score - ( 1 / ( 1 + pow ( 10 ,(( $elo2 - $elo1 ) / 400 ))))),
2016-06-09 22:53:04 +02:00
$elo2 + ELO_get_k ( $elo2 ) * ( 1 - $score - ( 1 / ( 1 + pow ( 10 ,(( $elo1 - $elo2 ) / 400 )))))
2016-06-09 22:44:58 +02:00
);
}
2015-11-29 00:03:07 +01:00
function save_battle ( $game , $bot1 , $bot2 , $resultat ){
//resultat: 0 match nul, 1 bot1 gagne 2 bot 2 gagne
2015-11-30 00:16:01 +01:00
global $lnMysql ;
2015-11-29 11:25:47 +01:00
$game = substr ( $game , 0 , 8 ); //limit 8 char for limitting mysql index size
2015-11-29 00:03:07 +01:00
2015-11-30 00:16:01 +01:00
2015-11-29 00:03:07 +01:00
//chercher les id de bot 1 et bot2
2016-06-09 22:44:58 +02:00
$rs = mysqli_query ( $lnMysql , " SELECT name,id,ELO FROM bots
2015-11-29 00:03:07 +01:00
WHERE name = '".mysqli_real_escape_string($lnMysql,$bot1)."'
2015-11-29 08:19:06 +01:00
OR name = '".mysqli_real_escape_string($lnMysql,$bot2)."' " );
2015-11-29 00:03:07 +01:00
while ( $r = mysqli_fetch_row ( $rs )){
$bots [ $r [ 0 ]] = $r [ 1 ];
2016-06-09 22:44:58 +02:00
$actualELO [ $r [ 0 ]] = $r [ 2 ];
2015-11-29 00:03:07 +01:00
}
if (( ! isset ( $bots [ $bot1 ])) OR ( ! isset ( $bots [ $bot2 ]))){
error ( 500 , " database corrupt " );
die ;
}
switch ( $resultat ){
case 0 :
$field = " nulCount " ;
2016-06-09 22:44:58 +02:00
$eloScore = 0.5 ;
2015-11-29 00:03:07 +01:00
break ;
case 1 :
$field = " player1_winsCount " ;
2016-06-09 22:44:58 +02:00
$eloScore = 1 ;
2015-11-29 00:03:07 +01:00
break ;
case 2 :
$field = " player2_winsCount " ;
2016-06-09 22:44:58 +02:00
$eloScore = 0 ;
2015-11-29 00:03:07 +01:00
break ;
default :
error ( 500 , " something impossible has happened " );
break ;
}
2016-06-09 22:44:58 +02:00
$newRanks = ELO_get_new_ranks ( $actualELO [ $bot1 ], $actualELO [ $bot2 ], $eloScore );
2016-06-09 22:50:40 +02:00
2016-06-09 22:44:58 +02:00
mysqli_multi_query ( $lnMysql ,
"
UPDATE bots
SET ELO = '".$newRanks[0]."'
WHERE id = '".$bots[$bot1]."' ;
UPDATE bots
SET ELO = '".$newRanks[1]."'
WHERE id = '".$bots[$bot2]."' ;
INSERT INTO arena_history ( game , player1_id , player2_id , " . $field . " ) VALUES
2015-11-29 00:04:13 +01:00
( '".mysqli_real_escape_string($lnMysql,$game)."' ,
2015-11-29 00:03:07 +01:00
'".$bots[$bot1]."' ,
'".$bots[$bot2]."' ,
'1' )
ON DUPLICATE KEY UPDATE " . $field . " = " . $field . " + 1 ; " );
2016-06-09 22:44:58 +02:00
2015-11-29 08:19:06 +01:00
}
2015-12-11 17:22:43 +01:00
function get_unique_id (){
2016-06-12 18:11:05 +02:00
//increment the number
2015-12-12 11:16:07 +01:00
2015-12-12 11:20:33 +01:00
$fp = fopen ( __DIR__ . '/../countBattles.txt' , 'c+' );
2015-12-11 17:22:43 +01:00
flock ( $fp , LOCK_EX );
2015-12-12 11:20:33 +01:00
$count = ( int ) fread ( $fp , filesize ( __DIR__ . '/../countBattles.txt' ));
2015-12-11 17:22:43 +01:00
ftruncate ( $fp , 0 );
fseek ( $fp , 0 );
fwrite ( $fp , $count + 1 );
flock ( $fp , LOCK_UN );
fclose ( $fp );
return $count ;
}
2016-06-10 18:33:18 +02:00
function get_default_aside_content ( $currentArena ){
2016-06-10 18:34:31 +02:00
global $lang ;
2016-06-12 18:11:05 +02:00
$asideSectionContent = '<h2>infos:</h2><p>' . $lang [ 'DEV-YOUR-OWN-BOT' ] . '<br/> <a href="/' . $currentArena . '/doc">' . $lang [ 'DOC_SPECS_LINKS' ] . ' </ a ></ p >
2016-06-10 18:33:18 +02:00
< h2 > Scores </ h2 > ' ;
$podium = ELO_get_podium ( $currentArena );
$count = 0 ;
2016-06-12 18:11:05 +02:00
$asideSectionContent .= '<ul class="podium">' ;
2016-06-10 18:33:18 +02:00
foreach ( $podium as $sc ){
$count ++ ;
switch ( $count ){
case 1 :
2016-06-12 18:11:05 +02:00
$img = '<img src="/imgs/Gold_Medal.svg" alt="Gold_Medal.svg"/>' ;
2016-06-10 18:33:18 +02:00
break ;
case 2 :
2016-06-12 18:11:05 +02:00
$img = '<img src="/imgs/Silver_Medal.svg" alt="Silver_Medal.svg"/>' ;
2016-06-10 18:33:18 +02:00
break ;
case 3 :
2016-06-12 18:11:05 +02:00
$img = '<img src="/imgs/Bronze_Medal.svg" alt="Bronze_Medal.svg"/>' ;
2016-06-10 18:33:18 +02:00
break ;
default :
2016-06-12 18:11:05 +02:00
$img = '<img src="/imgs/Emoji_u1f4a9.svg" alt="caca"/>' ;
2016-06-10 18:33:18 +02:00
break ;
}
$asideSectionContent .= '<li>' . $img . ' <a href="/p/aboutBot/' . urlencode ( htmlentities (( $sc [ 'name' ]))) . '">' . htmlentities ( $sc [ 'name' ]) . '</a> ELO rank: ' . $sc [ 'ELO' ] . '</li>' ;
}
$asideSectionContent .= '</ul><p><a href="/' . $currentArena . '/scores">Détail des matchs >></a></p>' ;
return $asideSectionContent ;
}
2015-12-27 19:50:21 +01:00
function does_arena_exist ( $string , $arenasArr ){
foreach ( $arenasArr as $arena ){
if ( $string == $arena [ 'id' ]){
return true ;
}
}
return false ;
2016-06-11 17:52:13 +02:00
}
2016-06-12 21:11:28 +02:00
function get_IA_Response ( $iaUrl , $postParams ){
// send params JSON as body
// return query detail on an array
2016-06-11 17:52:13 +02:00
$data_string = json_encode ( $postParams );
$ch = curl_init ( $iaUrl );
curl_setopt ( $ch , CURLOPT_CUSTOMREQUEST , " POST " );
curl_setopt ( $ch , CURLOPT_SSL_VERIFYHOST , false );
curl_setopt ( $ch , CURLOPT_SSL_VERIFYPEER , false );
curl_setopt ( $ch , CURLOPT_POSTFIELDS , $data_string );
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , true );
curl_setopt ( $ch , CURLOPT_HTTPHEADER , array (
'Content-Type: application/json' ,
'Content-Length: ' . strlen ( $data_string ))
);
2016-06-12 21:11:28 +02:00
//$httpCode = curl_getinfo($ch)['http_code'];
$output = curl_exec ( $ch );
2016-06-11 17:52:13 +02:00
curl_close ( $ch );
2016-06-12 21:11:28 +02:00
if ( ! $arr = json_decode ( $output , TRUE )){
$arr = array ();
2016-06-12 18:11:05 +02:00
}
2016-06-12 21:11:28 +02:00
return array (
'messageSend' => $data_string ,
2016-06-12 21:12:34 +02:00
'httpStatus' => curl_getinfo ( $ch )[ 'http_code' ],
2016-06-12 21:11:28 +02:00
'response' => $output ,
'responseArr' => $arr
);
2015-12-27 19:50:21 +01:00
}