worlds + fix map update

This commit is contained in:
_Bastler 2021-09-27 20:00:45 +02:00
parent 7ffbbae731
commit 8cc1584c2e
3 changed files with 78 additions and 6 deletions

View File

@ -3,7 +3,7 @@ import { BaseController } from "./BaseController";
import { parse } from "query-string";
import { adminApi } from "../Services/AdminApi";
import { ADMIN_API_URL } from "../Enum/EnvironmentVariable";
import { GameRoomPolicyTypes } from "../Model/PusherRoom";
import { PusherRoom, GameRoomPolicyTypes } from "../Model/PusherRoom";
import { MapDetailsData } from "../Services/AdminApi/MapDetailsData";
import { socketManager } from "../Services/SocketManager";
import { AuthTokenData, jwtTokenManager } from "../Services/JWTTokenManager";
@ -14,6 +14,7 @@ export class MapController extends BaseController {
super();
this.App = App;
this.getMapUrl();
this.getWorlds();
}
// Returns a map mapping map name to file name of the map
@ -51,7 +52,7 @@ export class MapController extends BaseController {
return;
}
const mapUrl = roomUrl.protocol + "//" + match[1];
const mapUrl = roomUrl.protocol + "//" + match[ 1 ];
res.writeStatus("200 OK");
this.addCorsHeaders(res);
@ -95,4 +96,61 @@ export class MapController extends BaseController {
})();
});
}
getWorlds() {
this.App.options("/worlds", (res: HttpResponse, req: HttpRequest) => {
this.addCorsHeaders(res);
res.end();
});
this.App.get("/worlds", async (res: HttpResponse, req: HttpRequest) => {
res.onAborted(() => {
console.warn("/message request was aborted");
});
const { userIdentify, token } = parse(req.getQuery());
try {
//verify connected by token
if (token != undefined) {
try {
const authTokenData: AuthTokenData = jwtTokenManager.verifyJWTToken(token as string, false);
if (authTokenData.hydraAccessToken == undefined) {
throw Error("Token cannot to be check on Hydra");
}
const worlds: Map<String, PusherRoom> = socketManager.getWorlds();
const result: any = {};
for (const room of worlds.values()) {
result[room.roomUrl] = room.getListeners().size;
/*
for (const listener of room.getListeners()) {
const position: any = {};
position.name = listener.name;
position.roomId = listener.roomId;
position.position = listener.position;
position.viewport = listener.viewport;
result.push(position);
}
*/
}
return res
.writeStatus("200 OK")
.writeHeader("Content-Type", "application/json")
.end(
JSON.stringify(result)
);
} catch (error) {
return this.errorToResponse(error, res);
}
}
} catch (error) {
console.error("getWorlds => ERROR", error);
this.errorToResponse(error, res);
}
});
}
}

View File

@ -80,6 +80,10 @@ export class PusherRoom {
}
}
public getListeners(): Set<ExSocketInterface> {
return this.listeners;
}
/**
* Creates a connection to the back server to track global messages relative to this room (like variable changes).
*/

View File

@ -34,6 +34,7 @@ import {
VariableMessage,
ErrorMessage,
WorldFullMessage,
PositionMessage,
} from "../Messages/generated/messages_pb";
import { ProtobufUtils } from "../Model/Websocket/ProtobufUtils";
import { ADMIN_API_URL, JITSI_ISS, JITSI_URL, SECRET_JITSI_KEY, PUSHER_FORCE_ROOM_UPDATE } from "../Enum/EnvironmentVariable";
@ -53,10 +54,10 @@ import { CharacterTexture } from "./AdminApi/CharacterTexture";
const debug = Debug("socket");
interface AdminSocketRoomsList {
[index: string]: number;
[ index: string ]: number;
}
interface AdminSocketUsersList {
[index: string]: boolean;
[ index: string ]: boolean;
}
export interface AdminSocketData {
@ -249,6 +250,13 @@ export class SocketManager implements ZoneEventListener {
client.backConnection.write(pusherToBackMessage);
const position = userMovesMessage.getPosition();
if (position === undefined) {
throw new Error("Missing position in UserMovesMessage");
}
// update position
client.position = ProtobufUtils.toPointInterface(position);
const viewport = userMovesMessage.getViewport();
if (viewport === undefined) {
throw new Error("Missing viewport in UserMovesMessage");
@ -366,13 +374,15 @@ export class SocketManager implements ZoneEventListener {
async getOrCreateRoom(roomUrl: string): Promise<PusherRoom> {
//check and create new world for a room
let room = this.rooms.get(roomUrl);
if (PUSHER_FORCE_ROOM_UPDATE || room === undefined) {
if (room === undefined) {
room = new PusherRoom(roomUrl, this);
if (ADMIN_API_URL) {
await this.updateRoomWithAdminData(room);
}
await room.init();
this.rooms.set(roomUrl, room);
} else if (PUSHER_FORCE_ROOM_UPDATE && ADMIN_API_URL) {
await this.updateRoomWithAdminData(room);
}
return room;
}
@ -625,7 +635,7 @@ export class SocketManager implements ZoneEventListener {
if (playGlobalMessageEvent.getBroadcasttoworld()) {
tabUrlRooms = await adminApi.getUrlRoomsFromSameWorld(clientRoomUrl);
} else {
tabUrlRooms = [clientRoomUrl];
tabUrlRooms = [ clientRoomUrl ];
}
const roomMessage = new AdminRoomMessage();