Switched group position to protobuf

This commit is contained in:
David Négrier 2020-09-24 10:05:16 +02:00
parent 76d3779438
commit b4f971c501
5 changed files with 38 additions and 22 deletions

View File

@ -27,7 +27,7 @@ import {
SetPlayerDetailsMessage,
SubMessage,
UserMovedMessage,
BatchMessage, GroupUpdateMessage, PointMessage
BatchMessage, GroupUpdateMessage, PointMessage, GroupDeleteMessage
} from "../../../messages/generated/messages_pb";
import {UserMovesMessage} from "../../../messages/generated/messages_pb";
import Direction = PositionMessage.Direction;
@ -573,7 +573,7 @@ export class IoSocketController {
clientListener.emit(SocketIoEvent.USER_LEFT, clientUser.userId);
//console.log("Sending USER_LEFT event");
} else if (thing instanceof Group) {
clientListener.emit(SocketIoEvent.GROUP_DELETE, thing.getId());
this.emitDeleteGroupEvent(clientListener, thing.getId());
} else {
console.error('Unexpected type for Movable.');
}
@ -584,10 +584,7 @@ export class IoSocketController {
// Dispatch groups position to newly connected user
world.getGroups().forEach((group: Group) => {
Client.emit(SocketIoEvent.GROUP_CREATE_UPDATE, {
position: group.getPosition(),
groupId: group.getId()
} as GroupUpdateInterface);
this.emitCreateUpdateGroupEvent(Client, group);
});
//join world
world.join(Client, Client.position);
@ -603,7 +600,23 @@ export class IoSocketController {
groupUpdateMessage.setGroupid(group.getId());
groupUpdateMessage.setPosition(pointMessage);
socket.emit(SocketIoEvent.GROUP_CREATE_UPDATE, groupUpdateMessage.serializeBinary().buffer);
const subMessage = new SubMessage();
subMessage.setGroupupdatemessage(groupUpdateMessage);
const client : ExSocketInterface = socket as ExSocketInterface;
emitInBatch(client, SocketIoEvent.GROUP_CREATE_UPDATE, subMessage);
//socket.emit(SocketIoEvent.GROUP_CREATE_UPDATE, groupUpdateMessage.serializeBinary().buffer);
}
private emitDeleteGroupEvent(socket: Socket, groupId: number): void {
const groupDeleteMessage = new GroupDeleteMessage();
groupDeleteMessage.setGroupid(groupId);
const subMessage = new SubMessage();
subMessage.setGroupdeletemessage(groupDeleteMessage);
const client : ExSocketInterface = socket as ExSocketInterface;
emitInBatch(client, SocketIoEvent.GROUP_DELETE, subMessage);
}
/**

View File

@ -90,9 +90,9 @@ export class World {
this.positionNotifier.updatePosition(user, userPosition, user.position);
const oldGroupPosition = user.group?.getPosition();
user.group?.updatePosition();
user.position = userPosition;
user.group?.updatePosition();
if (user.silent) {
return;

View File

@ -2,7 +2,7 @@ import Axios from "axios";
import {API_URL} from "./Enum/EnvironmentVariable";
import {MessageUI} from "./Logger/MessageUI";
import {
BatchMessage, GroupUpdateMessage,
BatchMessage, GroupDeleteMessage, GroupUpdateMessage,
PositionMessage,
SetPlayerDetailsMessage, UserMovedMessage,
UserMovesMessage,
@ -160,6 +160,12 @@ export class Connection implements Connection {
if (message.hasUsermovedmessage()) {
event = EventMessage.USER_MOVED;
payload = message.getUsermovedmessage();
} else if (message.hasGroupupdatemessage()) {
event = EventMessage.GROUP_CREATE_UPDATE;
payload = message.getGroupupdatemessage();
} else if (message.hasGroupdeletemessage()) {
event = EventMessage.GROUP_DELETE;
payload = message.getGroupdeletemessage();
} else {
throw new Error('Unexpected batch message type');
}
@ -230,7 +236,6 @@ export class Connection implements Connection {
if(!this.socket){
return;
}
const point = new Point(x, y, direction, moving);
const positionMessage = new PositionMessage();
positionMessage.setX(Math.floor(x));
positionMessage.setY(Math.floor(y));
@ -264,6 +269,8 @@ export class Connection implements Connection {
userMovesMessage.setPosition(positionMessage);
userMovesMessage.setViewport(viewportMessage);
//console.log('Sending position ', positionMessage.getX(), positionMessage.getY());
this.socket.emit(EventMessage.USER_POSITION, userMovesMessage.serializeBinary().buffer);
}
@ -301,16 +308,7 @@ export class Connection implements Connection {
}
public onGroupUpdatedOrCreated(callback: (groupCreateUpdateMessage: GroupCreatedUpdatedMessageInterface) => void): void {
// TODO: READ THIS FROM BINARY FORMAT
// TODO: READ THIS FROM BINARY FORMAT
// TODO: READ THIS FROM BINARY FORMAT
// TODO: CHANGE THIS EVENT TO BE PART OF THE BATCHES
// TODO: CHANGE THIS EVENT TO BE PART OF THE BATCHES
// TODO: CHANGE THIS EVENT TO BE PART OF THE BATCHES
// TODO: CHANGE THIS EVENT TO BE PART OF THE BATCHES
// TODO: CHANGE THIS EVENT TO BE PART OF THE BATCHES
this.socket.on(EventMessage.GROUP_CREATE_UPDATE, (buffer: ArrayBuffer) => {
const message = GroupUpdateMessage.deserializeBinary(new Uint8Array(buffer));
this.onBatchMessage(EventMessage.GROUP_CREATE_UPDATE, (message: GroupUpdateMessage) => {
const position = message.getPosition();
if (position === undefined) {
throw new Error('Missing position in GROUP_CREATE_UPDATE');
@ -321,12 +319,15 @@ export class Connection implements Connection {
position: position.toObject()
}
//console.log('Group position: ', position.toObject());
callback(groupCreateUpdateMessage);
});
}
public onGroupDeleted(callback: (groupId: number) => void): void {
this.socket.on(EventMessage.GROUP_DELETE, callback)
this.onBatchMessage(EventMessage.GROUP_DELETE, (message: GroupDeleteMessage) => {
callback(message.getGroupid());
});
}
public onConnectError(callback: (error: object) => void): void {

View File

@ -50,6 +50,8 @@ message UserMovedMessage {
message SubMessage {
oneof message {
UserMovedMessage userMovedMessage = 1;
GroupUpdateMessage groupUpdateMessage = 2;
GroupDeleteMessage groupDeleteMessage = 3;
}
}

View File

@ -5,7 +5,7 @@
"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": "inotifywait -q -m -e close_write messages.proto | while read -r filename event; do yarn run proto; done"
"proto:watch": "yarn run proto || inotifywait -q -m -e close_write messages.proto | while read -r filename event; do yarn run proto; done"
},
"repository": {
"type": "git",