Reducing Binary Message Size (amazing)

8 Jun 2022, 06:02

Hi, I'm building a game like Snake.io or Slither.io using colyseus & phaser.

The binary message size per message is increasing around 1Kb only with one player.
https://imgur.com/najWpxN.png

Here are the schema classes. Please help me regarding ways to improve this.

export class MyRoomState extends Schema {
  @type({ map: PlayerState })
  players: MapSchema<PlayerState> = new MapSchema<PlayerState>();

  @type({ map: Food })
  foodItems: MapSchema<Food> = new MapSchema<Food>();
}
export class PlayerState extends Schema {
  @type('string')
  publicAddress: string;

  @type('string')
  sessionId: string;

  @type('int16')
  x: number;

  @type('int16')
  y: number;

  @type('int16')
  score: number;

  @type('int16')
  angle: number = 0;

  @type('int8')
  snakeLength: number = 0;

  @type([SnakeSection])
  sections: ArraySchema<SnakeSection>;
export class SnakeSection extends Schema {
  constructor(x: number, y: number) {
    super();
    this.x = x;
    this.y = y;
  }

  @type('int16')
  x: number;

  @type('int16')
  y: number;

  setTo(x: number, y: number) {
    this.x = x;
    this.y = y;
  }
}

8 Jun 2022, 08:58

There must be large optimization, let me have a try.

8 Jun 2022, 09:09

@coco Thanks! Let me know if you need anything.

8 Jun 2022, 12:01

@lambrohan

OK, so I got a message about 7 Bytes per frame, that's pretty good I think.
0_1654689136053_4927b1e9-8356-4fad-b815-22540b611ed2-image.png

I'll share my test project for you and others who are insterested about it here:
https://github.com/CocosGames/bandwidth_test_colyseus_defold/tree/main/test

Cheers.

8 Jun 2022, 12:15

Dude, you're a saviour! I'll start implementing this right away.

8 Jun 2022, 16:31

As the players increase this size will keep on increasing. So as players increase and this snake sections increase the size will also increase. Will it be able to manage 500 concurrent players with such approach?

8 Jun 2022, 17:23

I don't think you need to transfer positions of all sections of a snake -- they can be computed by both server and client side.

9 Jun 2022, 08:17

@coco Yep just did that refractor & got size to 6 bytes per player.

9 Jun 2022, 08:48

Good job!