summaryrefslogtreecommitdiff
path: root/frontend/src/models/players.js
blob: 5f7a4d70c4822440743dad662b39638360c40ec0 (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: new Map(),
  current: '',
});
// $FlowFixMe
export default class PlayerState extends PlayersRecord {
  addPlayer(p: PlayerShape) {
    const player: Player = new Player(p);
    const playerMap = new Map(({ [player.username]: player }: { [key: string]: Player }));
    return this.addPlayers(playerMap).set('current', player.username);
  }

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