Teleport notification
- Create end point to permit the teleport notification. The map url will be /teleport/token_user - Create message teleport - Create receive feature in phaser to teleport TODO Teleport player on black room.
This commit is contained in:
parent
3c2f134e4e
commit
65406f844e
8 changed files with 96 additions and 19 deletions
|
@ -6,6 +6,7 @@ import {PrometheusController} from "./Controller/PrometheusController";
|
|||
import {FileController} from "./Controller/FileController";
|
||||
import {DebugController} from "./Controller/DebugController";
|
||||
import {App as uwsApp} from "./Server/sifrr.server";
|
||||
import {ReportController} from "./Controller/ReportController";
|
||||
|
||||
class App {
|
||||
public app: uwsApp;
|
||||
|
@ -15,6 +16,7 @@ class App {
|
|||
public mapController: MapController;
|
||||
public prometheusController: PrometheusController;
|
||||
private debugController: DebugController;
|
||||
private reportController: ReportController;
|
||||
|
||||
constructor() {
|
||||
this.app = new uwsApp();
|
||||
|
@ -24,6 +26,7 @@ class App {
|
|||
this.authenticateController = new AuthenticateController(this.app);
|
||||
this.fileController = new FileController(this.app);
|
||||
this.mapController = new MapController(this.app);
|
||||
this.reportController = new ReportController(this.app, this.ioSocketController);
|
||||
this.prometheusController = new PrometheusController(this.app, this.ioSocketController);
|
||||
this.debugController = new DebugController(this.app, this.ioSocketController);
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import {HttpResponse} from "uWebSockets.js";
|
||||
import {HttpRequest, HttpResponse} from "uWebSockets.js";
|
||||
import {ADMIN_API_TOKEN} from "../Enum/EnvironmentVariable";
|
||||
|
||||
|
||||
export class BaseController {
|
||||
|
@ -7,4 +8,11 @@ export class BaseController {
|
|||
res.writeHeader('access-control-allow-methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
|
||||
res.writeHeader('access-control-allow-origin', '*');
|
||||
}
|
||||
|
||||
protected checkAdminToken(req: HttpRequest): void {
|
||||
//TODO
|
||||
/*if(req.getHeader('Authorization') !== ADMIN_API_TOKEN){
|
||||
throw 'Error token api';
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
|
|
@ -49,7 +49,7 @@ import {
|
|||
SilentMessage,
|
||||
WebRtcSignalToClientMessage,
|
||||
WebRtcSignalToServerMessage,
|
||||
WebRtcStartMessage, WebRtcDisconnectMessage, PlayGlobalMessage, ReportPlayerMessage
|
||||
WebRtcStartMessage, WebRtcDisconnectMessage, PlayGlobalMessage, ReportPlayerMessage, TeleportMessageMessage
|
||||
} from "../Messages/generated/messages_pb";
|
||||
import {UserMovesMessage} from "../Messages/generated/messages_pb";
|
||||
import Direction = PositionMessage.Direction;
|
||||
|
@ -108,22 +108,6 @@ export class IoSocketController {
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param token
|
||||
*/
|
||||
/* searchClientByToken(token: string): ExSocketInterface | null {
|
||||
const clients: ExSocketInterface[] = Object.values(this.Io.sockets.sockets) as ExSocketInterface[];
|
||||
for (let i = 0; i < clients.length; i++) {
|
||||
const client = clients[i];
|
||||
if (client.token !== token) {
|
||||
continue
|
||||
}
|
||||
return client;
|
||||
}
|
||||
return null;
|
||||
}*/
|
||||
|
||||
private async authenticate(req: HttpRequest): Promise<{ token: string, userUuid: string }> {
|
||||
//console.log(socket.handshake.query.token);
|
||||
|
||||
|
@ -921,4 +905,27 @@ export class IoSocketController {
|
|||
public getWorlds(): Map<string, World> {
|
||||
return this.Worlds;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param token
|
||||
*/
|
||||
searchClientByUuid(uuid: string): ExSocketInterface | null {
|
||||
for(let socket of this.sockets.values()){
|
||||
if(socket.userUuid === uuid){
|
||||
return socket;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public teleport(userUuid: string) {
|
||||
let user = this.searchClientByUuid(userUuid);
|
||||
if(!user){
|
||||
throw 'User not found';
|
||||
}
|
||||
const teleportMessageMessage = new TeleportMessageMessage();
|
||||
teleportMessageMessage.setMap(`/teleport/${user.userUuid}`);
|
||||
user.send(teleportMessageMessage.serializeBinary().buffer, true);
|
||||
}
|
||||
}
|
||||
|
|
38
back/src/Controller/ReportController.ts
Normal file
38
back/src/Controller/ReportController.ts
Normal file
|
@ -0,0 +1,38 @@
|
|||
import {BaseController} from "./BaseController";
|
||||
import {HttpRequest, HttpResponse, TemplatedApp} from "uWebSockets.js";
|
||||
import {IoSocketController} from "./IoSocketController";
|
||||
|
||||
export class ReportController extends BaseController {
|
||||
|
||||
constructor(private App: TemplatedApp, private ioSocketController: IoSocketController) {
|
||||
super();
|
||||
this.teleport();
|
||||
}
|
||||
|
||||
teleport(){
|
||||
this.App.options("/teleport", (res: HttpResponse, req: HttpRequest) => {
|
||||
this.checkAdminToken(req);
|
||||
this.addCorsHeaders(res);
|
||||
res.end();
|
||||
});
|
||||
|
||||
this.App.post("/teleport", (res: HttpResponse, req: HttpRequest) => {
|
||||
(async () => {
|
||||
try {
|
||||
this.checkAdminToken(req);
|
||||
this.addCorsHeaders(res);
|
||||
|
||||
res.onAborted(() => {
|
||||
console.warn('Login request was aborted');
|
||||
})
|
||||
const param = await res.json();
|
||||
this.ioSocketController.teleport(param.userUuid);
|
||||
res.writeStatus("200 OK").end();
|
||||
} catch (e) {
|
||||
console.log("An error happened", e)
|
||||
res.writeStatus(e.status || "500 Internal Server Error").end('An error happened');
|
||||
}
|
||||
})();
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue