Added a check to make sure the server doesn't exist before creating it

This commit is contained in:
Gregory Ballantine 2022-10-06 21:11:54 -04:00
parent 4007b99833
commit 54199bf7c8
4 changed files with 40 additions and 3 deletions

View File

@ -2,6 +2,18 @@ body{
background-color: #e6e6e6; background-color: #e6e6e6;
} }
input[type=submit]:disabled,
button:disabled{
background-color: darkgrey;
}
input[type=submit]:disabled:hover,
button:disabled:hover{
background-color: darkgrey;
border: 1px solid #bbb;
color: #555;
cursor: not-allowed;
}
/* set the max-width for centered content */ /* set the max-width for centered content */
.container{ .container{
max-width: 1100px; max-width: 1100px;

View File

@ -1,7 +1,11 @@
$(document).ready(function () { $(document).ready(function () {
// periodically check for server status updates
$('.serverItem').each(function() { $('.serverItem').each(function() {
setInterval(updateServer, 5000, $(this)); setInterval(updateServer, 5000, $(this));
}); });
// set the serverName input field to check if the server exists
$('input#serverName').on('change', checkServerExists);
}); });
function updateServer(elem) { function updateServer(elem) {
@ -10,3 +14,16 @@ function updateServer(elem) {
elem.children('.serverState').eq(0).text(data.state); elem.children('.serverState').eq(0).text(data.state);
}); });
} }
function checkServerExists() {
$.get('/server/' + $(this).val() + '/status', function(data, state) {
if (data.exists) {
$('input#createSubmit').prop('disabled', true);
alert('That server name is already used; please use another name!');
} else {
if ($('input#createSubmit').prop('disabled')) {
$('input#createSubmit').prop('disabled', false);
}
}
});
}

View File

@ -73,9 +73,17 @@ $app->get('/server/{serverName}/status', function (Request $request, Response $r
$config = $this->get('config'); $config = $this->get('config');
$serverDir = join('/', array($config->get('server_directory'), $args['serverName'])); $serverDir = join('/', array($config->get('server_directory'), $args['serverName']));
// check if the server exists - if not, return a false result
if (!is_dir($serverDir)) {
$response->getBody()->write(json_encode(array('exists' => false)));
return $response
->withHeader('Content-Type', 'application/json');
}
// create server object and pass info back to client as JSON data // create server object and pass info back to client as JSON data
$server = new Server($serverDir); $server = new Server($serverDir);
$serverData = [ $serverData = [
'exists' => true,
'name' => $server->getName(), 'name' => $server->getName(),
'version' => $server->getVersion(), 'version' => $server->getVersion(),
'state' => $server->getState() ? 'Running' : 'Stopped', 'state' => $server->getState() ? 'Running' : 'Stopped',

View File

@ -16,16 +16,16 @@
<div class="row"> <div class="row">
<div class="columns six"> <div class="columns six">
<label for="serverName">Server name:</label> <label for="serverName">Server name:</label>
<input id="serverName" name="serverName" class="u-full-width" type="text" placeholder="MyServer"> <input id="serverName" name="serverName" class="u-full-width" type="text" placeholder="MyServer" required>
</div> </div>
<div class="columns six"> <div class="columns six">
<label for="serverVersion">Minecraft version:</label> <label for="serverVersion">Minecraft version:</label>
<input id="serverVersion" name="serverVersion" class="u-full-width" type="text" placeholder="1.19.2"> <input id="serverVersion" name="serverVersion" class="u-full-width" type="text" placeholder="1.19.2" required>
</div> </div>
</div> </div>
<input type="submit" value="Submit"> <input id="createSubmit" type="submit" value="Submit">
</form> </form>
</div> </div>
</section> </section>