Skip to content

Commit

Permalink
More API endpoints (#592)
Browse files Browse the repository at this point in the history
* More Event management APIs

* Apply fixes from StyleCI

---------

Co-authored-by: StyleCI Bot <bot@styleci.io>
  • Loading branch information
silasary and StyleCIBot authored Jan 18, 2024
1 parent 4048952 commit d6e36ec
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
15 changes: 12 additions & 3 deletions gatherling/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@

case 'addplayer':
case 'add_player':
$event = new Event($_GET['event']);
$player = $_GET['addplayer'];
$result = add_player_to_event($event, $player);
$event = new Event(arg('event'));
$player = arg('addplayer');
$decklist = arg('decklist', '');
$result = add_player_to_event($event, $player, $decklist);
break;

case 'delplayer':
Expand Down Expand Up @@ -131,6 +132,7 @@
break;

case 'create_event':
auth();
$result = create_event();
break;

Expand Down Expand Up @@ -216,6 +218,13 @@
$result = cardname_from_id(arg('id'));
break;

case 'start_event':
auth();
$event = new Event(arg('event'));
$event->startEvent(true);
$result = repr_json_event($event);
break;

default:
$result['error'] = "Unknown action '{$action}'";
break;
Expand Down
21 changes: 20 additions & 1 deletion gatherling/api_lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//## Helper Functions

use Gatherling\Database;
use Gatherling\Deck;
use Gatherling\Event;
use Gatherling\Player;
use Gatherling\Series;
Expand Down Expand Up @@ -212,6 +213,10 @@ function repr_json_deck($deck)
function repr_json_series($series)
{
$json = populate([], $series, ['name', 'active', 'start_day', 'start_time', 'organizers', 'mtgo_room', 'this_season_format', 'this_season_master_link', 'this_season_season', 'discord_guild_id', 'discord_channel_id', 'discord_channel_name', 'discord_guild_name']);
$mostRecent = $series->mostRecentEvent();
$json['most_recent_season'] = $mostRecent->season;
$json['most_recent_number'] = $mostRecent->number;
$json['most_recent_id'] = $mostRecent->id;

return $json;
}
Expand All @@ -235,10 +240,11 @@ function repr_json_player($player, $client = null)
/**
* @param Gatherling\Event $event
* @param string $name
* @param string $decklist
*
* @return (bool|string|int)[]|false[]|(string|false)[]
*/
function add_player_to_event($event, $name)
function add_player_to_event($event, $name, $decklist)
{
if ($event->authCheck($_SESSION['username'])) {
if ($event->addPlayer($name)) {
Expand All @@ -250,6 +256,19 @@ function add_player_to_event($event, $name)
} else {
$result['success'] = false;
}
if (!empty($decklist)) {
$decklist = str_replace('|', "\n", $decklist);

$deck = new Deck(0);
$deck->playername = $player->name;
$deck->eventname = $event->name;
$deck->event_id = $event->id;
$deck->maindeck_cards = parseCardsWithQuantity($decklist);
$deck->sideboard_cards = parseCardsWithQuantity('');
$deck->save();

return $deck;
}
} else {
$result['error'] = 'Unauthorized';
$result['success'] = false;
Expand Down

0 comments on commit d6e36ec

Please sign in to comment.