Skip to content

Commit

Permalink
Homepage|Master Browser: Updated the master server browser to use Ser…
Browse files Browse the repository at this point in the history
…verInfo (class)

Todo for later: Re-implement the server browser with JavaScript.
  • Loading branch information
danij-deng committed Jul 13, 2013
1 parent 2daae0a commit 24306ce
Showing 1 changed file with 20 additions and 81 deletions.
101 changes: 20 additions & 81 deletions web/plugins/masterbrowser/masterbrowser.php
Expand Up @@ -26,62 +26,6 @@

require_once(DIR_CLASSES.'/masterserver.class.php');

class ServerInfo
{
public $at = '';
public $port = '';
public $locked = '';
public $ver = '';
public $map = '';
public $game = '';
public $name = '';
public $info = '';
public $nump = '';
public $maxp = '';
public $open = '';
public $mode = '';
public $setup = '';
public $iwad = '';
public $pwads = '';
public $wcrc = '';
public $plrn = '';
public $data0 = '';
public $data1 = '';
public $data2 = '';

public function __construct()
{}

/**
* @param array:$record Record representation of a ServerInfo.
*/
public static function fromRecord(&$record)
{
$info = new ServerInfo();
if(array_key_exists('at', $record)) $info->at = $record['at'];
if(array_key_exists('port', $record)) $info->port = $record['port'];
if(array_key_exists('locked', $record)) $info->locked = $record['locked'];
if(array_key_exists('ver', $record)) $info->ver = $record['ver'];
if(array_key_exists('map', $record)) $info->map = $record['map'];
if(array_key_exists('game', $record)) $info->game = $record['game'];
if(array_key_exists('name', $record)) $info->name = $record['name'];
if(array_key_exists('info', $record)) $info->info = $record['info'];
if(array_key_exists('nump', $record)) $info->nump = $record['nump'];
if(array_key_exists('maxp', $record)) $info->maxp = $record['maxp'];
if(array_key_exists('open', $record)) $info->open = $record['open'];
if(array_key_exists('mode', $record)) $info->mode = $record['mode'];
if(array_key_exists('setup', $record)) $info->setup = $record['setup'];
if(array_key_exists('iwad', $record)) $info->iwad = $record['iwad'];
if(array_key_exists('pwads', $record)) $info->pwads = $record['pwads'];
if(array_key_exists('wcrc', $record)) $info->wcrc = $record['wcrc'];
if(array_key_exists('plrn', $record)) $info->plrn = $record['plrn'];
if(array_key_exists('data0', $record)) $info->data0 = $record['data0'];
if(array_key_exists('data1', $record)) $info->data1 = $record['data1'];
if(array_key_exists('data2', $record)) $info->data2 = $record['data2'];
return $info;
}
}

class MasterBrowserPlugin extends Plugin implements Actioner, RequestInterpreter
{
public static $name = 'masterbrowser';
Expand Down Expand Up @@ -124,38 +68,38 @@ private function mustUpdateServerSummaryHtml()
*/
private function generateServerBadgeHtml(ServerInfo &$info)
{
$addonArr = array_filter(explode(';', $info->pwads));
$setupArr = array_filter(explode(' ', $info->setup));
$addonArr = array_filter(explode(';', $info['pwads']));
$setupArr = array_filter(explode(' ', $info['setup']));

$playerCountLabel = 'Number of players currently in-game';

$playerMaxLabel = 'Maximum number of players';

$openStatusLabel = $info->locked? 'Server requires a password to join' : 'Server is open to all players';
$openStatusLabel = $info['locked']? 'Server requires a password to join' : 'Server is open to all players';

$addressLabel = 'IP address and port number of the server';

// Format the game setup argument string.
$setupStr = implode(' ', $setupArr);

// Begin html generation.
$html = '<span class="game-mode">'. htmlspecialchars($info->mode) .'</span>'.
$html = '<span class="game-mode">'. htmlspecialchars($info['mode']) .'</span>'.
'<div class="wrapper"><div class="heading collapsible" title="Toggle server info display">'.
'<div class="player-summary">'.
'<span class="player-count"><label title="'. htmlspecialchars($playerCountLabel) .'">'. htmlspecialchars($info->nump) .'</label></span>/'.
'<span class="player-max"><label title="'. htmlspecialchars($playerMaxLabel) .'">'. htmlspecialchars($info->maxp) .'</label></span>'.
'<span class="player-count"><label title="'. htmlspecialchars($playerCountLabel) .'">'. htmlspecialchars((string)$info['nump']) .'</label></span>/'.
'<span class="player-max"><label title="'. htmlspecialchars($playerMaxLabel) .'">'. htmlspecialchars((string)$info['maxp']) .'</label></span>'.
'</div>'.
'<span class="name">'. htmlspecialchars($info->name) .'</span>'.
'<span class="name">'. htmlspecialchars($info['name']) .'</span>'.
'<div class="server-metadata">'.
'<span class="address"><label title="'. htmlspecialchars($addressLabel) .'">'. htmlspecialchars($info->at) .'<span class="port" '. (((integer)$info->port) === 0? 'style="color:red;"' : '') .'>'. htmlspecialchars($info->port) .'</span></label></span>'.
'<span class="address"><label title="'. htmlspecialchars($addressLabel) .'">'. htmlspecialchars($info['at']) .'<span class="port" '. ($info['port'] === 0? 'style="color:red;"' : '') .'>'. htmlspecialchars((string)$info['port']) .'</span></label></span>'.
'<div class="'. ($info->locked? 'lock-on' : 'lock-off') .'" title="'. htmlspecialchars($openStatusLabel) .'"></div>'.
'</div>'.
'<div class="game-metadata">'.
'Setup: <span class="game-setup">'. htmlspecialchars($setupStr) .'</span>'.
'Current Map: <span class="game-current-map">'. htmlspecialchars($info->map) .'</span>'.
'Current Map: <span class="game-current-map">'. htmlspecialchars($info['map']) .'</span>'.
'</div></div>';

$html .='<div class="extended-info"><blockquote class="game-info"><p>'. htmlspecialchars($info->info) .'</p></blockquote>';
$html .='<div class="extended-info"><blockquote class="game-info"><p>'. htmlspecialchars($info['info']) .'</p></blockquote>';

// Any required addons?.
if(count($addonArr))
Expand Down Expand Up @@ -190,19 +134,19 @@ private function outputJavascript()
public static function serverSorter(ServerInfo $b, ServerInfo $a)
{
// Group servers according to lock status.
$diff = (integer)($b->locked - $a->locked);
$diff = (integer)($b['locked'] - $a['locked']);
if($diff) return $diff;

// Servers with active players get priority
$diff = (integer)($b->nump - $a->nump);
$diff = (integer)($b['nump'] - $a['nump']);
if($diff) return -($diff);

// Order by lexicographical difference in the server name.
$diff = strcmp($b->mode, $a->mode);
$diff = strcmp($b['mode'], $a['mode']);
if($diff) return $diff;

// Order by lexicographical difference in the server name.
return strcmp($b->name, $a->name);
return strcmp($b['name'], $a['name']);
}

/**
Expand Down Expand Up @@ -242,13 +186,8 @@ public function InterpretRequest($request)
$this->db = new MasterServer();
}

// Build the servers collection.
$servers = array();
while(list($ident, $info) = each($this->db->servers))
{
if(!is_array($info)) continue;
$servers[] = ServerInfo::fromRecord($info);
}
// Copy the servers array.
$servers = $this->db->servers;

// Sort the collection.
uasort($servers, array('self', 'serverSorter'));
Expand All @@ -258,18 +197,18 @@ public function InterpretRequest($request)
if($serverCount)
{
$playerCount = (integer)0;
foreach($servers as &$server)
foreach($servers as &$info)
{
$playerCount += $server->nump;
$playerCount += $info['nump'];
}

$content .= '<h3>Active servers</h3>'.
"<p id=\"summary\">{$serverCount} ". ($serverCount === 1? 'server':'servers') ." in total with {$playerCount} active ". ($playerCount === 1? 'player':'players') .'</p>'.
'<div class="server_list"><ul>';

foreach($servers as &$server)
foreach($servers as &$info)
{
$content .= '<li>'. $this->generateServerBadgeHtml($server) .'</li>';
$content .= '<li>'. $this->generateServerBadgeHtml($info) .'</li>';
}

$content .= '</ul></div>';
Expand Down

0 comments on commit 24306ce

Please sign in to comment.