Merge branch 'master' of https://github.com/thecodingmachine/workadventure into feature/back-players-proximity

This commit is contained in:
David MAECHLER 2020-04-06 23:02:01 +02:00
commit af5d2a5a97
9 changed files with 176 additions and 10 deletions

View File

@ -61,3 +61,7 @@ jobs:
- name: "Lint"
run: yarn run lint
working-directory: "back"
- name: "Jasmine"
run: yarn test
working-directory: "back"

5
back/jasmine.json Normal file
View File

@ -0,0 +1,5 @@
{
"spec_dir": "tests",
"spec_files": ["**/*[tT]est.ts"],
"stopSpecOnExpectationFailure": false
}

View File

@ -7,6 +7,7 @@
"tsc": "tsc",
"dev": "ts-node-dev --respawn --transpileOnly ./server.ts",
"prod": "tsc && node ./dist/server.js",
"test": "ts-node node_modules/jasmine/bin/jasmine --config=jasmine.json",
"lint": "node_modules/.bin/eslint src/ . --ext .ts"
},
"repository": {
@ -33,8 +34,10 @@
"typescript": "^3.8.3"
},
"devDependencies": {
"@types/jasmine": "^3.5.10",
"@typescript-eslint/eslint-plugin": "^2.26.0",
"@typescript-eslint/parser": "^2.26.0",
"eslint": "^6.8.0"
"eslint": "^6.8.0",
"jasmine": "^3.5.0"
}
}

View File

@ -16,14 +16,14 @@ let RefreshUserPositionFunction = function(rooms : ExtRooms, Io: socketIO.Server
//create mapping with all users in all rooms
let mapPositionUserByRoom = new Map();
for(let i = 0; i < socketsKey.length; i++){
let socket = clients.sockets[socketsKey[i]];
if(!(socket as ExSocketInterface).position){
let socket = clients.sockets[socketsKey[i]] as ExSocketInterface;
if(!socket.position){
continue;
}
let data = {
userId : (socket as ExSocketInterface).userId,
roomId : (socket as ExSocketInterface).roomId,
position : (socket as ExSocketInterface).position,
userId : socket.userId,
roomId : socket.roomId,
position : socket.position,
};
let dataArray = <any>[];
if(mapPositionUserByRoom.get(data.roomId)){

View File

@ -5,7 +5,7 @@ export class Message {
constructor(message: string) {
let data = JSON.parse(message);
if(!data.userId || !data.roomId){
throw Error("userId and roomId cannot be null");
throw Error("userId or roomId cannot be null");
}
this.userId = data.userId;
this.roomId = data.roomId;

31
back/tests/MessageTest.ts Normal file
View File

@ -0,0 +1,31 @@
import "jasmine";
import {Message} from "../src/Model/Websocket/Message";
describe("Message Model", () => {
it("should find userId and roomId", () => {
let message = JSON.stringify({userId: "test1", roomId: "test2"});
let messageObject = new Message(message);
expect(messageObject.userId).toBe("test1");
expect(messageObject.roomId).toBe("test2");
})
it("should expose a toJson method", () => {
let message = JSON.stringify({userId: "test1", roomId: "test2"});
let messageObject = new Message(message);
expect(messageObject.toJson()).toEqual({userId: "test1", roomId: "test2"});
})
it("should find throw error when no userId", () => {
let message = JSON.stringify({roomId: "test2"});
expect(() => {
let messageObject = new Message(message);
}).toThrow(new Error("userId or roomId cannot be null"));
})
it("should find throw error when no roomId", () => {
let message = JSON.stringify({userId: "test1"});
expect(() => {
let messageObject = new Message(message);
}).toThrow(new Error("userId or roomId cannot be null"));
})
})

View File

@ -73,6 +73,11 @@
dependencies:
http-status-codes "*"
"@types/jasmine@^3.5.10":
version "3.5.10"
resolved "https://registry.yarnpkg.com/@types/jasmine/-/jasmine-3.5.10.tgz#a1a41012012b5da9d4b205ba9eba58f6cce2ab7b"
integrity sha512-3F8qpwBAiVc5+HPJeXJpbrl+XjawGmciN5LgiO7Gv1pl1RHtjoMNqZpqEksaPJW05ViKe8snYInRs6xB25Xdew==
"@types/json-schema@^7.0.3":
version "7.0.4"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.4.tgz#38fd73ddfd9b55abb1e1b2ed578cb55bd7b7d339"
@ -915,7 +920,7 @@ glob-parent@^5.0.0:
dependencies:
is-glob "^4.0.1"
glob@^7.1.3, glob@^7.1.6:
glob@^7.1.3, glob@^7.1.4, glob@^7.1.6:
version "7.1.6"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.6.tgz#141f33b81a7c2492e125594307480c46679278a6"
integrity sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==
@ -1134,6 +1139,19 @@ isexe@^2.0.0:
resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
integrity sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=
jasmine-core@~3.5.0:
version "3.5.0"
resolved "https://registry.yarnpkg.com/jasmine-core/-/jasmine-core-3.5.0.tgz#132c23e645af96d85c8bca13c8758b18429fc1e4"
integrity sha512-nCeAiw37MIMA9w9IXso7bRaLl+c/ef3wnxsoSAlYrzS+Ot0zTG6nU8G/cIfGkqpkjX2wNaIW9RFG0TwIFnG6bA==
jasmine@^3.5.0:
version "3.5.0"
resolved "https://registry.yarnpkg.com/jasmine/-/jasmine-3.5.0.tgz#7101eabfd043a1fc82ac24e0ab6ec56081357f9e"
integrity sha512-DYypSryORqzsGoMazemIHUfMkXM7I7easFaxAvNM3Mr6Xz3Fy36TupTrAOxZWN8MVKEU5xECv22J4tUQf3uBzQ==
dependencies:
glob "^7.1.4"
jasmine-core "~3.5.0"
js-tokens@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

View File

@ -1,5 +1,16 @@
export class GameScene extends Phaser.Scene {
private player: Phaser.GameObjects.Sprite;
private keyZ: Phaser.Input.Keyboard.Key;
private keyQ: Phaser.Input.Keyboard.Key;
private keyS: Phaser.Input.Keyboard.Key;
private keyD: Phaser.Input.Keyboard.Key;
private keyRight: Phaser.Input.Keyboard.Key;
private keyLeft: Phaser.Input.Keyboard.Key;
private keyUp: Phaser.Input.Keyboard.Key;
private keyDown: Phaser.Input.Keyboard.Key;
private keyShift: Phaser.Input.Keyboard.Key;
constructor() {
super({
@ -10,9 +21,29 @@ export class GameScene extends Phaser.Scene {
preload(): void {
this.load.image('tiles', 'maps/tiles.png');
this.load.tilemapTiledJSON('map', 'maps/map2.json');
this.load.spritesheet('player',
'resources/characters/pipoya/Male 01-1.png',
{ frameWidth: 32, frameHeight: 32 }
);
}
init(): void {
this.keyShift = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SHIFT);
this.keyZ = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.Z);
this.keyQ = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.Q);
this.keyS = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.S);
this.keyD = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.D);
this.keyUp = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.UP);
this.keyLeft = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.LEFT);
this.keyDown = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.DOWN);
this.keyRight = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.RIGHT);
}
private moveCamera(x:number, y:number, speedMultiplier: number): void {
this.cameras.main.scrollX += speedMultiplier * 2 * x;
this.cameras.main.scrollY += speedMultiplier * 2 * y;
}
create(): void {
@ -22,14 +53,88 @@ export class GameScene extends Phaser.Scene {
let bottomLayer = mappy.createStaticLayer("Calque 1", [terrain], 0, 0);
let topLayer = mappy.createStaticLayer("Calque 2", [terrain], 0, 0);
// Let's manage animations of the player
this.anims.create({
key: 'down',
frames: this.anims.generateFrameNumbers('player', { start: 0, end: 2 }),
frameRate: 10,
repeat: -1
});
this.anims.create({
key: 'left',
frames: this.anims.generateFrameNumbers('player', { start: 3, end: 5 }),
frameRate: 10,
repeat: -1
});
this.anims.create({
key: 'right',
frames: this.anims.generateFrameNumbers('player', { start: 6, end: 8 }),
frameRate: 10,
repeat: -1
});
this.anims.create({
key: 'up',
frames: this.anims.generateFrameNumbers('player', { start: 9, end: 11 }),
frameRate: 10,
repeat: -1
});
//let player = this.add.sprite(450, 450, 'player');
//player.anims.play('down');
//player.setBounce(0.2);
//player.setCollideWorldBounds(true);
this.player = this.add.sprite(450, 450, 'player');
}
private angle: number = 0;
update(dt: number): void {
this.cameras.main.scrollX = Math.floor(300 + 300 * Math.cos(this.angle));
let speedMultiplier = this.keyShift.isDown ? 5 : 1;
if (this.keyUp.isDown) {
this.moveCamera(0, -1, speedMultiplier);
}
if (this.keyLeft.isDown) {
this.moveCamera(-1, 0, speedMultiplier);
}
if (this.keyDown.isDown) {
this.moveCamera(0, 1, speedMultiplier);
}
if (this.keyRight.isDown) {
this.moveCamera(1, 0, speedMultiplier);
}
if (this.keyZ.isDown) {
this.managePlayerAnimation('up');
this.player.setY(this.player.y - 2)
} else if (this.keyQ.isDown) {
this.managePlayerAnimation('left');
this.player.setX(this.player.x - 2)
} else if (this.keyS.isDown) {
this.managePlayerAnimation('down');
this.player.setY(this.player.y + 2)
} else if (this.keyD.isDown) {
this.managePlayerAnimation('right');
this.player.setX(this.player.x + 2)
} else {
this.managePlayerAnimation('none');
}
/*this.cameras.main.scrollX = Math.floor(300 + 300 * Math.cos(this.angle));
this.cameras.main.scrollY = Math.floor(300 + 300 * Math.sin(this.angle));
this.angle = dt * 0.0001;
this.angle = dt * 0.0001;*/
}
managePlayerAnimation(direction: string) {
if (!this.player.anims.currentAnim || this.player.anims.currentAnim.key !== direction) {
this.player.anims.play(direction);
} else if (direction === 'none' && this.player.anims.currentAnim) {
this.player.anims.currentAnim.destroy();
}
}
}