From 06637cee3d6c0bb93d3909bbc3edf06221d4f892 Mon Sep 17 00:00:00 2001 From: Gnieark Date: Fri, 11 Dec 2015 17:22:43 +0100 Subject: [PATCH] clean post vars --- .gitignore | 1 + src/arenas/Battleship/act.php | 61 +++++++++++++++++++++++++++++++++++ src/functions.php | 13 ++++++++ 3 files changed, 75 insertions(+) diff --git a/.gitignore b/.gitignore index 38942cb..149ba46 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ src/config.php +src/countBattles.txt \ No newline at end of file diff --git a/src/arenas/Battleship/act.php b/src/arenas/Battleship/act.php index 9eae0c9..0a69db6 100644 --- a/src/arenas/Battleship/act.php +++ b/src/arenas/Battleship/act.php @@ -1,6 +1,67 @@ $p[2]) + ) + { + error(500,'wrong parameters'); + die; + } + $postValues[$p[0]]=$value; + + } + //check if bots exists + $bot1Exists = false; + $bot2Exists = false; + foreach($bots as $bot){ + if($bot['id'] == $_POST['bot1']){ + $bot1 = $bot; + $bot1Exists =true; + } + if($bot['id'] == $_POST['bot2']){ + $bot2 = $bot; + $bot2Exists =true; + } + if ($bot1Exists && $bot2Exists){ + break; + } + } + if ((!$bot1Exists) OR (!$bot2Exists)){ + error (500,"missing parameter"; + } + + //vars checked, lets init the initGame + + $_SESSION['matchId']=get_unique_id(); + + break; default: break; diff --git a/src/functions.php b/src/functions.php index 1e09187..ed1d5be 100644 --- a/src/functions.php +++ b/src/functions.php @@ -213,3 +213,16 @@ function save_battle($game,$bot1,$bot2,$resultat){ '1') ON DUPLICATE KEY UPDATE ".$field." = ".$field." + 1;"); } +function get_unique_id(){ + $fp = fopen(__DIR__'countBattles.txt', 'c+'); + flock($fp, LOCK_EX); + + $count = (int)fread($fp, filesize('count.txt')); + ftruncate($fp, 0); + fseek($fp, 0); + fwrite($fp, $count + 1); + + flock($fp, LOCK_UN); + fclose($fp); + return $count; +}