Skip to content

III.A. The Player

Polo Moine edited this page Apr 24, 2020 · 1 revision

Here you will learn how to customize the Player class by overriding it.

First thing first, make sure you have done the quick start. Now that you are all set, we will create our new Player class!

Here we will do everything in Typescript but you can easily change it in JavaScript.

// player.custom.ts

import { Player } from '@smallprod/game-networking-mm';
import { Socket } from 'net';

export default class MyCustomPlayer extends Player {
  constructor(socket: Socket) {
    super(socket);
    this.registerEventsCustom();
  }

  protected registerEventsCustom = () => {
    this.on('find_match', this.onFindMatchCustom);
    this.on('cancel_find_match', this.onCancelFindMatchCustom);
    this.on('create_match', this.onCreateMatchCustom);
    this.on('join_match', this.onJoinMatchCustom);
    this.on('refuse_match', this.onRefuseMatchCustom);
    this.on('ready_match', this.onReadyMatchCustom);
    this.on('leave_match', this.onLeaveMatchCustom);
    this.on('disconnection', this.onDisconnectCustom);
  };

  protected onFindMatchCustom = (data: any) => {};

  protected onCancelFindMatchCustom = (data: any) => {};

  protected onCreateMatchCustom = (data: any) => {};

  protected onJoinMatchCustom = (data: any) => {};

  protected onRefuseMatchCustom = (data: any) => {};

  protected onReadyMatchCustom = (data: any) => {};

  protected onLeaveMatchCustom = (data: any) => {};

  protected onDisconnectCustom = () => {};
}

What we just did is not very usefull as it is because nothing will work any longer as we just override all the events of the player but you can see here that you can customize the treatment for every action the player does.

We will now see that now that we did that, it's very easy to add new events for the player:
Let's just add this method in our new class:

protected onAuth = (data: any) => {
    const token = data.token;
    if (token) {
      // Check in a database for exemple
      this.send('auth_success', { user: null });
    } else {
      this.send('auth_error', { infos: 'no token' });
    }
  };

This method is supposed to authenticate the player by checking into a database.
The interesting things here are the data parameter which has an any type but in fact, the type depends on the event fired, here we assumed that data contains a token attribute. We can also see that we use this.send() method, this method will send a message to the current player with a type and some data.

Even though we had a real database and everything, it will not work for now has our method will never be called. To fix this, we have to add this line in our registerEventsCustom method:

this.on('user_auth', this.onAuth);

With this line we are linking our new method to the event called user_auth.

The thing is... we are not done yet, for now, we never said that we wanted to use our new MyCustomPlayer class instead of the old Player class. To fix this, we can just add the following line just before starting the servers:

import MatchMaking from '@smallprod/game-networking-mm';
import { Socket } from 'net';
import MyCustomPlayer from './player.custom';

MatchMaking.manager.setClientCreator(
  (socket: Socket) => new MyCustomPlayer(socket),
);

Now it should work, if, from your client (ie. your game) you send a message with the type user_auth.

Perfect, you just made your own player!!

Clone this wiki locally