|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\SupportedApps\CraftyController; |
| 4 | + |
| 5 | +class CraftyController extends \App\SupportedApps implements \App\EnhancedApps |
| 6 | +{ |
| 7 | + public $config; |
| 8 | + |
| 9 | + private $token = null; |
| 10 | + |
| 11 | + //protected $login_first = true; // Uncomment if api requests need to be authed first |
| 12 | + protected $method = 'POST'; // Uncomment if requests to the API should be set by POST |
| 13 | + |
| 14 | + public function __construct() |
| 15 | + { |
| 16 | + //$this->jar = new \GuzzleHttp\Cookie\CookieJar; // Uncomment if cookies need to be set |
| 17 | + } |
| 18 | + |
| 19 | + private function getToken() |
| 20 | + { |
| 21 | + $res = parent::execute($this->url('api/v2/auth/login'), $this->authAttrs()); |
| 22 | + $data = json_decode($res->getBody()); |
| 23 | + if ($data->status == 'ok') { |
| 24 | + $this->token = $data->data->token; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + public function test() |
| 29 | + { |
| 30 | + try { |
| 31 | + $this->getToken(); |
| 32 | + echo "Successfully communicated with the API"; |
| 33 | + } catch (Exception $err) { |
| 34 | + echo $err->getMessage(); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public function livestats() |
| 39 | + { |
| 40 | + $status = "inactive"; |
| 41 | + $this->getToken(); |
| 42 | + |
| 43 | + $res = parent::execute( |
| 44 | + $this->url('api/v2/servers'), |
| 45 | + attrs: $this->attrs(), |
| 46 | + overridemethod: 'GET' |
| 47 | + ); |
| 48 | + $details = json_decode($res->getBody()); |
| 49 | + |
| 50 | + $vars['servers_total'] = count($details->data); |
| 51 | + |
| 52 | + $servers_online = 0; |
| 53 | + $players_online = 0; |
| 54 | + $mem = 0; |
| 55 | + |
| 56 | + foreach ($details->data as $server) { |
| 57 | + $server_res = parent::execute( |
| 58 | + $this->url('api/v2/servers/' . $server->server_id . '/stats'), |
| 59 | + attrs: $this->attrs(), |
| 60 | + overridemethod: 'GET' |
| 61 | + ); |
| 62 | + $server_details = json_decode($server_res->getBody()); |
| 63 | + $players_online += $server_details->data->online; |
| 64 | + $mem += $this->decodeSizeToGB($server_details->data->mem); |
| 65 | + if ($server_details->data->running == true) { |
| 66 | + $servers_online++; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + $vars['servers_online'] = $servers_online; |
| 71 | + $vars['players_online'] = $players_online; |
| 72 | + $vars['mem'] = $mem; |
| 73 | + |
| 74 | + return parent::getLiveStats($status, $vars); |
| 75 | + } |
| 76 | + |
| 77 | + private function authAttrs() |
| 78 | + { |
| 79 | + $ignoreTls = $this->getConfigValue("ignore_tls", false); |
| 80 | + return [ |
| 81 | + "body" => json_encode([ |
| 82 | + "username" => $this->getConfigValue("username", null), |
| 83 | + "password" => $this->getConfigValue("password", null) |
| 84 | + ]), |
| 85 | + "verify" => ($ignoreTls ? false : true) |
| 86 | + ]; |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + public function attrs() |
| 91 | + { |
| 92 | + $ignoreTls = $this->getConfigValue("ignore_tls", false); |
| 93 | + return [ |
| 94 | + "headers" => [ |
| 95 | + "content-type" => "application/json", |
| 96 | + "Authorization" => "Bearer " . $this->token, |
| 97 | + ], |
| 98 | + "verify" => ($ignoreTls ? false : true) |
| 99 | + ]; |
| 100 | + } |
| 101 | + |
| 102 | + public function url($endpoint) |
| 103 | + { |
| 104 | + $api_url = parent::normaliseurl($this->config->url) . $endpoint; |
| 105 | + return $api_url; |
| 106 | + } |
| 107 | + |
| 108 | + private function getConfigValue($key, $default = null) |
| 109 | + { |
| 110 | + return isset($this->config) && isset($this->config->$key) |
| 111 | + ? $this->config->$key |
| 112 | + : $default; |
| 113 | + } |
| 114 | + |
| 115 | + private function decodeSizeToGB($size) |
| 116 | + { |
| 117 | + $units = [ |
| 118 | + 'B' => 1 / (1024 * 1024 * 1024), // Convert bytes to GB |
| 119 | + 'KB' => 1 / (1024 * 1024), // Convert KB to GB |
| 120 | + 'MB' => 1 / 1024, // Convert MB to GB |
| 121 | + 'GB' => 1, // GB is the base unit |
| 122 | + 'TB' => 1024, // Convert TB to GB |
| 123 | + 'PB' => 1024 * 1024, // Convert PB to GB |
| 124 | + ]; |
| 125 | + |
| 126 | + if (preg_match('/^([\d\.]+)\s*([KMGTP]?B)$/i', strtoupper($size), $matches)) { |
| 127 | + $value = (float)$matches[1]; |
| 128 | + $unit = $matches[2]; |
| 129 | + |
| 130 | + return round($value * ($units[$unit] ?? 1), 1); |
| 131 | + } |
| 132 | + |
| 133 | + return false; // Invalid format |
| 134 | + } |
| 135 | +} |
0 commit comments