summaryrefslogtreecommitdiff
path: root/frontend/src/models/players.js
blob: 2af6b0f729595786a911b0b24374277f2208562e (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
// @flow
import { Map, Record } from 'immutable';

export type PlayerShape = {
  username: string,
  displayName: string,
  index: number,
  ready: boolean
};
export type PlayerType = Record<PlayerShape>;

const PlayerRecord: PlayerType = Record({
  username: null,
  displayName: null,
  index: 0,
  ready: false,
});
// $FlowFixMe
export class Player extends PlayerRecord {}

export type PlayersShape = {
  all: Map<string, PlayerType>,
  current: string
};
export type PlayersType = Record<PlayersShape>;

const PlayersRecord: PlayersType = Record({
  all: Map(),
  current: '',
});
// $FlowFixMe
export class PlayerState extends PlayersRecord {
  addPlayer(p: PlayerShape) {
    const player: Player = new Player(p);
    const playerMap = Map({ [player.username]: player });
    return this.addPlayers(playerMap).set('current', player.username);
  }

  addPlayers(p: Map<string, PlayerShape>) {
    const players: Map<string, PlayerShape> = Map(p);
    return this.mergeIn(['all'], players.map((player: PlayerShape): Player => new Player(player)));
  }
}
bgstack15