Skip to content

Instantly share code, notes, and snippets.

@tenub
Created February 13, 2016 05:39
Show Gist options
  • Select an option

  • Save tenub/4f24b198ca1bc404d1df to your computer and use it in GitHub Desktop.

Select an option

Save tenub/4f24b198ca1bc404d1df to your computer and use it in GitHub Desktop.
endo serverlist
var App = angular.module('App', ['ngTouch'])
.controller('ServerList', function ($scope, $http, $interval) {
// Set the server list to silently update every minute
$interval(function () {
query(true);
}, 60000);
/**
* Attach query to scope for init query and subsequent user queries.
* @return {void}
*/
$scope.queryServerList = function () {
query();
};
/**
* Perform a XHR to query all servers.
* @param {boolean} keep - whether or not to keep the current server information while the query executes
* @return {void}
*/
function query (keep) {
if (!keep) {
$scope.servers = [];
}
$http.get('api/servers', {
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
}).then(function (response) {
$scope.servers = response.data;
$scope.loaded = true;
}, function (error) {
console.log(error);
$scope.error = error.data;
});
}
});
<!-- resources/views/index.blade.php -->
@extends('layouts.default')
@section('content')
<section class="server-list" ng-controller="ServerList" ng-include="'assets/templates/servers.html'" data-ng-init="queryServerList()"></section>
...
<?php
namespace App;
use App\ServerMap as ServerMap;
use Illuminate\Database\Eloquent\Model;
class Server extends Model
{
public function __construct()
{
// steam condenser issue; must disable this error type for server queries to work
error_reporting(~E_USER_NOTICE);
}
public function queryServer($connection)
{
$server = $connection->gameFlag ? new \SourceServer($connection->ip, $connection->port) : new \GoldSrcServer($connection->ip, $connection->port);
$server->initialize();
$serverAddress = $connection->ip . ':' . $connection->port;
$serverInfo = $server->getServerInfo();
return [
'serverAddress' => $serverAddress,
'serverName' => $serverInfo['serverName'],
'mapName' => $serverInfo['mapName'],
'population' => $serverInfo['numberOfPlayers'] . '/' . $serverInfo['maxPlayers'],
'gameId' => $serverInfo['gameId'],
'players' => $this->getServerPlayers($server),
'maps' => $this->getServerMaps($connection->id)
];
}
public function getServerMaps($id, $amount = 4)
{
return ServerMap::where('server_id', $id)
->orderBy('created_at', 'desc')
->orderBy('id', 'desc')
->take($amount)
->get();
}
public function getServerPlayers($server)
{
return array_map(function ($player) {
return $player->getName();
}, $server->getPlayers());
}
}
<h2>Server List <span class="fa fa-refresh" ng-click="queryServerList()"></span></h2>
<table class="servers" ng-if="servers.length">
<thead>
<tr>
<th>Server</th>
<th>Map</th>
<th>Players</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="server in servers">
<td class="server">
<a data-game="{{ server.gameId }}" href="#">{{ server.serverName }}</a>
<a href="steam://connect/{{ server.serverAddress }}"><span class="fa fa-sign-in"></span></a>
</td>
<td class="current-map">
<a href="/records/{{ server.mapName }}">
{{ server.mapName }}
<ul class="extra">
<li>Recent Maps:</li>
<li ng-repeat="row in server.maps">{{ row.map }}</li>
</ul>
</a>
</td>
<td class="players">
<a href="#">
{{ server.population }}
<ul class="extra" ng-if="server.players">
<li ng-repeat="name in server.players">{{ name }}</li>
</ul>
</a>
</td>
</tr>
</tbody>
</table>
<p class="loading" ng-if="loaded && !servers.length">No servers available.</p>
<p class="loading" ng-if="!loaded && !error">Loading...</p>
<p class="invalid" ng-if="error">Error retrieving server information.</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment