From d7209d886430c1794d95c7126a7a8c949c6ed972 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Thu, 24 Sep 2020 11:16:08 +0200 Subject: [PATCH] Migrating messages locally into back and front --- back/Dockerfile | 2 +- back/package.json | 2 ++ back/src/Controller/IoSocketController.ts | 4 ++-- back/src/Messages/.gitignore | 1 + back/src/Model/Websocket/ExSocketInterface.ts | 2 +- back/src/Model/Websocket/ProtobufUtils.ts | 2 +- back/tsconfig.json | 2 +- back/yarn.lock | 10 ++++++++++ docker-compose.yaml | 4 ++-- front/Dockerfile | 2 +- front/package.json | 2 ++ front/src/Connection.ts | 2 +- front/src/Messages/.gitignore | 1 + front/src/Network/ProtobufClientUtils.ts | 2 +- front/src/Phaser/Game/GameScene.ts | 4 +++- front/src/Phaser/Game/PlayerMovement.ts | 3 ++- front/yarn.lock | 10 ++++++++++ messages/package.json | 5 ++++- 18 files changed, 46 insertions(+), 14 deletions(-) create mode 100644 back/src/Messages/.gitignore create mode 100644 front/src/Messages/.gitignore diff --git a/back/Dockerfile b/back/Dockerfile index f0c565f9..bd98d66a 100644 --- a/back/Dockerfile +++ b/back/Dockerfile @@ -6,7 +6,7 @@ RUN yarn install && yarn proto FROM thecodingmachine/nodejs:12 COPY --chown=docker:docker back . -COPY --from=builder --chown=docker:docker /var/www/messages /var/www/messages +COPY --from=builder --chown=docker:docker /var/www/messages/generated /var/www/html/src/Messages/generated RUN yarn install ENV NODE_ENV=production diff --git a/back/package.json b/back/package.json index 3306578a..17763861 100644 --- a/back/package.json +++ b/back/package.json @@ -39,6 +39,7 @@ "body-parser": "^1.19.0", "express": "^4.17.1", "generic-type-guard": "^3.2.0", + "google-protobuf": "^3.13.0", "http-status-codes": "^1.4.0", "jsonwebtoken": "^8.5.1", "prom-client": "^12.0.0", @@ -50,6 +51,7 @@ }, "devDependencies": { "@types/express": "^4.17.4", + "@types/google-protobuf": "^3.7.3", "@types/http-status-codes": "^1.2.0", "@types/jasmine": "^3.5.10", "@types/jsonwebtoken": "^8.3.8", diff --git a/back/src/Controller/IoSocketController.ts b/back/src/Controller/IoSocketController.ts index bf1452ac..7e13b9d0 100644 --- a/back/src/Controller/IoSocketController.ts +++ b/back/src/Controller/IoSocketController.ts @@ -28,8 +28,8 @@ import { SubMessage, UserMovedMessage, BatchMessage, GroupUpdateMessage, PointMessage, GroupDeleteMessage -} from "../../../messages/generated/messages_pb"; -import {UserMovesMessage} from "../../../messages/generated/messages_pb"; +} from "../Messages/generated/messages_pb"; +import {UserMovesMessage} from "../Messages/generated/messages_pb"; import Direction = PositionMessage.Direction; import {ProtobufUtils} from "../Model/Websocket/ProtobufUtils"; diff --git a/back/src/Messages/.gitignore b/back/src/Messages/.gitignore new file mode 100644 index 00000000..9e0adcc1 --- /dev/null +++ b/back/src/Messages/.gitignore @@ -0,0 +1 @@ +/generated/ diff --git a/back/src/Model/Websocket/ExSocketInterface.ts b/back/src/Model/Websocket/ExSocketInterface.ts index cd1f73ed..ace374f4 100644 --- a/back/src/Model/Websocket/ExSocketInterface.ts +++ b/back/src/Model/Websocket/ExSocketInterface.ts @@ -3,7 +3,7 @@ import {PointInterface} from "./PointInterface"; import {Identificable} from "./Identificable"; import {TokenInterface} from "../../Controller/AuthenticateController"; import {ViewportInterface} from "_Model/Websocket/ViewportMessage"; -import {BatchMessage, SubMessage} from "../../../../messages/generated/messages_pb"; +import {BatchMessage, SubMessage} from "../../Messages/generated/messages_pb"; export interface ExSocketInterface extends Socket, Identificable { token: string; diff --git a/back/src/Model/Websocket/ProtobufUtils.ts b/back/src/Model/Websocket/ProtobufUtils.ts index 516a744e..4e5aec02 100644 --- a/back/src/Model/Websocket/ProtobufUtils.ts +++ b/back/src/Model/Websocket/ProtobufUtils.ts @@ -1,5 +1,5 @@ import {PointInterface} from "./PointInterface"; -import {PositionMessage} from "../../../../messages/generated/messages_pb"; +import {PositionMessage} from "../../Messages/generated/messages_pb"; import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface"; import Direction = PositionMessage.Direction; diff --git a/back/tsconfig.json b/back/tsconfig.json index de6314a3..34ad9a5a 100644 --- a/back/tsconfig.json +++ b/back/tsconfig.json @@ -7,7 +7,7 @@ "downlevelIteration": true, "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ // "lib": [], /* Specify library files to be included in the compilation. */ - // "allowJs": true, /* Allow javascript files to be compiled. */ + "allowJs": true, /* Allow javascript files to be compiled. */ // "checkJs": true, /* Report errors in .js files. */ // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ // "declaration": true, /* Generates corresponding '.d.ts' file. */ diff --git a/back/yarn.lock b/back/yarn.lock index f660a5c8..feef36ea 100644 --- a/back/yarn.lock +++ b/back/yarn.lock @@ -57,6 +57,11 @@ "@types/qs" "*" "@types/serve-static" "*" +"@types/google-protobuf@^3.7.3": + version "3.7.3" + resolved "https://registry.yarnpkg.com/@types/google-protobuf/-/google-protobuf-3.7.3.tgz#429512e541bbd777f2c867692e6335ee08d1f6d4" + integrity sha512-FRwj40euE2bYkG+0X5w2nEA8yAzgJRcEa7RBd0Gsdkb9/tPM2pctVVAvnOUTbcXo2VmIHPo0Ae94Gl9vRHfKzg== + "@types/http-status-codes@^1.2.0": version "1.2.0" resolved "https://registry.yarnpkg.com/@types/http-status-codes/-/http-status-codes-1.2.0.tgz#6e5244835aaf7164dd306f1d4d2dfdbb2159d909" @@ -822,6 +827,11 @@ globals@^12.1.0: dependencies: type-fest "^0.8.1" +google-protobuf@^3.13.0: + version "3.13.0" + resolved "https://registry.yarnpkg.com/google-protobuf/-/google-protobuf-3.13.0.tgz#909c5983d75dd6101ed57c79e0528d000cdc3251" + integrity sha512-ZIf3qfLFayVrPvAjeKKxO5FRF1/NwRxt6Dko+fWEMuHwHbZx8/fcaAao9b0wCM6kr8qeg2te8XTpyuvKuD9aKw== + graceful-fs@^4.1.2: version "4.2.3" resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.3.tgz#4a12ff1b60376ef09862c2093edd908328be8423" diff --git a/docker-compose.yaml b/docker-compose.yaml index fce76204..a37fe28f 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -33,7 +33,6 @@ services: command: yarn run start volumes: - ./front:/usr/src/app - - ./messages:/usr/src/messages/ labels: - "traefik.http.routers.front.rule=Host(`play.workadventure.localhost`)" - "traefik.http.routers.front.entryPoints=web,traefik" @@ -76,7 +75,6 @@ services: ALLOW_ARTILLERY: "true" volumes: - ./back:/usr/src/app - - ./messages:/usr/src/messages/ labels: - "traefik.http.routers.back.rule=Host(`api.workadventure.localhost`)" - "traefik.http.routers.back.entryPoints=web" @@ -111,3 +109,5 @@ services: STARTUP_COMMAND_2: yarn run proto:watch volumes: - ./messages:/usr/src/app + - ./back:/usr/src/back + - ./front:/usr/src/front diff --git a/front/Dockerfile b/front/Dockerfile index 98e29a52..6c79ad6e 100644 --- a/front/Dockerfile +++ b/front/Dockerfile @@ -7,7 +7,7 @@ RUN yarn install && yarn proto FROM thecodingmachine/nodejs:14-apache COPY --chown=docker:docker front . -COPY --from=builder --chown=docker:docker /var/www/messages /var/www/messages +COPY --from=builder --chown=docker:docker /var/www/messages/generated /var/www/html/src/Messages/generated RUN yarn install ENV NODE_ENV=production diff --git a/front/package.json b/front/package.json index a9c7b3f8..048c7099 100644 --- a/front/package.json +++ b/front/package.json @@ -4,6 +4,7 @@ "main": "index.js", "license": "SEE LICENSE IN LICENSE.txt", "devDependencies": { + "@types/google-protobuf": "^3.7.3", "@types/jasmine": "^3.5.10", "@typescript-eslint/eslint-plugin": "^2.26.0", "@typescript-eslint/parser": "^2.26.0", @@ -23,6 +24,7 @@ "@types/simple-peer": "^9.6.0", "@types/socket.io-client": "^1.4.32", "generic-type-guard": "^3.2.0", + "google-protobuf": "^3.13.0", "phaser": "^3.22.0", "queue-typescript": "^1.0.1", "simple-peer": "^9.6.2", diff --git a/front/src/Connection.ts b/front/src/Connection.ts index 459e58c1..0a05673a 100644 --- a/front/src/Connection.ts +++ b/front/src/Connection.ts @@ -7,7 +7,7 @@ import { SetPlayerDetailsMessage, UserMovedMessage, UserMovesMessage, ViewportMessage -} from "../../messages/generated/messages_pb" +} from "./Messages/generated/messages_pb" const SocketIo = require('socket.io-client'); import Socket = SocketIOClient.Socket; diff --git a/front/src/Messages/.gitignore b/front/src/Messages/.gitignore new file mode 100644 index 00000000..9e0adcc1 --- /dev/null +++ b/front/src/Messages/.gitignore @@ -0,0 +1 @@ +/generated/ diff --git a/front/src/Network/ProtobufClientUtils.ts b/front/src/Network/ProtobufClientUtils.ts index 6755025c..1eb5b923 100644 --- a/front/src/Network/ProtobufClientUtils.ts +++ b/front/src/Network/ProtobufClientUtils.ts @@ -1,4 +1,4 @@ -import {PositionMessage} from "../../../messages/generated/messages_pb"; +import {PositionMessage} from "../Messages/generated/messages_pb"; import {PointInterface} from "../Connection"; import Direction = PositionMessage.Direction; diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 8abddfd3..f971a1e3 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -40,7 +40,7 @@ import {FourOFourSceneName} from "../Reconnecting/FourOFourScene"; import {ItemFactoryInterface} from "../Items/ItemFactoryInterface"; import {ActionableItem} from "../Items/ActionableItem"; import {UserInputManager} from "../UserInput/UserInputManager"; -import {UserMovedMessage} from "../../../../messages/generated/messages_pb"; +import {UserMovedMessage} from "../../Messages/generated/messages_pb"; import {ProtobufClientUtils} from "../../Network/ProtobufClientUtils"; @@ -220,6 +220,7 @@ export class GameScene extends Phaser.Scene implements CenterListener { if (position === undefined) { throw new Error('Position missing from UserMovedMessage'); } + //console.log('Received position ', position.getX(), position.getY(), "from user", message.getUserid()); const messageUserMoved: MessageUserMovedInterface = { userId: message.getUserid(), @@ -1079,6 +1080,7 @@ export class GameScene extends Phaser.Scene implements CenterListener { // We do not update the player position directly (because it is sent only every 200ms). // Instead we use the PlayersPositionInterpolator that will do a smooth animation over the next 200ms. const playerMovement = new PlayerMovement({ x: player.x, y: player.y }, this.currentTick, message.position, this.currentTick + POSITION_DELAY); + //console.log('Target position: ', player.x, player.y); this.playersPositionInterpolator.updatePlayerPosition(player.userId, playerMovement); } diff --git a/front/src/Phaser/Game/PlayerMovement.ts b/front/src/Phaser/Game/PlayerMovement.ts index 1458335d..56c4f113 100644 --- a/front/src/Phaser/Game/PlayerMovement.ts +++ b/front/src/Phaser/Game/PlayerMovement.ts @@ -20,12 +20,13 @@ export class PlayerMovement { public getPosition(tick: number): HasMovedEvent { // Special case: end position reached and end position is not moving if (tick >= this.endTick && this.endPosition.moving === false) { + //console.log('Movement finished ', this.endPosition) return this.endPosition; } const x = (this.endPosition.x - this.startPosition.x) * ((tick - this.startTick) / (this.endTick - this.startTick)) + this.startPosition.x; const y = (this.endPosition.y - this.startPosition.y) * ((tick - this.startTick) / (this.endTick - this.startTick)) + this.startPosition.y; - + //console.log('Computed position ', x, y) return { x, y, diff --git a/front/yarn.lock b/front/yarn.lock index 2e7007e1..2da1afa2 100644 --- a/front/yarn.lock +++ b/front/yarn.lock @@ -66,6 +66,11 @@ "@types/minimatch" "*" "@types/node" "*" +"@types/google-protobuf@^3.7.3": + version "3.7.3" + resolved "https://registry.yarnpkg.com/@types/google-protobuf/-/google-protobuf-3.7.3.tgz#429512e541bbd777f2c867692e6335ee08d1f6d4" + integrity sha512-FRwj40euE2bYkG+0X5w2nEA8yAzgJRcEa7RBd0Gsdkb9/tPM2pctVVAvnOUTbcXo2VmIHPo0Ae94Gl9vRHfKzg== + "@types/html-minifier-terser@^5.0.0": version "5.1.0" resolved "https://registry.yarnpkg.com/@types/html-minifier-terser/-/html-minifier-terser-5.1.0.tgz#551a4589b6ee2cc9c1dff08056128aec29b94880" @@ -2237,6 +2242,11 @@ globby@^6.1.0: pify "^2.0.0" pinkie-promise "^2.0.0" +google-protobuf@^3.13.0: + version "3.13.0" + resolved "https://registry.yarnpkg.com/google-protobuf/-/google-protobuf-3.13.0.tgz#909c5983d75dd6101ed57c79e0528d000cdc3251" + integrity sha512-ZIf3qfLFayVrPvAjeKKxO5FRF1/NwRxt6Dko+fWEMuHwHbZx8/fcaAao9b0wCM6kr8qeg2te8XTpyuvKuD9aKw== + graceful-fs@^4.1.11, graceful-fs@^4.1.15, graceful-fs@^4.1.2: version "4.2.4" resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.4.tgz#2256bde14d3632958c465ebc96dc467ca07a29fb" diff --git a/messages/package.json b/messages/package.json index 6f2bc3f1..f7c73863 100644 --- a/messages/package.json +++ b/messages/package.json @@ -5,7 +5,10 @@ "main": "generated/src/proto/messages_pb.js", "scripts": { "proto": "protoc --plugin=\"protoc-gen-ts=./node_modules/.bin/protoc-gen-ts\" --js_out=\"import_style=commonjs,binary:generated\" --ts_out=\"generated\" messages.proto", - "proto:watch": "yarn run proto || inotifywait -q -m -e close_write messages.proto | while read -r filename event; do yarn run proto; done" + "copy-to-back": "rm -rf ../back/src/Messages/generated && cp -rf generated/ ../back/src/Messages/generated", + "copy-to-front": "rm -rf ../front/src/Messages/generated && cp -rf generated/ ../front/src/Messages/generated", + "proto-all": "yarn run proto && yarn run copy-to-back && yarn run copy-to-front", + "proto:watch": "yarn run proto-all; inotifywait -q -m -e close_write messages.proto | while read -r filename event; do yarn run proto-all; done" }, "repository": { "type": "git",