Migrating user position messages to protobuf
This commit is contained in:
parent
e9ca8721a6
commit
df0636c513
10 changed files with 202 additions and 46 deletions
|
@ -3,6 +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";
|
||||
|
||||
export interface ExSocketInterface extends Socket, Identificable {
|
||||
token: string;
|
||||
|
@ -18,7 +19,7 @@ export interface ExSocketInterface extends Socket, Identificable {
|
|||
/**
|
||||
* Pushes an event that will be sent in the next batch of events
|
||||
*/
|
||||
emitInBatch: (event: string | symbol, payload: unknown) => void;
|
||||
batchedMessages: Array<{ event: string | symbol, payload: unknown }>;
|
||||
emitInBatch: (event: string, payload: SubMessage) => void;
|
||||
batchedMessages: BatchMessage;
|
||||
batchTimeout: NodeJS.Timeout|null;
|
||||
}
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
import {PointInterface} from "./PointInterface";
|
||||
|
||||
export class MessageUserMoved {
|
||||
constructor(public userId: number, public position: PointInterface) {
|
||||
}
|
||||
}
|
35
back/src/Model/Websocket/ProtobufUtils.ts
Normal file
35
back/src/Model/Websocket/ProtobufUtils.ts
Normal file
|
@ -0,0 +1,35 @@
|
|||
import {PointInterface} from "./PointInterface";
|
||||
import {PositionMessage} from "../../../../messages/generated/messages_pb";
|
||||
import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
|
||||
|
||||
export namespace ProtobufUtils {
|
||||
import Direction = PositionMessage.Direction;
|
||||
|
||||
export function toPositionMessage(point: PointInterface): PositionMessage {
|
||||
let direction: PositionMessage.DirectionMap[keyof PositionMessage.DirectionMap];
|
||||
switch (point.direction) {
|
||||
case 'up':
|
||||
direction = Direction.UP;
|
||||
break;
|
||||
case 'down':
|
||||
direction = Direction.DOWN;
|
||||
break;
|
||||
case 'left':
|
||||
direction = Direction.LEFT;
|
||||
break;
|
||||
case 'right':
|
||||
direction = Direction.RIGHT;
|
||||
break;
|
||||
default:
|
||||
throw new Error('unexpected direction');
|
||||
}
|
||||
|
||||
const position = new PositionMessage();
|
||||
position.setX(point.x);
|
||||
position.setY(point.y);
|
||||
position.setMoving(point.moving);
|
||||
position.setDirection(direction);
|
||||
|
||||
return position;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue