Skip to content

Commit

Permalink
Update Server model to new Query Builder
Browse files Browse the repository at this point in the history
- In addition to migrating to the new Query Builder, the Server model
  has gotten some new columns allowing us to classify servers based on
  their purpose. e.g. Match server vs replay server
- The form view for Server models have also been updated to allow edits
  for server types.
  • Loading branch information
allejo committed Apr 10, 2018
1 parent 248aa4b commit 9233e3c
Show file tree
Hide file tree
Showing 5 changed files with 350 additions and 82 deletions.
3 changes: 2 additions & 1 deletion controllers/ServerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public function listAction()
{
$servers = $this
->getQueryBuilder()
->sortBy('name')
->active()
->orderBy('name')
->getModels()
;

Expand Down
73 changes: 73 additions & 0 deletions migrations/20180211020823_server_status_column_conversation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

use Phinx\Migration\AbstractMigration;

class ServerStatusColumnConversation extends AbstractMigration
{
public function up()
{
$serversTable = $this->table('servers');
$serversTable
->addColumn('is_official_server', 'boolean', [
'after' => 'updated',
'null' => false,
'default' => false,
'comment' => 'Whether or not this server is capable of hosting official matches',
])
->addColumn('is_replay_server', 'boolean', [
'after' => 'is_official_server',
'null' => false,
'default' => false,
'comment' => 'Whether or not this server is dedicated to serving replays of matches',
])
->addColumn('is_inactive', 'boolean', [
'after' => 'is_replay_server',
'null' => false,
'default' => false,
'comment' => 'Whether or not this server is no longer active but still required for historical purposes',

])
->addColumn('is_deleted', 'boolean', [
'after' => 'is_inactive',
'null' => false,
'default' => false,
'comment' => 'Whether or not this server has been soft deleted',
])
->update()
;

// BZiON 0.10.x and below required that you soft deleted servers so they'd no longer appear on the server list.
// For this reason and because Leagues United is the only known installation, we'll be assuming that deleted
// servers were just meant to be marked as "inactive."
$this->query("UPDATE servers SET is_inactive = 1 WHERE status = 'deleted';");

$serversTable
->removeColumn('status')
->update()
;
}

public function down()
{
$serversTable = $this->table('servers');
$serversTable
->addColumn('status', 'set', [
'values' => ['active', 'disabled', 'deleted'],
'null' => false,
'default' => 'active',
'comment' => 'The status of the server relative to BZiON',
])
->update()
;

$this->query("UPDATE servers SET status = 'deleted' WHERE is_inactive = 1;");

$serversTable
->removeColumn('is_official_server')
->removeColumn('is_replay_server')
->removeColumn('is_inactive')
->removeColumn('is_deleted')
->update()
;
}
}
195 changes: 143 additions & 52 deletions models/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,27 @@ class Server extends UrlModel implements NamedModel
protected $updated;

/**
* The name of the database table used for queries
* Whether or not this server is capable of hosting official matches.
*
* @var bool
*/
protected $is_official_server;

/**
* Whether or not this server is dedicated to being a replay server.
*
* @var bool
*/
protected $is_replay_server;

/**
* Whether or not this server has been marked as "inactive" and is only kept for historical purposes.
*
* @var bool
*/
protected $is_inactive;

const DELETED_COLUMN = 'is_deleted';
const TABLE = "servers";

const CREATE_PERMISSION = Permission::ADD_SERVER;
Expand All @@ -92,36 +111,9 @@ protected function assignResult($server)
$this->info = unserialize($server['info']);
$this->api_key = ApiKey::get($server['api_key']);
$this->updated = TimeDate::fromMysql($server['updated']);
$this->status = $server['status'];
}

/**
* Add a new server
*
* @param string $name The name of the server
* @param string $domain The domain of the server (e.g. server.com)
* @param string $port The port of the server (e.g. 5154)
* @param int $country The ID of the country
* @param int $owner The ID of the server owner
*
* @return Server An object that represents the sent message
*/
public static function addServer($name, $domain, $port, $country, $owner)
{
$key = ApiKey::getKeyByOwner($owner);

$server = self::create(array(
'name' => $name,
'domain' => $domain,
'port' => $port,
'country' => $country,
'owner' => $owner,
'api_key' => $key->getId(),
'status' => 'active',
), 'updated');
$server->forceUpdate();

return $server;
$this->is_official_server = $server['is_official_server'];
$this->is_replay_server = $server['is_replay_server'];
$this->is_inactive = $server['is_inactive'];
}

/**
Expand Down Expand Up @@ -319,6 +311,38 @@ public function getLastUpdate()
return $this->updated;
}

/**
* Get whether or not this server is only kept for historical purposes.
*
* The server is now "retired" or no longer used but is **not** soft deleted.
*
* @return bool
*/
public function isInactive()
{
return (bool)$this->is_inactive;
}

/**
* Get whether or not this server is capable of hosting official matches
*
* @return bool
*/
public function isOfficialServer()
{
return (bool)$this->is_official_server;
}

/**
* Get whether or not this server is dedicated to serving replays.
*
* @return bool
*/
public function isReplayServer()
{
return (bool)$this->is_replay_server;
}

/**
* Set the name of the server
*
Expand Down Expand Up @@ -399,36 +423,104 @@ public function setCountry($countryId)
}

/**
* Get all the servers in the database that have an active status
* @return Server[] An array of server objects
* Set this server's inactivity status.
*
* @param bool $inactive
*
* @return static
*/
public static function getServers()
public function setInactive($inactive)
{
return self::arrayIdToModel(self::fetchIdsFrom("status", array("active"), false, "ORDER BY name"));
return $this->updateProperty($this->is_inactive, 'is_inactive', $inactive);
}

/**
* Set the official match capabilities of this server.
*
* @param bool $matchServer
*
* @return static
*/
public function setOfficialServer($matchServer)
{
return $this->updateProperty($this->is_official_server, 'is_official_server', $matchServer);
}

/**
* Set the replay status of this server.
*
* @param bool $replayServer
*
* @return static
*/
public function setReplayServer($replayServer)
{
return $this->updateProperty($this->is_replay_server, 'is_replay_server', $replayServer);
}

/**
* Add a new server
*
* @param string $name The name of the server
* @param string $domain The domain of the server (e.g. server.com)
* @param string $port The port of the server (e.g. 5154)
* @param int $countryID The ID of the country
* @param int $ownerID The ID of the server owner
*
* @return Server An object that represents the sent message
*/
public static function addServer($name, $domain, $port, $countryID, $ownerID)
{
$key = ApiKey::getKeyByOwner($ownerID);

$server = self::create([
'name' => $name,
'domain' => $domain,
'port' => $port,
'country' => $countryID,
'owner' => $ownerID,
'api_key' => $key->getId(),
], 'updated');
$server->forceUpdate();

return $server;
}

/**
* Get a query builder for servers
* @return QueryBuilder
*
* @throws Exception
*
* @return QueryBuilderFlex
*/
public static function getQueryBuilder()
{
return new QueryBuilder('Server', array(
'columns' => array(
'name' => 'name',
'domain' => 'domain',
'port' => 'port',
'status' => 'status',
),
'name' => 'name'
));
return QueryBuilderFlex::createForModel(Server::class)
->setNameColumn('name')
;
}

/**
* {@inheritdoc}
*/
public static function getActiveModels(QueryBuilderFlex &$qb)
{
$qb
->whereNot(self::DELETED_COLUMN, '=', self::DELETED_VALUE)
->whereNot('is_inactive', '=', true)
;

return true;
}

/**
* Get the Server model with the respective address
*
* @param string $address The address in the format of `domain:port`
*
* @throws \Pixie\Exception
* @throws Exception
*
* @return static
*/
public static function fetchFromAddress($address)
Expand All @@ -439,16 +531,15 @@ public static function fetchFromAddress($address)

list($domain, $port) = explode(':', $address);

$qb = self::getQueryBuilder();
$query = $qb
->where('domain')->equals($domain)
->where('port')->equals($port)
$results = self::getQueryBuilder()
->where('domain', '=', $domain)
->where('port', '=', $port)
->active()
->getModels($fast = true)
->getModels(true)
;

if (count($query) > 0) {
return $query[0];
if (count($results) > 0) {
return $results[0];
}

return Server::get(0);
Expand Down
Loading

0 comments on commit 9233e3c

Please sign in to comment.