Refactor code to change 'skip' to 'replace' referring to cards. Update logic for skipping cards to properly set played status.

This commit is contained in:
Junior 2026-08-01 12:38:16 -04:00
parent 9a09e2d87c
commit 4aeb30013a
2 changed files with 34 additions and 14 deletions

View File

@ -13,8 +13,8 @@ $second = false;
if ( isset($_REQUEST["second"]) && ($_REQUEST["second"] == "true") ) $second = true; if ( isset($_REQUEST["second"]) && ($_REQUEST["second"] == "true") ) $second = true;
$initialcard = false; $initialcard = false;
if ( isset($_REQUEST["initialcard"]) && ($_REQUEST["initialcard"] == "true") ) $initialcard = true; if ( isset($_REQUEST["initialcard"]) && ($_REQUEST["initialcard"] == "true") ) $initialcard = true;
$skip = false; $replace = false;
if ( isset($_REQUEST["skip"]) && ($_REQUEST["skip"] == "true") ) $skip = true; if ( isset($_REQUEST["replace"]) && ($_REQUEST["replace"] == "true") ) $replace = true;
$reverse = false; $reverse = false;
if ( isset($_REQUEST["reverse"]) && ($_REQUEST["reverse"] == "true") ) $reverse = true; if ( isset($_REQUEST["reverse"]) && ($_REQUEST["reverse"] == "true") ) $reverse = true;
@ -50,6 +50,30 @@ if ( $random_image === false ) {
sendResponse(); sendResponse();
} }
// If we are replacing a card set the previous card to unplayed,
// set the replacement card to played, then immediately send the response
if ( $replace ) {
if ( $initialcard ) {
$data["error"] = true;
$data["message"] = "Replacement card requested when no card present";
sendResponse();
}
$last_cardlist_index = array_key_last($_SESSION['cardlist']);
$last_played_index = array_key_last($_SESSION['cardlist'][$last_cardlist_index]);
$_SESSION['cardlist'][$last_cardlist_index][$last_played_index]->setPlayed(false);
$_SESSION['cardlist'][$last_cardlist_index][$last_played_index]->save();
$random_image->setPlayed(true);
$random_image->save();
$_SESSION['cardlist'][$last_cardlist_index][$last_played_index] = $random_image;
$data['cards'] = $_SESSION['cardlist'][$last_cardlist_index];
$data['cardcount'] = $_SESSION['cardcount'];
sendResponse();
}
// Always increment the card count unless it's the first card request
if ( !$initialcard ) $_SESSION['cardcount']++;
$data['cardcount'] = $_SESSION['cardcount'];
$new_cards = []; $new_cards = [];
if ( $second ) { if ( $second ) {
$new_cards[] = $previous_cards[0]; $new_cards[] = $previous_cards[0];
@ -57,13 +81,8 @@ if ( $second ) {
$new_cards[] = $random_image; $new_cards[] = $random_image;
$_SESSION['cardlist'][] = $new_cards; $_SESSION['cardlist'][] = $new_cards;
$data['cards'] = $new_cards; $data['cards'] = $new_cards;
if ( !$skip && !$initialcard ) $_SESSION['cardcount']++;
if ( !$skip ) {
$random_image->setPlayed(true); $random_image->setPlayed(true);
$random_image->save(); $random_image->save();
}
$data['cardcount'] = $_SESSION['cardcount'];
sendResponse(); sendResponse();

View File

@ -1,6 +1,6 @@
var resetting = false; var resetting = false;
var initialCard = true; var initialCard = true;
var skipCard = false; var replaceCard = 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;
@ -28,7 +28,8 @@ $(document).ready(function() {
$("#mana_sun_img").on("click", function() { rollDice(); }); $("#mana_sun_img").on("click", function() { rollDice(); });
$("#mana_water_img").on("click", function() { $(".diceroll").addClass("hidden"); }); $("#mana_water_img").on("click", function() { $(".diceroll").addClass("hidden"); });
$("#mana_fire_img").on("click", function() { $("#mana_fire_img").on("click", function() {
skipCard = true; if ( initialCard ) return;
replaceCard = true;
getCard(); getCard();
}); });
$("#mana_tree_img").on("click", function() { window.open("manage.php", "_blank"); }); $("#mana_tree_img").on("click", function() { window.open("manage.php", "_blank"); });
@ -75,7 +76,7 @@ function resetPage(count = 0) {
$(".diceroll").addClass("hidden"); $(".diceroll").addClass("hidden");
resetting = false; resetting = false;
initialCard = true; initialCard = true;
skipCard = false; replaceCard = false;
showingSecondCard = false; showingSecondCard = false;
if ( !initialReset ) { if ( !initialReset ) {
showCardCount(data.cardcount); showCardCount(data.cardcount);
@ -138,12 +139,12 @@ function getCard(reverse = false) {
data: { data: {
second: showingSecondCard, second: showingSecondCard,
initialcard: initialCard, initialcard: initialCard,
skip: skipCard, replace: replaceCard,
reverse: reverse reverse: reverse
}, },
dataType: 'json', dataType: 'json',
success: function(data, stat, jqo) { success: function(data, stat, jqo) {
skipCard = false; replaceCard = false;
if ( data.error ) { if ( data.error ) {
$("#firstimg").attr("src", ""); $("#firstimg").attr("src", "");
$("#secondimg").attr("src", ""); $("#secondimg").attr("src", "");