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
@ -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');
|
||||
}
|
||||
})();
|
||||
});
|
||||
}
|
||||
}
|
@ -24,6 +24,8 @@ export enum EventMessage{
|
||||
|
||||
PLAY_GLOBAL_MESSAGE = "play-global-message",
|
||||
STOP_GLOBAL_MESSAGE = "stop-global-message",
|
||||
|
||||
TELEPORT = "teleport",
|
||||
}
|
||||
|
||||
export interface PointInterface {
|
||||
|
@ -22,7 +22,8 @@ import {
|
||||
WebRtcSignalToClientMessage,
|
||||
WebRtcSignalToServerMessage,
|
||||
WebRtcStartMessage,
|
||||
ReportPlayerMessage
|
||||
ReportPlayerMessage,
|
||||
TeleportMessageMessage
|
||||
} from "../Messages/generated/messages_pb"
|
||||
|
||||
import {UserSimplePeerInterface} from "../WebRtc/SimplePeer";
|
||||
@ -129,6 +130,8 @@ export class RoomConnection implements RoomConnection {
|
||||
this.dispatch(EventMessage.PLAY_GLOBAL_MESSAGE, message.getPlayglobalmessage());
|
||||
} else if (message.hasStopglobalmessage()) {
|
||||
this.dispatch(EventMessage.STOP_GLOBAL_MESSAGE, message.getStopglobalmessage());
|
||||
} else if (message.hasTeleportmessagemessage()) {
|
||||
this.dispatch(EventMessage.TELEPORT, message.getTeleportmessagemessage());
|
||||
} else {
|
||||
throw new Error('Unknown message received');
|
||||
}
|
||||
@ -466,6 +469,12 @@ export class RoomConnection implements RoomConnection {
|
||||
});
|
||||
}
|
||||
|
||||
public receiveTeleportMessage(callback: (messageId: string) => void) {
|
||||
return this.onMessage(EventMessage.TELEPORT, (message: TeleportMessageMessage) => {
|
||||
callback(message.getMap());
|
||||
});
|
||||
}
|
||||
|
||||
public emitGlobalMessage(message: PlayGlobalMessageInterface){
|
||||
console.log('emitGlobalMessage', message);
|
||||
const playGlobalMessage = new PlayGlobalMessage();
|
||||
|
@ -269,6 +269,11 @@ export class GameScene extends ResizableScene implements CenterListener {
|
||||
item.fire(message.event, message.state, message.parameters);
|
||||
}));
|
||||
|
||||
connection.receiveTeleportMessage((map: string) => {
|
||||
//TODO
|
||||
console.log('receiveTeleportMessage', map);
|
||||
})
|
||||
|
||||
// When connection is performed, let's connect SimplePeer
|
||||
this.simplePeer = new SimplePeer(this.connection);
|
||||
this.GlobalMessageManager = new GlobalMessageManager(this.connection);
|
||||
|
@ -172,6 +172,10 @@ message WebRtcSignalToClientMessage {
|
||||
string signal = 2;
|
||||
}
|
||||
|
||||
message TeleportMessageMessage{
|
||||
string map = 1;
|
||||
}
|
||||
|
||||
message ServerToClientMessage {
|
||||
oneof message {
|
||||
BatchMessage batchMessage = 1;
|
||||
@ -184,5 +188,6 @@ message ServerToClientMessage {
|
||||
WebRtcDisconnectMessage webRtcDisconnectMessage = 8;
|
||||
PlayGlobalMessage playGlobalMessage = 9;
|
||||
StopGlobalMessage stopGlobalMessage = 10;
|
||||
TeleportMessageMessage teleportMessageMessage = 11;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user