Merge branch 'develop' of github.com:thecodingmachine/workadventure into player-report

# Conflicts:
#	back/src/Controller/AuthenticateController.ts
This commit is contained in:
David Négrier 2020-10-15 18:01:44 +02:00
commit c75f1edc40
22 changed files with 325 additions and 220 deletions

View File

@ -3,6 +3,7 @@ import {HttpRequest, HttpResponse, TemplatedApp} from "uWebSockets.js";
import {BaseController} from "./BaseController"; import {BaseController} from "./BaseController";
import {adminApi} from "../Services/AdminApi"; import {adminApi} from "../Services/AdminApi";
import {jwtTokenManager} from "../Services/JWTTokenManager"; import {jwtTokenManager} from "../Services/JWTTokenManager";
import {parse} from "query-string";
export interface TokenInterface { export interface TokenInterface {
userUuid: string userUuid: string
@ -13,11 +14,12 @@ export class AuthenticateController extends BaseController {
constructor(private App : TemplatedApp) { constructor(private App : TemplatedApp) {
super(); super();
this.register(); this.register();
this.verify();
this.anonymLogin(); this.anonymLogin();
} }
//Try to login with an admin token //Try to login with an admin token
register(){ private register(){
this.App.options("/register", (res: HttpResponse, req: HttpRequest) => { this.App.options("/register", (res: HttpResponse, req: HttpRequest) => {
this.addCorsHeaders(res); this.addCorsHeaders(res);
@ -26,8 +28,6 @@ export class AuthenticateController extends BaseController {
this.App.post("/register", (res: HttpResponse, req: HttpRequest) => { this.App.post("/register", (res: HttpResponse, req: HttpRequest) => {
(async () => { (async () => {
this.addCorsHeaders(res);
res.onAborted(() => { res.onAborted(() => {
console.warn('Login request was aborted'); console.warn('Login request was aborted');
}) })
@ -47,7 +47,9 @@ export class AuthenticateController extends BaseController {
const mapUrlStart = data.mapUrlStart; const mapUrlStart = data.mapUrlStart;
const authToken = jwtTokenManager.createJWTToken(userUuid); const authToken = jwtTokenManager.createJWTToken(userUuid);
res.writeStatus("200 OK").end(JSON.stringify({ res.writeStatus("200 OK");
this.addCorsHeaders(res);
res.end(JSON.stringify({
authToken, authToken,
userUuid, userUuid,
organizationSlug, organizationSlug,
@ -58,7 +60,9 @@ export class AuthenticateController extends BaseController {
} catch (e) { } catch (e) {
console.error("An error happened", e) console.error("An error happened", e)
res.writeStatus(e.status || "500 Internal Server Error").end('An error happened'); res.writeStatus(e.status || "500 Internal Server Error");
this.addCorsHeaders(res);
res.end('An error happened');
} }
@ -67,8 +71,44 @@ export class AuthenticateController extends BaseController {
} }
private verify(){
this.App.options("/verify", (res: HttpResponse, req: HttpRequest) => {
this.addCorsHeaders(res);
res.end();
});
this.App.get("/verify", (res: HttpResponse, req: HttpRequest) => {
(async () => {
const query = parse(req.getQuery());
res.onAborted(() => {
console.warn('verify request was aborted');
})
try {
await jwtTokenManager.getUserUuidFromToken(query.token as string);
} catch (e) {
res.writeStatus("400 Bad Request");
this.addCorsHeaders(res);
res.end(JSON.stringify({
"success": false,
"message": "Invalid JWT token"
}));
return;
}
res.writeStatus("200 OK");
this.addCorsHeaders(res);
res.end(JSON.stringify({
"success": true
}));
})();
});
}
//permit to login on application. Return token to connect on Websocket IO. //permit to login on application. Return token to connect on Websocket IO.
anonymLogin(){ private anonymLogin(){
this.App.options("/anonymLogin", (res: HttpResponse, req: HttpRequest) => { this.App.options("/anonymLogin", (res: HttpResponse, req: HttpRequest) => {
this.addCorsHeaders(res); this.addCorsHeaders(res);
@ -76,7 +116,6 @@ export class AuthenticateController extends BaseController {
}); });
this.App.post("/anonymLogin", (res: HttpResponse, req: HttpRequest) => { this.App.post("/anonymLogin", (res: HttpResponse, req: HttpRequest) => {
this.addCorsHeaders(res);
res.onAborted(() => { res.onAborted(() => {
console.warn('Login request was aborted'); console.warn('Login request was aborted');
@ -84,7 +123,9 @@ export class AuthenticateController extends BaseController {
const userUuid = v4(); const userUuid = v4();
const authToken = jwtTokenManager.createJWTToken(userUuid); const authToken = jwtTokenManager.createJWTToken(userUuid);
res.writeStatus("200 OK").end(JSON.stringify({ res.writeStatus("200 OK");
this.addCorsHeaders(res);
res.end(JSON.stringify({
authToken, authToken,
userUuid, userUuid,
})); }));

View File

@ -44,8 +44,6 @@ export class FileController extends BaseController {
this.App.post("/upload-audio-message", (res: HttpResponse, req: HttpRequest) => { this.App.post("/upload-audio-message", (res: HttpResponse, req: HttpRequest) => {
(async () => { (async () => {
this.addCorsHeaders(res);
res.onAborted(() => { res.onAborted(() => {
console.warn('upload-audio-message request was aborted'); console.warn('upload-audio-message request was aborted');
}) })
@ -80,14 +78,18 @@ export class FileController extends BaseController {
} }
}); });
res.writeStatus("200 OK").end(JSON.stringify({ res.writeStatus("200 OK");
this.addCorsHeaders(res);
res.end(JSON.stringify({
id: audioMessageId, id: audioMessageId,
path: `/download-audio-message/${audioMessageId}` path: `/download-audio-message/${audioMessageId}`
})); }));
} catch (e) { } catch (e) {
console.log("An error happened", e) console.log("An error happened", e)
res.writeStatus(e.status || "500 Internal Server Error").end('An error happened'); res.writeStatus(e.status || "500 Internal Server Error");
this.addCorsHeaders(res);
res.end('An error happened');
} }
})(); })();
}); });
@ -101,7 +103,6 @@ export class FileController extends BaseController {
}); });
this.App.get("/download-audio-message/:id", (res: HttpResponse, req: HttpRequest) => { this.App.get("/download-audio-message/:id", (res: HttpResponse, req: HttpRequest) => {
this.addCorsHeaders(res);
res.onAborted(() => { res.onAborted(() => {
console.warn('upload-audio-message request was aborted'); console.warn('upload-audio-message request was aborted');
@ -111,7 +112,9 @@ export class FileController extends BaseController {
const file = this.uploadedFileBuffers.get(id); const file = this.uploadedFileBuffers.get(id);
if (file === undefined) { if (file === undefined) {
res.writeStatus("404 Not found").end("Cannot find file"); res.writeStatus("404 Not found");
this.addCorsHeaders(res);
res.end("Cannot find file");
return; return;
} }

View File

@ -62,6 +62,26 @@ function emitInBatch(socket: ExSocketInterface, payload: SubMessage): void {
socket.batchTimeout = null; socket.batchTimeout = null;
}, 100); }, 100);
} }
// If we send a message, we don't need to keep the connection alive
resetPing(socket);
}
/**
* Schedule a ping to keep the connection open.
* If a ping is already set, the timeout of the ping is reset.
*/
function resetPing(ws: ExSocketInterface): void {
if (ws.pingTimeout) {
clearTimeout(ws.pingTimeout);
}
ws.pingTimeout = setTimeout(() => {
if (ws.disconnecting) {
return;
}
ws.ping();
resetPing(ws);
}, 29000);
} }
export class IoSocketController { export class IoSocketController {
@ -234,6 +254,8 @@ export class IoSocketController {
// Let's join the room // Let's join the room
this.handleJoinRoom(client, client.position, client.viewport); this.handleJoinRoom(client, client.position, client.viewport);
resetPing(client);
}, },
message: (ws, arrayBuffer, isBinary): void => { message: (ws, arrayBuffer, isBinary): void => {
const client = ws as ExSocketInterface; const client = ws as ExSocketInterface;

View File

@ -24,7 +24,6 @@ export class MapController extends BaseController{
}); });
this.App.get("/map", (res: HttpResponse, req: HttpRequest) => { this.App.get("/map", (res: HttpResponse, req: HttpRequest) => {
this.addCorsHeaders(res);
res.onAborted(() => { res.onAborted(() => {
console.warn('/map request was aborted'); console.warn('/map request was aborted');
@ -34,25 +33,35 @@ export class MapController extends BaseController{
if (typeof query.organizationSlug !== 'string') { if (typeof query.organizationSlug !== 'string') {
console.error('Expected organizationSlug parameter'); console.error('Expected organizationSlug parameter');
res.writeStatus("400 Bad request").end("Expected organizationSlug parameter"); res.writeStatus("400 Bad request");
this.addCorsHeaders(res);
res.end("Expected organizationSlug parameter");
} }
if (typeof query.worldSlug !== 'string') { if (typeof query.worldSlug !== 'string') {
console.error('Expected worldSlug parameter'); console.error('Expected worldSlug parameter');
res.writeStatus("400 Bad request").end("Expected worldSlug parameter"); res.writeStatus("400 Bad request");
this.addCorsHeaders(res);
res.end("Expected worldSlug parameter");
} }
if (typeof query.roomSlug !== 'string' && query.roomSlug !== undefined) { if (typeof query.roomSlug !== 'string' && query.roomSlug !== undefined) {
console.error('Expected only one roomSlug parameter'); console.error('Expected only one roomSlug parameter');
res.writeStatus("400 Bad request").end("Expected only one roomSlug parameter"); res.writeStatus("400 Bad request");
this.addCorsHeaders(res);
res.end("Expected only one roomSlug parameter");
} }
(async () => { (async () => {
try { try {
const mapDetails = await adminApi.fetchMapDetails(query.organizationSlug as string, query.worldSlug as string, query.roomSlug as string|undefined); const mapDetails = await adminApi.fetchMapDetails(query.organizationSlug as string, query.worldSlug as string, query.roomSlug as string|undefined);
res.writeStatus("200 OK").end(JSON.stringify(mapDetails)); res.writeStatus("200 OK");
this.addCorsHeaders(res);
res.end(JSON.stringify(mapDetails));
} catch (e) { } catch (e) {
console.error(e); console.error(e);
res.writeStatus("500 Internal Server Error").end("An error occurred"); res.writeStatus("500 Internal Server Error")
this.addCorsHeaders(res);
res.end("An error occurred");
} }
})(); })();

View File

@ -19,6 +19,7 @@ export interface ExSocketInterface extends WebSocket, Identificable {
emitInBatch: (payload: SubMessage) => void; emitInBatch: (payload: SubMessage) => void;
batchedMessages: BatchMessage; batchedMessages: BatchMessage;
batchTimeout: NodeJS.Timeout|null; batchTimeout: NodeJS.Timeout|null;
pingTimeout: NodeJS.Timeout|null;
disconnecting: boolean, disconnecting: boolean,
tags: string[] tags: string[]
} }

View File

@ -6,7 +6,7 @@ import {TokenInterface} from "../Controller/AuthenticateController";
class JWTTokenManager { class JWTTokenManager {
public createJWTToken(userUuid: string) { public createJWTToken(userUuid: string) {
return Jwt.sign({userUuid: userUuid}, SECRET_KEY, {expiresIn: '24h'}); return Jwt.sign({userUuid: userUuid}, SECRET_KEY, {expiresIn: '200d'}); //todo: add a mechanic to refresh or recreate token
} }
public async getUserUuidFromToken(token: unknown): Promise<string> { public async getUserUuidFromToken(token: unknown): Promise<string> {

View File

@ -29,24 +29,29 @@ class ConnectionManager {
const roomSlug = data.roomSlug; const roomSlug = data.roomSlug;
urlManager.editUrlForRoom(roomSlug, organizationSlug, worldSlug); urlManager.editUrlForRoom(roomSlug, organizationSlug, worldSlug);
const room = new Room(window.location.pathname); const room = new Room(window.location.pathname + window.location.hash);
return Promise.resolve(room); return Promise.resolve(room);
} else if (connexionType === GameConnexionTypes.anonymous || connexionType === GameConnexionTypes.empty) { } else if (connexionType === GameConnexionTypes.anonymous || connexionType === GameConnexionTypes.empty) {
const localUser = localUserStore.getLocalUser(); const localUser = localUserStore.getLocalUser();
if (localUser && localUser.jwtToken && localUser.uuid) { if (localUser && localUser.jwtToken && localUser.uuid) {
this.localUser = localUser this.localUser = localUser;
try {
await this.verifyToken(localUser.jwtToken);
} catch(e) {
// If the token is invalid, let's generate an anonymous one.
console.error('JWT token invalid. Did it expire? Login anonymously instead.');
await this.anonymousLogin();
}
} else { } else {
const data = await Axios.post(`${API_URL}/anonymLogin`).then(res => res.data); await this.anonymousLogin();
this.localUser = new LocalUser(data.userUuid, data.authToken);
localUserStore.saveUser(this.localUser);
} }
let roomId: string let roomId: string
if (connexionType === GameConnexionTypes.empty) { if (connexionType === GameConnexionTypes.empty) {
const defaultMapUrl = window.location.host.replace('play.', 'maps.') + URL_ROOM_STARTED; const defaultMapUrl = window.location.host.replace('play.', 'maps.') + URL_ROOM_STARTED;
roomId = urlManager.editUrlForRoom(defaultMapUrl, null, null); roomId = urlManager.editUrlForRoom(defaultMapUrl, null, null);
} else { } else {
roomId = window.location.pathname; roomId = window.location.pathname + window.location.hash;
} }
const room = new Room(roomId); const room = new Room(roomId);
return Promise.resolve(room); return Promise.resolve(room);
@ -54,8 +59,9 @@ class ConnectionManager {
const localUser = localUserStore.getLocalUser(); const localUser = localUserStore.getLocalUser();
if (localUser) { if (localUser) {
this.localUser = localUser this.localUser = localUser;
const room = new Room(window.location.pathname); await this.verifyToken(localUser.jwtToken);
const room = new Room(window.location.pathname + window.location.hash);
return Promise.resolve(room); return Promise.resolve(room);
} else { } else {
//todo: find some kind of fallback? //todo: find some kind of fallback?
@ -66,6 +72,16 @@ class ConnectionManager {
return Promise.reject('Invalid URL'); return Promise.reject('Invalid URL');
} }
private async verifyToken(token: string): Promise<void> {
await Axios.get(`${API_URL}/verify`, {params: {token}});
}
private async anonymousLogin(): Promise<void> {
const data = await Axios.post(`${API_URL}/anonymLogin`).then(res => res.data);
this.localUser = new LocalUser(data.userUuid, data.authToken);
localUserStore.saveUser(this.localUser);
}
public initBenchmark(): void { public initBenchmark(): void {
this.localUser = new LocalUser('', 'test'); this.localUser = new LocalUser('', 'test');
} }

View File

@ -1,5 +1,6 @@
import {LocalUser} from "./LocalUser"; import {LocalUser} from "./LocalUser";
//todo: add localstorage fallback
class LocalUserStore { class LocalUserStore {
saveUser(localUser: LocalUser) { saveUser(localUser: LocalUser) {
@ -11,6 +12,14 @@ class LocalUserStore {
return data ? JSON.parse(data) : null; return data ? JSON.parse(data) : null;
} }
setName(name:string): void {
window.localStorage.setItem('playerName', name);
}
getName(): string {
return window.localStorage.getItem('playerName') ?? '';
}
} }
export const localUserStore = new LocalUserStore(); export const localUserStore = new LocalUserStore();

View File

@ -6,8 +6,10 @@ export class Room {
public readonly isPublic: boolean; public readonly isPublic: boolean;
private mapUrl: string|undefined; private mapUrl: string|undefined;
private instance: string|undefined; private instance: string|undefined;
public readonly hash: string;
constructor(id: string) { constructor(id: string) {
this.hash = '';
if (id.startsWith('/')) { if (id.startsWith('/')) {
id = id.substr(1); id = id.substr(1);
} }
@ -19,6 +21,13 @@ export class Room {
} else { } else {
throw new Error('Invalid room ID'); throw new Error('Invalid room ID');
} }
const indexOfHash = this.id.indexOf('#');
if (indexOfHash !== -1) {
const idWithHash = this.id;
this.id = this.id.substr(0, indexOfHash);
this.hash = idWithHash.substr(indexOfHash + 1);
}
} }
public async getMapUrl(): Promise<string> { public async getMapUrl(): Promise<string> {

View File

@ -1,7 +1,10 @@
export class TextField extends Phaser.GameObjects.BitmapText { export class TextField extends Phaser.GameObjects.BitmapText {
constructor(scene: Phaser.Scene, x: number, y: number, text: string | string[]) { constructor(scene: Phaser.Scene, x: number, y: number, text: string | string[], center: boolean = true) {
super(scene, x, y, 'main_font', text, 8); super(scene, x, y, 'main_font', text, 8);
this.scene.add.existing(this) this.scene.add.existing(this);
if (center) {
this.setOrigin(0.5).setCenterAlign()
}
} }
} }

View File

@ -1,13 +1,15 @@
export class TextInput extends Phaser.GameObjects.BitmapText { export class TextInput extends Phaser.GameObjects.BitmapText {
private underLineLength = 10; private minUnderLineLength = 4;
private underLine: Phaser.GameObjects.Text; private underLine: Phaser.GameObjects.Text;
constructor(scene: Phaser.Scene, x: number, y: number, maxLength: number, text: string, onChange: (text: string) => void) { constructor(scene: Phaser.Scene, x: number, y: number, maxLength: number, text: string, onChange: (text: string) => void) {
super(scene, x, y, 'main_font', text, 32); super(scene, x, y, 'main_font', text, 32);
this.setOrigin(0.5).setCenterAlign()
this.scene.add.existing(this); this.scene.add.existing(this);
this.underLine = this.scene.add.text(x, y+1, '_______', { fontFamily: 'Arial', fontSize: "32px", color: '#ffffff'}) this.underLine = this.scene.add.text(x, y+1, this.getUnderLineBody(text.length), { fontFamily: 'Arial', fontSize: "32px", color: '#ffffff'})
this.underLine.setOrigin(0.5)
this.scene.input.keyboard.on('keydown', (event: KeyboardEvent) => { this.scene.input.keyboard.on('keydown', (event: KeyboardEvent) => {
@ -16,23 +18,27 @@ export class TextInput extends Phaser.GameObjects.BitmapText {
} else if ((event.keyCode === 32 || (event.keyCode >= 48 && event.keyCode <= 90)) && this.text.length < maxLength) { } else if ((event.keyCode === 32 || (event.keyCode >= 48 && event.keyCode <= 90)) && this.text.length < maxLength) {
this.addLetter(event.key); this.addLetter(event.key);
} }
this.underLine.text = this.getUnderLineBody(this.text.length);
onChange(this.text); onChange(this.text);
}); });
} }
private getUnderLineBody(textLength:number): string {
if (textLength < this.minUnderLineLength) textLength = this.minUnderLineLength;
let text = '_______';
for (let i = this.minUnderLineLength; i < textLength; i++) {
text += '__'
}
return text;
}
private deleteLetter() { private deleteLetter() {
this.text = this.text.substr(0, this.text.length - 1); this.text = this.text.substr(0, this.text.length - 1);
if (this.underLine.text.length > this.underLineLength) {
this.underLine.text = this.underLine.text.substr(0, this.underLine.text.length - 1);
}
} }
private addLetter(letter: string) { private addLetter(letter: string) {
this.text += letter; this.text += letter;
if (this.text.length > this.underLineLength) {
this.underLine.text += '_';
}
} }
getText(): string { getText(): string {

View File

@ -88,7 +88,7 @@ export abstract class Character extends Container {
this.add(this.teleportation);*/ this.add(this.teleportation);*/
this.PlayerValue = name; this.PlayerValue = name;
this.playerName = new BitmapText(scene, x, y - 25, 'main_font', name, 8); this.playerName = new BitmapText(scene, x, y - 25, 'main_font', name, 7);
this.playerName.setOrigin(0.5).setCenterAlign().setDepth(99999); this.playerName.setOrigin(0.5).setCenterAlign().setDepth(99999);
scene.add.existing(this.playerName); scene.add.existing(this.playerName);
@ -189,6 +189,7 @@ export abstract class Character extends Container {
//this.anims.playReverse(`${this.PlayerTexture}-${PlayerAnimationNames.WalkLeft}`, true); //this.anims.playReverse(`${this.PlayerTexture}-${PlayerAnimationNames.WalkLeft}`, true);
} }
//todo:remove this, use a container tech to move the bubble instead
if (this.bubble) { if (this.bubble) {
this.bubble.moveBubble(this.x, this.y); this.bubble.moveBubble(this.x, this.y);
} }

View File

@ -64,7 +64,7 @@ export class GameManager {
public async goToStartingMap(scenePlugin: Phaser.Scenes.ScenePlugin) { public async goToStartingMap(scenePlugin: Phaser.Scenes.ScenePlugin) {
const url = await this.startRoom.getMapUrl(); const url = await this.startRoom.getMapUrl();
console.log('Starting scene '+url); console.log('Starting scene '+url);
scenePlugin.start(url, {startLayerName: 'global'}); scenePlugin.start(url);
} }
} }

View File

@ -54,8 +54,7 @@ export enum Textures {
} }
export interface GameSceneInitInterface { export interface GameSceneInitInterface {
initPosition: PointInterface|null, initPosition: PointInterface|null
startLayerName: string|undefined
} }
interface InitUserPositionEventInterface { interface InitUserPositionEventInterface {
@ -130,7 +129,6 @@ export class GameScene extends ResizableScene implements CenterListener {
} }
private PositionNextScene: Array<Array<{ key: string, hash: string }>> = new Array<Array<{ key: string, hash: string }>>(); private PositionNextScene: Array<Array<{ key: string, hash: string }>> = new Array<Array<{ key: string, hash: string }>>();
private startLayerName: string|undefined;
private presentationModeSprite!: Sprite; private presentationModeSprite!: Sprite;
private chatModeSprite!: Sprite; private chatModeSprite!: Sprite;
private gameMap!: GameMap; private gameMap!: GameMap;
@ -303,8 +301,6 @@ export class GameScene extends ResizableScene implements CenterListener {
init(initData : GameSceneInitInterface) { init(initData : GameSceneInitInterface) {
if (initData.initPosition !== undefined) { if (initData.initPosition !== undefined) {
this.initPosition = initData.initPosition; this.initPosition = initData.initPosition;
} else if (initData.startLayerName !== undefined) {
this.startLayerName = initData.startLayerName;
} }
} }
@ -329,7 +325,10 @@ export class GameScene extends ResizableScene implements CenterListener {
this.addLayer(this.Map.createStaticLayer(layer.name, this.Terrains, 0, 0).setDepth(depth)); this.addLayer(this.Map.createStaticLayer(layer.name, this.Terrains, 0, 0).setDepth(depth));
} }
if (layer.type === 'tilelayer' && this.getExitSceneUrl(layer) !== undefined) { if (layer.type === 'tilelayer' && this.getExitSceneUrl(layer) !== undefined) {
this.loadNextGame(layer, this.mapFile.width, this.mapFile.tilewidth, this.mapFile.tileheight); this.loadNextGameFromExitSceneUrl(layer, this.mapFile.width);
} else if (layer.type === 'tilelayer' && this.getExitUrl(layer) !== undefined) {
console.log('Loading exitUrl ', this.getExitUrl(layer))
this.loadNextGameFromExitUrl(layer, this.mapFile.width);
} }
if (layer.type === 'objectgroup' && layer.name === 'floorLayer') { if (layer.type === 'objectgroup' && layer.name === 'floorLayer') {
depth = 10000; depth = 10000;
@ -345,9 +344,9 @@ export class GameScene extends ResizableScene implements CenterListener {
this.startY = this.initPosition.y; this.startY = this.initPosition.y;
} else { } else {
// Now, let's find the start layer // Now, let's find the start layer
if (this.startLayerName) { if (this.room.hash) {
for (const layer of this.mapFile.layers) { for (const layer of this.mapFile.layers) {
if (this.startLayerName === layer.name && layer.type === 'tilelayer' && this.isStartLayer(layer)) { if (this.room.hash === layer.name && layer.type === 'tilelayer' && this.isStartLayer(layer)) {
const startPosition = this.startUser(layer); const startPosition = this.startUser(layer);
this.startX = startPosition.x; this.startX = startPosition.x;
this.startY = startPosition.y; this.startY = startPosition.y;
@ -405,6 +404,13 @@ export class GameScene extends ResizableScene implements CenterListener {
context.stroke(); context.stroke();
this.circleTexture.refresh(); this.circleTexture.refresh();
// Let's alter browser history
let path = this.room.id;
if (this.room.hash) {
path += '#'+this.room.hash;
}
window.history.pushState({}, 'WorkAdventure', path);
// Let's pause the scene if the connection is not established yet // Let's pause the scene if the connection is not established yet
if (this.connection === undefined) { if (this.connection === undefined) {
// Let's wait 0.5 seconds before printing the "connecting" screen to avoid blinking // Let's wait 0.5 seconds before printing the "connecting" screen to avoid blinking
@ -637,6 +643,10 @@ export class GameScene extends ResizableScene implements CenterListener {
} }
} }
private getExitUrl(layer: ITiledMapLayer): string|undefined {
return this.getProperty(layer, "exitUrl") as string|undefined;
}
private getExitSceneUrl(layer: ITiledMapLayer): string|undefined { private getExitSceneUrl(layer: ITiledMapLayer): string|undefined {
return this.getProperty(layer, "exitSceneUrl") as string|undefined; return this.getProperty(layer, "exitSceneUrl") as string|undefined;
} }
@ -661,15 +671,7 @@ export class GameScene extends ResizableScene implements CenterListener {
return obj.value; return obj.value;
} }
/** private loadNextGameFromExitSceneUrl(layer: ITiledMapLayer, mapWidth: number) {
*
* @param layer
* @param mapWidth
* @param tileWidth
* @param tileHeight
*/
//todo: push that into the gameManager
private loadNextGame(layer: ITiledMapLayer, mapWidth: number, tileWidth: number, tileHeight: number){
const exitSceneUrl = this.getExitSceneUrl(layer); const exitSceneUrl = this.getExitSceneUrl(layer);
if (exitSceneUrl === undefined) { if (exitSceneUrl === undefined) {
throw new Error('Layer is not an exit scene layer.'); throw new Error('Layer is not an exit scene layer.');
@ -679,17 +681,33 @@ export class GameScene extends ResizableScene implements CenterListener {
instance = this.instance; instance = this.instance;
} }
console.log('existSceneUrl', exitSceneUrl); //console.log('existSceneUrl', exitSceneUrl);
console.log('existSceneInstance', instance); //console.log('existSceneInstance', instance);
// TODO: eventually compute a relative URL
// TODO: handle /@/ URL CASES!
const absoluteExitSceneUrl = new URL(exitSceneUrl, this.MapUrlFile).href; const absoluteExitSceneUrl = new URL(exitSceneUrl, this.MapUrlFile).href;
const absoluteExitSceneUrlWithoutProtocol = absoluteExitSceneUrl.toString().substr(absoluteExitSceneUrl.toString().indexOf('://')+3); const absoluteExitSceneUrlWithoutProtocol = absoluteExitSceneUrl.toString().substr(absoluteExitSceneUrl.toString().indexOf('://')+3);
const roomId = '_/'+instance+'/'+absoluteExitSceneUrlWithoutProtocol; const roomId = '_/'+instance+'/'+absoluteExitSceneUrlWithoutProtocol;
console.log("Foo", instance, absoluteExitSceneUrlWithoutProtocol);
this.loadNextGame(layer, mapWidth, roomId);
}
private loadNextGameFromExitUrl(layer: ITiledMapLayer, mapWidth: number) {
const exitUrl = this.getExitUrl(layer);
if (exitUrl === undefined) {
throw new Error('Layer is not an exit layer.');
}
const fullPath = new URL(exitUrl, window.location.toString()).pathname;
this.loadNextGame(layer, mapWidth, fullPath);
}
/**
*
* @param layer
* @param mapWidth
*/
//todo: push that into the gameManager
private loadNextGame(layer: ITiledMapLayer, mapWidth: number, roomId: string){
const room = new Room(roomId); const room = new Room(roomId);
gameManager.loadMap(room, this.scene); gameManager.loadMap(room, this.scene);
const exitSceneKey = roomId; const exitSceneKey = roomId;
@ -704,7 +722,7 @@ export class GameScene extends ResizableScene implements CenterListener {
const y : number = parseInt(((key + 1) / mapWidth).toString()); const y : number = parseInt(((key + 1) / mapWidth).toString());
const x : number = key - (y * mapWidth); const x : number = key - (y * mapWidth);
let hash = new URL(exitSceneUrl, this.MapUrlFile).hash; let hash = new URL(roomId, this.MapUrlFile).hash;
if (hash) { if (hash) {
hash = hash.substr(1); hash = hash.substr(1);
} }
@ -941,9 +959,7 @@ export class GameScene extends ResizableScene implements CenterListener {
this.simplePeer.unregister(); this.simplePeer.unregister();
this.scene.stop(); this.scene.stop();
this.scene.remove(this.scene.key); this.scene.remove(this.scene.key);
this.scene.start(nextSceneKey.key, { this.scene.start(nextSceneKey.key);
startLayerName: nextSceneKey.hash
});
} }
} }

View File

@ -54,12 +54,8 @@ export class CustomizeScene extends ResizableScene {
create() { create() {
this.textField = new TextField(this, this.game.renderer.width / 2, 30, 'Customize your own Avatar!'); this.textField = new TextField(this, this.game.renderer.width / 2, 30, 'Customize your own Avatar!');
this.textField.setOrigin(0.5).setCenterAlign();
this.textField.setVisible(true);
this.enterField = new TextField(this, this.game.renderer.width / 2, 500, 'you can start the game by pressing SPACE..'); this.enterField = new TextField(this, this.game.renderer.width / 2, 40, 'you can start the game by pressing SPACE..');
this.enterField.setOrigin(0.5).setCenterAlign();
this.enterField.setVisible(true);
this.logo = new Image(this, this.game.renderer.width - 30, this.game.renderer.height - 20, CustomizeTextures.icon); this.logo = new Image(this, this.game.renderer.width - 30, this.game.renderer.height - 20, CustomizeTextures.icon);
this.add.existing(this.logo); this.add.existing(this.logo);

View File

@ -53,16 +53,12 @@ export class EnableCameraScene extends Phaser.Scene {
create() { create() {
this.textField = new TextField(this, this.game.renderer.width / 2, 20, 'Turn on your camera and microphone'); this.textField = new TextField(this, this.game.renderer.width / 2, 20, 'Turn on your camera and microphone');
this.textField.setOrigin(0.5).setCenterAlign();
this.pressReturnField = new TextField(this, this.game.renderer.width / 2, this.game.renderer.height - 30, 'Press enter to start'); this.pressReturnField = new TextField(this, this.game.renderer.width / 2, this.game.renderer.height - 30, 'Press enter to start');
this.pressReturnField.setOrigin(0.5).setCenterAlign();
this.cameraNameField = new TextField(this, this.game.renderer.width / 2, this.game.renderer.height - 60, ''); this.cameraNameField = new TextField(this, this.game.renderer.width / 2, this.game.renderer.height - 60, '');
this.cameraNameField.setOrigin(0.5).setCenterAlign();
this.microphoneNameField = new TextField(this, this.game.renderer.width / 2, this.game.renderer.height - 40, ''); this.microphoneNameField = new TextField(this, this.game.renderer.width / 2, this.game.renderer.height - 40, '');
this.microphoneNameField.setOrigin(0.5).setCenterAlign();
this.arrowRight = new Image(this, 0, 0, LoginTextures.arrowRight); this.arrowRight = new Image(this, 0, 0, LoginTextures.arrowRight);
this.arrowRight.setOrigin(0.5, 0.5); this.arrowRight.setOrigin(0.5, 0.5);

View File

@ -8,6 +8,7 @@ import {PLAYER_RESOURCES, PlayerResourceDescriptionInterface} from "../Entity/Ch
import {cypressAsserter} from "../../Cypress/CypressAsserter"; import {cypressAsserter} from "../../Cypress/CypressAsserter";
import {SelectCharacterSceneName} from "./SelectCharacterScene"; import {SelectCharacterSceneName} from "./SelectCharacterScene";
import {ResizableScene} from "./ResizableScene"; import {ResizableScene} from "./ResizableScene";
import {localUserStore} from "../../Connexion/LocalUserStore";
//todo: put this constants in a dedicated file //todo: put this constants in a dedicated file
export const LoginSceneName = "LoginScene"; export const LoginSceneName = "LoginScene";
@ -28,9 +29,7 @@ export class LoginScene extends ResizableScene {
super({ super({
key: LoginSceneName key: LoginSceneName
}); });
if (window.localStorage) { this.name = localUserStore.getName();
this.name = window.localStorage.getItem('playerName') ?? '';
}
} }
preload() { preload() {
@ -54,22 +53,18 @@ export class LoginScene extends ResizableScene {
cypressAsserter.initStarted(); cypressAsserter.initStarted();
this.textField = new TextField(this, this.game.renderer.width / 2, 50, 'Enter your name:'); this.textField = new TextField(this, this.game.renderer.width / 2, 50, 'Enter your name:');
this.textField.setOrigin(0.5).setCenterAlign() this.nameInput = new TextInput(this, this.game.renderer.width / 2, 70, 8, this.name,(text: string) => {
this.nameInput = new TextInput(this, this.game.renderer.width / 2 - 64, 70, 4, this.name,(text: string) => {
this.name = text; this.name = text;
if (window.localStorage) { localUserStore.setName(text);
window.localStorage.setItem('playerName', text);
}
}); });
this.pressReturnField = new TextField(this, this.game.renderer.width / 2, 130, 'Press enter to start'); this.pressReturnField = new TextField(this, this.game.renderer.width / 2, 130, 'Press enter to start');
this.pressReturnField.setOrigin(0.5).setCenterAlign()
this.logo = new Image(this, this.game.renderer.width - 30, this.game.renderer.height - 20, LoginTextures.icon); this.logo = new Image(this, this.game.renderer.width - 30, this.game.renderer.height - 20, LoginTextures.icon);
this.add.existing(this.logo); this.add.existing(this.logo);
const infoText = "Commands: \n - Arrows or Z,Q,S,D to move\n - SHIFT to run"; const infoText = "Commands: \n - Arrows or Z,Q,S,D to move\n - SHIFT to run";
this.infoTextField = new TextField(this, 10, this.game.renderer.height - 35, infoText); this.infoTextField = new TextField(this, 10, this.game.renderer.height - 35, infoText, false);
this.input.keyboard.on('keyup-ENTER', () => { this.input.keyboard.on('keyup-ENTER', () => {
if (this.name === '') { if (this.name === '') {

View File

@ -57,10 +57,8 @@ export class SelectCharacterScene extends ResizableScene {
create() { create() {
this.textField = new TextField(this, this.game.renderer.width / 2, 50, 'Select your character'); this.textField = new TextField(this, this.game.renderer.width / 2, 50, 'Select your character');
this.textField.setOrigin(0.5).setCenterAlign()
this.pressReturnField = new TextField(this, this.game.renderer.width / 2, 256, 'Press enter to start'); this.pressReturnField = new TextField(this, this.game.renderer.width / 2, 256, 'Press enter to start');
this.pressReturnField.setOrigin(0.5).setCenterAlign()
const rectangleXStart = this.game.renderer.width / 2 - (this.nbCharactersPerRow / 2) * 32 + 16; const rectangleXStart = this.game.renderer.width / 2 - (this.nbCharactersPerRow / 2) * 32 + 16;
@ -123,30 +121,6 @@ export class SelectCharacterScene extends ResizableScene {
} else { } else {
this.scene.start(CustomizeSceneName); this.scene.start(CustomizeSceneName);
} }
// Do we have a start URL in the address bar? If so, let's redirect to this address
/*const instanceAndMapUrl = this.findMapUrl();
if (instanceAndMapUrl !== null) {
const [mapUrl, instance] = instanceAndMapUrl;
const key = gameManager.loadMap(mapUrl, this.scene, instance);
this.scene.start(key, {
startLayerName: window.location.hash ? window.location.hash.substr(1) : undefined
} as GameSceneInitInterface);
return {
mapUrlStart: mapUrl,
startInstance: instance
};
} else {
// If we do not have a map address in the URL, let's ask the server for a start map.
return gameManager.loadStartMap().then((startMap: StartMapInterface) => {
const key = gameManager.loadMap(window.location.protocol + "//" + startMap.mapUrlStart, this.scene, startMap.startInstance);
this.scene.start(key);
return startMap;
}).catch((err) => {
console.error(err);
throw err;
});
}*/
} }
/** /**

View File

@ -45,7 +45,6 @@ export class FourOFourScene extends Phaser.Scene {
this.add.existing(this.logo); this.add.existing(this.logo);
this.mapNotFoundField = new TextField(this, this.game.renderer.width / 2, this.game.renderer.height / 2, "404 - File not found"); this.mapNotFoundField = new TextField(this, this.game.renderer.width / 2, this.game.renderer.height / 2, "404 - File not found");
this.mapNotFoundField.setOrigin(0.5, 0.5).setCenterAlign();
let text: string = ''; let text: string = '';
if (this.file !== undefined) { if (this.file !== undefined) {
@ -56,7 +55,6 @@ export class FourOFourScene extends Phaser.Scene {
} }
this.couldNotFindField = new TextField(this, this.game.renderer.width / 2, this.game.renderer.height / 2 + 24, text); this.couldNotFindField = new TextField(this, this.game.renderer.width / 2, this.game.renderer.height / 2 + 24, text);
this.couldNotFindField.setOrigin(0.5, 0.5).setCenterAlign();
const url = this.file ? this.file : this.url; const url = this.file ? this.file : this.url;
if (url !== undefined) { if (url !== undefined) {

View File

@ -34,7 +34,6 @@ export class ReconnectingScene extends Phaser.Scene {
this.add.existing(this.logo); this.add.existing(this.logo);
this.reconnectingField = new TextField(this, this.game.renderer.width / 2, this.game.renderer.height / 2, "Connection lost. Reconnecting..."); this.reconnectingField = new TextField(this, this.game.renderer.width / 2, this.game.renderer.height / 2, "Connection lost. Reconnecting...");
this.reconnectingField.setOrigin(0.5, 0.5).setCenterAlign();
const cat = this.physics.add.sprite(this.game.renderer.width / 2, this.game.renderer.height / 2 - 32, 'cat'); const cat = this.physics.add.sprite(this.game.renderer.width / 2, this.game.renderer.height / 2 - 32, 'cat');
this.anims.create({ this.anims.create({

View File

@ -191,7 +191,7 @@
"opacity":1, "opacity":1,
"properties":[ "properties":[
{ {
"name":"exitSceneUrl", "name":"exitUrl",
"type":"string", "type":"string",
"value":"..\/Floor1\/floor1.json" "value":"..\/Floor1\/floor1.json"
}], }],

View File

@ -44,7 +44,7 @@
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<div class="col"> < class="col">
<h2 id="tools-you-will-need" class="pixel-title">Tools you will need</h2> <h2 id="tools-you-will-need" class="pixel-title">Tools you will need</h2>
<p>In order to build your own map for WorkAdventure, you need:</p> <p>In order to build your own map for WorkAdventure, you need:</p>
<ul> <ul>
@ -131,11 +131,22 @@
<p>In order to place an exit on your scene that leads to another scene:</p> <p>In order to place an exit on your scene that leads to another scene:</p>
<ul> <ul>
<li>You must create a specific layer. When a character reaches ANY tile of that layer, it will exit the scene.</li> <li>You must create a specific layer. When a character reaches ANY tile of that layer, it will exit the scene.</li>
<li>In layer properties, you MUST add &quot;exitSceneUrl&quot; property. It represents the map URL of the next scene. For example : <code>/&lt;map folder&gt;/&lt;map&gt;.json</code>. Be careful, if you want the next map to be correctly loaded, you must check that the map files are in folder <code>back/src/Assets/Maps/&lt;your map folder&gt;</code>. The files will be accessible by url <code>&lt;HOST&gt;/map/files/&lt;your map folder&gt;/...</code>.</li> <li>In layer properties, you MUST add "exitUrl&quot; property. It represents the URL of the next scene. You can put relative or absolute URLs.</li>
<li>In layer properties, you CAN add an &quot;exitInstance&quot; property. If set, you will join the map of the specified instance. Otherwise, you will stay on the same instance.</li>
<li>If you want to have multiple exits, you can create many layers with name &quot;exit&quot;. Each layer has a different key <code>exitSceneUrl</code> and have tiles that represent exits to another scene.</li> <li>If you want to have multiple exits, you can create many layers with name &quot;exit&quot;. Each layer has a different key <code>exitSceneUrl</code> and have tiles that represent exits to another scene.</li>
</ul> </ul>
<p>
<strong>Understanding map URLs in WorkAdventure:</strong><br/>
There are 2 kinds of URLs in WorkAdventure:
<ul>
<li>Public URLs are in the form https://play.workadventu.re/_/[instance]/[server]/[path to map]</li>
<li>Private URLs (used in paid accounts) are in the form https://play.workadventu.re/@/[organization]/[world]/[map]</li>
</ul>
Assuming your JSON map is hosted at "https://example.com/my/map.json", then you can browse your map at "https://play.workadventu.re/_/global/example.com/my/map.json".
Here, "global" is a name of an "instance" of your map. You can put anything instead of "global" here. People on the same instance of the map can see each others.
If 2 users use 2 different instances, they are on the same map, but in 2 parallel universes. They cannot see each other.
</p>
<p class="text-center"><img src="docs/exit_layer_map.png" alt="" style="width: 90%"></p> <p class="text-center"><img src="docs/exit_layer_map.png" alt="" style="width: 90%"></p>
<p>Note: in older releases of WorkAdventure, you could link to a map file directly using properties "exitSceneUrl&quot; and &quot;exitInstance&quot;. Those properties are now deprecated. Use "exitUrl" instead.</p>
<h3 id="defining-several-entry-points" class="pixel-title">Defining several entry points</h3> <h3 id="defining-several-entry-points" class="pixel-title">Defining several entry points</h3>
<p>Often your map will have several exits, and therefore, several entry points. For instance, if there <p>Often your map will have several exits, and therefore, several entry points. For instance, if there
is an exit by a door that leads to the garden map, when you come back from the garden you expect to is an exit by a door that leads to the garden map, when you come back from the garden you expect to
@ -146,7 +157,7 @@
<li>You must create a specific layer. When a character enters the map by this entry point, it will enter the map randomly on ANY tile of that layer.</li> <li>You must create a specific layer. When a character enters the map by this entry point, it will enter the map randomly on ANY tile of that layer.</li>
<li>In layer properties, you MUST add a boolean &quot;startLayer&quot; property. It should be set to true.</li> <li>In layer properties, you MUST add a boolean &quot;startLayer&quot; property. It should be set to true.</li>
<li>The name of the entry point is the name of the layer</li> <li>The name of the entry point is the name of the layer</li>
<li>To enter via this entry point, simply add a hash with the entry point name to the URL (&quot;#[<em>startLayerName</em>]&quot;). For instance: &quot;<a href="https://workadventu.re/_/global/mymap.com/path/map.json#my-entry-point">https://workadventu.re/_/global/mymap.com/path/map.json#my-entry-point</a>&quot;.</li> <li>To enter via this entry point, simply add a hash with the entry point name to the URL (&quot;#[<em>startLayerName</em>]&quot;). For instance: &quot;https://workadventu.re/_/global/mymap.com/path/map.json#my-entry-point&quot;.</li>
<li>You can of course use the &quot;#&quot; notation in an exit scene URL (so an exit scene URL will point to a given entry scene URL)</li> <li>You can of course use the &quot;#&quot; notation in an exit scene URL (so an exit scene URL will point to a given entry scene URL)</li>
</ul> </ul>