server for my chess game
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { pick } from "./utils.js";

/**
 * Creates data
 *
 * @param {WebSocket} ws Clients websocket
 * @param {String} id Clients id
 * @param {String} name Clients name
 * @param {String} country Clients country
 * @return {Object}
 */
function create_data(ws, id, name, country) {
  return {
    name: name,
    id: id,
    ws: ws,
    country: country,
  };
}

/**
 * Creates empty data
 *
 * @return {Object}
 */
function create_empty_data() {
  return {
    empty: true,
  };
}

/**
 * The clients class
 * Used by the `Game` class
 * Acts like a array
 *
 * @export
 * @class Clients
 */
export class Clients {
  /**
   * Creates an instance of `Clients`.
   * @param {WebSocket} ws The websocket that hosted
   * @param {String} id Hosters id
   * @param {String} name Hosters name
   * @param {String} country Hosters country
   * @param {Boolean} is_white Is hoster white
   * @memberof Clients
   * @constructor
   */
  constructor(ws, id, name, country, is_white) {
    this.add(ws, name, id, country, is_white);
    if (is_white) this.b = create_empty_data();
    else this.w = create_empty_data();
  }

  /**
   * Gets the websocket of a color
   *
   * @param {String} color The color
   * @return {WebSocket} The websocket
   * @memberof Clients
   */
  get_ws(color) {
    return color === "w" ? this.w.ws : this.b.ws;
  }

  /**
   * Gets the info of a color
   *
   * @param {String} color The color
   * @return {Object} The info
   * @memberof Clients
   */
  get_info(color) {
    return pick(color === "w" ? this.w : this.b, "name", "country");
  }

  /**
   * Returns a client list
   *
   * @return {WebSocket[]} The client list
   * @memberof Clients
   */
  get client_list() {
    return [this.get_ws("w"), this.get_ws("b")];
  }

  /**
   * Gets the number of players
   * players !== number of clients
   *
   * @return {Number} the number of players
   * @memberof Clients
   */
  get players() {
    let players = 0;
    if (!this.w.empty) players++;
    if (!this.b.empty) players++;
    return players;
  }

  /**
   * Gets the number of clients
   * clients !== number of players
   *
   * @return {Number} the number of clients
   * @memberof Clients
   */
  get length() {
    let client_count = 0;
    if (this.w.ws != undefined) client_count++;
    if (this.b.ws != undefined) client_count++;
    return client_count;
  }

  /**
   * Gets the info of white & black
   *
   * @return {Object}
   * @memberof Clients
   */
  get_all_info() {
    return { w: this.get_info("w"), b: this.get_info("b") };
  }

  /**
   * Adds a client
   *
   * @param {WebSocket} ws Client ws
   * @param {String} name Client name
   * @param {String} id Client id
   * @param {String} country Client country
   * @param {Boolean} [is_white=this.w.empty]
   * @memberof Clients
   */
  add(ws, name, id, country, is_white = this.w.empty) {
    if (is_white) this.w = create_data(ws, id, name, country);
    else this.b = create_data(ws, id, name, country);
  }

  /**
   * Is the client of color alive?
   *
   * @param {String} color The color
   * @return {Boolean} aliveness
   * @memberof Clients
   */
  alive(color) {
    return color === "w" ? !!this.w.ws : !!this.b.ws;
  }

  /**
   * Checks the color of a client
   * Employs fake function overloading
   *
   * @param {(String|WebSocket)} nameorws Clients name, or websocket
   * @param {(String|undefined)} country Clients country
   * @param {(String|undefined)} id Clients id
   * @return {*}
   * @memberof Clients
   */
  color_of(nameorws, country, id) {
    if (typeof nameorws == "string") {
      // if you check name, country AND id, even if there are duplicate ids (impossible) it wont have problems
      function check(on) {
        // if on doesnt have a name property, this will not error, because js
        return on.name === nameorws && on.country === country && on.id === id;
      }
      if (check(this.w)) return "w";
      else if (check(this.b)) return "b";
    } else if (typeof nameorws === "object") {
      if (this.w.ws === nameorws) return "w";
      if (this.b.ws === nameorws) return "b";
    }
  }

  /**
   * Erase the client
   *
   * @param {WebSocket} client The client to erase
   * @memberof Clients
   */
  erase(client) {
    if (this.w.ws === client) this.w.ws = undefined;
    else if (this.b.ws === client) this.b.ws = undefined;
  }
}