Skip to content

Commit

Permalink
Merge pull request #16 from Taraman17/dev
Browse files Browse the repository at this point in the history
Improve update and example webIF
  • Loading branch information
Taraman17 authored Oct 28, 2020
2 parents 06e390d + 032c7c0 commit 40016cb
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 48 deletions.
15 changes: 5 additions & 10 deletions example/gameserver.php → example/gameserver.htm
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
<html>
<head>
<title>CS:GO Gameserver</title>
<link href="gameserver.css" rel="stylesheet" type="text/css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<link href="gameserver.css" rel="stylesheet" type="text/css">
</head>

<body alink="#000099"
Expand All @@ -27,7 +28,7 @@
type="button"
class="text"
value="Update"
onclick="clickButton(this);"/>
onclick="doUpdate(this);"/>
<input id="buttonStart"
type="button"
class="text"
Expand All @@ -52,13 +53,7 @@ class="text"
<div id="startMap" class="label clearfix">
Starten mit:&nbsp;&nbsp;
<select id="mapAuswahl">
<?php
// change path here to load the list of maps to start the server with.
$maplist = file("/home/csgo/csgo_ds/csgo/maplist.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach($maplist as $value){ // Loop through each element
print(" <option value=".$value.">".$value."</option>\n");
}
?>
<!-- Will be filled by Javascript -->
</select>
</div>
</div>
Expand Down Expand Up @@ -109,7 +104,7 @@ class="text"
<p onclick='movePlayer(event);' command="spec">Move to Spectators</p>
<p onclick='alert("test");'>Kick from Server</p>
</div>
<script type="text/javascript" src="js/jquery-3.4.1.min.js"></script>
<script type="text/javascript" src="js/jquery-3.5.1.min.js"></script>
<script type="text/javascript" src="js/gameserver.js"></script>
</body>
</html>
70 changes: 37 additions & 33 deletions example/js/gameserver.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var address, mapSelector;
var address;

function doLogin() {
window.location.href = `${address}/login`;
Expand All @@ -23,9 +23,8 @@ $( document ).ready(() => {
// Change here if you don't host the webInterfae on the same host as the NodeJS API
let ip = window.location.hostname;
address = `https://${ip}:8090`;

mapSelector = $("#map").html();

loadMaplist();
setupPage();

var socket = new WebSocket(`wss://${ip}:8091`);
Expand All @@ -45,7 +44,7 @@ $( document ).ready(() => {
$('#rounds').html(
`Rounds: ${serverInfo.maxRounds} / Left: ${serverInfo.maxRounds - (serverInfo.score.T + serverInfo.score.C)}`
);

$('.playerDiv ul').empty();
$('.playerDiv').hide(0);
if (serverInfo.players.length > 0) {
Expand All @@ -57,25 +56,23 @@ $( document ).ready(() => {
}
if ($('#mapList li').length < 1) {
let maplist = data.mapsAvail;
$("#mapList").empty();
for (map of maplist) {
var li = document.createElement("li");
li.appendChild(document.createTextNode(map));
$("#mapList").append(li);
}
if ($("#noMaps").length) {
$("#noMaps").remove();
}
}
} else if (data.type == "updateProgress") {
$('#popupText').html(`${data.payload.step}: ${data.payload.progress}%`);
if (data.payload.step == 'Update Successful!') {
window.setTimeout( () => {
$('.container-popup').css('display', 'none');
}, 2000);
}, 1500);
}
} else if (data.type == "mapchange") {
if (data.payload.success) {
setupPage('mapchange');
setupPage();
$('.container-popup').css('display', 'none');
} else {
$('#popupText').html(`Mapchange failed!`);
Expand All @@ -87,8 +84,19 @@ $( document ).ready(() => {
}
});

// Load the maplist for serverstart from maplist.txt
function loadMaplist() {
// The Maplist file can be taken from the csgo folder.
$.get('./maplist.txt', (data) => {
let lines = data.split(/\r\n|\n/);
lines.forEach( (map) => {
$("#mapAuswahl").append(`<option value="${map}">${map}</option>`);
});
});
}

// Setup the Elements according to server status.
function setupPage(action) {
function setupPage() {
$('#popupCaption').text('Querying Server');
function loggedIn() {
return Promise.resolve(sendGet(`${address}/loginStatus`));
Expand Down Expand Up @@ -121,7 +129,7 @@ function setupPage(action) {
}

function setupNotLoggedIn() {
$('#power-image').hide()0;
$('#power-image').hide(0);
$('#startMap').hide(0);
$('#buttonStop').hide(0);
$('#buttonStart').hide(0);
Expand All @@ -132,7 +140,7 @@ function setupNotLoggedIn() {
}
function setupServerRunning() {
$('#power-image').attr('src', 'pic/power-on.png');
getCurrentMap();
getMaps();
$('#startMap').hide(0);
$('#mapList').on( 'click', showPlay);
$('#mapList').on( 'dblclick', changeMap);
Expand All @@ -145,7 +153,6 @@ function setupServerRunning() {
}
function setupServerStopped() {
$('#power-image').attr('src', 'pic/power-off.png');
$("#map").html(mapSelector);
$('#startMap').show(0);
$('#buttonStart').show(0);
$('#buttonStop').hide(0);
Expand All @@ -156,6 +163,19 @@ function setupServerStopped() {
$('#mapSelector').hide('fast');
}

function doUpdate(aButton) {
action = aButton.value.toLowerCase();
$('#popupCaption').text(`Updating Server`);
$('#popupText').text('Moment bitte!');
$('.container-popup').css('display', 'flex');

sendGet(`${address}/control`, `action=update`, ( data ) => {
if(!data.success) {
alert('command' + action + ' failed!');
}
});
}

function clickButton(aButton) {
action = aButton.value.toLowerCase();
$('#popupCaption').text(`${action}ing Server`);
Expand All @@ -164,7 +184,7 @@ function clickButton(aButton) {
startMap = document.getElementById('mapAuswahl').value;

sendGet(`${address}/control`, `action=${action}&startmap=${startMap}`, ( data ) => {
setupPage(action);
setupPage();
if(!data.success) {
alert('command' + action + ' failed!');
}
Expand Down Expand Up @@ -194,21 +214,20 @@ function movePlayer(event) {
});
}

function getCurrentMap() {
function getMaps() {
function getServerInfo() {
return Promise.resolve(sendGet(`${address}/serverInfo`));
}
let serverInfo = getServerInfo();
serverInfo.then((data) => {
$("#map").html(`Current map: ${data.map}`);
$("#currentMap").html(`Current map: ${data.map}`);
maplist = data.mapsAvail;
$("#mapList").empty();
for (map of maplist) {
var li = document.createElement("li");
li.appendChild(document.createTextNode(map));
// option.value = map;
$("#mapList").append(li);
}
$("#noMaps").remove();
}).catch((error) => {
// do nothing for now
});
Expand Down Expand Up @@ -255,18 +274,3 @@ function restartRound() {
}, 1000);
});
}

// Bot Training functions
function setBotRules() {
sendGet(`${address}/rcon`, `message=mp_autoteambalance 0`);
sendGet(`${address}/rcon`, `message=mp_limitteams 0`);
sendGet(`${address}/rcon`, `message=bot_difficulty 3`);
}
function addBots(team, quantity) {
for(let i=0; i < quantity; i++) {
setTimeout(sendGet(`${address}/rcon`, `message=bot_add_${team}`), 100);
}
}
function kickBots() {
sendGet(`${address}/rcon`, `message=bot_kick all`);
}
2 changes: 0 additions & 2 deletions example/js/jquery-3.4.1.min.js

This file was deleted.

2 changes: 2 additions & 0 deletions example/js/jquery-3.5.1.min.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions example/maplist.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cs_italy
cs_office
de_aztec
de_dust
de_dust2
de_inferno
de_nuke
de_train
22 changes: 19 additions & 3 deletions serverControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ passport.use(
// Cut the SteamID64 from the returned User-URI
let steamID64 = identifier.split('/')[5];
profile.identifier = steamID64;
console.log(`User with steamID ${steamID64} logged in`);
return done(null, profile);
});
}
Expand Down Expand Up @@ -275,6 +276,7 @@ app.get('/login/return',
}
);
app.get('/logout', (req, res) => {
console.log(`User with steamID ${req.user.identifier} logged out`);
req.logout();
res.redirect(cfg.redirectPage);
});
Expand Down Expand Up @@ -354,6 +356,7 @@ app.get("/control", ensureAuthenticated, (req, res) => {
let updateSuccess = false;
console.log('Updating Server.');
let updateProcess = pty.spawn(cfg.updateCommand, cfg.updateArguments);

updateProcess.on('data', (data) => {
console.log(data);
if (data.indexOf('Checking for available updates') != -1) {
Expand All @@ -379,13 +382,26 @@ app.get("/control", ensureAuthenticated, (req, res) => {
state.operationPending = false;
}
});
updateProcess.once('close', (code) => {

if (cfg.webSockets) {
res.writeHeader(200, {"Content-Type": "application/json"});
res.write(`{ "success": ${updateSuccess} }`);
if (updateProcess) {
res.write(`{ "success": true }`);
} else {
res.write(`{ "success": false }`);
}
res.end();
updateProcess.removeAllListeners();
state.operationPending = false;
});
} else {
updateProcess.once('close', (code) => {
res.writeHeader(200, {"Content-Type": "application/json"});
res.write(`{ "success": ${updateSuccess} }`);
res.end();
updateProcess.removeAllListeners();
state.operationPending = false;
});
}

// Send Status
} else if (args.action == "status") {
Expand Down

0 comments on commit 40016cb

Please sign in to comment.