Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions CraftyController/CraftyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace App\SupportedApps\CraftyController;

class CraftyController extends \App\SupportedApps implements \App\EnhancedApps
{
public $config;

private $token = null;

//protected $login_first = true; // Uncomment if api requests need to be authed first
protected $method = 'POST'; // Uncomment if requests to the API should be set by POST

public function __construct()
{
//$this->jar = new \GuzzleHttp\Cookie\CookieJar; // Uncomment if cookies need to be set
}

private function getToken()
{
$res = parent::execute($this->url('api/v2/auth/login'), $this->authAttrs());
$data = json_decode($res->getBody());
if ($data->status == 'ok') {
$this->token = $data->data->token;
}
}

public function test()
{
try {
$this->getToken();
echo "Successfully communicated with the API";
} catch (Exception $err) {
echo $err->getMessage();
}
}

public function livestats()
{
$status = "inactive";
$this->getToken();

$res = parent::execute(
$this->url('api/v2/servers'),
attrs: $this->attrs(),
overridemethod: 'GET'
);
$details = json_decode($res->getBody());

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

$servers_online = 0;
$players_online = 0;
$mem = 0;

foreach ($details->data as $server) {
$server_res = parent::execute(
$this->url('api/v2/servers/' . $server->server_id . '/stats'),
attrs: $this->attrs(),
overridemethod: 'GET'
);
$server_details = json_decode($server_res->getBody());
$players_online += $server_details->data->online;
$mem += $this->decodeSizeToGB($server_details->data->mem);
if ($server_details->data->running == true) {
$servers_online++;
}
}

$vars['servers_online'] = $servers_online;
$vars['players_online'] = $players_online;
$vars['mem'] = $mem;

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

private function authAttrs()
{
$ignoreTls = $this->getConfigValue("ignore_tls", false);
return [
"body" => json_encode([
"username" => $this->getConfigValue("username", null),
"password" => $this->getConfigValue("password", null)
]),
"verify" => ($ignoreTls ? false : true)
];
}


public function attrs()
{
$ignoreTls = $this->getConfigValue("ignore_tls", false);
return [
"headers" => [
"content-type" => "application/json",
"Authorization" => "Bearer " . $this->token,
],
"verify" => ($ignoreTls ? false : true)
];
}

public function url($endpoint)
{
$api_url = parent::normaliseurl($this->config->url) . $endpoint;
return $api_url;
}

private function getConfigValue($key, $default = null)
{
return isset($this->config) && isset($this->config->$key)
? $this->config->$key
: $default;
}

private function decodeSizeToGB($size)
{
$units = [
'B' => 1 / (1024 * 1024 * 1024), // Convert bytes to GB
'KB' => 1 / (1024 * 1024), // Convert KB to GB
'MB' => 1 / 1024, // Convert MB to GB
'GB' => 1, // GB is the base unit
'TB' => 1024, // Convert TB to GB
'PB' => 1024 * 1024, // Convert PB to GB
];

if (preg_match('/^([\d\.]+)\s*([KMGTP]?B)$/i', strtoupper($size), $matches)) {
$value = (float)$matches[1];
$unit = $matches[2];

return round($value * ($units[$unit] ?? 1), 1);
}

return false; // Invalid format
}
}
10 changes: 10 additions & 0 deletions CraftyController/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"appid": "200fe80f6b54e75f36a0ae671663f47b417de384",
"name": "Crafty Controller",
"website": "https://craftycontrol.com",
"license": "GNU General Public License v3.0 or later",
"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.",
"enhanced": true,
"tile_background": "dark",
"icon": "craftycontroller.png"
}
36 changes: 36 additions & 0 deletions CraftyController/config.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<h2>{{ __('app.apps.config') }} ({{ __('app.optional') }}) @include('items.enable')</h2>
<div class="items">
<div class="input">
<label>{{ strtoupper(__('app.url')) }}</label>
{!! Form::text('config[override_url]', isset($item) ? $item->getconfig()->override_url : null, array('placeholder' => __('app.apps.override'), 'id' => 'override_url', 'class' => 'form-control')) !!}
</div>
<div class="input">
<label>{{ __('app.apps.username') }}</label>
{!! Form::text('config[username]', isset($item) ? $item->getconfig()->username : null, array('placeholder' => __('app.apps.username'), 'data-config' => 'username', 'class' => 'form-control config-item')) !!}
</div>
<div class="input">
<label>{{ __('app.apps.password') }}</label>
{!! Form::input('password', 'config[password]', '', ['placeholder' => __('app.apps.password'), 'data-config' => 'password', 'class' => 'form-control config-item']) !!}
</div>
<div class="input">
<label>Skip TLS verification</label>
<div class="toggleinput" style="margin-top: 26px; padding-left: 15px;">
{!! Form::hidden('config[ignore_tls]', 0, ['class' => 'config-item', 'data-config' => 'ignore_tls']) !!}
<label class="switch">
<?php
$checked = false;
if (isset($item) && !empty($item) && isset($item->getconfig()->ignore_tls)) {
$checked = $item->getconfig()->ignore_tls;
}
$set_checked = $checked ? ' checked="checked"' : '';
?>
<input type="checkbox" class="config-item" data-config="ignore_tls" name="config[ignore_tls]" value="1" <?php echo $set_checked; ?> />
<span class="slider round"></span>
</label>
</div>
</div>
<div class="input">
<button style="margin-top: 32px;" class="btn test" id="test_config">Test</button>
</div>
</div>

Binary file added CraftyController/craftycontroller.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions CraftyController/livestats.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<ul class="livestats">
<li>
<span class="title">servers:</span><strong>{!! $servers_total !!}</strong>
<span class="title">players:</span><strong>{!! $players_online !!}</strong>
</li>
<li>
<span class="title">online:</span><strong>{!! $servers_online !!}</strong>
<span class="title">memory:</span><strong>{!! $mem !!} GB</strong>
</li>
</ul>