improvments

This commit is contained in:
arp 2020-09-28 15:02:37 +02:00
parent af4611ed29
commit 3f9659ef3c
10 changed files with 72 additions and 71 deletions

View file

@ -2,51 +2,61 @@ import Axios from "axios";
import {API_URL} from "../Enum/EnvironmentVariable";
import {RoomConnection} from "./RoomConnection";
interface LoginApiData {
authToken: string
userUuid: string
mapUrlStart: string
newUrl: string
}
class ConnectionManager {
private initPromise: Promise<LoginApiData> = Promise.reject();
private mapUrlStart: string|null = null;
private authToken:string|null = null;
private userUuid: string|null = null;
private userName:string|null = null;
public async init(): Promise<void> {
const match = /\/register\/(.+)/.exec(window.location.toString());
const organizationMemberToken = match ? match[1] : null;
const res = await Axios.post(`${API_URL}/login`, {organizationMemberToken});
this.authToken = res.data.authToken;
this.userUuid = res.data.userUuid;
this.mapUrlStart = res.data.mapUrlStart;
const newUrl = res.data.newUrl;
this.initPromise = Axios.post(`${API_URL}/login`, {organizationMemberToken}).then(res => res.data);
const data = await this.initPromise
this.authToken = data.authToken;
this.userUuid = data.userUuid;
this.mapUrlStart = data.mapUrlStart;
const newUrl = data.newUrl;
if (newUrl) {
history.pushState({}, '', newUrl);
}
}
public async setUserName(name:string): Promise<void> {
//todo
public connectToRoomSocket(): Promise<RoomConnection> {
return new Promise<RoomConnection>((resolve, reject) => {
const connection = new RoomConnection(this.authToken as string);
connection.onConnectError((error: object) => {
console.log('An error occurred while connecting to socket server. Retrying');
reject(error);
});
resolve(connection);
}).catch((err) => {
// Let's retry in 4-6 seconds
return new Promise<RoomConnection>((resolve, reject) => {
setTimeout(() => {
//todo: allow a way to break recurrsion?
this.connectToRoomSocket().then((connection) => resolve(connection));
}, 4000 + Math.floor(Math.random() * 2000) );
});
});
}
public connectToRoomSocket(): Promise<RoomConnection> {
return Axios.post(`${API_URL}/connectToSocket`, {authToken: this.authToken}).then((res) => {
return new Promise<RoomConnection>((resolve, reject) => {
const connection = new RoomConnection(res.data.roomToken);
connection.onConnectError((error: object) => {
console.log('An error occurred while connecting to socket server. Retrying');
reject(error);
});
resolve(connection);
});
})
.catch((err) => {
// Let's retry in 4-6 seconds
return new Promise<RoomConnection>((resolve, reject) => {
setTimeout(() => {
//todo: allow a way to break recurrsion?
this.connectToRoomSocket().then((connection) => resolve(connection));
}, 4000 + Math.floor(Math.random() * 2000) );
});
});
public getMapUrlStart(): Promise<string> {
return this.initPromise.then(() => {
if (!this.mapUrlStart) {
throw new Error('No map url set!');
}
return this.mapUrlStart;
})
}
}

View file

@ -83,9 +83,9 @@ export class RoomConnection implements RoomConnection {
})
}
public emitPlayerDetailsMessage(characterLayersSelected: string[]) {
public emitPlayerDetailsMessage(userName: string, characterLayersSelected: string[]) {
const message = new SetPlayerDetailsMessage();
message.setName(name);
message.setName(userName);
message.setCharacterlayersList(characterLayersSelected);
this.socket.emit(EventMessage.SET_PLAYER_DETAILS, message.serializeBinary().buffer, (id: number) => {
this.userId = id;

View file

@ -1,4 +1,4 @@
import {PointInterface} from "../../Connexion/Connection";
import {PointInterface} from "../../Connexion/ConnexionModels";
export interface AddPlayerInterface {
userId: number;

View file

@ -4,7 +4,7 @@ import {
} from "../../Connexion/ConnexionModels";
import Axios from "axios";
import {API_URL} from "../../Enum/EnvironmentVariable";
import {adminDataFetchPromise} from "../../register";
import {connectionManager} from "../../Connexion/ConnectionManager";
export interface HasMovedEvent {
direction: string;
@ -30,23 +30,12 @@ export class GameManager {
}
loadStartMap() : Promise<StartMapInterface> {
if (adminDataFetchPromise) {
return adminDataFetchPromise.then(data => {
return {
mapUrlStart: data.mapUrlStart,
startInstance: data.startInstance,
}
})
} else {
//todo: remove this call, merge with the admin workflow?
return Axios.get(`${API_URL}/start-map`)
.then((res) => {
return res.data;
}).catch((err) => {
console.error(err);
throw err;
});
}
return connectionManager.getMapUrlStart().then(mapUrlStart => {
return {
mapUrlStart: mapUrlStart,
startInstance: "global", //todo: is this property still usefull?
}
});
}
getPlayerName(): string {

View file

@ -205,8 +205,8 @@ export class GameScene extends Phaser.Scene implements CenterListener {
this.connectionPromise = connectionManager.connectToRoomSocket().then((connection : RoomConnection) => {
this.connection = connection;
this.connection.emitPlayerDetailsMessage(gameManager.getCharacterSelected())
this.connection.emitPlayerDetailsMessage(gameManager.getPlayerName(), gameManager.getCharacterSelected())
connection.onUserJoins((message: MessageUserJoined) => {
const userMessage: AddPlayerInterface = {