2021-06-24 10:09:10 +02:00
|
|
|
import { HttpRequest, HttpResponse, TemplatedApp } from "uWebSockets.js";
|
|
|
|
import { BaseController } from "./BaseController";
|
|
|
|
import { parse } from "query-string";
|
|
|
|
import { adminApi } from "../Services/AdminApi";
|
2020-05-09 17:50:47 +02:00
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
export class MapController extends BaseController {
|
|
|
|
constructor(private App: TemplatedApp) {
|
2020-09-28 18:52:54 +02:00
|
|
|
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-10-13 15:29:08 +02:00
|
|
|
res.onAborted(() => {
|
2021-06-24 10:09:10 +02:00
|
|
|
console.warn("/map request was aborted");
|
|
|
|
});
|
2020-10-13 15:29:08 +02:00
|
|
|
|
2020-10-13 15:12:24 +02:00
|
|
|
const query = parse(req.getQuery());
|
|
|
|
|
2021-06-24 10:09:10 +02:00
|
|
|
if (typeof query.organizationSlug !== "string") {
|
|
|
|
console.error("Expected organizationSlug parameter");
|
2020-10-15 17:27:40 +02:00
|
|
|
res.writeStatus("400 Bad request");
|
|
|
|
this.addCorsHeaders(res);
|
|
|
|
res.end("Expected organizationSlug parameter");
|
2020-11-13 12:11:59 +01:00
|
|
|
return;
|
2020-10-13 15:12:24 +02:00
|
|
|
}
|
2021-06-24 10:09:10 +02:00
|
|
|
if (typeof query.worldSlug !== "string") {
|
|
|
|
console.error("Expected worldSlug parameter");
|
2020-10-15 17:27:40 +02:00
|
|
|
res.writeStatus("400 Bad request");
|
|
|
|
this.addCorsHeaders(res);
|
|
|
|
res.end("Expected worldSlug parameter");
|
2020-11-13 12:11:59 +01:00
|
|
|
return;
|
2020-10-13 15:12:24 +02:00
|
|
|
}
|
2021-06-24 10:09:10 +02:00
|
|
|
if (typeof query.roomSlug !== "string" && query.roomSlug !== undefined) {
|
|
|
|
console.error("Expected only one roomSlug parameter");
|
2020-10-15 17:27:40 +02:00
|
|
|
res.writeStatus("400 Bad request");
|
|
|
|
this.addCorsHeaders(res);
|
|
|
|
res.end("Expected only one roomSlug parameter");
|
2020-11-13 12:11:59 +01:00
|
|
|
return;
|
2020-10-13 15:12:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
(async () => {
|
2020-10-13 15:29:08 +02:00
|
|
|
try {
|
2021-06-24 10:09:10 +02:00
|
|
|
const mapDetails = await adminApi.fetchMapDetails(
|
|
|
|
query.organizationSlug as string,
|
|
|
|
query.worldSlug as string,
|
|
|
|
query.roomSlug as string | undefined
|
|
|
|
);
|
2020-10-13 15:29:08 +02:00
|
|
|
|
2020-10-15 17:27:40 +02:00
|
|
|
res.writeStatus("200 OK");
|
|
|
|
this.addCorsHeaders(res);
|
|
|
|
res.end(JSON.stringify(mapDetails));
|
2020-10-13 15:29:08 +02:00
|
|
|
} catch (e) {
|
2021-01-17 20:42:33 +01:00
|
|
|
this.errorToResponse(e, res);
|
2020-10-13 15:29:08 +02:00
|
|
|
}
|
2020-10-13 15:12:24 +02:00
|
|
|
})();
|
2020-05-09 17:50:47 +02:00
|
|
|
});
|
|
|
|
}
|
2020-05-10 14:48:34 +02:00
|
|
|
}
|