Merge pull request #8 from gnieark/dev

Dev
pull/9/head
Gnieark 9 years ago
commit fb2383fedb

1
.gitignore vendored

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

@ -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`);

@ -66,6 +66,7 @@ switch ($_POST['act']){
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;
}
@ -79,6 +80,7 @@ switch ($_POST['act']){
}
if($full){
echo "<p>Match nul</p>";
save_battle('tictactoe',$bots[$bot1]['name'],$bots[$bot2]['name'],0);
$end=true;
break;
}
@ -100,4 +102,4 @@ switch ($_POST['act']){
break;
default:
break;
}
}

@ -4,4 +4,4 @@ $mysqlParams=array(
'user' => '',
'pass' => '',
'database'=>''
);
);

@ -90,7 +90,7 @@ function error($code,$message){
die;
case 500:
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;
default:
@ -98,4 +98,59 @@ function error($code,$message){
break;
}
}
}
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);
}

Loading…
Cancel
Save