2020-04-27 15:03:05 +02:00
|
|
|
import {GameManager} from "./Phaser/Game/GameManager";
|
2020-04-05 20:57:14 +02:00
|
|
|
import Axios from "axios";
|
2020-05-10 17:31:27 +02:00
|
|
|
import {API_URL} from "./Enum/EnvironmentVariable";
|
2020-05-13 20:22:42 +02:00
|
|
|
import {MessageUI} from "./Logger/MessageUI";
|
2020-05-15 22:40:06 +02:00
|
|
|
import {SetPlayerDetailsMessage} from "./Messages/SetPlayerDetailsMessage";
|
|
|
|
|
|
|
|
const SocketIo = require('socket.io-client');
|
|
|
|
import Socket = SocketIOClient.Socket;
|
2020-05-22 22:59:43 +02:00
|
|
|
import {PlayerAnimationNames} from "./Phaser/Player/Animation";
|
2020-06-05 13:07:18 +02:00
|
|
|
import {UserSimplePeer} from "./WebRtc/SimplePeer";
|
2020-05-15 22:40:06 +02:00
|
|
|
|
2020-04-05 20:57:14 +02:00
|
|
|
|
2020-04-25 16:05:33 +02:00
|
|
|
enum EventMessage{
|
|
|
|
WEBRTC_SIGNAL = "webrtc-signal",
|
|
|
|
WEBRTC_START = "webrtc-start",
|
2020-05-02 20:46:02 +02:00
|
|
|
WEBRTC_JOIN_ROOM = "webrtc-join-room",
|
2020-05-19 19:11:12 +02:00
|
|
|
JOIN_ROOM = "join-room", // bi-directional
|
|
|
|
USER_POSITION = "user-position", // bi-directional
|
|
|
|
USER_MOVED = "user-moved", // From server to client
|
|
|
|
USER_LEFT = "user-left", // From server to client
|
2020-05-02 20:46:02 +02:00
|
|
|
MESSAGE_ERROR = "message-error",
|
2020-05-08 00:35:36 +02:00
|
|
|
WEBRTC_DISCONNECT = "webrtc-disconect",
|
|
|
|
GROUP_CREATE_UPDATE = "group-create-update",
|
|
|
|
GROUP_DELETE = "group-delete",
|
2020-06-17 15:37:02 +02:00
|
|
|
SET_PLAYER_DETAILS = "set-player-details", // Send the name and character to the server (on connect), receive back the id.
|
2020-05-13 20:22:42 +02:00
|
|
|
|
|
|
|
CONNECT_ERROR = "connect_error",
|
2020-05-14 23:19:48 +02:00
|
|
|
RECONNECT = "reconnect",
|
2020-06-17 15:37:02 +02:00
|
|
|
RECONNECTING = "reconnecting",
|
|
|
|
RECONNECT_ERROR = "reconnect_error",
|
|
|
|
RECONNECT_FAILED = "reconnect_failed"
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
class Message {
|
2020-04-05 20:57:14 +02:00
|
|
|
userId: string;
|
2020-05-03 22:24:14 +02:00
|
|
|
name: string;
|
2020-05-08 15:18:22 +02:00
|
|
|
character: string;
|
2020-04-05 20:57:14 +02:00
|
|
|
|
2020-05-16 00:19:27 +02:00
|
|
|
constructor(userId : string, name: string, character: string) {
|
2020-04-05 20:57:14 +02:00
|
|
|
this.userId = userId;
|
2020-05-03 22:24:14 +02:00
|
|
|
this.name = name;
|
2020-05-08 15:18:22 +02:00
|
|
|
this.character = character;
|
2020-04-05 20:57:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
export interface PointInterface {
|
|
|
|
x: number;
|
|
|
|
y: number;
|
|
|
|
direction : string;
|
2020-05-22 22:59:43 +02:00
|
|
|
moving: boolean;
|
2020-04-10 12:54:05 +02:00
|
|
|
}
|
|
|
|
|
2020-05-19 19:11:12 +02:00
|
|
|
export class Point implements PointInterface{
|
2020-05-22 22:59:43 +02:00
|
|
|
constructor(public x : number, public y : number, public direction : string = PlayerAnimationNames.WalkDown, public moving : boolean = false) {
|
2020-04-05 20:57:14 +02:00
|
|
|
if(x === null || y === null){
|
|
|
|
throw Error("position x and y cannot be null");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
export interface MessageUserPositionInterface {
|
|
|
|
userId: string;
|
2020-05-03 22:24:14 +02:00
|
|
|
name: string;
|
2020-05-08 15:18:22 +02:00
|
|
|
character: string;
|
2020-04-10 12:54:05 +02:00
|
|
|
position: PointInterface;
|
|
|
|
}
|
2020-04-25 16:05:33 +02:00
|
|
|
|
2020-05-19 19:11:12 +02:00
|
|
|
export interface MessageUserMovedInterface {
|
|
|
|
userId: string;
|
|
|
|
position: PointInterface;
|
|
|
|
}
|
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
class MessageUserPosition extends Message implements MessageUserPositionInterface{
|
2020-04-05 20:57:14 +02:00
|
|
|
position: PointInterface;
|
|
|
|
|
2020-05-16 00:19:27 +02:00
|
|
|
constructor(userId : string, point : Point, name: string, character: string) {
|
|
|
|
super(userId, name, character);
|
2020-04-05 20:57:14 +02:00
|
|
|
this.position = point;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 19:11:12 +02:00
|
|
|
export interface MessageUserJoined {
|
|
|
|
userId: string;
|
|
|
|
name: string;
|
|
|
|
character: string;
|
2020-05-22 22:59:43 +02:00
|
|
|
position: PointInterface
|
2020-05-19 19:11:12 +02:00
|
|
|
}
|
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
export interface ListMessageUserPositionInterface {
|
2020-04-25 16:05:33 +02:00
|
|
|
roomId: string;
|
2020-04-10 12:54:05 +02:00
|
|
|
listUsersPosition: Array<MessageUserPosition>;
|
|
|
|
}
|
2020-04-25 16:05:33 +02:00
|
|
|
|
2020-05-08 00:35:36 +02:00
|
|
|
export interface PositionInterface {
|
|
|
|
x: number,
|
|
|
|
y: number
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface GroupCreatedUpdatedMessageInterface {
|
|
|
|
position: PositionInterface,
|
|
|
|
groupId: string
|
|
|
|
}
|
|
|
|
|
2020-06-05 13:07:18 +02:00
|
|
|
export interface WebRtcStartMessageInterface {
|
|
|
|
roomId: string,
|
|
|
|
clients: UserSimplePeer[]
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface WebRtcDisconnectMessageInterface {
|
|
|
|
userId: string
|
|
|
|
}
|
|
|
|
|
2020-05-24 23:14:12 +02:00
|
|
|
export interface ConnectionInterface {
|
2020-06-03 11:55:31 +02:00
|
|
|
socket: Socket|null;
|
|
|
|
token: string|null;
|
|
|
|
name: string|null;
|
|
|
|
userId: string|null;
|
2020-04-25 16:05:33 +02:00
|
|
|
|
2020-05-24 23:14:12 +02:00
|
|
|
createConnection(name: string, characterSelected: string): Promise<any>;
|
2020-04-25 16:05:33 +02:00
|
|
|
|
2020-05-24 22:53:10 +02:00
|
|
|
loadStartMap(): Promise<any>;
|
2020-05-09 19:41:21 +02:00
|
|
|
|
2020-05-22 22:59:43 +02:00
|
|
|
joinARoom(roomId: string, startX: number, startY: number, direction: string, moving: boolean): void;
|
2020-04-25 16:05:33 +02:00
|
|
|
|
2020-05-22 22:59:43 +02:00
|
|
|
sharePosition(x: number, y: number, direction: string, moving: boolean): void;
|
2020-04-25 16:05:33 +02:00
|
|
|
|
|
|
|
/*webrtc*/
|
2020-06-03 22:32:43 +02:00
|
|
|
sendWebrtcSignal(signal: any, roomId: string, userId?: string|null, receiverId?: string): void;
|
2020-04-25 16:05:33 +02:00
|
|
|
|
|
|
|
receiveWebrtcSignal(callBack: Function): void;
|
|
|
|
|
2020-06-05 13:07:18 +02:00
|
|
|
receiveWebrtcStart(callBack: (message: WebRtcStartMessageInterface) => void): void;
|
2020-05-02 20:46:02 +02:00
|
|
|
|
2020-06-05 13:07:18 +02:00
|
|
|
disconnectMessage(callBack: (message: WebRtcDisconnectMessageInterface) => void): void;
|
2020-04-12 13:57:00 +02:00
|
|
|
}
|
2020-04-25 16:05:33 +02:00
|
|
|
|
2020-05-24 23:14:12 +02:00
|
|
|
export class Connection implements ConnectionInterface {
|
2020-06-03 11:55:31 +02:00
|
|
|
socket: Socket|null = null;
|
|
|
|
token: string|null = null;
|
|
|
|
name: string|null = null; // TODO: drop "name" storage here
|
|
|
|
character: string|null = null;
|
|
|
|
userId: string|null = null;
|
2020-04-05 20:57:14 +02:00
|
|
|
|
2020-04-27 15:03:05 +02:00
|
|
|
GameManager: GameManager;
|
2020-04-07 20:41:35 +02:00
|
|
|
|
2020-06-03 11:55:31 +02:00
|
|
|
lastPositionShared: PointInterface|null = null;
|
2020-05-15 23:24:04 +02:00
|
|
|
lastRoom: string|null = null;
|
2020-05-13 20:22:42 +02:00
|
|
|
|
2020-05-15 22:40:06 +02:00
|
|
|
constructor(GameManager: GameManager) {
|
2020-04-07 20:41:35 +02:00
|
|
|
this.GameManager = GameManager;
|
2020-04-10 12:54:05 +02:00
|
|
|
}
|
|
|
|
|
2020-05-24 23:14:12 +02:00
|
|
|
createConnection(name: string, characterSelected: string): Promise<ConnectionInterface> {
|
2020-05-15 22:40:06 +02:00
|
|
|
this.name = name;
|
|
|
|
this.character = characterSelected;
|
2020-05-23 14:00:36 +02:00
|
|
|
return Axios.post(`${API_URL}/login`, {name: name})
|
2020-04-05 20:57:14 +02:00
|
|
|
.then((res) => {
|
|
|
|
this.token = res.data.token;
|
|
|
|
this.socket = SocketIo(`${API_URL}`, {
|
2020-05-23 14:00:36 +02:00
|
|
|
query: {
|
2020-04-05 20:57:14 +02:00
|
|
|
token: this.token
|
2020-05-23 14:00:36 +02:00
|
|
|
}
|
2020-04-05 20:57:14 +02:00
|
|
|
});
|
2020-06-11 13:34:25 +02:00
|
|
|
|
|
|
|
//listen event
|
|
|
|
this.disconnectServer();
|
|
|
|
this.errorMessage();
|
|
|
|
this.groupUpdatedOrCreated();
|
|
|
|
this.groupDeleted();
|
|
|
|
this.onUserJoins();
|
|
|
|
this.onUserMoved();
|
|
|
|
this.onUserLeft();
|
|
|
|
|
2020-05-15 22:40:06 +02:00
|
|
|
return this.connectSocketServer();
|
2020-04-05 20:57:14 +02:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
throw err;
|
2020-05-23 14:00:36 +02:00
|
|
|
});
|
2020-04-05 20:57:14 +02:00
|
|
|
}
|
2020-05-10 17:31:27 +02:00
|
|
|
|
2020-06-03 11:55:31 +02:00
|
|
|
private getSocket(): Socket {
|
|
|
|
if (this.socket === null) {
|
|
|
|
throw new Error('Socket not initialized while using Connection')
|
|
|
|
}
|
|
|
|
return this.socket;
|
|
|
|
}
|
|
|
|
|
2020-05-13 20:22:42 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param character
|
|
|
|
*/
|
2020-05-24 23:14:12 +02:00
|
|
|
connectSocketServer(): Promise<ConnectionInterface>{
|
|
|
|
return new Promise<ConnectionInterface>((resolve, reject) => {
|
2020-06-03 11:55:31 +02:00
|
|
|
this.getSocket().emit(EventMessage.SET_PLAYER_DETAILS, {
|
2020-05-15 22:40:06 +02:00
|
|
|
name: this.name,
|
|
|
|
character: this.character
|
|
|
|
} as SetPlayerDetailsMessage, (id: string) => {
|
|
|
|
this.userId = id;
|
|
|
|
});
|
2020-05-15 23:40:05 +02:00
|
|
|
|
|
|
|
//if try to reconnect with last position
|
2020-05-23 15:43:26 +02:00
|
|
|
/*if(this.lastRoom) {
|
2020-05-15 23:40:05 +02:00
|
|
|
//join the room
|
2020-05-22 22:59:43 +02:00
|
|
|
this.joinARoom(this.lastRoom,
|
|
|
|
this.lastPositionShared ? this.lastPositionShared.x : 0,
|
|
|
|
this.lastPositionShared ? this.lastPositionShared.y : 0,
|
|
|
|
this.lastPositionShared ? this.lastPositionShared.direction : PlayerAnimationNames.WalkDown,
|
|
|
|
this.lastPositionShared ? this.lastPositionShared.moving : false);
|
2020-05-23 15:43:26 +02:00
|
|
|
}*/
|
2020-05-15 23:40:05 +02:00
|
|
|
|
2020-05-22 22:59:43 +02:00
|
|
|
/*if(this.lastPositionShared) {
|
2020-05-15 23:40:05 +02:00
|
|
|
|
|
|
|
//share your first position
|
|
|
|
this.sharePosition(
|
|
|
|
this.lastPositionShared ? this.lastPositionShared.x : 0,
|
|
|
|
this.lastPositionShared ? this.lastPositionShared.y : 0,
|
2020-05-22 22:59:43 +02:00
|
|
|
this.lastPositionShared.direction,
|
|
|
|
this.lastPositionShared.moving
|
2020-05-15 23:40:05 +02:00
|
|
|
);
|
2020-05-22 22:59:43 +02:00
|
|
|
}*/
|
2020-05-15 23:40:05 +02:00
|
|
|
|
2020-05-15 22:40:06 +02:00
|
|
|
resolve(this);
|
|
|
|
});
|
2020-05-13 20:22:42 +02:00
|
|
|
}
|
|
|
|
|
2020-05-10 17:31:27 +02:00
|
|
|
//TODO add middleware with access token to secure api
|
2020-05-24 22:53:10 +02:00
|
|
|
loadStartMap() : Promise<any> {
|
|
|
|
return Axios.get(`${API_URL}/start-map`)
|
2020-05-10 17:31:27 +02:00
|
|
|
.then((res) => {
|
|
|
|
return res.data;
|
|
|
|
}).catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
throw err;
|
|
|
|
});
|
2020-05-09 19:41:21 +02:00
|
|
|
}
|
2020-04-05 20:57:14 +02:00
|
|
|
|
2020-05-22 22:59:43 +02:00
|
|
|
joinARoom(roomId: string, startX: number, startY: number, direction: string, moving: boolean): void {
|
2020-06-11 10:00:30 +02:00
|
|
|
let point = new Point(startX, startY, direction, moving);
|
|
|
|
this.lastPositionShared = point;
|
2020-06-03 11:55:31 +02:00
|
|
|
this.getSocket().emit(EventMessage.JOIN_ROOM, { roomId, position: {x: startX, y: startY, direction, moving }}, (userPositions: MessageUserPositionInterface[]) => {
|
2020-05-19 19:11:12 +02:00
|
|
|
this.GameManager.initUsersPosition(userPositions);
|
|
|
|
});
|
2020-05-15 23:24:04 +02:00
|
|
|
this.lastRoom = roomId;
|
2020-04-05 20:57:14 +02:00
|
|
|
}
|
|
|
|
|
2020-05-22 22:59:43 +02:00
|
|
|
sharePosition(x : number, y : number, direction : string, moving: boolean) : void{
|
2020-04-07 20:46:30 +02:00
|
|
|
if(!this.socket){
|
|
|
|
return;
|
|
|
|
}
|
2020-05-22 22:59:43 +02:00
|
|
|
let point = new Point(x, y, direction, moving);
|
2020-05-15 23:40:05 +02:00
|
|
|
this.lastPositionShared = point;
|
2020-06-03 11:55:31 +02:00
|
|
|
this.getSocket().emit(EventMessage.USER_POSITION, point);
|
2020-04-05 20:57:14 +02:00
|
|
|
}
|
|
|
|
|
2020-05-24 23:27:32 +02:00
|
|
|
private onUserJoins(): void {
|
2020-06-03 11:55:31 +02:00
|
|
|
this.getSocket().on(EventMessage.JOIN_ROOM, (message: MessageUserJoined) => {
|
2020-05-19 19:11:12 +02:00
|
|
|
this.GameManager.onUserJoins(message);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-24 23:27:32 +02:00
|
|
|
private onUserMoved(): void {
|
2020-06-03 11:55:31 +02:00
|
|
|
this.getSocket().on(EventMessage.USER_MOVED, (message: MessageUserMovedInterface) => {
|
2020-05-19 19:11:12 +02:00
|
|
|
this.GameManager.onUserMoved(message);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-24 23:27:32 +02:00
|
|
|
private onUserLeft(): void {
|
2020-06-03 11:55:31 +02:00
|
|
|
this.getSocket().on(EventMessage.USER_LEFT, (userId: string) => {
|
2020-05-19 19:11:12 +02:00
|
|
|
this.GameManager.onUserLeft(userId);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-08 00:35:36 +02:00
|
|
|
private groupUpdatedOrCreated(): void {
|
2020-06-03 11:55:31 +02:00
|
|
|
this.getSocket().on(EventMessage.GROUP_CREATE_UPDATE, (groupCreateUpdateMessage: GroupCreatedUpdatedMessageInterface) => {
|
2020-05-08 00:35:36 +02:00
|
|
|
//console.log('Group ', groupCreateUpdateMessage.groupId, " position :", groupCreateUpdateMessage.position.x, groupCreateUpdateMessage.position.y)
|
|
|
|
this.GameManager.shareGroupPosition(groupCreateUpdateMessage);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private groupDeleted(): void {
|
2020-06-03 11:55:31 +02:00
|
|
|
this.getSocket().on(EventMessage.GROUP_DELETE, (groupId: string) => {
|
2020-05-08 00:35:36 +02:00
|
|
|
this.GameManager.deleteGroup(groupId);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-03 22:32:43 +02:00
|
|
|
sendWebrtcSignal(signal: any, roomId: string, userId? : string|null, receiverId? : string) {
|
2020-06-03 11:55:31 +02:00
|
|
|
return this.getSocket().emit(EventMessage.WEBRTC_SIGNAL, {
|
2020-04-26 17:43:21 +02:00
|
|
|
userId: userId ? userId : this.userId,
|
2020-04-26 19:12:01 +02:00
|
|
|
receiverId: receiverId ? receiverId : this.userId,
|
2020-04-25 16:05:33 +02:00
|
|
|
roomId: roomId,
|
|
|
|
signal: signal
|
2020-05-15 22:04:49 +02:00
|
|
|
});
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
2020-06-05 13:07:18 +02:00
|
|
|
receiveWebrtcStart(callback: (message: WebRtcStartMessageInterface) => void) {
|
2020-06-03 11:55:31 +02:00
|
|
|
this.getSocket().on(EventMessage.WEBRTC_START, callback);
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
receiveWebrtcSignal(callback: Function) {
|
2020-06-03 11:55:31 +02:00
|
|
|
return this.getSocket().on(EventMessage.WEBRTC_SIGNAL, callback);
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
2020-05-24 23:27:32 +02:00
|
|
|
private errorMessage(): void {
|
2020-06-03 11:55:31 +02:00
|
|
|
this.getSocket().on(EventMessage.MESSAGE_ERROR, (message: string) => {
|
2020-04-25 16:05:33 +02:00
|
|
|
console.error(EventMessage.MESSAGE_ERROR, message);
|
2020-04-05 20:57:14 +02:00
|
|
|
})
|
|
|
|
}
|
2020-05-02 20:46:02 +02:00
|
|
|
|
2020-05-24 23:27:32 +02:00
|
|
|
private disconnectServer(): void {
|
2020-06-03 11:55:31 +02:00
|
|
|
this.getSocket().on(EventMessage.CONNECT_ERROR, () => {
|
2020-05-23 15:43:26 +02:00
|
|
|
this.GameManager.switchToDisconnectedScene();
|
2020-05-13 20:22:42 +02:00
|
|
|
});
|
|
|
|
|
2020-06-17 15:37:02 +02:00
|
|
|
this.getSocket().on(EventMessage.RECONNECTING, () => {
|
|
|
|
console.log('Trying to reconnect');
|
|
|
|
});
|
|
|
|
|
|
|
|
this.getSocket().on(EventMessage.RECONNECT_ERROR, () => {
|
|
|
|
console.log('Error while trying to reconnect.');
|
|
|
|
});
|
|
|
|
|
|
|
|
this.getSocket().on(EventMessage.RECONNECT_FAILED, () => {
|
|
|
|
console.error('Reconnection failed. Giving up.');
|
|
|
|
});
|
|
|
|
|
2020-06-03 11:55:31 +02:00
|
|
|
this.getSocket().on(EventMessage.RECONNECT, () => {
|
2020-06-17 16:06:02 +02:00
|
|
|
console.log('Reconnect event triggered');
|
2020-05-13 20:22:42 +02:00
|
|
|
this.connectSocketServer();
|
2020-06-03 11:55:31 +02:00
|
|
|
if (this.lastPositionShared === null) {
|
|
|
|
throw new Error('No last position shared found while reconnecting');
|
|
|
|
}
|
2020-05-23 15:43:26 +02:00
|
|
|
this.GameManager.reconnectToGameScene(this.lastPositionShared);
|
2020-05-13 20:22:42 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-05 13:07:18 +02:00
|
|
|
disconnectMessage(callback: (message: WebRtcDisconnectMessageInterface) => void): void {
|
2020-06-03 11:55:31 +02:00
|
|
|
this.getSocket().on(EventMessage.WEBRTC_DISCONNECT, callback);
|
2020-05-02 20:46:02 +02:00
|
|
|
}
|
2020-05-08 00:35:36 +02:00
|
|
|
}
|