2020-04-27 15:03:05 +02:00
|
|
|
import {GameManager} from "./Phaser/Game/GameManager";
|
2020-04-07 20:41:35 +02:00
|
|
|
|
2020-04-05 20:57:14 +02:00
|
|
|
const SocketIo = require('socket.io-client');
|
|
|
|
import Axios from "axios";
|
2020-04-27 15:03:05 +02:00
|
|
|
import {API_URL, ROOM} from "./Enum/EnvironmentVariable";
|
2020-04-05 20:57:14 +02:00
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
class Message {
|
2020-04-05 20:57:14 +02:00
|
|
|
userId: string;
|
|
|
|
roomId: string;
|
|
|
|
|
|
|
|
constructor(userId : string, roomId : string) {
|
|
|
|
this.userId = userId;
|
|
|
|
this.roomId = roomId;
|
|
|
|
}
|
|
|
|
|
|
|
|
toJson() {
|
|
|
|
return {
|
|
|
|
userId: this.userId,
|
|
|
|
roomId: this.roomId,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
export interface PointInterface {
|
|
|
|
x: number;
|
|
|
|
y: number;
|
|
|
|
direction : string;
|
|
|
|
toJson() : object;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Point implements PointInterface{
|
2020-04-05 20:57:14 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
export interface MessageUserPositionInterface {
|
|
|
|
userId: string;
|
|
|
|
roomId: string;
|
|
|
|
position: PointInterface;
|
|
|
|
}
|
|
|
|
class MessageUserPosition extends Message implements MessageUserPositionInterface{
|
2020-04-05 20:57:14 +02:00
|
|
|
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()
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
export interface ListMessageUserPositionInterface {
|
|
|
|
roomId : string;
|
|
|
|
listUsersPosition: Array<MessageUserPosition>;
|
|
|
|
}
|
|
|
|
class ListMessageUserPosition{
|
|
|
|
roomId : string;
|
|
|
|
listUsersPosition: Array<MessageUserPosition>;
|
|
|
|
|
|
|
|
constructor(roomId : string, data : any) {
|
|
|
|
this.roomId = roomId;
|
|
|
|
this.listUsersPosition = new Array<MessageUserPosition>();
|
|
|
|
data.forEach((userPosition: any) => {
|
|
|
|
this.listUsersPosition.push(new MessageUserPosition(
|
|
|
|
userPosition.userId,
|
|
|
|
userPosition.roomId,
|
|
|
|
new Point(
|
|
|
|
userPosition.position.x,
|
|
|
|
userPosition.position.y,
|
|
|
|
userPosition.position.direction
|
|
|
|
)
|
|
|
|
));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-04-12 13:57:00 +02:00
|
|
|
export interface ConnexionInterface {
|
|
|
|
socket : any;
|
|
|
|
token : string;
|
|
|
|
email : string;
|
|
|
|
userId: string;
|
|
|
|
startedRoom : string;
|
|
|
|
createConnexion() : Promise<any>;
|
|
|
|
joinARoom(roomId : string) : void;
|
2020-04-27 15:03:05 +02:00
|
|
|
sharePosition(x : number, y : number, direction : string) : void;
|
2020-04-12 13:57:00 +02:00
|
|
|
positionOfAllUser() : void;
|
|
|
|
}
|
|
|
|
export class Connexion implements ConnexionInterface{
|
2020-04-05 20:57:14 +02:00
|
|
|
socket : any;
|
|
|
|
token : string;
|
|
|
|
email : string;
|
2020-04-10 12:54:05 +02:00
|
|
|
userId: string;
|
2020-04-05 20:57:14 +02:00
|
|
|
startedRoom : string;
|
|
|
|
|
2020-04-27 15:03:05 +02:00
|
|
|
GameManager: GameManager;
|
2020-04-07 20:41:35 +02:00
|
|
|
|
2020-04-27 15:03:05 +02:00
|
|
|
constructor(email : string, GameManager: GameManager) {
|
2020-04-05 20:57:14 +02:00
|
|
|
this.email = email;
|
2020-04-07 20:41:35 +02:00
|
|
|
this.GameManager = GameManager;
|
2020-04-10 12:54:05 +02:00
|
|
|
}
|
|
|
|
|
2020-04-12 13:57:00 +02:00
|
|
|
createConnexion() : Promise<ConnexionInterface>{
|
2020-04-10 12:54:05 +02:00
|
|
|
return Axios.post(`${API_URL}/login`, {email: this.email})
|
2020-04-05 20:57:14 +02:00
|
|
|
.then((res) => {
|
|
|
|
this.token = res.data.token;
|
|
|
|
this.startedRoom = res.data.roomId;
|
2020-04-10 12:54:05 +02:00
|
|
|
this.userId = res.data.userId;
|
2020-04-05 20:57:14 +02:00
|
|
|
|
|
|
|
this.socket = SocketIo(`${API_URL}`, {
|
|
|
|
query: {
|
|
|
|
token: this.token
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
//join the room
|
|
|
|
this.joinARoom(this.startedRoom);
|
|
|
|
|
|
|
|
//share your first position
|
2020-04-27 15:03:05 +02:00
|
|
|
this.sharePosition(0, 0);
|
2020-04-05 20:57:14 +02:00
|
|
|
|
|
|
|
this.positionOfAllUser();
|
|
|
|
|
|
|
|
this.errorMessage();
|
2020-04-10 12:54:05 +02:00
|
|
|
|
2020-04-12 13:57:00 +02:00
|
|
|
return this;
|
2020-04-05 20:57:14 +02:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Permit to join a room
|
|
|
|
* @param roomId
|
|
|
|
*/
|
2020-04-12 13:57:00 +02:00
|
|
|
joinARoom(roomId : string) : void {
|
2020-04-10 12:54:05 +02:00
|
|
|
let messageUserPosition = new MessageUserPosition(this.userId, this.startedRoom, new Point(0, 0));
|
2020-04-05 20:57:14 +02:00
|
|
|
this.socket.emit('join-room', messageUserPosition.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-04-07 21:02:23 +02:00
|
|
|
*
|
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-27 15:03:05 +02:00
|
|
|
sharePosition(x : number, y : number, direction : string = "none") : void{
|
2020-04-07 20:46:30 +02:00
|
|
|
if(!this.socket){
|
|
|
|
return;
|
|
|
|
}
|
2020-04-27 15:03:05 +02:00
|
|
|
let messageUserPosition = new MessageUserPosition(this.userId, ROOM[0], 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>,
|
2020-04-07 21:03:33 +02:00
|
|
|
* y : <number>,
|
|
|
|
* direction: <string>
|
2020-04-05 20:57:14 +02:00
|
|
|
* }
|
|
|
|
* },
|
|
|
|
* ...
|
|
|
|
* ]
|
|
|
|
**/
|
2020-04-12 13:57:00 +02:00
|
|
|
positionOfAllUser() : void {
|
2020-04-10 12:54:05 +02:00
|
|
|
this.socket.on("user-position", (message: string) => {
|
|
|
|
let dataList = JSON.parse(message);
|
|
|
|
dataList.forEach((UserPositions: any) => {
|
|
|
|
let listMessageUserPosition = new ListMessageUserPosition(UserPositions[0], UserPositions[1]);
|
|
|
|
this.GameManager.shareUserPosition(listMessageUserPosition);
|
2020-04-07 20:41:35 +02:00
|
|
|
});
|
2020-04-05 20:57:14 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-12 13:57:00 +02:00
|
|
|
errorMessage() : void {
|
2020-04-05 20:57:14 +02:00
|
|
|
this.socket.on('message-error', (message : string) => {
|
|
|
|
console.error("message-error", message);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|