From e81e9efc4683a5f1b071e867ad40e42774b889e6 Mon Sep 17 00:00:00 2001 From: Gnieark Date: Sat, 28 Nov 2015 19:16:35 +0100 Subject: [PATCH 01/10] gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..38942cb --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +src/config.php From 1dd15ec617a31cccd89e7005e0478aed063d90df Mon Sep 17 00:00:00 2001 From: Gnieark Date: Sat, 28 Nov 2015 19:17:09 +0100 Subject: [PATCH 02/10] gitignore test --- src/config.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/config.php b/src/config.php index 2b76525..3979dcf 100644 --- a/src/config.php +++ b/src/config.php @@ -1,7 +1,7 @@ 'localhost', - 'user' => '', + 'user' => 'sdkjfhdskjfh', 'pass' => '', 'database'=>'' -); \ No newline at end of file +); From 26821297389641ff3dab6d5992e4dd1b9b68e40d Mon Sep 17 00:00:00 2001 From: Gnieark Date: Sat, 28 Nov 2015 19:20:40 +0100 Subject: [PATCH 03/10] gitignore test --- src/config.php | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 src/config.php diff --git a/src/config.php b/src/config.php deleted file mode 100644 index 3979dcf..0000000 --- a/src/config.php +++ /dev/null @@ -1,7 +0,0 @@ - 'localhost', - 'user' => 'sdkjfhdskjfh', - 'pass' => '', - 'database'=>'' -); From 94f14872ed5714a50770564073dc6dbbf1a978bc Mon Sep 17 00:00:00 2001 From: Gnieark Date: Sat, 28 Nov 2015 19:22:48 +0100 Subject: [PATCH 04/10] config --- src/config.php.empty | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/config.php.empty diff --git a/src/config.php.empty b/src/config.php.empty new file mode 100644 index 0000000..10830b4 --- /dev/null +++ b/src/config.php.empty @@ -0,0 +1,7 @@ + 'localhost', + 'user' => '', + 'pass' => '', + 'database'=>'' +); From 364932316ad6745f42f824d0c62bda336a956f60 Mon Sep 17 00:00:00 2001 From: Gnieark Date: Sat, 28 Nov 2015 20:14:10 +0100 Subject: [PATCH 05/10] sql structure --- install.sql | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 install.sql diff --git a/install.sql b/install.sql new file mode 100644 index 0000000..22baddd --- /dev/null +++ b/install.sql @@ -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`); + From b7512b3a50c237dc95bbd1db7320c70c442f7d81 Mon Sep 17 00:00:00 2001 From: gnieark Date: Sun, 29 Nov 2015 00:03:07 +0100 Subject: [PATCH 06/10] save_battle --- src/functions.php | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/functions.php b/src/functions.php index e73c20a..4dca275 100644 --- a/src/functions.php +++ b/src/functions.php @@ -98,4 +98,66 @@ function error($code,$message){ break; } +} +function conn_bdd(){ + require (__DIR__."/config.php"); + $mysqlParams=array( + 'host' => 'localhost', + 'user' => '', + 'pass' => '', + 'database'=>'' + ); + + if (!$linkMysql=mysqli_connect($mysqlParams['host'], $mysqlParams['user'], $mysqlParams['pass'])) { + error(500,'database connexion failed'); + die; + } + mysqli_select_db($linkMysql,$mysqlParams['mysql_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); } \ No newline at end of file From 9a542943b4e9d02ac15c565736402d2d38cd3ee7 Mon Sep 17 00:00:00 2001 From: gnieark Date: Sun, 29 Nov 2015 00:04:13 +0100 Subject: [PATCH 07/10] oups --- src/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/functions.php b/src/functions.php index 4dca275..c903ff0 100644 --- a/src/functions.php +++ b/src/functions.php @@ -153,7 +153,7 @@ function save_battle($game,$bot1,$bot2,$resultat){ mysqli_query($lnMysql, "INSERT INTO arena_history(game,player1_id,player2_id,".$field.") VALUES - ('"..mysqli_real_escape_string($lnMysql,$game)."', + ('".mysqli_real_escape_string($lnMysql,$game)."', '".$bots[$bot1]."', '".$bots[$bot2]."', '1') From 4ca9c500e46aeec989217475b7de7f82a8525ef7 Mon Sep 17 00:00:00 2001 From: gnieark Date: Sun, 29 Nov 2015 00:11:21 +0100 Subject: [PATCH 08/10] . --- src/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/functions.php b/src/functions.php index c903ff0..a09cd99 100644 --- a/src/functions.php +++ b/src/functions.php @@ -90,7 +90,7 @@ function error($code,$message){ die; case 500: header ("HTTP/1.0 500 Internal Server Error"); - echo 'Bad request

'.$message.'

'; + echo 'Internal Server Error

'.$message.'

'; die; default: From f3ad6a9ee15eb2cb5b8e221d41e424c3efad5d4b Mon Sep 17 00:00:00 2001 From: gnieark Date: Sun, 29 Nov 2015 07:58:04 +0100 Subject: [PATCH 09/10] history --- src/arenas/tictactoe/act.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/arenas/tictactoe/act.php b/src/arenas/tictactoe/act.php index 9a72928..015765a 100644 --- a/src/arenas/tictactoe/act.php +++ b/src/arenas/tictactoe/act.php @@ -66,6 +66,7 @@ switch ($_POST['act']){ OR (($map['0-2']==$map['1-1'])&&($map['1-1']==$map['2-0'])&&($map['2-0']!=="")) ){ echo "

".$playerName." ".$playerCHAR." a gagné.

"; + save_battle('tictactoe',$bot1,$bot2,$playerPlayingNow); $end=true; break; } @@ -79,6 +80,7 @@ switch ($_POST['act']){ } if($full){ echo "

Match nul

"; + save_battle('tictactoe',$bot1,$bot2,0); $end=true; break; } From fec759b4b9391d6d2404c2a8cb2a88e00a9d0892 Mon Sep 17 00:00:00 2001 From: Gnieark Date: Sun, 29 Nov 2015 08:19:06 +0100 Subject: [PATCH 10/10] fixes --- src/arenas/tictactoe/act.php | 6 +++--- src/functions.php | 13 +++---------- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/src/arenas/tictactoe/act.php b/src/arenas/tictactoe/act.php index 015765a..38e919e 100644 --- a/src/arenas/tictactoe/act.php +++ b/src/arenas/tictactoe/act.php @@ -66,7 +66,7 @@ switch ($_POST['act']){ OR (($map['0-2']==$map['1-1'])&&($map['1-1']==$map['2-0'])&&($map['2-0']!=="")) ){ echo "

".$playerName." ".$playerCHAR." a gagné.

"; - save_battle('tictactoe',$bot1,$bot2,$playerPlayingNow); + save_battle('tictactoe',$bots[$bot1]['name'],$bots[$bot2]['name'],$playerPlayingNow); $end=true; break; } @@ -80,7 +80,7 @@ switch ($_POST['act']){ } if($full){ echo "

Match nul

"; - save_battle('tictactoe',$bot1,$bot2,0); + save_battle('tictactoe',$bots[$bot1]['name'],$bots[$bot2]['name'],0); $end=true; break; } @@ -102,4 +102,4 @@ switch ($_POST['act']){ break; default: break; -} \ No newline at end of file +} diff --git a/src/functions.php b/src/functions.php index a09cd99..fe4b7f0 100644 --- a/src/functions.php +++ b/src/functions.php @@ -101,18 +101,12 @@ function error($code,$message){ } function conn_bdd(){ require (__DIR__."/config.php"); - $mysqlParams=array( - 'host' => 'localhost', - 'user' => '', - 'pass' => '', - 'database'=>'' - ); if (!$linkMysql=mysqli_connect($mysqlParams['host'], $mysqlParams['user'], $mysqlParams['pass'])) { error(500,'database connexion failed'); die; } - mysqli_select_db($linkMysql,$mysqlParams['mysql_database']); + mysqli_select_db($linkMysql,$mysqlParams['database']); mysqli_set_charset($linkMysql, 'utf8'); return $linkMysql; //does PHP can do that? @@ -125,8 +119,7 @@ function save_battle($game,$bot1,$bot2,$resultat){ //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)."'"); - + OR name='".mysqli_real_escape_string($lnMysql,$bot2)."'"); while($r=mysqli_fetch_row($rs)){ $bots[$r[0]]=$r[1]; } @@ -160,4 +153,4 @@ function save_battle($game,$bot1,$bot2,$resultat){ ON DUPLICATE KEY UPDATE ".$field." = ".$field." + 1;"); mysqli_close($lnMysql); -} \ No newline at end of file +}