Skip to content

Commit

Permalink
Merge pull request #12 from Taraman17/updateMaps
Browse files Browse the repository at this point in the history
Update maps
  • Loading branch information
Taraman17 authored Oct 2, 2020
2 parents 4da7038 + e50a348 commit 6808b1a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ The /control message will return a JSON-String.
'action' can have the following values:
- status -> fetch the servers running status: { "running": true/false }
- update -> update the server (may take quite some time): { "success": true/false }
- start -> optional additional argument "starmap" (action=start&startmap=mapname): { "success": true/false }<br>
- start -> optional additional argument "starmap" (action=start&startmap=mapname): { "success": true/false }
If run without startmap, server will be started with famous de_dust2.
- changemap -> additional argument "map" (action=changemap&map=mapname):
If you do not use websockets, the answer will be { "success": true/false }. true if start of new map is logged, false in case of error or after a timeout of 30 seconds.
If you use websockets, answer will be { "success": true/false } if the rcon command to server was sauccessful or not. Completion of mapchange is sent via the websocket in the format:
```javascript
{ "type": "mapchange", "payload": { "success": true/false } }
```
- reloadmaplist -> reload the available maps on the server (action=reloadmaplist): { "success": true/false }
- stop -> stop the server: { "success": true/false }

### RCON
Expand Down
46 changes: 42 additions & 4 deletions serverControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,31 @@ exec('/bin/ps -a', (error, stdout, stderr) => {
}
});

/**
* Get available maps from server and store them in serverInfo
* @returns {Promise<JSON-string>} - Promise object that yields the result of reload.
*/
function reloadMaplist() {
return new Promise((resolve, reject) => {
executeRcon('maps *').then((answer) => {
let re = /\(fs\) (\S+).bsp/g;
let maplist = [];
let mapsArray = getMatches(answer, re, 1);
mapsArray.forEach((mapString) => {
maplist.push(cutMapName(mapString));
});
maplist.sort();
if (maplist.length > 0) {
serverInfo.mapsAvail = maplist;
resolve(`{ "sucess": true }`);
} else {
resolve(`{ "sucess": false }`);
}
}).catch((err) => {
resolve(`{ "sucess": false }`);
});
});
}

// Event Emitters
var mapChangeEmitter = new events.EventEmitter();
Expand Down Expand Up @@ -110,7 +135,12 @@ authEmitter.on('authenticated', () => {
let mapstring = matches[1];
serverInfo.map = cutMapName(mapstring);
});
executeRcon('maps *').then((answer) => {
reloadMaplist().then((answer) => {
if (answer == '{ "sucess": false }') {
console.log ("Maps could not be loaded");
}
});
/* executeRcon('maps *').then((answer) => {
let re = /\(fs\) (\S+).bsp/g;
let maplist = [];
let mapsArray = getMatches(answer, re, 1);
Expand All @@ -119,13 +149,13 @@ authEmitter.on('authenticated', () => {
});
maplist.sort();
serverInfo.mapsAvail = maplist;
});
}); */
});


/**
* Authenticate rcon with server
* @returns {Promise<JSON-string>}- Promise object that yields the result of authentication.
* @returns {Promise<JSON-string>} - Promise object that yields the result of authentication.
* @emits authEmitter.authenticated
*/
function authenticate() {
Expand Down Expand Up @@ -335,6 +365,15 @@ app.get("/control", (req, res) => {
res.write(`{ "completed": ${result == 'success'} }`);
res.end();
});

// Update Maps available on server
} else if (args.action == "reloadmaplist") {
reloadMaplist().then( (answer) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.writeHeader(200, { 'Content-Type': 'application/json' });
res.write(answer);
res.end();
});
}
});

Expand Down Expand Up @@ -415,7 +454,6 @@ if (cfg.webSockets) {
ws.on('message', (message) => {
if (message.search("infoRequest") != -1) {
sendUpdate();
//ws.send(`{ "type": "serverInfo", "payload": ${JSON.stringify(serverInfo.getAll())} }`);
}
});

Expand Down

0 comments on commit 6808b1a

Please sign in to comment.