I am trying to create a deck validation system that will anaylize a deck list and let the user know if it is a legal list, and if not, what is wrong with it so that the user can fix it. This deck validation system will accept as input a deck list for the game (in text format), and then generate errors that are placed in a table called deckerrors if any exist. The specific data saved is a description of the error and the DeckID. When a deck list is viewed, the list of errors for that deck are then loaded. The user then has the opportunity to make edits to the deck to correct the errors given and then re-save the deck list.
So the save function when called is where the errors are generated. The first thing it needs to do though is delete all the old errors before re-parsing the newly edited list and generating any new errors. The problem is that for some reason the errors for the deck list are not being deleted. Instead the errors just keep getting added. So if a player re-saves a deck list after correcting the problem, the old error message will still appear when he views the list. Here is the snippet of the code that does the deleting:
- Code: Select all
$succ = $db->query("DELETE FROM deckerrors WHERE deck = {$this->id}");
if (!$succ) {
$db->rollback();
$db->autocommit(TRUE);
throw new Exception("Can't update deck contents {$this->id}");
}
I know this code works, because if I move it to other functions, it deletes the errors. For example it works fine from this function:
- Code: Select all
function isValid() {
$db = Database::getConnection();
$succ = $db->query("DELETE FROM deckerrors WHERE deck = {$this->id}");
if (!$succ) {
$db->rollback();
$db->autocommit(TRUE);
throw new Exception("Can't update deck contents {$this->id}");
}
return (count($this->errors) == 0);
}
So I am not sure exactly why it doesn't work from my save() function. Been racking my brains on it. I would greatly appreciate any incite. Below is my save function in its entirety. Upon request I would be happy to supply more code if needed. There are no errors generated by this code. It runs fine, it just doesn't delete the records from the deckerrors table. You will notice that just before it, I am deleting records from the deckcontents table using pretty much exactly the same code. That works fine. I tried inverting these two segments to see if they were interferring with each other and that maybe only the first one was getting done, but with the same result.
Thank you in advance for any help that I receive.
- Code: Select all
function save() {
$db = Database::getConnection();
$db->autocommit(FALSE);
if ($this->name == NULL || $this->name == "") {
$this->name = "{$this->playername}'s Deck for {$this->eventname}";
}
if ($this->archetype != "Unclassified" && !in_array($this->archetype, Deck::getArchetypes())) {
$this->archetype = "Unclassified";
}
if ($this->id == 0) {
// New record. Set up the decks entry and the Entry.
$stmt = $db->prepare("INSERT INTO decks (archetype, name, notes)
values(?, ?, ?)");
$stmt->bind_param("sss", $this->archetype, $this->name, $this->notes);
$stmt->execute();
$this->id = $stmt->insert_id;
$stmt = $db->prepare("UPDATE entries SET deck = {$this->id} WHERE player = ? AND event = ?");
$stmt->bind_param("ss", $this->playername, $this->eventname);
$stmt->execute();
if ($stmt->affected_rows != 1) {
$db->rollback();
$db->autocommit(TRUE);
throw new Exception('Entry for '. $this->playername .' in '. $this->eventname .' not found');
}
} else {
$stmt = $db->prepare("UPDATE decks SET archetype = ?, name = ?,
notes = ? WHERE id = ?");
if (!$stmt) {
echo $db->error;
}
$stmt->bind_param("sssd", $this->archetype, $this->name, $this->notes, $this->id);
if (!$stmt->execute()) {
$db->rollback();
$db->autocommit(TRUE);
throw new Exception('Can\'t update deck '. $this->id);
}
}
$succ = $db->query("DELETE FROM deckcontents WHERE deck = {$this->id}");
if (!$succ) {
$db->rollback();
$db->autocommit(TRUE);
throw new Exception("Can't update deck contents {$this->id}");
}
$succ = $db->query("DELETE FROM deckerrors WHERE deck = {$this->id}");
if (!$succ) {
$db->rollback();
$db->autocommit(TRUE);
throw new Exception("Can't update deck contents {$this->id}");
}
$newmaindeck = array();
$this->maindeck_cardcount = 0;
foreach ($this->maindeck_cards as $card => $amt) {
$card = stripslashes($card);
$cardar = $this->getCard($card);
if (is_null($cardar)) {
$this->errors[] = "Could not find maindeck card: {$amt} {$card}";
if (!isset($this->unparsed_cards[$card])) {
$this->unparsed_cards[$card] = 0;
}
$this->unparsed_cards[$card] += $amt;
continue;
}
$this->maindeck_cardcount += $amt;
$stmt = $db->prepare("INSERT INTO deckcontents (deck, card, issideboard, qty) values(?, ?, 0, ?)");
$stmt->bind_param("ddd", $this->id, $cardar['id'], $amt);
$stmt->execute();
$newmaindeck[$cardar['name']] = $amt;
}
$this->maindeck_cards = $newmaindeck;
$newsideboard = array();
$this->sideboard_cardcount = 0;
foreach ($this->sideboard_cards as $card => $amt) {
$card = stripslashes($card);
$cardar = $this->getCard($card);
if (is_null($cardar)) {
$this->errors[] = "Could not find sideboard card: {$amt} {$card}";
if (!isset($this->unparsed_side[$card])) {
$this->unparsed_side[$card] = 0;
}
$this->unparsed_side[$card] += $amt;
continue;
}
$this->sideboard_cardcount += $amt;
$stmt = $db->prepare("INSERT INTO deckcontents (deck, card, issideboard, qty) values(?, ?, 1, ?)");
$stmt->bind_param("ddd", $this->id, $cardar['id'], $amt);
$stmt->execute();
$newsideboard[$cardar['name']] = $amt;
}
$this->sideboard_cards = $newsideboard;
$stmt = $db->prepare("UPDATE decks SET notes = ? WHERE id = ?");
if (!$stmt) {
echo $db->error;
}
$stmt->bind_param("sd", $this->notes, $this->id);
if (!$stmt->execute()) {
$db->rollback();
$db->autocommit(TRUE);
throw new Exception('Can\'t update deck '. $this->id);
}
$this->deck_contents_cache = implode('|', array_merge(array_keys($this->maindeck_cards),
array_keys($this->sideboard_cards)));
$stmt = $db->prepare("UPDATE decks set deck_contents_cache = ? WHERE id = ?");
$stmt->bind_param("sd", $this->deck_contents_cache, $this->id);
$stmt->execute();
$db->commit();
$db->autocommit(TRUE);
$this->calculateHashes();
if ($this->maindeck_cardcount < 60) {
$this->errors[] = "There must be at least 60 Maindeck Cards";
}
if ($this->sideboard_cardcount != 15 && $this->sideboard_cardcount != 0) {
$this->errors[] = "If you have a sideboard there must be 15 cards exactly";
}
foreach($this->errors as $error) {
$stmt = $db->prepare("INSERT INTO deckerrors (deck, error) values(?, ?)");
$stmt->bind_param("ds", $this->id, $error);
$stmt->execute();
}
return true;
}


