Merge pull request #8 from gnieark/dev

Dev
This commit is contained in:
Gnieark 2015-11-29 08:20:20 +01:00
commit fb2383fedb
5 changed files with 84 additions and 4 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
src/config.php

22
install.sql Normal file
View File

@ -0,0 +1,22 @@
CREATE TABLE IF NOT EXISTS `arena_history` (
`game` int(11) NOT NULL,
`player1_id` int(11) NOT NULL,
`player2_id` int(11) NOT NULL,
`player1_winsCount` int(11) NOT NULL,
`player2_winsCount` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `arena_history`
ADD PRIMARY KEY (`game`,`player1_id`,`player2_id`);
CREATE TABLE IF NOT EXISTS `bots` (
`id` int(11) NOT NULL,
`name` text NOT NULL,
`game` varchar(10) NOT NULL,
`url` int(11) NOT NULL,
`description` text NOT NULL,
`active` int(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `bots`
ADD PRIMARY KEY (`id`);

View File

@ -66,6 +66,7 @@ switch ($_POST['act']){
OR (($map['0-2']==$map['1-1'])&&($map['1-1']==$map['2-0'])&&($map['2-0']!=="")) OR (($map['0-2']==$map['1-1'])&&($map['1-1']==$map['2-0'])&&($map['2-0']!==""))
){ ){
echo "<p>".$playerName." ".$playerCHAR." a gagné.</p>"; echo "<p>".$playerName." ".$playerCHAR." a gagné.</p>";
save_battle('tictactoe',$bots[$bot1]['name'],$bots[$bot2]['name'],$playerPlayingNow);
$end=true; $end=true;
break; break;
} }
@ -79,6 +80,7 @@ switch ($_POST['act']){
} }
if($full){ if($full){
echo "<p>Match nul</p>"; echo "<p>Match nul</p>";
save_battle('tictactoe',$bots[$bot1]['name'],$bots[$bot2]['name'],0);
$end=true; $end=true;
break; break;
} }

View File

@ -90,7 +90,7 @@ function error($code,$message){
die; die;
case 500: case 500:
header ("HTTP/1.0 500 Internal Server Error"); header ("HTTP/1.0 500 Internal Server Error");
echo '<!DOCTYPE html><html lang="fr"><head><meta charset="UTF-8" /><title>Bad request</title></head><body><p>'.$message.'</p></body></html>'; echo '<!DOCTYPE html><html lang="fr"><head><meta charset="UTF-8" /><title>Internal Server Error</title></head><body><p>'.$message.'</p></body></html>';
die; die;
default: default:
@ -99,3 +99,58 @@ function error($code,$message){
} }
} }
function conn_bdd(){
require (__DIR__."/config.php");
if (!$linkMysql=mysqli_connect($mysqlParams['host'], $mysqlParams['user'], $mysqlParams['pass'])) {
error(500,'database connexion failed');
die;
}
mysqli_select_db($linkMysql,$mysqlParams['database']);
mysqli_set_charset($linkMysql, 'utf8');
return $linkMysql; //does PHP can do that?
}
function save_battle($game,$bot1,$bot2,$resultat){
//resultat: 0 match nul, 1 bot1 gagne 2 bot 2 gagne
$lnMysql=conn_bdd();
//chercher les id de bot 1 et bot2
$rs=mysqli_query($lnMysql,"SELECT name,id FROM bots
WHERE name='".mysqli_real_escape_string($lnMysql,$bot1)."'
OR name='".mysqli_real_escape_string($lnMysql,$bot2)."'");
while($r=mysqli_fetch_row($rs)){
$bots[$r[0]]=$r[1];
}
if((!isset($bots[$bot1])) OR (!isset($bots[$bot2]))){
error (500,"database corrupt");
die;
}
switch($resultat){
case 0:
$field="nulCount";
break;
case 1:
$field="player1_winsCount";
break;
case 2:
$field="player2_winsCount";
break;
default:
error (500,"something impossible has happened");
break;
}
mysqli_query($lnMysql,
"INSERT INTO arena_history(game,player1_id,player2_id,".$field.") VALUES
('".mysqli_real_escape_string($lnMysql,$game)."',
'".$bots[$bot1]."',
'".$bots[$bot2]."',
'1')
ON DUPLICATE KEY UPDATE ".$field." = ".$field." + 1;");
mysqli_close($lnMysql);
}