rewrote the login workflow
This commit is contained in:
parent
783d58d3cb
commit
af4611ed29
@ -1,36 +0,0 @@
|
|||||||
import {Application, Request, Response} from "express";
|
|
||||||
import {OK} from "http-status-codes";
|
|
||||||
import {ADMIN_API_TOKEN, ADMIN_API_URL} from "../Enum/EnvironmentVariable";
|
|
||||||
import Axios from "axios";
|
|
||||||
|
|
||||||
export class AdminController {
|
|
||||||
App : Application;
|
|
||||||
|
|
||||||
constructor(App : Application) {
|
|
||||||
this.App = App;
|
|
||||||
this.getLoginUrlByToken();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
getLoginUrlByToken(){
|
|
||||||
this.App.get("/register/:token", async (req: Request, res: Response) => {
|
|
||||||
if (!ADMIN_API_URL) {
|
|
||||||
return res.status(500).send('No admin backoffice set!');
|
|
||||||
}
|
|
||||||
const token:string = req.params.token;
|
|
||||||
|
|
||||||
let response = null
|
|
||||||
try {
|
|
||||||
response = await Axios.get(ADMIN_API_URL+'/api/login-url/'+token, { headers: {"Authorization" : `${ADMIN_API_TOKEN}`} })
|
|
||||||
} catch (e) {
|
|
||||||
console.log(e.message)
|
|
||||||
return res.status(e.status || 500).send('An error happened');
|
|
||||||
}
|
|
||||||
|
|
||||||
const organizationSlug = response.data.organizationSlug;
|
|
||||||
const worldSlug = response.data.worldSlug;
|
|
||||||
const roomSlug = response.data.roomSlug;
|
|
||||||
return res.status(OK).send({organizationSlug, worldSlug, roomSlug});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,8 +1,9 @@
|
|||||||
import {Application, Request, Response} from "express";
|
import {Application, Request, Response} from "express";
|
||||||
import Jwt from "jsonwebtoken";
|
import Jwt from "jsonwebtoken";
|
||||||
import {BAD_REQUEST, OK} from "http-status-codes";
|
import {OK} from "http-status-codes";
|
||||||
import {SECRET_KEY, URL_ROOM_STARTED} from "../Enum/EnvironmentVariable"; //TODO fix import by "_Enum/..."
|
import {ADMIN_API_TOKEN, ADMIN_API_URL, SECRET_KEY, URL_ROOM_STARTED} from "../Enum/EnvironmentVariable"; //TODO fix import by "_Enum/..."
|
||||||
import { uuid } from 'uuidv4';
|
import { uuid } from 'uuidv4';
|
||||||
|
import Axios from "axios";
|
||||||
|
|
||||||
export interface TokenInterface {
|
export interface TokenInterface {
|
||||||
name: string,
|
name: string,
|
||||||
@ -20,21 +21,53 @@ export class AuthenticateController {
|
|||||||
//permit to login on application. Return token to connect on Websocket IO.
|
//permit to login on application. Return token to connect on Websocket IO.
|
||||||
login(){
|
login(){
|
||||||
// For now, let's completely forget the /login route.
|
// For now, let's completely forget the /login route.
|
||||||
this.App.post("/login", (req: Request, res: Response) => {
|
this.App.post("/login", async (req: Request, res: Response) => {
|
||||||
const param = req.body;
|
//todo: what to do if the organizationMemberToken is already used?
|
||||||
/*if(!param.name){
|
const organizationMemberToken:string|null = req.body.organizationMemberToken;
|
||||||
return res.status(BAD_REQUEST).send({
|
|
||||||
message: "email parameter is empty"
|
try {
|
||||||
|
let userUuid;
|
||||||
|
let mapUrlStart;
|
||||||
|
let newUrl = null;
|
||||||
|
|
||||||
|
if (organizationMemberToken) {
|
||||||
|
if (!ADMIN_API_URL) {
|
||||||
|
return res.status(401).send('No admin backoffice set!');
|
||||||
|
}
|
||||||
|
//todo: this call can fail if the corresponding world is not activated or if the token is invalid. Handle that case.
|
||||||
|
const response = await Axios.get(ADMIN_API_URL+'/api/login-url/'+organizationMemberToken,
|
||||||
|
{ headers: {"Authorization" : `${ADMIN_API_TOKEN}`} }
|
||||||
|
);
|
||||||
|
|
||||||
|
userUuid = response.data.userUuid;
|
||||||
|
mapUrlStart = response.data.mapUrlStart;
|
||||||
|
newUrl = this.getNewUrlOnAdminAuth(response.data)
|
||||||
|
} else {
|
||||||
|
userUuid = uuid();
|
||||||
|
mapUrlStart= URL_ROOM_STARTED;
|
||||||
|
newUrl = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const authToken = Jwt.sign({userUuid: userUuid} as TokenInterface, SECRET_KEY, {expiresIn: '24h'});
|
||||||
|
return res.status(OK).send({
|
||||||
|
authToken,
|
||||||
|
userUuid,
|
||||||
|
mapUrlStart,
|
||||||
|
newUrl,
|
||||||
});
|
});
|
||||||
}*/
|
|
||||||
//TODO check user email for The Coding Machine game
|
} catch (e) {
|
||||||
const userUuid = uuid();
|
console.log(e.message)
|
||||||
const token = Jwt.sign({name: param.name, userUuid: userUuid} as TokenInterface, SECRET_KEY, {expiresIn: '24h'});
|
return res.status(e.status || 500).send('An error happened');
|
||||||
return res.status(OK).send({
|
}
|
||||||
token: token,
|
|
||||||
mapUrlStart: URL_ROOM_STARTED,
|
|
||||||
userId: userUuid,
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getNewUrlOnAdminAuth(data:any): string {
|
||||||
|
const organizationSlug = data.organizationSlug;
|
||||||
|
const worldSlug = data.worldSlug;
|
||||||
|
const roomSlug = data.roomSlug;
|
||||||
|
return '/@/'+organizationSlug+'/'+worldSlug+'/'+roomSlug;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
import {Connection} from "../front/src/Connection";
|
import {RoomConnection} from "../front/src/Connexion/Connection";
|
||||||
|
|
||||||
function sleep(ms) {
|
function sleep(ms) {
|
||||||
return new Promise(resolve => setTimeout(resolve, ms));
|
return new Promise(resolve => setTimeout(resolve, ms));
|
||||||
}
|
}
|
||||||
|
|
||||||
async function startOneUser(): Promise<void> {
|
async function startOneUser(): Promise<void> {
|
||||||
const connection = await Connection.createConnection('foo', ['male3']);
|
const connection = await RoomConnection.createConnection('foo', ['male3']);
|
||||||
|
|
||||||
await connection.joinARoom('global__maps.workadventure.localhost/Floor0/floor0', 783, 170, 'down', false, {
|
await connection.joinARoom('global__maps.workadventure.localhost/Floor0/floor0', 783, 170, 'down', false, {
|
||||||
top: 0,
|
top: 0,
|
||||||
|
53
front/src/Connexion/ConnectionManager.ts
Normal file
53
front/src/Connexion/ConnectionManager.ts
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
import Axios from "axios";
|
||||||
|
import {API_URL} from "../Enum/EnvironmentVariable";
|
||||||
|
import {RoomConnection} from "./RoomConnection";
|
||||||
|
|
||||||
|
class ConnectionManager {
|
||||||
|
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;
|
||||||
|
|
||||||
|
if (newUrl) {
|
||||||
|
history.pushState({}, '', newUrl);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async setUserName(name:string): Promise<void> {
|
||||||
|
//todo
|
||||||
|
}
|
||||||
|
|
||||||
|
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) );
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const connectionManager = new ConnectionManager();
|
117
front/src/Connexion/ConnexionModels.ts
Normal file
117
front/src/Connexion/ConnexionModels.ts
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
import {PlayerAnimationNames} from "../Phaser/Player/Animation";
|
||||||
|
import {UserSimplePeerInterface} from "../WebRtc/SimplePeer";
|
||||||
|
import {SignalData} from "simple-peer";
|
||||||
|
|
||||||
|
export enum EventMessage{
|
||||||
|
WEBRTC_SIGNAL = "webrtc-signal",
|
||||||
|
WEBRTC_SCREEN_SHARING_SIGNAL = "webrtc-screen-sharing-signal",
|
||||||
|
WEBRTC_START = "webrtc-start",
|
||||||
|
JOIN_ROOM = "join-room", // bi-directional
|
||||||
|
USER_POSITION = "user-position", // From client to server
|
||||||
|
USER_MOVED = "user-moved", // From server to client
|
||||||
|
USER_LEFT = "user-left", // From server to client
|
||||||
|
MESSAGE_ERROR = "message-error",
|
||||||
|
WEBRTC_DISCONNECT = "webrtc-disconect",
|
||||||
|
GROUP_CREATE_UPDATE = "group-create-update",
|
||||||
|
GROUP_DELETE = "group-delete",
|
||||||
|
SET_PLAYER_DETAILS = "set-player-details", // Send the name and character to the server (on connect), receive back the id.
|
||||||
|
ITEM_EVENT = 'item-event',
|
||||||
|
|
||||||
|
CONNECT_ERROR = "connect_error",
|
||||||
|
SET_SILENT = "set_silent", // Set or unset the silent mode for this user.
|
||||||
|
SET_VIEWPORT = "set-viewport",
|
||||||
|
BATCH = "batch",
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface PointInterface {
|
||||||
|
x: number;
|
||||||
|
y: number;
|
||||||
|
direction : string;
|
||||||
|
moving: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Point implements PointInterface{
|
||||||
|
constructor(public x : number, public y : number, public direction : string = PlayerAnimationNames.WalkDown, public moving : boolean = false) {
|
||||||
|
if(x === null || y === null){
|
||||||
|
throw Error("position x and y cannot be null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface MessageUserPositionInterface {
|
||||||
|
userId: number;
|
||||||
|
name: string;
|
||||||
|
characterLayers: string[];
|
||||||
|
position: PointInterface;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface MessageUserMovedInterface {
|
||||||
|
userId: number;
|
||||||
|
position: PointInterface;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface MessageUserJoined {
|
||||||
|
userId: number;
|
||||||
|
name: string;
|
||||||
|
characterLayers: string[];
|
||||||
|
position: PointInterface
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface PositionInterface {
|
||||||
|
x: number,
|
||||||
|
y: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface GroupCreatedUpdatedMessageInterface {
|
||||||
|
position: PositionInterface,
|
||||||
|
groupId: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface WebRtcStartMessageInterface {
|
||||||
|
roomId: string,
|
||||||
|
clients: UserSimplePeerInterface[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface WebRtcDisconnectMessageInterface {
|
||||||
|
userId: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface WebRtcSignalSentMessageInterface {
|
||||||
|
receiverId: number,
|
||||||
|
signal: SignalData
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface WebRtcSignalReceivedMessageInterface {
|
||||||
|
userId: number,
|
||||||
|
signal: SignalData
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface StartMapInterface {
|
||||||
|
mapUrlStart: string,
|
||||||
|
startInstance: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ViewportInterface {
|
||||||
|
left: number,
|
||||||
|
top: number,
|
||||||
|
right: number,
|
||||||
|
bottom: number,
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface BatchedMessageInterface {
|
||||||
|
event: string,
|
||||||
|
payload: unknown
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ItemEventMessageInterface {
|
||||||
|
itemId: number,
|
||||||
|
event: string,
|
||||||
|
state: unknown,
|
||||||
|
parameters: unknown
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RoomJoinedMessageInterface {
|
||||||
|
users: MessageUserPositionInterface[],
|
||||||
|
groups: GroupCreatedUpdatedMessageInterface[],
|
||||||
|
items: { [itemId: number] : unknown }
|
||||||
|
}
|
@ -1,142 +1,34 @@
|
|||||||
import Axios from "axios";
|
import {API_URL} from "../Enum/EnvironmentVariable";
|
||||||
import {API_URL} from "./Enum/EnvironmentVariable";
|
|
||||||
import {MessageUI} from "./Logger/MessageUI";
|
|
||||||
import {
|
import {
|
||||||
BatchMessage, GroupDeleteMessage, GroupUpdateMessage, ItemEventMessage,
|
BatchMessage, GroupDeleteMessage, GroupUpdateMessage, ItemEventMessage,
|
||||||
PositionMessage,
|
PositionMessage,
|
||||||
SetPlayerDetailsMessage, UserJoinedMessage, UserLeftMessage, UserMovedMessage,
|
SetPlayerDetailsMessage, UserJoinedMessage, UserLeftMessage, UserMovedMessage,
|
||||||
UserMovesMessage,
|
UserMovesMessage,
|
||||||
ViewportMessage
|
ViewportMessage
|
||||||
} from "./Messages/generated/messages_pb"
|
} from "../Messages/generated/messages_pb"
|
||||||
|
|
||||||
const SocketIo = require('socket.io-client');
|
const SocketIo = require('socket.io-client');
|
||||||
import Socket = SocketIOClient.Socket;
|
import Socket = SocketIOClient.Socket;
|
||||||
import {PlayerAnimationNames} from "./Phaser/Player/Animation";
|
|
||||||
import {UserSimplePeerInterface} from "./WebRtc/SimplePeer";
|
|
||||||
import {SignalData} from "simple-peer";
|
|
||||||
import Direction = PositionMessage.Direction;
|
import Direction = PositionMessage.Direction;
|
||||||
import {ProtobufClientUtils} from "./Network/ProtobufClientUtils";
|
import {ProtobufClientUtils} from "../Network/ProtobufClientUtils";
|
||||||
|
import {
|
||||||
|
EventMessage,
|
||||||
|
GroupCreatedUpdatedMessageInterface, ItemEventMessageInterface,
|
||||||
|
MessageUserJoined,
|
||||||
|
RoomJoinedMessageInterface,
|
||||||
|
ViewportInterface, WebRtcDisconnectMessageInterface,
|
||||||
|
WebRtcSignalReceivedMessageInterface,
|
||||||
|
WebRtcSignalSentMessageInterface,
|
||||||
|
WebRtcStartMessageInterface
|
||||||
|
} from "./ConnexionModels";
|
||||||
|
|
||||||
enum EventMessage{
|
|
||||||
WEBRTC_SIGNAL = "webrtc-signal",
|
|
||||||
WEBRTC_SCREEN_SHARING_SIGNAL = "webrtc-screen-sharing-signal",
|
|
||||||
WEBRTC_START = "webrtc-start",
|
|
||||||
JOIN_ROOM = "join-room", // bi-directional
|
|
||||||
USER_POSITION = "user-position", // From client to server
|
|
||||||
USER_MOVED = "user-moved", // From server to client
|
|
||||||
USER_LEFT = "user-left", // From server to client
|
|
||||||
MESSAGE_ERROR = "message-error",
|
|
||||||
WEBRTC_DISCONNECT = "webrtc-disconect",
|
|
||||||
GROUP_CREATE_UPDATE = "group-create-update",
|
|
||||||
GROUP_DELETE = "group-delete",
|
|
||||||
SET_PLAYER_DETAILS = "set-player-details", // Send the name and character to the server (on connect), receive back the id.
|
|
||||||
ITEM_EVENT = 'item-event',
|
|
||||||
|
|
||||||
CONNECT_ERROR = "connect_error",
|
export class RoomConnection implements RoomConnection {
|
||||||
SET_SILENT = "set_silent", // Set or unset the silent mode for this user.
|
|
||||||
SET_VIEWPORT = "set-viewport",
|
|
||||||
BATCH = "batch",
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface PointInterface {
|
|
||||||
x: number;
|
|
||||||
y: number;
|
|
||||||
direction : string;
|
|
||||||
moving: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class Point implements PointInterface{
|
|
||||||
constructor(public x : number, public y : number, public direction : string = PlayerAnimationNames.WalkDown, public moving : boolean = false) {
|
|
||||||
if(x === null || y === null){
|
|
||||||
throw Error("position x and y cannot be null");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface MessageUserPositionInterface {
|
|
||||||
userId: number;
|
|
||||||
name: string;
|
|
||||||
characterLayers: string[];
|
|
||||||
position: PointInterface;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface MessageUserMovedInterface {
|
|
||||||
userId: number;
|
|
||||||
position: PointInterface;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface MessageUserJoined {
|
|
||||||
userId: number;
|
|
||||||
name: string;
|
|
||||||
characterLayers: string[];
|
|
||||||
position: PointInterface
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface PositionInterface {
|
|
||||||
x: number,
|
|
||||||
y: number
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface GroupCreatedUpdatedMessageInterface {
|
|
||||||
position: PositionInterface,
|
|
||||||
groupId: number
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface WebRtcStartMessageInterface {
|
|
||||||
roomId: string,
|
|
||||||
clients: UserSimplePeerInterface[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface WebRtcDisconnectMessageInterface {
|
|
||||||
userId: number
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface WebRtcSignalSentMessageInterface {
|
|
||||||
receiverId: number,
|
|
||||||
signal: SignalData
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface WebRtcSignalReceivedMessageInterface {
|
|
||||||
userId: number,
|
|
||||||
signal: SignalData
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface StartMapInterface {
|
|
||||||
mapUrlStart: string,
|
|
||||||
startInstance: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ViewportInterface {
|
|
||||||
left: number,
|
|
||||||
top: number,
|
|
||||||
right: number,
|
|
||||||
bottom: number,
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface BatchedMessageInterface {
|
|
||||||
event: string,
|
|
||||||
payload: unknown
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ItemEventMessageInterface {
|
|
||||||
itemId: number,
|
|
||||||
event: string,
|
|
||||||
state: unknown,
|
|
||||||
parameters: unknown
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface RoomJoinedMessageInterface {
|
|
||||||
users: MessageUserPositionInterface[],
|
|
||||||
groups: GroupCreatedUpdatedMessageInterface[],
|
|
||||||
items: { [itemId: number] : unknown }
|
|
||||||
}
|
|
||||||
|
|
||||||
export class Connection implements Connection {
|
|
||||||
private readonly socket: Socket;
|
private readonly socket: Socket;
|
||||||
private userId: number|null = null;
|
private userId: number|null = null;
|
||||||
private batchCallbacks: Map<string, Function[]> = new Map<string, Function[]>();
|
private batchCallbacks: Map<string, Function[]> = new Map<string, Function[]>();
|
||||||
|
|
||||||
private constructor(token: string) {
|
public constructor(token: string) {
|
||||||
|
|
||||||
this.socket = SocketIo(`${API_URL}`, {
|
this.socket = SocketIo(`${API_URL}`, {
|
||||||
query: {
|
query: {
|
||||||
@ -190,38 +82,14 @@ export class Connection implements Connection {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
public static createConnection(name: string, characterLayersSelected: string[]): Promise<Connection> {
|
public emitPlayerDetailsMessage(characterLayersSelected: string[]) {
|
||||||
return Axios.post(`${API_URL}/login`, {name: name})
|
const message = new SetPlayerDetailsMessage();
|
||||||
.then((res) => {
|
message.setName(name);
|
||||||
|
message.setCharacterlayersList(characterLayersSelected);
|
||||||
return new Promise<Connection>((resolve, reject) => {
|
this.socket.emit(EventMessage.SET_PLAYER_DETAILS, message.serializeBinary().buffer, (id: number) => {
|
||||||
const connection = new Connection(res.data.token);
|
this.userId = id;
|
||||||
|
});
|
||||||
connection.onConnectError((error: object) => {
|
|
||||||
console.log('An error occurred while connecting to socket server. Retrying');
|
|
||||||
reject(error);
|
|
||||||
});
|
|
||||||
|
|
||||||
const message = new SetPlayerDetailsMessage();
|
|
||||||
message.setName(name);
|
|
||||||
message.setCharacterlayersList(characterLayersSelected);
|
|
||||||
connection.socket.emit(EventMessage.SET_PLAYER_DETAILS, message.serializeBinary().buffer, (id: number) => {
|
|
||||||
connection.userId = id;
|
|
||||||
});
|
|
||||||
|
|
||||||
resolve(connection);
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
// Let's retry in 4-6 seconds
|
|
||||||
return new Promise<Connection>((resolve, reject) => {
|
|
||||||
setTimeout(() => {
|
|
||||||
Connection.createConnection(name, characterLayersSelected).then((connection) => resolve(connection))
|
|
||||||
.catch((error) => reject(error));
|
|
||||||
}, 4000 + Math.floor(Math.random() * 2000) );
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public closeConnection(): void {
|
public closeConnection(): void {
|
@ -1,6 +1,6 @@
|
|||||||
import {PositionMessage} from "../Messages/generated/messages_pb";
|
import {PositionMessage} from "../Messages/generated/messages_pb";
|
||||||
import {PointInterface} from "../Connection";
|
|
||||||
import Direction = PositionMessage.Direction;
|
import Direction = PositionMessage.Direction;
|
||||||
|
import {PointInterface} from "../Connexion/ConnexionModels";
|
||||||
|
|
||||||
export class ProtobufClientUtils {
|
export class ProtobufClientUtils {
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import {GameScene} from "../Game/GameScene";
|
import {GameScene} from "../Game/GameScene";
|
||||||
import {PointInterface} from "../../Connection";
|
import {PointInterface} from "../../Connexion/ConnexionModels";
|
||||||
import {Character} from "../Entity/Character";
|
import {Character} from "../Entity/Character";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import {PointInterface} from "../../Connection";
|
import {PointInterface} from "../../Connexion/Connection";
|
||||||
|
|
||||||
export interface AddPlayerInterface {
|
export interface AddPlayerInterface {
|
||||||
userId: number;
|
userId: number;
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
import {GameScene} from "./GameScene";
|
import {GameScene} from "./GameScene";
|
||||||
import {
|
import {
|
||||||
StartMapInterface
|
StartMapInterface
|
||||||
} from "../../Connection";
|
} from "../../Connexion/ConnexionModels";
|
||||||
import Axios from "axios";
|
import Axios from "axios";
|
||||||
import {API_URL} from "../../Enum/EnvironmentVariable";
|
import {API_URL} from "../../Enum/EnvironmentVariable";
|
||||||
|
import {adminDataFetchPromise} from "../../register";
|
||||||
|
|
||||||
export interface HasMovedEvent {
|
export interface HasMovedEvent {
|
||||||
direction: string;
|
direction: string;
|
||||||
@ -29,13 +30,23 @@ export class GameManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
loadStartMap() : Promise<StartMapInterface> {
|
loadStartMap() : Promise<StartMapInterface> {
|
||||||
return Axios.get(`${API_URL}/start-map`)
|
if (adminDataFetchPromise) {
|
||||||
.then((res) => {
|
return adminDataFetchPromise.then(data => {
|
||||||
return res.data;
|
return {
|
||||||
}).catch((err) => {
|
mapUrlStart: data.mapUrlStart,
|
||||||
console.error(err);
|
startInstance: data.startInstance,
|
||||||
throw err;
|
}
|
||||||
});
|
})
|
||||||
|
} 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;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getPlayerName(): string {
|
getPlayerName(): string {
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import {GameManager, gameManager, HasMovedEvent} from "./GameManager";
|
import {GameManager, gameManager, HasMovedEvent} from "./GameManager";
|
||||||
import {
|
import {
|
||||||
Connection,
|
|
||||||
GroupCreatedUpdatedMessageInterface,
|
GroupCreatedUpdatedMessageInterface,
|
||||||
MessageUserJoined,
|
MessageUserJoined,
|
||||||
MessageUserMovedInterface,
|
MessageUserMovedInterface,
|
||||||
@ -8,7 +7,7 @@ import {
|
|||||||
PointInterface,
|
PointInterface,
|
||||||
PositionInterface,
|
PositionInterface,
|
||||||
RoomJoinedMessageInterface
|
RoomJoinedMessageInterface
|
||||||
} from "../../Connection";
|
} from "../../Connexion/ConnexionModels";
|
||||||
import {CurrentGamerInterface, hasMovedEventName, Player} from "../Player/Player";
|
import {CurrentGamerInterface, hasMovedEventName, Player} from "../Player/Player";
|
||||||
import {DEBUG_MODE, JITSI_URL, POSITION_DELAY, RESOLUTION, ZOOM_LEVEL} from "../../Enum/EnvironmentVariable";
|
import {DEBUG_MODE, JITSI_URL, POSITION_DELAY, RESOLUTION, ZOOM_LEVEL} from "../../Enum/EnvironmentVariable";
|
||||||
import {
|
import {
|
||||||
@ -42,6 +41,8 @@ import {ActionableItem} from "../Items/ActionableItem";
|
|||||||
import {UserInputManager} from "../UserInput/UserInputManager";
|
import {UserInputManager} from "../UserInput/UserInputManager";
|
||||||
import {UserMovedMessage} from "../../Messages/generated/messages_pb";
|
import {UserMovedMessage} from "../../Messages/generated/messages_pb";
|
||||||
import {ProtobufClientUtils} from "../../Network/ProtobufClientUtils";
|
import {ProtobufClientUtils} from "../../Network/ProtobufClientUtils";
|
||||||
|
import {connectionManager} from "../../Connexion/ConnectionManager";
|
||||||
|
import {RoomConnection} from "../../Connexion/RoomConnection";
|
||||||
|
|
||||||
|
|
||||||
export enum Textures {
|
export enum Textures {
|
||||||
@ -100,9 +101,9 @@ export class GameScene extends Phaser.Scene implements CenterListener {
|
|||||||
pendingEvents: Queue<InitUserPositionEventInterface|AddPlayerEventInterface|RemovePlayerEventInterface|UserMovedEventInterface|GroupCreatedUpdatedEventInterface|DeleteGroupEventInterface> = new Queue<InitUserPositionEventInterface|AddPlayerEventInterface|RemovePlayerEventInterface|UserMovedEventInterface|GroupCreatedUpdatedEventInterface|DeleteGroupEventInterface>();
|
pendingEvents: Queue<InitUserPositionEventInterface|AddPlayerEventInterface|RemovePlayerEventInterface|UserMovedEventInterface|GroupCreatedUpdatedEventInterface|DeleteGroupEventInterface> = new Queue<InitUserPositionEventInterface|AddPlayerEventInterface|RemovePlayerEventInterface|UserMovedEventInterface|GroupCreatedUpdatedEventInterface|DeleteGroupEventInterface>();
|
||||||
private initPosition: PositionInterface|null = null;
|
private initPosition: PositionInterface|null = null;
|
||||||
private playersPositionInterpolator = new PlayersPositionInterpolator();
|
private playersPositionInterpolator = new PlayersPositionInterpolator();
|
||||||
private connection!: Connection;
|
private connection!: RoomConnection;
|
||||||
private simplePeer!: SimplePeer;
|
private simplePeer!: SimplePeer;
|
||||||
private connectionPromise!: Promise<Connection>
|
private connectionPromise!: Promise<RoomConnection>
|
||||||
private connectionAnswerPromise: Promise<RoomJoinedMessageInterface>;
|
private connectionAnswerPromise: Promise<RoomJoinedMessageInterface>;
|
||||||
private connectionAnswerPromiseResolve!: (value?: RoomJoinedMessageInterface | PromiseLike<RoomJoinedMessageInterface>) => void;
|
private connectionAnswerPromiseResolve!: (value?: RoomJoinedMessageInterface | PromiseLike<RoomJoinedMessageInterface>) => void;
|
||||||
// A promise that will resolve when the "create" method is called (signaling loading is ended)
|
// A promise that will resolve when the "create" method is called (signaling loading is ended)
|
||||||
@ -202,8 +203,10 @@ export class GameScene extends Phaser.Scene implements CenterListener {
|
|||||||
|
|
||||||
this.load.bitmapFont('main_font', 'resources/fonts/arcade.png', 'resources/fonts/arcade.xml');
|
this.load.bitmapFont('main_font', 'resources/fonts/arcade.png', 'resources/fonts/arcade.xml');
|
||||||
|
|
||||||
this.connectionPromise = Connection.createConnection(gameManager.getPlayerName(), gameManager.getCharacterSelected()).then((connection : Connection) => {
|
this.connectionPromise = connectionManager.connectToRoomSocket().then((connection : RoomConnection) => {
|
||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
|
|
||||||
|
this.connection.emitPlayerDetailsMessage(gameManager.getCharacterSelected())
|
||||||
|
|
||||||
connection.onUserJoins((message: MessageUserJoined) => {
|
connection.onUserJoins((message: MessageUserJoined) => {
|
||||||
const userMessage: AddPlayerInterface = {
|
const userMessage: AddPlayerInterface = {
|
||||||
@ -778,7 +781,7 @@ export class GameScene extends Phaser.Scene implements CenterListener {
|
|||||||
this.createCollisionObject();
|
this.createCollisionObject();
|
||||||
|
|
||||||
//join room
|
//join room
|
||||||
this.connectionPromise.then((connection: Connection) => {
|
this.connectionPromise.then((connection: RoomConnection) => {
|
||||||
const camera = this.cameras.main;
|
const camera = this.cameras.main;
|
||||||
connection.joinARoom(this.RoomId,
|
connection.joinARoom(this.RoomId,
|
||||||
this.startX,
|
this.startX,
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import {HasMovedEvent} from "./GameManager";
|
import {HasMovedEvent} from "./GameManager";
|
||||||
import {MAX_EXTRAPOLATION_TIME} from "../../Enum/EnvironmentVariable";
|
import {MAX_EXTRAPOLATION_TIME} from "../../Enum/EnvironmentVariable";
|
||||||
import {PositionInterface} from "../../Connection";
|
import {PositionInterface} from "../../Connexion/ConnexionModels";
|
||||||
|
|
||||||
export class PlayerMovement {
|
export class PlayerMovement {
|
||||||
public constructor(private startPosition: PositionInterface, private startTick: number, private endPosition: HasMovedEvent, private endTick: number) {
|
public constructor(private startPosition: PositionInterface, private startTick: number, private endPosition: HasMovedEvent, private endTick: number) {
|
||||||
|
@ -1,12 +1,9 @@
|
|||||||
import {gameManager} from "../Game/GameManager";
|
import {gameManager} from "../Game/GameManager";
|
||||||
import {TextField} from "../Components/TextField";
|
import {TextField} from "../Components/TextField";
|
||||||
import {ClickButton} from "../Components/ClickButton";
|
|
||||||
import Image = Phaser.GameObjects.Image;
|
import Image = Phaser.GameObjects.Image;
|
||||||
import Rectangle = Phaser.GameObjects.Rectangle;
|
|
||||||
import {PLAYER_RESOURCES, PlayerResourceDescriptionInterface} from "../Entity/Character";
|
|
||||||
import {GameSceneInitInterface} from "../Game/GameScene";
|
import {GameSceneInitInterface} from "../Game/GameScene";
|
||||||
import {StartMapInterface} from "../../Connection";
|
import {StartMapInterface} from "../../Connexion/ConnexionModels";
|
||||||
import {mediaManager, MediaManager} from "../../WebRtc/MediaManager";
|
import {mediaManager} from "../../WebRtc/MediaManager";
|
||||||
import {RESOLUTION} from "../../Enum/EnvironmentVariable";
|
import {RESOLUTION} from "../../Enum/EnvironmentVariable";
|
||||||
import {SoundMeter} from "../Components/SoundMeter";
|
import {SoundMeter} from "../Components/SoundMeter";
|
||||||
import {SoundMeterSprite} from "../Components/SoundMeterSprite";
|
import {SoundMeterSprite} from "../Components/SoundMeterSprite";
|
||||||
|
@ -3,8 +3,6 @@ import {TextField} from "../Components/TextField";
|
|||||||
import Image = Phaser.GameObjects.Image;
|
import Image = Phaser.GameObjects.Image;
|
||||||
import Rectangle = Phaser.GameObjects.Rectangle;
|
import Rectangle = Phaser.GameObjects.Rectangle;
|
||||||
import {PLAYER_RESOURCES, PlayerResourceDescriptionInterface} from "../Entity/Character";
|
import {PLAYER_RESOURCES, PlayerResourceDescriptionInterface} from "../Entity/Character";
|
||||||
import {GameSceneInitInterface} from "../Game/GameScene";
|
|
||||||
import {StartMapInterface} from "../../Connection";
|
|
||||||
import {EnableCameraSceneName} from "./EnableCameraScene";
|
import {EnableCameraSceneName} from "./EnableCameraScene";
|
||||||
import {CustomizeSceneName} from "./CustomizeScene";
|
import {CustomizeSceneName} from "./CustomizeScene";
|
||||||
|
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
import {PlayerAnimationNames} from "./Animation";
|
import {PlayerAnimationNames} from "./Animation";
|
||||||
import {GameScene, Textures} from "../Game/GameScene";
|
import {GameScene} from "../Game/GameScene";
|
||||||
import {MessageUserPositionInterface, PointInterface} from "../../Connection";
|
import {UserInputEvent, UserInputManager} from "../UserInput/UserInputManager";
|
||||||
import {ActiveEventList, UserInputEvent, UserInputManager} from "../UserInput/UserInputManager";
|
|
||||||
import {Character} from "../Entity/Character";
|
import {Character} from "../Entity/Character";
|
||||||
import {OutlinePipeline} from "../Shaders/OutlinePipeline";
|
|
||||||
|
|
||||||
|
|
||||||
export const hasMovedEventName = "hasMoved";
|
export const hasMovedEventName = "hasMoved";
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import * as SimplePeerNamespace from "simple-peer";
|
import * as SimplePeerNamespace from "simple-peer";
|
||||||
import {mediaManager} from "./MediaManager";
|
import {mediaManager} from "./MediaManager";
|
||||||
import {Connection} from "../Connection";
|
|
||||||
import {TURN_SERVER, TURN_USER, TURN_PASSWORD} from "../Enum/EnvironmentVariable";
|
import {TURN_SERVER, TURN_USER, TURN_PASSWORD} from "../Enum/EnvironmentVariable";
|
||||||
|
import {RoomConnection} from "../Connexion/RoomConnection";
|
||||||
|
|
||||||
const Peer: SimplePeerNamespace.SimplePeer = require('simple-peer');
|
const Peer: SimplePeerNamespace.SimplePeer = require('simple-peer');
|
||||||
|
|
||||||
@ -14,7 +14,7 @@ export class ScreenSharingPeer extends Peer {
|
|||||||
*/
|
*/
|
||||||
private isReceivingStream:boolean = false;
|
private isReceivingStream:boolean = false;
|
||||||
|
|
||||||
constructor(private userId: number, initiator: boolean, private connection: Connection) {
|
constructor(private userId: number, initiator: boolean, private connection: RoomConnection) {
|
||||||
super({
|
super({
|
||||||
initiator: initiator ? initiator : false,
|
initiator: initiator ? initiator : false,
|
||||||
reconnectTimer: 10000,
|
reconnectTimer: 10000,
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
import {
|
import {
|
||||||
Connection,
|
|
||||||
WebRtcDisconnectMessageInterface,
|
WebRtcDisconnectMessageInterface,
|
||||||
WebRtcSignalReceivedMessageInterface,
|
WebRtcSignalReceivedMessageInterface,
|
||||||
WebRtcStartMessageInterface
|
WebRtcStartMessageInterface
|
||||||
} from "../Connection";
|
} from "../Connexion/ConnexionModels";
|
||||||
import {
|
import {
|
||||||
mediaManager,
|
mediaManager,
|
||||||
StartScreenSharingCallback,
|
StartScreenSharingCallback,
|
||||||
@ -13,6 +12,7 @@ import {
|
|||||||
import * as SimplePeerNamespace from "simple-peer";
|
import * as SimplePeerNamespace from "simple-peer";
|
||||||
import {ScreenSharingPeer} from "./ScreenSharingPeer";
|
import {ScreenSharingPeer} from "./ScreenSharingPeer";
|
||||||
import {VideoPeer} from "./VideoPeer";
|
import {VideoPeer} from "./VideoPeer";
|
||||||
|
import {RoomConnection} from "../Connexion/RoomConnection";
|
||||||
const Peer: SimplePeerNamespace.SimplePeer = require('simple-peer');
|
const Peer: SimplePeerNamespace.SimplePeer = require('simple-peer');
|
||||||
|
|
||||||
export interface UserSimplePeerInterface{
|
export interface UserSimplePeerInterface{
|
||||||
@ -31,7 +31,7 @@ export interface PeerConnectionListener {
|
|||||||
* This class manages connections to all the peers in the same group as me.
|
* This class manages connections to all the peers in the same group as me.
|
||||||
*/
|
*/
|
||||||
export class SimplePeer {
|
export class SimplePeer {
|
||||||
private Connection: Connection;
|
private Connection: RoomConnection;
|
||||||
private WebRtcRoomId: string;
|
private WebRtcRoomId: string;
|
||||||
private Users: Array<UserSimplePeerInterface> = new Array<UserSimplePeerInterface>();
|
private Users: Array<UserSimplePeerInterface> = new Array<UserSimplePeerInterface>();
|
||||||
|
|
||||||
@ -42,7 +42,7 @@ export class SimplePeer {
|
|||||||
private readonly stopLocalScreenSharingStreamCallback: StopScreenSharingCallback;
|
private readonly stopLocalScreenSharingStreamCallback: StopScreenSharingCallback;
|
||||||
private readonly peerConnectionListeners: Array<PeerConnectionListener> = new Array<PeerConnectionListener>();
|
private readonly peerConnectionListeners: Array<PeerConnectionListener> = new Array<PeerConnectionListener>();
|
||||||
|
|
||||||
constructor(Connection: Connection, WebRtcRoomId: string = "test-webrtc") {
|
constructor(Connection: RoomConnection, WebRtcRoomId: string = "test-webrtc") {
|
||||||
this.Connection = Connection;
|
this.Connection = Connection;
|
||||||
this.WebRtcRoomId = WebRtcRoomId;
|
this.WebRtcRoomId = WebRtcRoomId;
|
||||||
// We need to go through this weird bound function pointer in order to be able to "free" this reference later.
|
// We need to go through this weird bound function pointer in order to be able to "free" this reference later.
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import * as SimplePeerNamespace from "simple-peer";
|
import * as SimplePeerNamespace from "simple-peer";
|
||||||
import {mediaManager} from "./MediaManager";
|
import {mediaManager} from "./MediaManager";
|
||||||
import {Connection} from "../Connection";
|
|
||||||
import {TURN_PASSWORD, TURN_SERVER, TURN_USER} from "../Enum/EnvironmentVariable";
|
import {TURN_PASSWORD, TURN_SERVER, TURN_USER} from "../Enum/EnvironmentVariable";
|
||||||
|
import {RoomConnection} from "../Connexion/RoomConnection";
|
||||||
|
|
||||||
const Peer: SimplePeerNamespace.SimplePeer = require('simple-peer');
|
const Peer: SimplePeerNamespace.SimplePeer = require('simple-peer');
|
||||||
|
|
||||||
@ -9,7 +9,7 @@ const Peer: SimplePeerNamespace.SimplePeer = require('simple-peer');
|
|||||||
* A peer connection used to transmit video / audio signals between 2 peers.
|
* A peer connection used to transmit video / audio signals between 2 peers.
|
||||||
*/
|
*/
|
||||||
export class VideoPeer extends Peer {
|
export class VideoPeer extends Peer {
|
||||||
constructor(private userId: number, initiator: boolean, private connection: Connection) {
|
constructor(private userId: number, initiator: boolean, private connection: RoomConnection) {
|
||||||
super({
|
super({
|
||||||
initiator: initiator ? initiator : false,
|
initiator: initiator ? initiator : false,
|
||||||
reconnectTimer: 10000,
|
reconnectTimer: 10000,
|
||||||
|
@ -11,11 +11,10 @@ import WebGLRenderer = Phaser.Renderer.WebGL.WebGLRenderer;
|
|||||||
import {OutlinePipeline} from "./Phaser/Shaders/OutlinePipeline";
|
import {OutlinePipeline} from "./Phaser/Shaders/OutlinePipeline";
|
||||||
import {CustomizeScene} from "./Phaser/Login/CustomizeScene";
|
import {CustomizeScene} from "./Phaser/Login/CustomizeScene";
|
||||||
import {CoWebsiteManager} from "./WebRtc/CoWebsiteManager";
|
import {CoWebsiteManager} from "./WebRtc/CoWebsiteManager";
|
||||||
import {redirectIfToken} from "./register";
|
import {connectionManager} from "./Connexion/ConnectionManager";
|
||||||
|
|
||||||
//CoWebsiteManager.loadCoWebsite('https://thecodingmachine.com');
|
//CoWebsiteManager.loadCoWebsite('https://thecodingmachine.com');
|
||||||
let connectionData //todo: do something with this data
|
connectionManager.init();
|
||||||
redirectIfToken().then(res => connectionData = res);
|
|
||||||
|
|
||||||
// Load Jitsi if the environment variable is set.
|
// Load Jitsi if the environment variable is set.
|
||||||
if (JITSI_URL) {
|
if (JITSI_URL) {
|
||||||
|
@ -1,29 +0,0 @@
|
|||||||
import Axios from "axios";
|
|
||||||
import {API_URL} from "./Enum/EnvironmentVariable";
|
|
||||||
declare let history:History;
|
|
||||||
|
|
||||||
//todo: better naming
|
|
||||||
export interface ConnexionData {
|
|
||||||
organizationSlug: string,
|
|
||||||
worldSlug: string,
|
|
||||||
roomSlug: string,
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function redirectIfToken(): Promise<ConnexionData | null> {
|
|
||||||
const match = /\/register\/(.+)/.exec(window.location.toString());
|
|
||||||
if (!match) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
let res = null;
|
|
||||||
try {
|
|
||||||
res = await Axios.get(`${API_URL}/register/`+match[1])
|
|
||||||
} catch (e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
const organizationSlug = res.data.organizationSlug;
|
|
||||||
const worldSlug = res.data.worldSlug;
|
|
||||||
const roomSlug = res.data.roomSlug;
|
|
||||||
const connexionUrl = '/@/'+organizationSlug+'/'+worldSlug+'/'+roomSlug;
|
|
||||||
history.pushState({}, '', connexionUrl);
|
|
||||||
return {organizationSlug, worldSlug, roomSlug};
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user