Add ability to track played status of cards
This commit is contained in:
parent
5e5d4af707
commit
bf77d44c1f
|
|
@ -27,7 +27,9 @@ $data["secondimg"] = $second;
|
||||||
if ( $reverse ) {
|
if ( $reverse ) {
|
||||||
if ( $_SESSION['cardcount'] > $_SESSION['startcount'] ) {
|
if ( $_SESSION['cardcount'] > $_SESSION['startcount'] ) {
|
||||||
$_SESSION['cardcount']--;
|
$_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 {
|
} else {
|
||||||
}
|
}
|
||||||
$data['cards'] = $_SESSION['cardlist'][array_key_last($_SESSION['cardlist'])];
|
$data['cards'] = $_SESSION['cardlist'][array_key_last($_SESSION['cardlist'])];
|
||||||
|
|
@ -35,7 +37,7 @@ if ( $reverse ) {
|
||||||
sendResponse();
|
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 ) {
|
if ( $second ) {
|
||||||
$random_image = MTGImage::getRandomImage($previous_cards[0]->getFileName());
|
$random_image = MTGImage::getRandomImage($previous_cards[0]->getFileName());
|
||||||
|
|
@ -57,6 +59,10 @@ $_SESSION['cardlist'][] = $new_cards;
|
||||||
$data['cards'] = $new_cards;
|
$data['cards'] = $new_cards;
|
||||||
|
|
||||||
if ( !$skip && !$initialcard ) $_SESSION['cardcount']++;
|
if ( !$skip && !$initialcard ) $_SESSION['cardcount']++;
|
||||||
|
if ( !$skip ) {
|
||||||
|
$random_image->setPlayed(true);
|
||||||
|
$random_image->save();
|
||||||
|
}
|
||||||
$data['cardcount'] = $_SESSION['cardcount'];
|
$data['cardcount'] = $_SESSION['cardcount'];
|
||||||
|
|
||||||
sendResponse();
|
sendResponse();
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
require "../header.php";
|
require "../header.php";
|
||||||
|
|
||||||
|
MTGImage::resetPlayed();
|
||||||
|
|
||||||
if ( isset($_REQUEST['cardcount']) ) {
|
if ( isset($_REQUEST['cardcount']) ) {
|
||||||
$_SESSION['cardcount'] = intval($_REQUEST['cardcount']);
|
$_SESSION['cardcount'] = intval($_REQUEST['cardcount']);
|
||||||
if ( $_SESSION['cardcount'] < 0 ) $_SESSION['cardcount'] = 0;
|
if ( $_SESSION['cardcount'] < 0 ) $_SESSION['cardcount'] = 0;
|
||||||
|
|
|
||||||
|
|
@ -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 MIME_TYPES = array("gif" => "image/gif", "jpg" => "image/jpeg", "jpeg" => "image/jpeg", "png" => "image/png", "webp" => "image/webp");
|
||||||
|
|
||||||
const MTGI_BOOLEANDB = 1000001;
|
const MTGI_BOOLEANDB = 1000001;
|
||||||
|
const MTGI_INCLUDEPLAYED = false;
|
||||||
|
|
||||||
private $id = 0;
|
private $id = 0;
|
||||||
private $filename = "";
|
private $filename = "";
|
||||||
private $enabled = true;
|
private $enabled = true;
|
||||||
|
private $played = false;
|
||||||
private $width = 0;
|
private $width = 0;
|
||||||
private $height = 0;
|
private $height = 0;
|
||||||
private $created = "1972-08-16 00:00:00";
|
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() {
|
public function getWidth() {
|
||||||
return $this->width;
|
return $this->width;
|
||||||
}
|
}
|
||||||
|
|
@ -92,6 +105,18 @@ class MTGImage implements JsonSerializable {
|
||||||
return true;
|
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) {
|
public function setWidth($value = null) {
|
||||||
if ( is_null($value) ) return false;
|
if ( is_null($value) ) return false;
|
||||||
if ( !is_int($value) || ($value < 0) ) return false;
|
if ( !is_int($value) || ($value < 0) ) return false;
|
||||||
|
|
@ -125,14 +150,33 @@ class MTGImage implements JsonSerializable {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getRandomImage($id = 0) {
|
public static function resetPlayed() {
|
||||||
global $globaldbh;
|
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 ) {
|
if ( $id == 0 ) {
|
||||||
|
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";
|
$query = "SELECT id FROM images WHERE enabled = TRUE ORDER BY RAND() LIMIT 1";
|
||||||
|
}
|
||||||
$sth = $globaldbh->prepare($query);
|
$sth = $globaldbh->prepare($query);
|
||||||
} else {
|
} 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 = $globaldbh->prepare($query);
|
||||||
$sth->bindValue(":id", intval($id), PDO::PARAM_INT);
|
$sth->bindValue(":id", intval($id), PDO::PARAM_INT);
|
||||||
}
|
}
|
||||||
|
|
@ -184,6 +228,7 @@ class MTGImage implements JsonSerializable {
|
||||||
'filename' => $this->getFileName(),
|
'filename' => $this->getFileName(),
|
||||||
'title' => pathinfo($this->getFileName(), PATHINFO_FILENAME),
|
'title' => pathinfo($this->getFileName(), PATHINFO_FILENAME),
|
||||||
'enabled' => $this->getEnabled(),
|
'enabled' => $this->getEnabled(),
|
||||||
|
'played' => $this->getPlayed(),
|
||||||
'width' => $this->getWidth(),
|
'width' => $this->getWidth(),
|
||||||
'height' => $this->getHeight(),
|
'height' => $this->getHeight(),
|
||||||
'dimensions' => $this->getDimensions(),
|
'dimensions' => $this->getDimensions(),
|
||||||
|
|
@ -211,11 +256,12 @@ class MTGImage implements JsonSerializable {
|
||||||
public function save() {
|
public function save() {
|
||||||
global $globaldbh;
|
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 = $globaldbh->prepare($query);
|
||||||
$sth->bindValue(":id", (int) $this->getId(), PDO::PARAM_INT);
|
$sth->bindValue(":id", (int) $this->getId(), PDO::PARAM_INT);
|
||||||
$sth->bindValue(":filename", $this->getFileName(), PDO::PARAM_STR);
|
$sth->bindValue(":filename", $this->getFileName(), PDO::PARAM_STR);
|
||||||
$sth->bindValue(":enabled", (int) $this->getEnabled(MTGImage::MTGI_BOOLEANDB), PDO::PARAM_INT);
|
$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(":width", (int) $this->getWidth(), PDO::PARAM_INT);
|
||||||
$sth->bindValue(":height", (int) $this->getHeight(), PDO::PARAM_INT);
|
$sth->bindValue(":height", (int) $this->getHeight(), PDO::PARAM_INT);
|
||||||
$sth->execute();
|
$sth->execute();
|
||||||
|
|
@ -227,7 +273,7 @@ class MTGImage implements JsonSerializable {
|
||||||
global $globaldbh;
|
global $globaldbh;
|
||||||
|
|
||||||
if ( !is_int($imgid) || $imgid == 0 ) return;
|
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 = $globaldbh->prepare($query);
|
||||||
$sth->bindValue(":id", (int) $imgid, PDO::PARAM_INT);
|
$sth->bindValue(":id", (int) $imgid, PDO::PARAM_INT);
|
||||||
$sth->execute();
|
$sth->execute();
|
||||||
|
|
@ -235,6 +281,7 @@ class MTGImage implements JsonSerializable {
|
||||||
$this->setId($row["id"]);
|
$this->setId($row["id"]);
|
||||||
$this->setFileName($row["filename"]);
|
$this->setFileName($row["filename"]);
|
||||||
$this->setEnabled($row["enabled"]);
|
$this->setEnabled($row["enabled"]);
|
||||||
|
$this->setPlayed($row["played"]);
|
||||||
$this->setWidth($row["width"]);
|
$this->setWidth($row["width"]);
|
||||||
$this->setHeight($row["height"]);
|
$this->setHeight($row["height"]);
|
||||||
$this->created = $row["created"];
|
$this->created = $row["created"];
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ CREATE TABLE `images` (
|
||||||
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
|
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
|
||||||
`filename` varchar(255) NOT NULL,
|
`filename` varchar(255) NOT NULL,
|
||||||
`enabled` tinyint(1) DEFAULT 1,
|
`enabled` tinyint(1) DEFAULT 1,
|
||||||
|
`played` tinyint(1) DEFAULT 0,
|
||||||
`width` mediumint(8) unsigned NOT NULL DEFAULT 0,
|
`width` mediumint(8) unsigned NOT NULL DEFAULT 0,
|
||||||
`height` mediumint(8) unsigned NOT NULL DEFAULT 0,
|
`height` mediumint(8) unsigned NOT NULL DEFAULT 0,
|
||||||
`created` datetime NOT NULL DEFAULT current_timestamp(),
|
`created` datetime NOT NULL DEFAULT current_timestamp(),
|
||||||
|
|
|
||||||
11
js/main.js
11
js/main.js
|
|
@ -4,6 +4,7 @@ var skipCard = false;
|
||||||
var showingSecondCard = false;
|
var showingSecondCard = false;
|
||||||
const audioDice = new Audio("audio/dice.mp3");
|
const audioDice = new Audio("audio/dice.mp3");
|
||||||
const PREVIOUS = true;
|
const PREVIOUS = true;
|
||||||
|
const INITIALRESET = -1;
|
||||||
|
|
||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
// when ready
|
// when ready
|
||||||
|
|
@ -49,9 +50,15 @@ $(document).ready(function() {
|
||||||
toggleState($(this));
|
toggleState($(this));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
resetPage(INITIALRESET);
|
||||||
});
|
});
|
||||||
|
|
||||||
function resetPage(count = 0) {
|
function resetPage(count = 0) {
|
||||||
|
pageReset = false;
|
||||||
|
if ( count == INITIALRESET ) {
|
||||||
|
initialReset = true;
|
||||||
|
count = 0;
|
||||||
|
}
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: 'ajax/resetpage.php',
|
url: 'ajax/resetpage.php',
|
||||||
data: {cardcount: count},
|
data: {cardcount: count},
|
||||||
|
|
@ -70,7 +77,11 @@ function resetPage(count = 0) {
|
||||||
initialCard = true;
|
initialCard = true;
|
||||||
skipCard = false;
|
skipCard = false;
|
||||||
showingSecondCard = false;
|
showingSecondCard = false;
|
||||||
|
if ( !initialReset ) {
|
||||||
showCardCount(data.cardcount);
|
showCardCount(data.cardcount);
|
||||||
|
} else {
|
||||||
|
initialReset = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user