Added some rudimentary server finding

This commit is contained in:
Gregory Ballantine 2022-09-24 16:26:57 -04:00
parent 1fd7bb7a9a
commit 25b68c4bc6
3 changed files with 59 additions and 2 deletions

45
src/Minecraft/Server.php Normal file
View File

@ -0,0 +1,45 @@
<?php
namespace BitGoblin\MCST\Minecraft;
class Server {
// server root directory
private string $rootDir;
// PID file path
private string $pidFilePath;
// server run status
private bool $state = false;
// class constructor
public function __construct($dir) {
$this->rootDir = $dir;
$this->pidFilePath = $dir . '/pid.txt';
// search for server state
if (file_exists($this->pidFilePath)) {
$this->state = true;
}
}
// start the server
public function start(): void {
// TODO - for now just set the state to true
$this->state = true;
}
// stop the server
public function stop(): void {
// TODO - for now just set the state to false
$this->state = false;
}
// getters & setters
public function getDirectory(): string {
return $this->rootDir;
}
public function getState(): bool {
return $this->state;
}
}

View File

@ -4,13 +4,23 @@ use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Views\Twig; use Slim\Views\Twig;
use BitGoblin\MCST\Minecraft\Server;
// index GET route - this page should welcome the user and direct them to the available actions // index GET route - this page should welcome the user and direct them to the available actions
$app->get('/', function (Request $request, Response $response, $args) { $app->get('/', function (Request $request, Response $response, $args) {
$config = $this->get('config'); $config = $this->get('config');
// find servers
$minecraftDir = $config->get('server_directory');
$serverDirs = glob($minecraftDir . "/*", GLOB_ONLYDIR);
$minecraftServers = array();
foreach ($serverDirs as $i => $m) {
array_push($minecraftServers, new Server($m));
}
$view = Twig::fromRequest($request); $view = Twig::fromRequest($request);
return $view->render($response, 'index.twig', [ return $view->render($response, 'index.twig', [
'server_dir' => $config->get('server_directory'), 'servers' => $minecraftServers,
]); ]);
})->setName('index'); })->setName('index');

View File

@ -15,7 +15,9 @@
<div class="columns twelve"> <div class="columns twelve">
<h3>List of servers:</h3> <h3>List of servers:</h3>
<span><p>There are currently no servers registered.</p></span> <span><p>There are currently no servers registered.</p></span>
<p>Home directory: {{ server_dir }}</p> {% for m in servers %}
<p>Server directory: {{ m.getDirectory() }}</p>
{% endfor %}
</div> </div>
</section> </section>