Skip to content

Commit

Permalink
Card Catalogue for client-side validation
Browse files Browse the repository at this point in the history
  • Loading branch information
silasary committed Oct 28, 2023
1 parent 25094cb commit 0f2bab5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
8 changes: 8 additions & 0 deletions gatherling/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@
$result['key'] = $player->setApiKey();
break;

case 'known_cards_catalog':
$result = card_catalog();
break;

case 'cardname_from_id':
$result = cardname_from_id(arg('id'));
break;

default:
$result['error'] = "Unknown action '{$action}'";
break;
Expand Down
22 changes: 22 additions & 0 deletions gatherling/api_lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

//## Helper Functions

use Gatherling\Database;
use Gatherling\Event;
use Gatherling\Player;
use Gatherling\Series;
Expand Down Expand Up @@ -429,3 +430,24 @@ function create_pairing($event, $round, $a, $b, $res)
$event->addMatch($playerA, $playerB, $round, $res, $pAWins, $pBWins);
}
}

/** @return string[] */
function card_catalog()
{
$result = [];
$db = Database::getConnection();
$query = $db->query('SELECT c.name as name FROM cards c');
while ($row = $query->fetch_assoc()) {
if (!in_array($row['name'], $result)) {
$result[] = $row['name'];
}
}
$query->close();
return $result;
}

function cardname_from_id($id) {
$sql = "SELECT c.name as name FROM cards c WHERE c.scryfallId = ?";
$name = Database::single_result_single_param($sql, 's', $id);
return $name;
}

0 comments on commit 0f2bab5

Please sign in to comment.