Skip to content

Commit be6b431

Browse files
rootroot
authored andcommitted
Added some features
1 parent e434790 commit be6b431

3 files changed

Lines changed: 90 additions & 24 deletions

File tree

CraftyController/CraftyController.php

Lines changed: 69 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,42 @@ public function __construct()
1616
//$this->jar = new \GuzzleHttp\Cookie\CookieJar; // Uncomment if cookies need to be set
1717
}
1818

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+
1928
public function test()
2029
{
21-
$test = parent::appTest($this->url('api/v2/auth/login'), $this->authAttrs());
22-
echo $test->status;
30+
try {
31+
$this->getToken();
32+
echo "Successfully communicated with the API";
33+
} catch (Exception $err) {
34+
echo $err->getMessage();
35+
}
2336
}
2437

2538
public function livestats()
2639
{
2740
$status = "inactive";
28-
$res = parent::execute($this->url('api/v2/auth/login'), $this->authAttrs());
29-
$data = json_decode($res->getBody());
30-
if ($data->status == 'ok') {
31-
$this->token = $data->data->token;
32-
}
41+
$this->getToken();
3342

34-
$res = parent::execute($this->url('api/v2/servers'), attrs: $this->attrs(), overridemethod: 'GET');
43+
$res = parent::execute(
44+
$this->url('api/v2/servers'),
45+
attrs: $this->attrs(),
46+
overridemethod: 'GET'
47+
);
3548
$details = json_decode($res->getBody());
3649

3750
$vars['servers_total'] = count($details->data);
3851

39-
$online = 0;
52+
$servers_online = 0;
53+
$players_online = 0;
54+
$mem = 0;
4055

4156
foreach ($details->data as $server) {
4257
$server_res = parent::execute(
@@ -45,42 +60,76 @@ public function livestats()
4560
overridemethod: 'GET'
4661
);
4762
$server_details = json_decode($server_res->getBody());
63+
$players_online += $server_details->data->online;
64+
$mem += $this->decodeSizeToGB($server_details->data->mem);
4865
if ($server_details->data->running == true) {
49-
$online++;
66+
$servers_online++;
5067
}
5168
}
5269

53-
$vars['servers_online'] = $online;
70+
$vars['servers_online'] = $servers_online;
71+
$vars['players_online'] = $players_online;
72+
$vars['mem'] = $mem;
5473

5574
return parent::getLiveStats($status, $vars);
5675
}
5776

5877
private function authAttrs()
5978
{
79+
$ignoreTls = $this->getConfigValue("ignore_tls", false);
6080
return [
6181
"body" => json_encode([
62-
"username" => $this->config->username,
63-
"password" => $this->config->password
82+
"username" => $this->getConfigValue("username", null),
83+
"password" => $this->getConfigValue("password", null)
6484
]),
65-
"verify" => false
85+
"verify" => ($ignoreTls ? false : true)
6686
];
6787
}
6888

6989

7090
public function attrs()
7191
{
72-
$attrs["headers"] = [
73-
"content-type" => "application/json",
74-
"Authorization" => "Bearer " . $this->token,
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)
7599
];
76-
$attrs["verify"] = false;
77-
78-
return $attrs;
79100
}
80101

81102
public function url($endpoint)
82103
{
83104
$api_url = parent::normaliseurl($this->config->url) . $endpoint;
84105
return $api_url;
85106
}
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 $value * ($units[$unit] ?? 1);
131+
}
132+
133+
return false; // Invalid format
134+
}
86135
}

CraftyController/config.blade.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@
1212
<label>{{ __('app.apps.password') }}</label>
1313
{!! Form::input('password', 'config[password]', '', ['placeholder' => __('app.apps.password'), 'data-config' => 'password', 'class' => 'form-control config-item']) !!}
1414
</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>
1532
<div class="input">
1633
<button style="margin-top: 32px;" class="btn test" id="test_config">Test</button>
1734
</div>
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<ul class="livestats">
22
<li>
3-
<span class="title">servers</span>
4-
<strong>{!! $servers_total !!}</strong>
3+
<span class="title">servers: {!! $servers_total !!}</span>
4+
<span class="title">players: {!! $players_online !!}</span>
55
</li>
66
<li>
7-
<span class="title">online</span>
8-
<strong>{!! $servers_online !!}</strong>
7+
<span class="title">online: {!! $servers_online !!}</span>
8+
<span class="title">{!! $mem !!} GB</span>
99
</li>
1010
</ul>

0 commit comments

Comments
 (0)