Skip to content

Commit 5bbbf9e

Browse files
committed
add fallback for maps if api call fails
1 parent 4653238 commit 5bbbf9e

File tree

3 files changed

+396
-13
lines changed

3 files changed

+396
-13
lines changed

modules/apiV10.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,11 @@ router.get('/control/changemap', (req, res) => {
626626
if (map.official) {
627627
mapchangeCommand = `map ${map.name}`;
628628
} else {
629-
mapchangeCommand = `host_workshop_map ${map.workshopID}`
629+
if (map.workshopID != '') {
630+
mapchangeCommand = `host_workshop_map ${map.workshopID}`;
631+
} else {
632+
mapchangeCommand = `ds_workshop_changelevel ${map.name}`;
633+
}
630634
}
631635

632636
sf.executeRcon(mapchangeCommand).then((answer) => {

modules/sharedFunctions.js

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var cfg = require('./configClass.js');
66
var serverInfo = require('./serverInfo.js');
77
var controlEmitter = require('./controlEmitter.js');
88
const config = require('../config.js');
9+
const { response } = require('express');
910

1011
rconQ = new queue({ "autostart": true, "timeout": 500, "concurrency": 1 });
1112

@@ -90,12 +91,12 @@ async function reloadMaplist() {
9091
})
9192
resolve(colMaps);
9293
} catch (e) {
93-
reject([]);
94+
reject(e);
9495
}
9596
});
9697
}).on('error', (error) => {
9798
logger.warn(`Steam Workshop Collection request failed: ${error}`);
98-
reject([]);
99+
reject(error);
99100
});
100101
});
101102
}
@@ -133,7 +134,7 @@ async function reloadMaplist() {
133134
"name": _mapName,
134135
"official": official,
135136
"title": details.title,
136-
"workshopID": details.publishedfileid,
137+
"workshopID": details.publishedfileid.toString(),
137138
"description": details.description,
138139
"previewLink": details.preview_url,
139140
"tags": details.tags })
@@ -147,12 +148,38 @@ async function reloadMaplist() {
147148
});
148149
}).on('error', (error) => {
149150
logger.warn(`Steam Workshop Maps Request failed: ${error}`);
150-
reject([]);
151+
reject(error);
151152
});
152153

153154
});
154155
}
155156

157+
function getWorkshopCollectionMapsFromServer() {
158+
return new Promise((resolve, reject) => {
159+
executeRcon('ds_workshop_listmaps ').then((response) => {
160+
let mapArray = response.split(/\r?\n/);
161+
let details = [];
162+
mapArray.forEach((value) => {
163+
mapdetails.push({
164+
"name": value,
165+
"official": false,
166+
"title": value,
167+
"workshopID": "",
168+
"description": "",
169+
"previewLink": "",
170+
"tags": [],
171+
});
172+
});
173+
174+
resolve(details);
175+
}).catch((err) => {
176+
logger.warn(`Could not get workshop collection maps from server: ${err}`);
177+
reject(err);
178+
});
179+
});
180+
}
181+
182+
156183

157184
// Available maps will be built from OfficialMaps.json static file,
158185
// workshop collection and mapsfrom config.
@@ -172,25 +199,59 @@ async function reloadMaplist() {
172199
mapdetails = await getMapDetails(officialMapIds, true);
173200
} catch(error) {
174201
logger.warn(`Getting official maps details failed: ${error}`);
202+
logger.warn('Falling back to name and ID only');
203+
// As fallback use name and id from local file.
204+
let alternateDetails = [];
205+
omJson.forEach( (map) => {
206+
alternateDetails.push( {
207+
"name": map.name,
208+
"official": true,
209+
"title": map.name,
210+
"workshopID": map.id,
211+
"description": "",
212+
"previewLink": "",
213+
"tags": [],
214+
});
215+
});
216+
mapdetails = alternateDetails;
175217
}
176-
218+
177219
if (cfg.workshopCollection != '') {
178220
logger.debug("getting collection ids");
179221
try {
180222
workshopMapIds = await getWorkshopCollection(cfg.workshopCollection);
181223
} catch (error) {
182-
logger.warn(`Getting Workshop map IDs failed: ${error}`);
224+
logger.warn(`Getting Workshop map IDs failed: ${error}
225+
Trying to get names from server.`);
226+
// As a fallback try to get workshop maps from server via rcon command.
227+
try {
228+
mapdetails.push(...await getWorkshopCollectionMapsFromServer());
229+
} catch (err) {
230+
logger.warn(`Loading workshop maps from server failed: ${err}
231+
Workshop maps not available.`);
232+
}
183233
}
184234
}
185235
workshopMapIds.push(...cfg.workshopMaps);
186236

187-
logger.debug("getting workshop maps");
188-
try {
189-
mapdetails.push(...await getMapDetails(workshopMapIds, false));
190-
} catch(error) {
191-
logger.warn(`Getting Workshop maps details failed: ${error}`);
237+
if(workshopMapIds.length > 0) {
238+
logger.debug("getting workshop maps details");
239+
try {
240+
mapdetails.push(...await getMapDetails(workshopMapIds, false));
241+
} catch(error) {
242+
logger.warn(`Getting Workshop maps details failed: ${error}`);
243+
// As a fallback try to get workshop maps from server via rcon command.
244+
try {
245+
mapdetails.push(...await getWorkshopCollectionMapsFromServer());
246+
} catch (err) {
247+
logger.warn(`Loading workshop maps from server failed: ${err}
248+
Workshop maps not available.`);
249+
}
250+
}
251+
}
252+
if (mapdetails.length > 1) {
253+
mapdetails.sort((a, b) => a.title.localeCompare(b.title));
192254
}
193-
// mapdetails.sort((a, b) => a.name.localeCompare(b.title));
194255

195256
serverInfo.mapsDetails = mapdetails;
196257
// TODO: Check if this is still needed.

0 commit comments

Comments
 (0)