Skip to content

Commit

Permalink
Start without Deckcheck and more API endpoints (#567)
Browse files Browse the repository at this point in the history
* Waiting On API

* Start event without checks

* Card Catalogue for client-side validation

* Apply fixes from StyleCI

* A whole bunch of PHPDocs

* Apply fixes from StyleCI

---------

Co-authored-by: StyleCI Bot <bot@styleci.io>
  • Loading branch information
silasary and StyleCIBot authored Oct 29, 2023
1 parent ebf84a4 commit 3571192
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 5 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
39 changes: 39 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 @@ -131,6 +132,7 @@ function repr_json_event($event)
if ($matches) {
$json['matches'] = [];
$json['unreported'] = [];
$json['waiting_on'] = [];
$addrounds = 0;
$roundnum = 0;
$timing = 0;
Expand All @@ -143,6 +145,7 @@ function repr_json_event($event)
if ($roundnum != $m->round) {
$roundnum = $m->round;
$json['unreported'] = [];
$json['waiting_on'] = [];
}
$data['round'] = $m->round + $addrounds;
$json['matches'][] = $data;
Expand All @@ -153,6 +156,11 @@ function repr_json_event($event)
if (!$m->reportSubmitted($m->playerb)) {
$json['unreported'][] = $m->playerb;
}
if (!$m->reportSubmitted($m->playera) && $m->reportSubmitted($m->playerb)) {
$json['waiting_on'][] = $m->playera;
} elseif ($m->reportSubmitted($m->playera) && !$m->reportSubmitted($m->playerb)) {
$json['waiting_on'][] = $m->playerb;
}
}
}
}
Expand Down Expand Up @@ -422,3 +430,34 @@ 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;
}

/**
* @param string $id
*
* @throws Exception
*
* @return string
*/
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;
}
8 changes: 6 additions & 2 deletions gatherling/event.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,9 @@ function content()
authFailed();
} else {
if (mode_is('Start Event')) {
$event->startEvent();
$event->startEvent(true);
} elseif (mode_is('Start Event (No Deck Check)')) {
$event->startEvent(false);
} elseif (mode_is('Recalculate Standings')) {
$structure = $event->mainstruct;
$event->recalculateScores($structure);
Expand Down Expand Up @@ -851,7 +853,9 @@ function playerList($event)
echo "<input type=\"hidden\" name=\"name\" value=\"{$event->name}\" />";
echo '<table><th>Round Actions</th><tr>';
if ($event->active == 0 && $event->finalized == 0) {
echo '<td><input id="start_event" class="inputbutton" type="submit" name="mode" value="Start Event" /></td></tr>';
echo '<td><input id="start_event" class="inputbutton" type="submit" name="mode" value="Start Event" />';
echo '<input id="start_event" class="inputbutton" type="submit" name="mode" value="Start Event (No Deck Check)" />';
echo '</td></tr>';
echo '<p>Paste stuff:<br />';
echo "<code>{$deckless}</code></p>";
} elseif ($event->active == 1) {
Expand Down
4 changes: 2 additions & 2 deletions gatherling/models/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -1827,9 +1827,9 @@ public function updateDecksFormat($format)
}
}

public function startEvent()
public function startEvent($precheck)
{
$entries = $this->getRegisteredEntries(true);
$entries = $this->getRegisteredEntries($precheck);
Standings::startEvent($entries, $this->name);
// $this->dropInvalidEntries();
$this->pairCurrentRound();
Expand Down
50 changes: 49 additions & 1 deletion tests/EventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@
use Gatherling\Event;
use Gatherling\Matchup;
use Gatherling\Series;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;
use SebastianBergmann\RecursionContext\InvalidArgumentException;

final class EventsTest extends TestCase
{
/**
* @throws InvalidArgumentException
* @throws ExpectationFailedException
*
* @return Series
*/
public function testSeriesCreation()
{
if (!Series::exists('Test')) {
Expand All @@ -30,6 +38,14 @@ public function testSeriesCreation()
}

/**
* @param Series $series
*
* @throws Exception
* @throws InvalidArgumentException
* @throws ExpectationFailedException
*
* @return Event
*
* @depends testSeriesCreation
*/
public function testEventCreation($series)
Expand Down Expand Up @@ -78,6 +94,14 @@ public function testEventCreation($series)
}

/**
* @param Event $event
*
* @throws InvalidArgumentException
* @throws ExpectationFailedException
* @throws Exception
*
* @return Event
*
* @depends testEventCreation
*/
public function testRegistration($event)
Expand Down Expand Up @@ -113,14 +137,21 @@ public function testRegistration($event)
}

/**
* @param Event $event
*
* @throws InvalidArgumentException
* @throws ExpectationFailedException
*
* @return Event
*
* @depends testRegistration
*/
public function testEventStart($event)
{
$this->assertEquals($event->active, 0);
$this->assertEquals($event->current_round, 0);

$event->startEvent();
$event->startEvent(true);

$event = new Event($event->name);
$this->assertEquals($event->active, 1);
Expand All @@ -130,6 +161,13 @@ public function testEventStart($event)
}

/**
* @param Event $event
*
* @throws InvalidArgumentException
* @throws ExpectationFailedException
*
* @return mixed
*
* @depends testEventStart
*/
public function testReporting($event)
Expand All @@ -151,6 +189,16 @@ public function testReporting($event)
}
}

/**
* @param string $player
* @param Event $event
* @param string $main
* @param string $side
*
* @throws Exception
*
* @return Deck
*/
function insertDeck($player, $event, $main, $side)
{
$deck = new Deck(0);
Expand Down

0 comments on commit 3571192

Please sign in to comment.