|
| 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 | +} |
0 commit comments