bf070c33b5
The ts-proto lib has the huge advantage of producing code the "Typescript" way and not the "Java" way. In particular, for "oneof" types in protobuf, it is generating "ADT" that can be used to check if we forgot or not to deal with a type.
34 lines
1.1 KiB
TypeScript
34 lines
1.1 KiB
TypeScript
import { PositionMessage, PositionMessage_Direction } from "../Messages/ts-proto-generated/messages";
|
|
|
|
import type { PointInterface } from "../Connexion/ConnexionModels";
|
|
|
|
export class ProtobufClientUtils {
|
|
public static toPointInterface(position: PositionMessage): PointInterface {
|
|
let direction: string;
|
|
switch (position.direction) {
|
|
case PositionMessage_Direction.UP:
|
|
direction = "up";
|
|
break;
|
|
case PositionMessage_Direction.DOWN:
|
|
direction = "down";
|
|
break;
|
|
case PositionMessage_Direction.LEFT:
|
|
direction = "left";
|
|
break;
|
|
case PositionMessage_Direction.RIGHT:
|
|
direction = "right";
|
|
break;
|
|
default:
|
|
throw new Error("Unexpected direction");
|
|
}
|
|
|
|
// sending to all clients in room except sender
|
|
return {
|
|
x: position.x,
|
|
y: position.y,
|
|
direction,
|
|
moving: position.moving,
|
|
};
|
|
}
|
|
}
|