+/* A single player can have multiple connections, (think, multiple
+ * browser windows with a common session cookie). */
+class Player {
+ constructor(id, session_id, name, connection) {
+ this.id = id;
+ this.session_id = session_id;
+ this.name = name;
+ this.connections = [connection];
+ }
+
+ add_connection(connection) {
+ /* Don't add a duplicate connection if this player already has it. */
+ for (let c of this.connections) {
+ if (c === connection)
+ return;
+ }
+
+ this.connections.push(connection);
+ }
+
+ /* Returns the number of remaining connections after this one is removed. */
+ remove_connection(connection) {
+ this.connections.filter(c => c !== connection);
+ return this.connections.length;
+ }
+
+ /* Send a string to all connections for this player. */
+ send(data) {
+ this.connections.forEach(connection => connection.write(data));
+ }
+
+ info_json() {
+ return JSON.stringify({
+ id: this.id,
+ name: this.name,
+ });
+ }
+}
+