Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
AsteroidusTv committed Jul 26, 2024
1 parent c28ef51 commit 106d97c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
15 changes: 8 additions & 7 deletions api/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

class Database
{

private $conn;

public function __construct()
{
include_once(dirname(__FILE__) . "/secrets.php");
Expand All @@ -16,14 +16,14 @@ public function __construct()

public function select($sql_prompt)
{
$result = mysqli_query($this->conn, $sql_prompt);
$result = $this->conn->query($sql_prompt);

if (!$result) {
die("Query failed: " . mysqli_error($this->conn));
die("Query failed: " . $this->conn->error);
}

$data = array();
while ($row = mysqli_fetch_assoc($result)) {
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}

Expand All @@ -35,19 +35,20 @@ public function query($sql_prompt)
$result = $this->conn->query($sql_prompt);

if (!$result) {
die("Query failed: " . mysqli_error($this->conn));
die("Query failed: " . $this->conn->error);
}

return $result;
}

public function closeConnection()
{
mysqli_close($this->conn);
$this->conn->close();
}

public function escapeStrings($str)
{
return mysqli_real_escape_string($this->conn, $str);
return $this->conn->real_escape_string($str);
}
}
?>
32 changes: 15 additions & 17 deletions api/getSmurfs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);


include_once(dirname(__FILE__) . "/database.php");
$db = new Database;

$searchQuery = $db->escapeStrings($_GET["searchQuery"]);
$sort = $db->escapeStrings($_GET["sort"]);
$searchQuery = isset($_GET["searchQuery"]) ? $db->escapeStrings($_GET["searchQuery"]) : '';
$sort = isset($_GET["sort"]) ? $db->escapeStrings($_GET["sort"]) : 'name';

function compareSmurfs($a, $b, $sort)
{
Expand All @@ -22,41 +21,40 @@ function compareSmurfs($a, $b, $sort)
} elseif ($sort == "worst_score") {
return $a["intelligence"] + $a["utility"] + $a["beauty"] - $b["intelligence"] - $b["utility"] - $b["beauty"];
}
return 0;
}


if (isset($searchQuery)) {
if (!empty($searchQuery)) {
$smurfs = $db->select("SELECT * FROM gargameleaks_smurfs WHERE name LIKE '%$searchQuery%' ORDER BY name");
} else {
$smurfs = $db->select("SELECT * FROM gargameleaks_smurfs ORDER BY name");
}

foreach ($smurfs as &$smurfs) {
$comments = $db->select("SELECT * FROM gargameleaks_comments WHERE smurfs_ID = '{$smurfs["ID"]}'");
$votesData = $db->select("SELECT * FROM gargameleaks_votes WHERE smurfs_ID = '{$smurfs["ID"]}'");
foreach ($smurfs as &$smurf) {
$comments = $db->select("SELECT * FROM gargameleaks_comments WHERE smurfs_ID = '{$smurf["ID"]}'");
$votesData = $db->select("SELECT * FROM gargameleaks_votes WHERE smurfs_ID = '{$smurf["ID"]}'");
$votesCount = count($votesData);

$intelligenceTotal = 0;
$utilityTotal = 0;
$beautyTotal = 0;

$votesCount = 0;
foreach ($votesData as $vote) {
$votesCount++;
$intelligenceTotal += $vote["intelligence"];
$utilityTotal += $vote["utility"];
$beautyTotal += $vote["beauty"];
}

$smurfs["comments_count"] = count($comments);
$smurfs["votes_count"] = $votesCount;
$smurfs["intelligence"] = ($votesCount > 0) ? $intelligenceTotal / $votesCount : 0;
$smurfs["utility"] = ($votesCount > 0) ? $utilityTotal / $votesCount : 0;
$smurfs["beauty"] = ($votesCount > 0) ? $beautyTotal / $votesCount : 0;
$smurf["comments_count"] = count($comments);
$smurf["votes_count"] = $votesCount;
$smurf["intelligence"] = ($votesCount > 0) ? $intelligenceTotal / $votesCount : 0;
$smurf["utility"] = ($votesCount > 0) ? $utilityTotal / $votesCount : 0;
$smurf["beauty"] = ($votesCount > 0) ? $beautyTotal / $votesCount : 0;
}

usort($Smurfs, function ($a, $b) use ($sort) {
usort($smurfs, function ($a, $b) use ($sort) {
return compareSmurfs($a, $b, $sort);
});

echo json_encode($Smurfs);
echo json_encode($smurfs);
?>

0 comments on commit 106d97c

Please sign in to comment.