2023-08-25 18:48:17 -04:00
|
|
|
import { System, SystemNames } from '.';
|
|
|
|
import { Game } from '../Game';
|
2023-09-02 16:40:46 -04:00
|
|
|
import { ComponentNames, NetworkUpdateable } from '../components';
|
2023-08-23 21:44:59 -04:00
|
|
|
import {
|
|
|
|
type MessageQueueProvider,
|
|
|
|
type MessagePublisher,
|
2023-08-26 19:55:27 -04:00
|
|
|
type MessageProcessor,
|
|
|
|
MessageType,
|
|
|
|
EntityUpdateBody
|
2023-08-25 18:48:17 -04:00
|
|
|
} from '../network';
|
2023-08-29 14:05:02 -04:00
|
|
|
import { stringify } from '../utils';
|
|
|
|
|
|
|
|
type EntityUpdateInfo = { timer: number; hash: string };
|
2023-08-13 18:47:58 -04:00
|
|
|
|
|
|
|
export class NetworkUpdate extends System {
|
2023-08-15 20:30:19 -04:00
|
|
|
private queueProvider: MessageQueueProvider;
|
|
|
|
private publisher: MessagePublisher;
|
2023-08-23 21:44:59 -04:00
|
|
|
private messageProcessor: MessageProcessor;
|
2023-08-29 14:05:02 -04:00
|
|
|
private entityUpdateInfo: Map<string, EntityUpdateInfo>;
|
2023-08-26 19:55:27 -04:00
|
|
|
|
2023-08-29 14:26:58 -04:00
|
|
|
private nextPublishInterval: number;
|
|
|
|
|
2023-08-15 20:30:19 -04:00
|
|
|
constructor(
|
|
|
|
queueProvider: MessageQueueProvider,
|
2023-08-23 21:44:59 -04:00
|
|
|
publisher: MessagePublisher,
|
2023-08-25 18:48:17 -04:00
|
|
|
messageProcessor: MessageProcessor
|
2023-08-15 20:30:19 -04:00
|
|
|
) {
|
2023-08-13 18:47:58 -04:00
|
|
|
super(SystemNames.NetworkUpdate);
|
2023-08-15 20:30:19 -04:00
|
|
|
|
|
|
|
this.queueProvider = queueProvider;
|
|
|
|
this.publisher = publisher;
|
2023-08-23 21:44:59 -04:00
|
|
|
this.messageProcessor = messageProcessor;
|
2023-08-26 19:55:27 -04:00
|
|
|
|
2023-08-29 14:05:02 -04:00
|
|
|
this.entityUpdateInfo = new Map();
|
2023-09-02 19:46:03 -04:00
|
|
|
this.nextPublishInterval = this.getNextUpdateInterval();
|
2023-08-13 18:47:58 -04:00
|
|
|
}
|
|
|
|
|
2023-08-26 19:55:27 -04:00
|
|
|
public update(dt: number, game: Game) {
|
2023-08-29 14:05:02 -04:00
|
|
|
// 0. remove unnecessary info for removed entities
|
|
|
|
const networkUpdateableEntities = game.componentEntities.get(
|
|
|
|
ComponentNames.NetworkUpdateable
|
|
|
|
);
|
|
|
|
for (const entityId of this.entityUpdateInfo.keys()) {
|
|
|
|
if (!networkUpdateableEntities?.has(entityId)) {
|
|
|
|
this.entityUpdateInfo.delete(entityId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-26 19:55:27 -04:00
|
|
|
// 1. process new messages
|
2023-08-23 21:44:59 -04:00
|
|
|
this.queueProvider
|
|
|
|
.getNewMessages()
|
|
|
|
.forEach((message) => this.messageProcessor.process(message));
|
2023-08-15 20:30:19 -04:00
|
|
|
this.queueProvider.clearMessages();
|
|
|
|
|
2023-08-26 19:55:27 -04:00
|
|
|
// 2. send entity updates
|
|
|
|
const updateMessages: EntityUpdateBody[] = [];
|
2023-08-29 14:05:02 -04:00
|
|
|
|
2023-08-15 20:30:19 -04:00
|
|
|
game.forEachEntityWithComponent(
|
|
|
|
ComponentNames.NetworkUpdateable,
|
|
|
|
(entity) => {
|
2023-09-02 16:40:46 -04:00
|
|
|
const networkUpdateableComponent =
|
|
|
|
entity.getComponent<NetworkUpdateable>(
|
|
|
|
ComponentNames.NetworkUpdateable
|
|
|
|
);
|
|
|
|
const nextUpdateTime = networkUpdateableComponent.getNextUpdateTime();
|
|
|
|
|
2023-08-29 14:05:02 -04:00
|
|
|
const newHash = stringify(entity.serialize());
|
|
|
|
let updateInfo: EntityUpdateInfo = this.entityUpdateInfo.get(
|
|
|
|
entity.id
|
|
|
|
) ?? {
|
2023-09-02 16:40:46 -04:00
|
|
|
timer: nextUpdateTime,
|
2023-08-29 14:05:02 -04:00
|
|
|
hash: newHash
|
|
|
|
};
|
|
|
|
|
|
|
|
// update timer
|
|
|
|
updateInfo.timer -= dt;
|
|
|
|
this.entityUpdateInfo.set(entity.id, updateInfo);
|
|
|
|
if (updateInfo.timer > 0) return;
|
2023-09-02 16:40:46 -04:00
|
|
|
updateInfo.timer = nextUpdateTime;
|
2023-08-29 14:05:02 -04:00
|
|
|
this.entityUpdateInfo.set(entity.id, updateInfo);
|
|
|
|
|
2023-09-02 16:40:46 -04:00
|
|
|
// maybe update, if hash is not consistent
|
|
|
|
if (updateInfo.hash == newHash) return;
|
2023-08-29 14:05:02 -04:00
|
|
|
updateInfo.hash = newHash;
|
|
|
|
this.entityUpdateInfo.set(entity.id, updateInfo);
|
|
|
|
|
|
|
|
updateMessages.push({
|
|
|
|
id: entity.id,
|
|
|
|
args: entity.serialize()
|
|
|
|
});
|
2023-08-25 18:48:17 -04:00
|
|
|
}
|
2023-08-15 20:30:19 -04:00
|
|
|
);
|
2023-08-29 14:05:02 -04:00
|
|
|
|
|
|
|
if (updateMessages.length)
|
|
|
|
this.publisher.addMessage({
|
|
|
|
type: MessageType.UPDATE_ENTITIES,
|
|
|
|
body: updateMessages
|
|
|
|
});
|
2023-08-23 21:44:59 -04:00
|
|
|
|
2023-08-29 14:26:58 -04:00
|
|
|
// 3. maybe publish changes - we don't want to overload the socket
|
|
|
|
this.nextPublishInterval -= dt;
|
|
|
|
if (this.nextPublishInterval < 0) {
|
|
|
|
this.publisher.publish();
|
|
|
|
this.nextPublishInterval = this.getNextUpdateInterval();
|
|
|
|
}
|
2023-08-15 20:30:19 -04:00
|
|
|
}
|
2023-08-26 19:55:27 -04:00
|
|
|
|
2023-09-02 16:40:46 -04:00
|
|
|
private getNextUpdateInterval() {
|
|
|
|
return Math.random() * 30 + 20;
|
2023-08-26 19:55:27 -04:00
|
|
|
}
|
2023-08-13 18:47:58 -04:00
|
|
|
}
|