From bf77d44c1fe7f194cd51c9aaa0769af10d115c2a Mon Sep 17 00:00:00 2001 From: Junior Date: Thu, 30 Jul 2026 18:19:10 -0400 Subject: [PATCH] Add ability to track played status of cards --- ajax/getcard.php | 10 ++++++-- ajax/resetpage.php | 2 ++ class_image.php | 57 ++++++++++++++++++++++++++++++++++++++++---- install/initial_db_mysql.sql | 1 + js/main.js | 13 +++++++++- 5 files changed, 75 insertions(+), 8 deletions(-) diff --git a/ajax/getcard.php b/ajax/getcard.php index 63c20e2..e0fb361 100644 --- a/ajax/getcard.php +++ b/ajax/getcard.php @@ -27,7 +27,9 @@ $data["secondimg"] = $second; if ( $reverse ) { if ( $_SESSION['cardcount'] > $_SESSION['startcount'] ) { $_SESSION['cardcount']--; - $junk = array_pop($_SESSION["cardlist"]); + $reverse_cards = array_pop($_SESSION["cardlist"]); + $reverse_cards[array_key_last($reverse_cards)]->setPlayed(false); + $reverse_cards[array_key_last($reverse_cards)]->save(false); } else { } $data['cards'] = $_SESSION['cardlist'][array_key_last($_SESSION['cardlist'])]; @@ -35,7 +37,7 @@ if ( $reverse ) { sendResponse(); } -$previous_cards = $_SESSION['cardlist'][array_key_last($_SESSION['cardlist'])]; +$previous_cards = (count($_SESSION['cardlist']) > 0) ? $_SESSION['cardlist'][array_key_last($_SESSION['cardlist'])] : []; if ( $second ) { $random_image = MTGImage::getRandomImage($previous_cards[0]->getFileName()); @@ -57,6 +59,10 @@ $_SESSION['cardlist'][] = $new_cards; $data['cards'] = $new_cards; if ( !$skip && !$initialcard ) $_SESSION['cardcount']++; +if ( !$skip ) { + $random_image->setPlayed(true); + $random_image->save(); +} $data['cardcount'] = $_SESSION['cardcount']; sendResponse(); diff --git a/ajax/resetpage.php b/ajax/resetpage.php index e5e8101..2dc4510 100644 --- a/ajax/resetpage.php +++ b/ajax/resetpage.php @@ -2,6 +2,8 @@ require "../header.php"; +MTGImage::resetPlayed(); + if ( isset($_REQUEST['cardcount']) ) { $_SESSION['cardcount'] = intval($_REQUEST['cardcount']); if ( $_SESSION['cardcount'] < 0 ) $_SESSION['cardcount'] = 0; diff --git a/class_image.php b/class_image.php index 628c4ff..028798c 100644 --- a/class_image.php +++ b/class_image.php @@ -6,10 +6,12 @@ class MTGImage implements JsonSerializable { const MIME_TYPES = array("gif" => "image/gif", "jpg" => "image/jpeg", "jpeg" => "image/jpeg", "png" => "image/png", "webp" => "image/webp"); const MTGI_BOOLEANDB = 1000001; + const MTGI_INCLUDEPLAYED = false; private $id = 0; private $filename = ""; private $enabled = true; + private $played = false; private $width = 0; private $height = 0; private $created = "1972-08-16 00:00:00"; @@ -37,6 +39,17 @@ class MTGImage implements JsonSerializable { } } + public function getPlayed($flag = 0) { + switch ($flag) { + case MTGImage::MTGI_BOOLEANDB: + return ($this->played) ? 1 : 0; + break; + default: + return $this->played; + break; + } + } + public function getWidth() { return $this->width; } @@ -92,6 +105,18 @@ class MTGImage implements JsonSerializable { return true; } + public function setPlayed($value = null) { + if ( is_null($value) ) return false; + if ( is_int($value) && (($value == 1) || ($value == 0)) ) { + $this->played = ($value == 1) ? true : false; + } elseif ( is_bool($value) ) { + $this->played = $value; + } else { + return false; + } + return true; + } + public function setWidth($value = null) { if ( is_null($value) ) return false; if ( !is_int($value) || ($value < 0) ) return false; @@ -125,14 +150,33 @@ class MTGImage implements JsonSerializable { return true; } - public static function getRandomImage($id = 0) { + public static function resetPlayed() { global $globaldbh; + $query = "UPDATE images SET played = FALSE"; + $sth = $globaldbh->prepare($query); + $sth->execute(); + } + + public static function getRandomImage(int|string $id = 0, bool $onlyUnplayed = true) { + global $globaldbh; + + if ( is_string($id) ) $id = intval($id); if ( $id == 0 ) { - $query = "SELECT id FROM images WHERE enabled = TRUE ORDER BY RAND() LIMIT 1"; + if ( $onlyUnplayed ) { + $query = "SELECT id FROM images WHERE enabled = TRUE AND played = FALSE ORDER BY RAND() LIMIT 1"; + } else { + $query = "SELECT id FROM images WHERE enabled = TRUE ORDER BY RAND() LIMIT 1"; + } $sth = $globaldbh->prepare($query); } else { - $query = "SELECT id FROM images WHERE enabled = TRUE AND id <> :id ORDER BY RAND() LIMIT 1"; + if ( $onlyUnplayed ) { + $query = "SELECT id FROM images WHERE enabled = TRUE "; + $query .= "AND PLAYED = FALSE AND id <> :id ORDER BY RAND() LIMIT 1"; + } else { + $query = "SELECT id FROM images WHERE enabled = TRUE "; + $query .= "AND id <> :id ORDER BY RAND() LIMIT 1"; + } $sth = $globaldbh->prepare($query); $sth->bindValue(":id", intval($id), PDO::PARAM_INT); } @@ -184,6 +228,7 @@ class MTGImage implements JsonSerializable { 'filename' => $this->getFileName(), 'title' => pathinfo($this->getFileName(), PATHINFO_FILENAME), 'enabled' => $this->getEnabled(), + 'played' => $this->getPlayed(), 'width' => $this->getWidth(), 'height' => $this->getHeight(), 'dimensions' => $this->getDimensions(), @@ -211,11 +256,12 @@ class MTGImage implements JsonSerializable { public function save() { global $globaldbh; - $query = "INSERT INTO images (id, filename, width, height) VALUES(:id, :filename, :width, :height) ON DUPLICATE KEY UPDATE filename=:filename, enabled=:enabled, width=:width, height=:height"; + $query = "INSERT INTO images (id, filename, width, height) VALUES(:id, :filename, :width, :height) ON DUPLICATE KEY UPDATE filename=:filename, enabled=:enabled, played=:played, width=:width, height=:height"; $sth = $globaldbh->prepare($query); $sth->bindValue(":id", (int) $this->getId(), PDO::PARAM_INT); $sth->bindValue(":filename", $this->getFileName(), PDO::PARAM_STR); $sth->bindValue(":enabled", (int) $this->getEnabled(MTGImage::MTGI_BOOLEANDB), PDO::PARAM_INT); + $sth->bindValue(":played", (int) $this->getPlayed(MTGImage::MTGI_BOOLEANDB), PDO::PARAM_INT); $sth->bindValue(":width", (int) $this->getWidth(), PDO::PARAM_INT); $sth->bindValue(":height", (int) $this->getHeight(), PDO::PARAM_INT); $sth->execute(); @@ -227,7 +273,7 @@ class MTGImage implements JsonSerializable { global $globaldbh; if ( !is_int($imgid) || $imgid == 0 ) return; - $query = "SELECT id, filename, enabled, width, height, created FROM images where id=:id"; + $query = "SELECT id, filename, enabled, played, width, height, created FROM images where id=:id"; $sth = $globaldbh->prepare($query); $sth->bindValue(":id", (int) $imgid, PDO::PARAM_INT); $sth->execute(); @@ -235,6 +281,7 @@ class MTGImage implements JsonSerializable { $this->setId($row["id"]); $this->setFileName($row["filename"]); $this->setEnabled($row["enabled"]); + $this->setPlayed($row["played"]); $this->setWidth($row["width"]); $this->setHeight($row["height"]); $this->created = $row["created"]; diff --git a/install/initial_db_mysql.sql b/install/initial_db_mysql.sql index b5ef182..b685495 100644 --- a/install/initial_db_mysql.sql +++ b/install/initial_db_mysql.sql @@ -27,6 +27,7 @@ CREATE TABLE `images` ( `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, `filename` varchar(255) NOT NULL, `enabled` tinyint(1) DEFAULT 1, + `played` tinyint(1) DEFAULT 0, `width` mediumint(8) unsigned NOT NULL DEFAULT 0, `height` mediumint(8) unsigned NOT NULL DEFAULT 0, `created` datetime NOT NULL DEFAULT current_timestamp(), diff --git a/js/main.js b/js/main.js index ddfb50d..53f9cce 100644 --- a/js/main.js +++ b/js/main.js @@ -4,6 +4,7 @@ var skipCard = false; var showingSecondCard = false; const audioDice = new Audio("audio/dice.mp3"); const PREVIOUS = true; +const INITIALRESET = -1; $(document).ready(function() { // when ready @@ -49,9 +50,15 @@ $(document).ready(function() { toggleState($(this)); } }); + resetPage(INITIALRESET); }); function resetPage(count = 0) { + pageReset = false; + if ( count == INITIALRESET ) { + initialReset = true; + count = 0; + } $.ajax({ url: 'ajax/resetpage.php', data: {cardcount: count}, @@ -70,7 +77,11 @@ function resetPage(count = 0) { initialCard = true; skipCard = false; showingSecondCard = false; - showCardCount(data.cardcount); + if ( !initialReset ) { + showCardCount(data.cardcount); + } else { + initialReset = false; + } } }); }