Skip to content

Commit 9f15392

Browse files
scebbersroot
andauthored
Add Crafty controller as an enhanced app (#820)
* Create CraftyController.php * Add files via upload * Update CraftyController.php * Update CraftyController.php * Added some features * Improve linting * Memory value rounding * Updated config and livestats * Update app.json --------- Co-authored-by: root <[email protected]>
1 parent 351cfa0 commit 9f15392

5 files changed

Lines changed: 191 additions & 0 deletions

File tree

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
}

CraftyController/app.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"appid": "200fe80f6b54e75f36a0ae671663f47b417de384",
3+
"name": "Crafty Controller",
4+
"website": "https://craftycontrol.com",
5+
"license": "GNU General Public License v3.0 or later",
6+
"description": "Crafty Controller is a free and open-source Minecraft launcher and manager that allows users to start and administer Minecraft servers from a user-friendly interface.",
7+
"enhanced": true,
8+
"tile_background": "dark",
9+
"icon": "craftycontroller.png"
10+
}

CraftyController/config.blade.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<h2>{{ __('app.apps.config') }} ({{ __('app.optional') }}) @include('items.enable')</h2>
2+
<div class="items">
3+
<div class="input">
4+
<label>{{ strtoupper(__('app.url')) }}</label>
5+
{!! Form::text('config[override_url]', isset($item) ? $item->getconfig()->override_url : null, array('placeholder' => __('app.apps.override'), 'id' => 'override_url', 'class' => 'form-control')) !!}
6+
</div>
7+
<div class="input">
8+
<label>{{ __('app.apps.username') }}</label>
9+
{!! Form::text('config[username]', isset($item) ? $item->getconfig()->username : null, array('placeholder' => __('app.apps.username'), 'data-config' => 'username', 'class' => 'form-control config-item')) !!}
10+
</div>
11+
<div class="input">
12+
<label>{{ __('app.apps.password') }}</label>
13+
{!! Form::input('password', 'config[password]', '', ['placeholder' => __('app.apps.password'), 'data-config' => 'password', 'class' => 'form-control config-item']) !!}
14+
</div>
15+
<div class="input">
16+
<label>Skip TLS verification</label>
17+
<div class="toggleinput" style="margin-top: 26px; padding-left: 15px;">
18+
{!! Form::hidden('config[ignore_tls]', 0, ['class' => 'config-item', 'data-config' => 'ignore_tls']) !!}
19+
<label class="switch">
20+
<?php
21+
$checked = false;
22+
if (isset($item) && !empty($item) && isset($item->getconfig()->ignore_tls)) {
23+
$checked = $item->getconfig()->ignore_tls;
24+
}
25+
$set_checked = $checked ? ' checked="checked"' : '';
26+
?>
27+
<input type="checkbox" class="config-item" data-config="ignore_tls" name="config[ignore_tls]" value="1" <?php echo $set_checked; ?> />
28+
<span class="slider round"></span>
29+
</label>
30+
</div>
31+
</div>
32+
<div class="input">
33+
<button style="margin-top: 32px;" class="btn test" id="test_config">Test</button>
34+
</div>
35+
</div>
36+
6.28 KB
Loading
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<ul class="livestats">
2+
<li>
3+
<span class="title">servers:</span><strong>{!! $servers_total !!}</strong>
4+
<span class="title">players:</span><strong>{!! $players_online !!}</strong>
5+
</li>
6+
<li>
7+
<span class="title">online:</span><strong>{!! $servers_online !!}</strong>
8+
<span class="title">memory:</span><strong>{!! $mem !!} GB</strong>
9+
</li>
10+
</ul>

0 commit comments

Comments
 (0)