OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 |
| 6 /** |
| 7 * @fileoverview Keeps track of all the existing |
| 8 * PlayerProperty objects and is the entry-point for messages from the backend. |
| 9 */ |
| 10 var PlayerManager = (function () { |
| 11 "use strict"; |
| 12 |
| 13 function PlayerManager() { |
| 14 this.players = {}; |
| 15 this.renderman = new RenderManager(this); |
| 16 |
| 17 var removeCheckbox = document.querySelector("#removal-checkbox"); |
| 18 this.shouldRemovePlayer = function () { |
| 19 return removeCheckbox.checked; |
| 20 }; |
| 21 removeCheckbox.onclick = function () { |
| 22 if (removeCheckbox.checked) { |
| 23 goog.object.forEach(this.players, function (playerInfo, id) { |
| 24 if (playerInfo.toRemove) { |
| 25 this.removePlayer(id); |
| 26 } |
| 27 }, this); |
| 28 } |
| 29 }.bind(this); |
| 30 } |
| 31 |
| 32 |
| 33 /** |
| 34 * Adds a player to the list of players to manage. |
| 35 */ |
| 36 PlayerManager.prototype.addPlayer = function (id) { |
| 37 if (this.players[id]) { |
| 38 return; |
| 39 } |
| 40 // Make the PlayerProperty and add it to the mapping |
| 41 this.players[id] = new PlayerInfo(id); |
| 42 |
| 43 this.renderman.redrawList(); |
| 44 }; |
| 45 |
| 46 PlayerManager.prototype.removePlayer = function (id) { |
| 47 if (this.shouldRemovePlayer()) { |
| 48 delete this.players[id]; |
| 49 } else if (this.players[id]) { |
| 50 this.players[id].toRemove = true; |
| 51 } |
| 52 this.renderman.redrawList(); |
| 53 }; |
| 54 |
| 55 |
| 56 PlayerManager.prototype.selectPlayer = function (id) { |
| 57 if (!this.players[id]) { |
| 58 throw new Error("[selectPlayer] Id " + id + " does not exist."); |
| 59 } |
| 60 |
| 61 this.renderman.select(id); |
| 62 }; |
| 63 |
| 64 PlayerManager.prototype.updatePlayerInfoNoRecord = |
| 65 function (id, timestamp, key, value) { |
| 66 if (!this.players[id]) { |
| 67 console.error("[updatePlayerInfo] Id " + id + |
| 68 " does not exist"); |
| 69 return; |
| 70 } |
| 71 |
| 72 this.players[id].addPropertyNoRecord(timestamp, key, value); |
| 73 |
| 74 |
| 75 // If we can potentially rename the player, do so. |
| 76 if (key === 'name' || key === 'url') { |
| 77 this.renderman.redrawList(); |
| 78 } |
| 79 |
| 80 this.renderman.update(); |
| 81 }; |
| 82 |
| 83 PlayerManager.prototype.updatePlayerInfo = |
| 84 function (id, timestamp, key, value) { |
| 85 if (!this.players[id]) { |
| 86 console.error("[updatePlayerInfo] Id " + id + |
| 87 " does not exist"); |
| 88 return; |
| 89 } |
| 90 |
| 91 this.players[id].addProperty(timestamp, key, value); |
| 92 |
| 93 |
| 94 // If we can potentially rename the player, do so. |
| 95 if (key === 'name' || key === 'url') { |
| 96 this.renderman.redrawList(); |
| 97 } |
| 98 |
| 99 this.renderman.update(); |
| 100 }; |
| 101 |
| 102 return PlayerManager; |
| 103 }()); |
OLD | NEW |