Migrating messages locally into back and front

This commit is contained in:
David Négrier 2020-09-24 11:16:08 +02:00
parent b4f971c501
commit d7209d8864
18 changed files with 46 additions and 14 deletions

View File

@ -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

View File

@ -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",

View File

@ -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";

1
back/src/Messages/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/generated/

View File

@ -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;

View File

@ -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;

View File

@ -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. */

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -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",

View File

@ -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;

1
front/src/Messages/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/generated/

View File

@ -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;

View File

@ -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);
}

View File

@ -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,

View File

@ -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"

View File

@ -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",