aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--backend/auth.php4
-rw-r--r--backend/index.php37
-rw-r--r--backend/join.php39
3 files changed, 63 insertions, 17 deletions
diff --git a/backend/auth.php b/backend/auth.php
index cf646d8..36ddc86 100644
--- a/backend/auth.php
+++ b/backend/auth.php
@@ -25,7 +25,7 @@ elseif (!isset($wrkarr["password"]) || trim($wrkarr["password"]) == ""):
die('{ "status": "password was not provided.\ncould not attempt to log in." }');
else:
- $query = $pdo->prepare("SELECT name, password FROM users WHERE name LIKE ?");
+ $query = $pdo->prepare("SELECT name, password, token, team FROM users WHERE name LIKE ?");
$query -> execute([$wrkarr["name"]]);
$found = $query->fetch(PDO::FETCH_ASSOC);
if ($found):
@@ -34,7 +34,7 @@ else:
die('{ "status": "wrong password." }');
else:
- die('{ "status": "success" }');
+ die("{ \"status\": \"success\", \"token\": \"$found[token]\", \"team\": \"$found[team]\" }");
endif;
diff --git a/backend/index.php b/backend/index.php
index dead696..2aba190 100644
--- a/backend/index.php
+++ b/backend/index.php
@@ -4,14 +4,47 @@ header('Content-Type: application/json; charset=UTF-8');
try {
$pdo = new PDO("mysql:host=127.0.0.1;dbname=hitlerclicker", "root", "aA1234Aa");
} catch(PDOException $e) {
+ echo "$e";
die('{ "status": "database offline" }');
};
+/*
+$query = $pdo->prepare("DROP TABLE `hitlerclicker`.`stats`");
+$query->execute();
+$query = $pdo->prepare("DROP TABLE `hitlerclicker`.`users`");
+$query->execute();
+*/
+
$query = $pdo->prepare("CREATE TABLE IF NOT EXISTS `hitlerclicker`.`stats` ( `team` VARCHAR(256) NOT NULL DEFAULT uuid() , `clicks` INT(128) unsigned zerofill NOT NULL DEFAULT '0', PRIMARY KEY (`team`) ) ENGINE = InnoDB;");
$query->execute();
-$query = $pdo->prepare("CREATE TABLE IF NOT EXISTS `hitlerclicker`.`users` ( `name` VARCHAR(256) NOT NULL DEFAULT uuid() , `password` VARCHAR(256) NOT NULL , `team` VARCHAR(256) NOT NULL DEFAULT 'axis' , `clicks` INT(128) unsigned zerofill NOT NULL DEFAULT '0', PRIMARY KEY (`name`) ) ENGINE = InnoDB;");
+$query = $pdo->prepare("CREATE TABLE IF NOT EXISTS `hitlerclicker`.`users` ( `name` VARCHAR(256) NOT NULL DEFAULT uuid() , `password` VARCHAR(256) NOT NULL , `token` VARCHAR(256) NOT NULL DEFAULT uuid() , `team` VARCHAR(256) NOT NULL DEFAULT 'axis' , `clicks` INT(128) unsigned NOT NULL DEFAULT '0', PRIMARY KEY (`name`) ) ENGINE = InnoDB;");
+$query->execute();
+
+$query = $pdo->prepare("INSERT IGNORE INTO `stats` (`team`) VALUES (:team)");
+$query->execute([ "team" => "axis" ]);
+$query = $pdo->prepare("INSERT IGNORE INTO `stats` (`team`) VALUES (:team)");
+$query->execute([ "team" => "allies" ]);
+$query = $pdo->prepare("INSERT IGNORE INTO `stats` (`team`) VALUES (:team)");
+$query->execute([ "team" => "soviet" ]);
+
+$query = $pdo->prepare("SELECT team, clicks FROM stats WHERE team LIKE '%' ORDER BY clicks DESC");
$query->execute();
+$found = $query->fetchALL(PDO::FETCH_ASSOC);
+
+echo '{ "status": "online", ';
+
+$i = 0;
+foreach($found as $row):
+
+ $i++;
+ if ($i === sizeof($found)) {
+ echo " \"$row[team]\": \"$row[clicks]\" ";
+ } else {
+ echo " \"$row[team]\": \"$row[clicks]\", ";
+ };
+
+endforeach;
-echo '{ "status": "online" }';
+echo ' }';
?>
diff --git a/backend/join.php b/backend/join.php
index 2290f26..4a8c455 100644
--- a/backend/join.php
+++ b/backend/join.php
@@ -8,7 +8,7 @@ try {
};
$wrkarr = [];
-$ifarr = [ "name", "password" ];
+$ifarr = [ "name", "password", "team" ];
$postjson = json_decode(file_get_contents('php://input'), true);
foreach ($ifarr as $i):
if (isset($postjson[$i])):
@@ -23,21 +23,34 @@ if (!isset($wrkarr["name"]) || trim($wrkarr["name"]) == ""):
die('{ "status": "login (name) was not provided.\ncould not attempt to join." }');
elseif (!isset($wrkarr["password"]) || trim($wrkarr["password"]) == ""):
die('{ "status": "password was not provided.\ncould not attempt to join." }');
+elseif (!isset($wrkarr["team"]) || trim($wrkarr["team"]) == ""):
+ die('{ "status": "team was not provided.\ncould not attempt to join." }');
else:
- $query = $pdo->prepare("SELECT name, password FROM users WHERE name LIKE ?");
- $query -> execute([$wrkarr["name"]]);
+ $query = $pdo->prepare("SELECT team, clicks FROM stats WHERE team LIKE ? ORDER BY clicks DESC");
+ $query->execute([$wrkarr["team"]]);
$found = $query->fetch(PDO::FETCH_ASSOC);
- if ($found) {
- die('{ "status": "name already exists in the database." }');
- }
-
- $query = $pdo->prepare("INSERT INTO users (name, password) VALUES (:name, :password)");
- $query->execute([
- "name" => filter_var($wrkarr["name"]),
- "password" => password_hash($wrkarr["password"], PASSWORD_DEFAULT),
- ]);
- die('{ "status": "success" }');
+ if ($found):
+
+ $query = $pdo->prepare("SELECT name, password FROM users WHERE name LIKE ?");
+ $query -> execute([$wrkarr["name"]]);
+ $found = $query->fetch(PDO::FETCH_ASSOC);
+ if ($found) {
+ die('{ "status": "name already exists in the database." }');
+ }
+
+ $query = $pdo->prepare("INSERT INTO users (name, password, token, team) VALUES (:name, :password, :token, :team)");
+ $query->execute([
+ "name" => filter_var($wrkarr["name"]),
+ "password" => password_hash($wrkarr["password"], PASSWORD_DEFAULT),
+ "token" => bin2hex(random_bytes(64)),
+ "team" => filter_var($wrkarr["team"]),
+ ]);
+ die('{ "status": "success" }');
+
+ else:
+ die('{ "status": "team does not exist." }');
+ endif;
endif;