Skip to content

Commit

Permalink
Series API (#544)
Browse files Browse the repository at this point in the history
* More docstrings

* Series improvements

* Apply fixes from StyleCI

* fix typo

---------

Co-authored-by: StyleCI Bot <bot@styleci.io>
  • Loading branch information
silasary and StyleCIBot authored Aug 2, 2023
1 parent 336b2a1 commit 1d599d0
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 4 deletions.
4 changes: 4 additions & 0 deletions gatherling/admin/clean_decks.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

use Gatherling\Database;
use Gatherling\Deck;
use Gatherling\Player;

require '../lib.php';

session_start();
Expand Down
6 changes: 6 additions & 0 deletions gatherling/admin/db-upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,12 @@ function redirect_deck_update($latest_id = 0)
do_query('ALTER TABLE `players`
CHANGE COLUMN `discord_id` `discord_id` VARCHAR(20) NULL DEFAULT NULL AFTER `theme`;');
});
upgrade_db(50, 'Fix series discord fields', function () {
do_query('ALTER TABLE `series`
CHANGE COLUMN `discord_guild_id` `discord_guild_id` VARCHAR(20) NULL DEFAULT NULL AFTER `mtgo_room`,
ADD COLUMN `discord_channel_id` VARCHAR(20) NULL DEFAULT NULL AFTER `discord_guild_id`,
CHANGE COLUMN `discord_require_membership` `discord_require_membership` INT NULL DEFAULT NULL AFTER `discord_guild_invite`;');
});
$db->autocommit(true);

info('DB is up to date!');
4 changes: 2 additions & 2 deletions gatherling/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
try {
$series = new Series($seriesname);
$result = repr_json_series($series);
$result['sucess'] = true;
$result['success'] = true;
} catch (Exception $e) {
$result['sucess'] = false;
$result['success'] = false;
$result['error'] = $e->getMessage();
}
break;
Expand Down
29 changes: 28 additions & 1 deletion gatherling/api_lib.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

require_once 'lib.php';

//## Helper Functions

use Gatherling\Event;
Expand All @@ -16,6 +18,7 @@ function populate($array, $src, $keys)
return $array;
}

/** @return bool */
function is_admin()
{
if (!isset($_SESSION['infobot'])) {
Expand Down Expand Up @@ -62,6 +65,12 @@ function auth()
return Player::isLoggedIn();
}

/**
* @param string $msg
* @param mixed $extra
*
* @return never
*/
function error($msg, $extra = null)
{
$result = [];
Expand All @@ -74,6 +83,12 @@ function error($msg, $extra = null)
exit(json_encode($result));
}

/**
* @param string $key
* @param mixed $default
*
* @return mixed
*/
function arg($key, $default = null)
{
if (!isset($_REQUEST[$key])) {
Expand Down Expand Up @@ -181,9 +196,14 @@ function repr_json_deck($deck)
return $json;
}

/**
* @param Gatherling\Series $series
*
* @return mixed
*/
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']);
$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']);

return $json;
}
Expand Down Expand Up @@ -272,6 +292,13 @@ function drop_player_from_event($event, $name)
return $result;
}

/**
* @param string $newseries
* @param bool $active
* @param string $day
*
* @return array
*/
function create_series($newseries, $active, $day)
{
$result = [];
Expand Down
23 changes: 23 additions & 0 deletions gatherling/models/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,11 @@ public function getTrophyImageLink()
.self::trophy_image_tag($this->name)."\n</a>\n";
}

/**
* @param string $name
*
* @return bool
*/
public function isHost($name)
{
$ishost = !is_null($this->host) && strcasecmp($name, $this->host) == 0;
Expand All @@ -507,11 +512,17 @@ public function isHost($name)
return $ishost || $iscohost;
}

/** @return bool */
public function isFinalized()
{
return $this->finalized != 0;
}

/**
* @param string $name
*
* @return bool
*/
public function isOrganizer($name)
{
$isOrganizer = false;
Expand All @@ -528,6 +539,11 @@ public function isOrganizer($name)
return $isOrganizer;
}

/**
* @param string $playername
*
* @return bool
*/
public function authCheck($playername)
{
$player = new Player($playername);
Expand All @@ -541,6 +557,11 @@ public function authCheck($playername)
return false;
}

/**
* @throws Exception
*
* @return int
*/
public function getPlayerCount()
{
return Database::single_result_single_param('SELECT count(*) FROM entries WHERE event_id = ?', 'd', $this->id);
Expand Down Expand Up @@ -1707,6 +1728,8 @@ public function AssignMedalsbyStandings()

$t8 = [];
$t4 = [];
$sec = null;
$win = null;

switch ($medalCount) {
case 8:
Expand Down
4 changes: 3 additions & 1 deletion gatherling/models/Series.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Series
public $prereg_default;

public $discord_guild_id;
public $discord_channel_id;
public $discord_channel_name;
public $discord_guild_name;
public $discord_guild_invite;
Expand All @@ -46,7 +47,7 @@ public function __construct($name)
}

$db = Database::getConnection();
$sql = 'SELECT isactive, day, normalstart, prereg_default, mtgo_room, discord_guild_id, discord_channel_name, discord_guild_name, discord_guild_invite, discord_require_membership FROM series WHERE name = ?';
$sql = 'SELECT isactive, day, normalstart, prereg_default, mtgo_room, discord_guild_id, discord_channel_id, discord_channel_name, discord_guild_name, discord_guild_invite, discord_require_membership FROM series WHERE name = ?';
$stmt = $db->prepare($sql);
$stmt or exit($db->error);
$stmt->bind_param('s', $name);
Expand All @@ -58,6 +59,7 @@ public function __construct($name)
$this->prereg_default,
$this->mtgo_room,
$this->discord_guild_id,
$this->discord_channel_id,
$this->discord_channel_name,
$this->discord_guild_name,
$this->discord_guild_invite,
Expand Down
6 changes: 6 additions & 0 deletions gatherling/models/Standings.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ public function save()
}
}

/**
* @param string $eventname
* @param int $isactive
*
* @return Standings[]
*/
public static function getEventStandings($eventname, $isactive)
{
$db = Database::getConnection();
Expand Down

0 comments on commit 1d599d0

Please sign in to comment.