From 73bb3a8cd14f27820de9b65cf89a215ba0c5f3b3 Mon Sep 17 00:00:00 2001 From: Sean Finch Date: Wed, 10 May 2017 18:22:15 -0700 Subject: [PATCH] added dates and proper sorting --- cropFBws.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/cropFBws.php b/cropFBws.php index 33d04a7..e3b52f1 100644 --- a/cropFBws.php +++ b/cropFBws.php @@ -44,10 +44,10 @@ public function SaveUser() { $q = $this->pdo->prepare("SELECT COUNT(*) FROM cropUsers WHERE fbID = :i"); $q->execute(array("i" => $userId)); if(intval($q->fetchColumn()) == 0) { - $q2 = $this->pdo->prepare("INSERT INTO cropUsers (fbID, name, picURL) VALUES (:i, :n, :p)"); + $q2 = $this->pdo->prepare("INSERT INTO cropUsers (fbID, name, picURL, created, modified) VALUES (:i, :n, :p, NOW(), NOW())"); $q2->execute(array("i" => $userId, "n" => $userName, "p" => $picURL)); } else { - $q2 = $this->pdo->prepare("UPDATE cropUsers SET name = :n, picURL = :p WHERE fbID = :i"); + $q2 = $this->pdo->prepare("UPDATE cropUsers SET name = :n, picURL = :p, modified = NOW() WHERE fbID = :i"); $q2->execute(array("i" => $userId, "n" => $userName, "p" => $picURL)); } echo json_encode(["success" => true, "result" => "Saved that shite!"]); @@ -69,21 +69,22 @@ public function SubmitScore() { $q = $this->pdo->prepare("SELECT COUNT(*) FROM cropScores WHERE fbID = :i AND level = :l"); $q->execute(array("i" => $userId, "l" => $level)); if(intval($q->fetchColumn()) == 0) { - $q2 = $this->pdo->prepare("INSERT INTO cropScores (fbID, level, score, time) VALUES (:i, :l, :s, :t)"); + $q2 = $this->pdo->prepare("INSERT INTO cropScores (fbID, level, score, time, posted) VALUES (:i, :l, :s, :t, NOW())"); $q2->execute(array("i" => $userId, "l" => $level, "s" => $score, "t" => $time)); } else { - $q2 = $this->pdo->prepare("UPDATE cropScores SET score = :s, time = :t WHERE fbID = :i AND level = :l"); + $q2 = $this->pdo->prepare("UPDATE cropScores SET score = :s, time = :t, posted = NOW() WHERE fbID = :i AND level = :l"); $q2->execute(array("i" => $userId, "l" => $level, "s" => $score, "t" => $time)); } echo json_encode(["success" => true, "result" => "Saved that shit!"]); } public function GetHighScoresOrTimes() { - if(!isset($_GET["users"])) { $this->Fail("Invalid User List A"); } + if(!isset($_GET["users"])) { $this->Fail("Invalid User List"); } if(!isset($_GET["level"])) { $this->Fail("Invalid Level"); } if(!isset($_GET["type"])) { $this->Fail("Invalid Type"); } $level = $_GET["level"]; $type = $_GET["type"]; if(!ctype_digit($level) || $level <= 0 || $level > 101) { $this->Fail("Invalid Level"); } + $sortOrder = ($type == "score" || $level >= 100) ? "DESC" : "ASC"; if($type != "score" && $type != "time") { $this->Fail("Invalid Type"); } $users = $_GET["users"]; @@ -98,7 +99,7 @@ public function GetHighScoresOrTimes() { $inQueryArray[] = ":u$i"; } $inQuery = implode(", ", $inQueryArray); - $q = $this->pdo->prepare("SELECT u.fbID, u.name, u.picURL, s.$type FROM cropScores s INNER JOIN cropUsers u ON s.fbID = u.fbID WHERE s.level = :l AND (s.fbID IN ($inQuery) OR s.fbID LIKE 'T%') ORDER BY s.$type DESC LIMIT 0, 10"); + $q = $this->pdo->prepare("SELECT u.fbID, u.name, u.picURL, s.$type FROM cropScores s INNER JOIN cropUsers u ON s.fbID = u.fbID WHERE s.level = :l AND (s.fbID IN ($inQuery) OR s.fbID LIKE 'T%') ORDER BY s.$type $sortOrder LIMIT 0, 10"); $q->execute($params); echo json_encode(["success" => true, "result" => $q->fetchAll(PDO::FETCH_ASSOC)]); }