Skip to content
This repository was archived by the owner on Dec 6, 2019. It is now read-only.

Commit 83085f9

Browse files
committed
Add plugin usage tracking
1 parent 0457c39 commit 83085f9

15 files changed

+591
-196
lines changed

_ide_helper.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* A helper file for Laravel 5, to provide autocomplete information to your IDE
4-
* Generated for Laravel 5.2.30 on 2016-04-26.
4+
* Generated for Laravel 5.2.31 on 2016-05-11.
55
*
66
* @author Barry vd. Heuvel <barryvdh@gmail.com>
77
* @see https://github.com/barryvdh/laravel-ide-helper
@@ -7193,6 +7193,17 @@ public static function hasFile($key){
71937193
return \Illuminate\Http\Request::hasFile($key);
71947194
}
71957195

7196+
/**
7197+
* Determine if a header is set on the request.
7198+
*
7199+
* @param string $key
7200+
* @return bool
7201+
* @static
7202+
*/
7203+
public static function hasHeader($key){
7204+
return \Illuminate\Http\Request::hasHeader($key);
7205+
}
7206+
71967207
/**
71977208
* Retrieve a header from the request.
71987209
*
@@ -7786,7 +7797,7 @@ public static function getHttpMethodParameterOverride(){
77867797
* Order of precedence: PATH (routing placeholders or custom attributes), GET, BODY
77877798
*
77887799
* @param string $key the key
7789-
* @param mixed $default the default value
7800+
* @param mixed $default the default value if the parameter key does not exist
77907801
* @return mixed
77917802
* @static
77927803
*/
@@ -8238,7 +8249,7 @@ public static function setFormat($format, $mimeTypes){
82388249
* Here is the process to determine the format:
82398250
*
82408251
* * format defined by the user (with setRequestFormat())
8241-
* * _format request parameter
8252+
* * _format request attribute
82428253
* * $default
82438254
*
82448255
* @param string $default The default format

app/Console/Commands/Ping.php

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,15 @@ public function handle() {
4444
}
4545

4646
function pingServer(Server $server) {
47+
$address = $server->address;
48+
$port = Server::DEFAULT_PORT;
4749
try {
48-
//the minecraft ping packets from existing libraries doesn't seem to be fast enough
49-
$address = $server->address;
50-
$ip = $address;
51-
$port = Server::DEFAULT_PORT;
52-
53-
$this->Get_Minecraft_IP($address, $ip, $port);
50+
$this->Get_Minecraft_IP($address, $port);
5451

55-
$server->ping = self::pingDomain($ip, $port);
52+
//the minecraft ping packets from existing libraries doesn't seem to be fast enough
53+
$server->ping = self::pingDomain($address, $port);
5654

57-
$ping = new MinecraftPing($ip, $port, 1);
55+
$ping = new MinecraftPing($address, $port, 1);
5856
$result = $ping->Query();
5957

6058
$this->parsePingData($server, $result);
@@ -179,10 +177,10 @@ function saveIcon($hostname, $favicon) {
179177
}
180178

181179
//extracted from https://github.com/xPaw/PHP-Minecraft-Query/issues/34
182-
function Get_Minecraft_IP($addr, &$ip, &$port) {
180+
function Get_Minecraft_IP(&$addr, &$port) {
183181
if (ip2long($addr) !== FALSE) {
184182
//server address is an ip
185-
return $ip = $addr;
183+
return;
186184
}
187185

188186
$port = Server::DEFAULT_PORT;
@@ -199,28 +197,5 @@ function Get_Minecraft_IP($addr, &$ip, &$port) {
199197

200198
$this->info("Found SRV-Record");
201199
}
202-
203-
$ip = $addr;
204200
}
205-
// /**
206-
// * needs enabled-qurey=true
207-
// *
208-
// * Contains the following data:
209-
// * hostname 'A Minecraft Server' MOTD for the current server
210-
// * gametype 'SMP' hardcoded to SMP
211-
// * game_id 'MINECRAFT' hardcoded to MINECRAFT
212-
// * version '1.2.5' Server version
213-
// * plugins 'CraftBukkit on Bukkit 1.2.5-R4.0:
214-
// * map 'world' Name of the current map
215-
// * numplayers '1' Number of online players. The string could be parsed to a number.
216-
// * maxplayers '20' Max number of players on the server. The string could be parsed to a number
217-
// * hostport '25565' Server port. The string could be parsed to a number
218-
// * hostip
219-
// *
220-
// * @param \App\Server $server
221-
// * @param array $data
222-
// */
223-
// public function parseQueryData($server, $data) {
224-
//
225-
// }
226201
}

app/Console/Commands/Query.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use \xPaw\MinecraftQuery;
7+
use \xPaw\MinecraftQueryException;
8+
use \App\Server;
9+
use \App\PluginUsage;
10+
11+
class Query extends Command {
12+
13+
protected $signature = 'app:query {address?}';
14+
protected $description = 'Ping all server instances from the database';
15+
16+
public function handle() {
17+
$this->info("Query server instances");
18+
19+
$address = $this->argument("address");
20+
if ($address) {
21+
$this->info("Query server: " . $address);
22+
$server = Server::where("address", '=', $address)->first();
23+
if ($server) {
24+
$this->queryServer($server);
25+
} else {
26+
$this->error("Server not in the database");
27+
}
28+
29+
return;
30+
}
31+
32+
$servers = Server::whereOnline(true)->get();
33+
$bar = $this->output->createProgressBar($servers->count());
34+
35+
/* @var $server \App\Server */
36+
foreach ($servers as $server) {
37+
$this->info("Query server: " . $server->address);
38+
$this->queryServer($server);
39+
$bar->advance();
40+
}
41+
42+
$bar->finish();
43+
$this->output->writeln("");
44+
}
45+
46+
function queryServer(Server $server, $port = 25565) {
47+
$address = $server->address;
48+
try {
49+
$serverId = $server->id;
50+
PluginUsage::whereServerId($serverId)->delete();
51+
52+
$query = new MinecraftQuery();
53+
54+
$query->Connect($address, $port, 1);
55+
56+
$infoData = $query->GetInfo();
57+
$playerData = $query->GetPlayers();
58+
59+
$this->parseQueryData($server, $infoData, $playerData);
60+
$server->save();
61+
} catch (MinecraftQueryException $queryException) {
62+
if ($port === 25565) {
63+
$this->queryServer($server, Server::DEFAULT_BUNGEE_QUERY_PORT);
64+
}
65+
} catch (\Exception $exception) {
66+
$this->error($exception);
67+
$this->error($server->address . " " . $exception->getMessage());
68+
}
69+
}
70+
71+
/**
72+
* needs enabled-qurey=true
73+
*
74+
* Contains the following data:
75+
* HostName 'A Minecraft Server' MOTD for the current server
76+
* GameType 'SMP' hardcoded to SMP
77+
* GameName 'MINECRAFT' hardcoded to MINECRAFT
78+
* Version '1.2.5' Server version
79+
* Plugins 'CraftBukkit on Bukkit 1.2.5-R4.0:
80+
* Map 'world' Name of the current map
81+
* Players '1' Number of online players. The string could be parsed to a number.
82+
* MaxPlayers '20' Max number of players on the server. The string could be parsed to a number
83+
* HostPort '25565' Server port. The string could be parsed to a number
84+
* HostIp
85+
* RawPlugins
86+
* Software
87+
*
88+
* @param Server $server
89+
* @param array $infoData
90+
* @param array $playerData containing only the player names
91+
*/
92+
function parseQueryData(Server $server, $infoData, $playerData) {
93+
//ignore everything because we already handle that in the ping part
94+
$plugins = $infoData['Plugins'];
95+
if (is_array($plugins) && !empty($plugins)) {
96+
foreach ($plugins as $plugin) {
97+
$pluginUsage = new PluginUsage();
98+
$pluginUsage->server_id = $server->id;
99+
100+
$components = explode(' ', $plugin);
101+
$pluginName = $components[0];
102+
$version = implode(array_slice($components, 1));
103+
104+
$pluginUsage->plugin = $pluginName;
105+
$pluginUsage->version = $version;
106+
$pluginUsage->save();
107+
}
108+
}
109+
}
110+
}

app/Console/Kernel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Kernel extends ConsoleKernel {
1515
protected $commands = [
1616
// Commands\Inspire::class,
1717
Commands\Ping::class,
18+
Commands\Query::class,
1819
// Commands\NameResolve::class,
1920
// Commands\NameHistory::class,
2021
];
@@ -29,6 +30,7 @@ protected function schedule(Schedule $schedule) {
2930
// $schedule->command('inspire')
3031
// ->hourly();
3132
$schedule->command('app:ping')->everyThirtyMinutes()->sendOutputTo(storage_path() . '/logs/ping.log', true);
33+
$schedule->command('app:query')->weekly()->sendOutputTo(storage_path() . '/logs/query.log', true);
3234

3335
// $schedule->command('queue:work --daemon')->everyMinute()->withoutOverlapping();
3436
}

app/Http/Controllers/ApiController.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace App\Http\Controllers;
44

5-
use Illuminate\Http\Request;
6-
use App\Http\Requests;
5+
use App\Server;
6+
use App\Skin;
7+
use App\Player;
78

89
class ApiController extends Controller {
910

@@ -20,24 +21,24 @@ public function getIcon($address) {
2021
}
2122

2223
public function stats() {
23-
$totalServers = App\Server::count();
24-
$onlineServers = \App\Server::whereOnline(true)->count();
24+
$totalServers = Server::count();
25+
$onlineServers = Server::whereOnline(true)->count();
2526

26-
$serverPlayers = App\Server::whereOnline(true)->sum('players');
27-
$totalServerPlayers = App\Server::whereOnline(true)->sum('maxplayers');
27+
$serverPlayers = Server::whereOnline(true)->sum('players');
28+
$totalServerPlayers = Server::whereOnline(true)->sum('maxplayers');
2829

29-
$onlineModeServer = App\Server::whereOnline(true)->whereOnlinemode(1)->count();
30-
$offlineModeServer = App\Server::whereOnline(true)->whereOnlinemode(0)->count();
31-
$unkownModeServer = App\Server::whereOnline(true)->whereOnlinemode(NULL)->count();
30+
$onlineModeServer = Server::whereOnline(true)->whereOnlinemode(1)->count();
31+
$offlineModeServer = Server::whereOnline(true)->whereOnlinemode(0)->count();
32+
$unkownModeServer = Server::whereOnline(true)->whereOnlinemode(NULL)->count();
3233

3334
//todo: server geo
3435
//server software stats
3536
//server version stats
3637

37-
$avgPing = App\Server::whereOnline(true)->avg('ping');
38+
$avgPing = Server::whereOnline(true)->avg('ping');
3839

39-
$players = App\Player::count();
40-
$skins = App\Skin::count();
40+
$players = Player::count();
41+
$skins = Skin::count();
4142
return response()->json(
4243
[
4344
'totalServers' => $totalServers,

app/Http/Controllers/ServerController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function addServer(Request $request) {
3232

3333
$validator = validator()->make($request->input(), $debugRules);
3434
} else {
35-
$validator = validator()-make($request->input(), $rules);
35+
$validator = validator()->make($request->input(), $rules);
3636
}
3737

3838

app/Http/routes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
//API
3232
Route::group(['prefix' => 'api', 'middleware' => ['api']], function () {
33-
Route::get('/', 'ApiController@index');
33+
Route::get('/server', 'ApiController@index');
3434

3535
Route::get('/server/{address}', 'ApiController@getServer');
3636
Route::get('/server/{address}/favicon', 'ApiController@getIcon');

app/PluginUsage.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
/**
8+
* App\PluginUsage
9+
*
10+
* @property integer $id
11+
* @property integer $server_id
12+
* @property string $plugin
13+
* @property string $version
14+
* @property \Carbon\Carbon $created_at
15+
* @property \Carbon\Carbon $updated_at
16+
* @method static \Illuminate\Database\Query\Builder|\App\PluginUsage whereId($value)
17+
* @method static \Illuminate\Database\Query\Builder|\App\PluginUsage whereServerId($value)
18+
* @method static \Illuminate\Database\Query\Builder|\App\PluginUsage wherePlugin($value)
19+
* @method static \Illuminate\Database\Query\Builder|\App\PluginUsage whereVersion($value)
20+
* @method static \Illuminate\Database\Query\Builder|\App\PluginUsage whereCreatedAt($value)
21+
* @method static \Illuminate\Database\Query\Builder|\App\PluginUsage whereUpdatedAt($value)
22+
* @mixin \Eloquent
23+
* @property-read \App\Server $server
24+
*/
25+
class PluginUsage extends Model {
26+
27+
protected $table = 'plugin_usages';
28+
29+
public function server() {
30+
return $this->belongsTo('App\Server');
31+
}
32+
}

app/Server.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,21 @@
3333
* @method static \Illuminate\Database\Query\Builder|\App\Server whereUpdatedAt($value)
3434
* @method static \Illuminate\Database\Query\Builder|\App\Server whereDeletedAt($value)
3535
* @mixin \Eloquent
36+
* @property-read \Illuminate\Database\Eloquent\Collection|\App\PluginUsage[] $plugins
3637
*/
3738
class Server extends Model {
3839

3940
const DEFAULT_PORT = 25565;
41+
const DEFAULT_BUNGEE_QUERY_PORT = 25577;
4042

4143
use SoftDeletes;
4244

43-
/**
44-
* The database table used by the model.
45-
*
46-
* @var string
47-
*/
4845
protected $table = 'servers';
4946

47+
public function plugins() {
48+
return $this->hasMany('App\PluginUsage');
49+
}
50+
5051
public function getHtmlMotd() {
5152
return \MinecraftColors::convertToHTML($this->motd, true);
5253
}

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
"barryvdh/laravel-debugbar": "^2.2",
1818
"xpaw/php-minecraft-query": "^1.0",
1919
"greggilbert/recaptcha": "^2.1",
20-
"roumen/sitemap": "^2.6"
20+
"roumen/sitemap": "^2.6",
21+
"games647/minecraft-skin-renderer": "^0.2.0"
2122
},
2223
"require-dev": {
23-
"fzaninotto/faker": "~1.4",
24+
"fzaninotto/faker": "~1.5",
2425
"mockery/mockery": "0.9.*",
25-
"phpunit/phpunit": "~4.0",
26+
"phpunit/phpunit": "^5.2",
2627
"symfony/css-selector": "2.8.*|3.0.*",
2728
"symfony/dom-crawler": "2.8.*|3.0.*",
2829
"laravel/homestead": "^3.0"

0 commit comments

Comments
 (0)