2020-04-07 20:41:35 +02:00
|
|
|
import {GameManagerInterface} from "./Phaser/Game/GameManager";
|
|
|
|
|
2020-04-05 20:57:14 +02:00
|
|
|
const SocketIo = require('socket.io-client');
|
|
|
|
import Axios from "axios";
|
|
|
|
import {API_URL} from "./Enum/EnvironmentVariable";
|
|
|
|
|
|
|
|
export interface PointInterface {
|
|
|
|
x: number;
|
|
|
|
y: number;
|
|
|
|
toJson() : object;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Message {
|
|
|
|
userId: string;
|
|
|
|
roomId: string;
|
|
|
|
|
|
|
|
constructor(userId : string, roomId : string) {
|
|
|
|
this.userId = userId;
|
|
|
|
this.roomId = roomId;
|
|
|
|
}
|
|
|
|
|
|
|
|
toJson() {
|
|
|
|
return {
|
|
|
|
userId: this.userId,
|
|
|
|
roomId: this.roomId,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Point implements PointInterface{
|
|
|
|
x: number;
|
|
|
|
y: number;
|
2020-04-07 21:02:23 +02:00
|
|
|
direction : string;
|
2020-04-05 20:57:14 +02:00
|
|
|
|
2020-04-07 21:02:23 +02:00
|
|
|
constructor(x : number, y : number, direction : string = "none") {
|
2020-04-05 20:57:14 +02:00
|
|
|
if(x === null || y === null){
|
|
|
|
throw Error("position x and y cannot be null");
|
|
|
|
}
|
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
2020-04-07 21:02:23 +02:00
|
|
|
this.direction = direction;
|
2020-04-05 20:57:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
toJson(){
|
|
|
|
return {
|
|
|
|
x : this.x,
|
2020-04-07 21:02:23 +02:00
|
|
|
y: this.y,
|
|
|
|
direction: this.direction
|
2020-04-05 20:57:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class MessageUserPosition extends Message{
|
|
|
|
position: PointInterface;
|
|
|
|
|
|
|
|
constructor(userId : string, roomId : string, point : Point) {
|
|
|
|
super(userId, roomId);
|
|
|
|
this.position = point;
|
|
|
|
}
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
return JSON.stringify(
|
|
|
|
Object.assign(
|
|
|
|
super.toJson(),
|
|
|
|
{
|
|
|
|
position: this.position.toJson()
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Connexion {
|
|
|
|
socket : any;
|
|
|
|
token : string;
|
|
|
|
email : string;
|
|
|
|
startedRoom : string;
|
|
|
|
|
2020-04-07 20:41:35 +02:00
|
|
|
GameManager: GameManagerInterface;
|
|
|
|
|
|
|
|
constructor(email : string, GameManager: GameManagerInterface) {
|
2020-04-05 20:57:14 +02:00
|
|
|
this.email = email;
|
2020-04-07 20:41:35 +02:00
|
|
|
this.GameManager = GameManager;
|
2020-04-05 20:57:14 +02:00
|
|
|
Axios.post(`${API_URL}/login`, {email: email})
|
|
|
|
.then((res) => {
|
|
|
|
this.token = res.data.token;
|
|
|
|
this.startedRoom = res.data.roomId;
|
|
|
|
|
|
|
|
this.socket = SocketIo(`${API_URL}`, {
|
|
|
|
query: {
|
|
|
|
token: this.token
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
//join the room
|
|
|
|
this.joinARoom(this.startedRoom);
|
|
|
|
|
|
|
|
//share your first position
|
2020-04-07 20:41:35 +02:00
|
|
|
this.sharePosition(this.startedRoom, 0, 0);
|
2020-04-05 20:57:14 +02:00
|
|
|
|
|
|
|
this.positionOfAllUser();
|
|
|
|
|
|
|
|
this.errorMessage();
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Permit to join a room
|
|
|
|
* @param roomId
|
|
|
|
*/
|
|
|
|
joinARoom(roomId : string){
|
|
|
|
let messageUserPosition = new MessageUserPosition(this.email, this.startedRoom, new Point(0, 0));
|
|
|
|
this.socket.emit('join-room', messageUserPosition.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-07 21:02:23 +02:00
|
|
|
*
|
|
|
|
* @param roomId
|
2020-04-05 20:57:14 +02:00
|
|
|
* @param x
|
|
|
|
* @param y
|
2020-04-07 21:02:23 +02:00
|
|
|
* @param direction
|
2020-04-05 20:57:14 +02:00
|
|
|
*/
|
2020-04-07 21:02:23 +02:00
|
|
|
sharePosition(roomId : string, x : number, y : number, direction : string = "none"){
|
2020-04-07 20:46:30 +02:00
|
|
|
if(!this.socket){
|
|
|
|
return;
|
|
|
|
}
|
2020-04-07 21:02:23 +02:00
|
|
|
let messageUserPosition = new MessageUserPosition(this.email, roomId, new Point(x, y, direction));
|
2020-04-05 20:57:14 +02:00
|
|
|
this.socket.emit('user-position', messageUserPosition.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The data sent is an array with information for each user :
|
|
|
|
* [
|
|
|
|
* {
|
|
|
|
* userId: <string>,
|
|
|
|
* roomId: <string>,
|
|
|
|
* position: {
|
|
|
|
* x : <number>,
|
|
|
|
* y : <number>
|
|
|
|
* }
|
|
|
|
* },
|
|
|
|
* ...
|
|
|
|
* ]
|
|
|
|
**/
|
|
|
|
positionOfAllUser(){
|
|
|
|
this.socket.on("user-position", (message : string) => {
|
2020-04-07 20:41:35 +02:00
|
|
|
let data = JSON.parse(message);
|
|
|
|
data.forEach((UserPositions : any) => {
|
|
|
|
this.GameManager.sharedUserPosition(UserPositions);
|
|
|
|
});
|
2020-04-05 20:57:14 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
errorMessage(){
|
|
|
|
this.socket.on('message-error', (message : string) => {
|
|
|
|
console.error("message-error", message);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|