summaryrefslogtreecommitdiff
path: root/engine/systems/NetworkUpdate.ts
blob: f4c8f9c3b8d0708f8b401a91b245531ac7701f80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { System, SystemNames } from '.';
import { Game } from '../Game';
import { ComponentNames } from '../components';
import {
  type MessageQueueProvider,
  type MessagePublisher,
  type MessageProcessor,
  MessageType,
  EntityUpdateBody
} from '../network';
import { stringify } from '../utils';

type EntityUpdateInfo = { timer: number; hash: string };

export class NetworkUpdate extends System {
  private queueProvider: MessageQueueProvider;
  private publisher: MessagePublisher;
  private messageProcessor: MessageProcessor;
  private entityUpdateInfo: Map<string, EntityUpdateInfo>;

  private nextPublishInterval: number;

  constructor(
    queueProvider: MessageQueueProvider,
    publisher: MessagePublisher,
    messageProcessor: MessageProcessor
  ) {
    super(SystemNames.NetworkUpdate);

    this.queueProvider = queueProvider;
    this.publisher = publisher;
    this.messageProcessor = messageProcessor;

    this.entityUpdateInfo = new Map();
    this.nextPublishInterval = 0;
  }

  public update(dt: number, game: Game) {
    // 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);
      }
    }

    // 1. process new messages
    this.queueProvider
      .getNewMessages()
      .forEach((message) => this.messageProcessor.process(message));
    this.queueProvider.clearMessages();

    // 2. send entity updates
    const updateMessages: EntityUpdateBody[] = [];

    // todo: figure out if we can use the controllable component to determine if we should publish an update
    game.forEachEntityWithComponent(
      ComponentNames.NetworkUpdateable,
      (entity) => {
        const newHash = stringify(entity.serialize());
        let updateInfo: EntityUpdateInfo = this.entityUpdateInfo.get(
          entity.id
        ) ?? {
          timer: this.getNextUpdateTimeMs(),
          hash: newHash
        };

        // update timer
        updateInfo.timer -= dt;
        this.entityUpdateInfo.set(entity.id, updateInfo);
        if (updateInfo.timer > 0) return;
        updateInfo.timer = entity.getNextUpdateInterval();
        this.entityUpdateInfo.set(entity.id, updateInfo);

        // maybe update if hash is not consitent
        if (updateInfo.hash == newHash) {
          return;
        }
        updateInfo.hash = newHash;
        this.entityUpdateInfo.set(entity.id, updateInfo);

        updateMessages.push({
          id: entity.id,
          args: entity.serialize()
        });
      }
    );

    if (updateMessages.length)
      this.publisher.addMessage({
        type: MessageType.UPDATE_ENTITIES,
        body: updateMessages
      });

    // 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();
    }
  }

  private getNextUpdateInterval(): number {
    return Math.random() * 30;
  }
}