2020-05-09 17:50:47 +02:00
|
|
|
import {OK} from "http-status-codes";
|
2020-05-10 18:34:55 +02:00
|
|
|
import {URL_ROOM_STARTED} from "../Enum/EnvironmentVariable";
|
2020-09-28 18:52:54 +02:00
|
|
|
import {HttpRequest, HttpResponse, TemplatedApp} from "uWebSockets.js";
|
|
|
|
import {BaseController} from "./BaseController";
|
2020-10-13 15:12:24 +02:00
|
|
|
import {parse} from "query-string";
|
|
|
|
import {adminApi} from "../Services/AdminApi";
|
2020-05-09 17:50:47 +02:00
|
|
|
|
2020-09-28 15:02:37 +02:00
|
|
|
//todo: delete this
|
2020-09-28 18:52:54 +02:00
|
|
|
export class MapController extends BaseController{
|
2020-05-09 17:50:47 +02:00
|
|
|
|
2020-09-28 18:52:54 +02:00
|
|
|
constructor(private App : TemplatedApp) {
|
|
|
|
super();
|
2020-05-09 17:50:47 +02:00
|
|
|
this.App = App;
|
2020-10-13 15:12:24 +02:00
|
|
|
this.getMapUrl();
|
2020-05-09 17:50:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-10 14:48:34 +02:00
|
|
|
// Returns a map mapping map name to file name of the map
|
2020-10-13 15:12:24 +02:00
|
|
|
getMapUrl() {
|
|
|
|
this.App.options("/map", (res: HttpResponse, req: HttpRequest) => {
|
2020-09-28 18:52:54 +02:00
|
|
|
this.addCorsHeaders(res);
|
|
|
|
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
|
2020-10-13 15:12:24 +02:00
|
|
|
this.App.get("/map", (res: HttpResponse, req: HttpRequest) => {
|
2020-09-28 18:52:54 +02:00
|
|
|
this.addCorsHeaders(res);
|
|
|
|
|
2020-10-13 15:29:08 +02:00
|
|
|
res.onAborted(() => {
|
|
|
|
console.warn('/map request was aborted');
|
|
|
|
})
|
|
|
|
|
2020-10-13 15:12:24 +02:00
|
|
|
const query = parse(req.getQuery());
|
|
|
|
|
|
|
|
if (typeof query.organizationSlug !== 'string') {
|
|
|
|
console.error('Expected organizationSlug parameter');
|
|
|
|
res.writeStatus("400 Bad request").end("Expected organizationSlug parameter");
|
|
|
|
}
|
|
|
|
if (typeof query.worldSlug !== 'string') {
|
|
|
|
console.error('Expected worldSlug parameter');
|
|
|
|
res.writeStatus("400 Bad request").end("Expected worldSlug parameter");
|
|
|
|
}
|
|
|
|
if (typeof query.roomSlug !== 'string' && query.roomSlug !== undefined) {
|
|
|
|
console.error('Expected only one roomSlug parameter');
|
|
|
|
res.writeStatus("400 Bad request").end("Expected only one roomSlug parameter");
|
|
|
|
}
|
|
|
|
|
|
|
|
(async () => {
|
2020-10-13 15:29:08 +02:00
|
|
|
try {
|
|
|
|
const mapDetails = await adminApi.fetchMapDetails(query.organizationSlug as string, query.worldSlug as string, query.roomSlug as string|undefined);
|
|
|
|
|
|
|
|
res.writeStatus("200 OK").end(JSON.stringify(mapDetails));
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
res.writeStatus("500 Internal Server Error").end("An error occurred");
|
|
|
|
}
|
2020-10-13 15:12:24 +02:00
|
|
|
})();
|
|
|
|
|
2020-05-09 17:50:47 +02:00
|
|
|
});
|
|
|
|
}
|
2020-05-10 14:48:34 +02:00
|
|
|
}
|