-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.js
114 lines (113 loc) · 3.65 KB
/
player.js
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
/**
* @file player.js
* @author Bryan Hazelbaker <[email protected]>
*
* @copyright Copyright (c) 2013 Bryan Hazelbaker <[email protected]>
* Released under the MIT license. Read the entire license located in the
* project root or at http://opensource.org/licenses/mit-license.php
*
* @brief Store and retrieve player information.
*
* @see https://github.com/delphian/astro-empires-javascript-library
*/
/**
* @class AstroEmpires.Player
*
* @brief Keeps track of players.
*
* @param AstroEmpires.AE ae
* The instantiated ae object these players are associated with.
*/
AstroEmpires.Player = function(ae) {
this.ae = ae;
Observable.call(this);
// All players.
this.players = {};
// Individual message properties.
// (int) Unique player identifier.
this.id = null;
// (string) Player name;
this.name = null;
// (int) Guild identifier, if any.
this.guild = null;
// (int) Age of the account.
this.age = null;
// (string) Economic income per hour.
this.economy = null;
// Player rank.
this.rank = null;
// Player level.
this.level = null;
};
/**
* Make the player observable.
*/
AstroEmpires.Player.prototype = new Observable();
/**
* Determine if a player identifier already exists or not.
*
* @param int playerId
* The unique message identifier.
*
* @return bool
* true if the message already exists, false otherwise.
*/
AstroEmpires.Player.prototype.exists = function(playerId) {
var exists = false;
if (typeof(this.players[playerId]) != 'undefined') {
exists = true;
}
return exists;
};
/**
* Make the requested player the current player.
*
* @return AstroEmpires.Player|false
* AstroEmpires.Msg on success, false on not found.
*/
AstroEmpires.Player.prototype.get = function(playerId) {
if (this.exists(playerId)) {
this.id = this.players[playerId].id;
this.name = this.players[playerId].name;
this.guild = typeof(this.players[playerId].guild) == 'undefined' ? null : this.players[playerId].guild;
this.rank = typeof(this.players[playerId].rank) == 'undefined' ? null : this.players[playerId].rank;
this.level = typeof(this.players[playerId].level) == 'undefined' ? null : this.players[playerId].level;
this.economy = typeof(this.players[playerId].economy) == 'undefined' ? null : this.players[playerId].economy;
this.age = typeof(this.players[playerId].age) == 'undefined' ? null : this.players[playerId].age;
return this;
}
return false;
};
/**
* Add or update a player.
*
* If the player already exists then properties that exist in the paramter
* will be updated. The values of properties in the existing object will not
* be changed if they are not specified in the paramter object.
*
* @param object player
* The player object consisting of:
* 'id': (int) The player id.
* 'time': (int) Unix timestamp.
* 'name': (string) Player name.
* 'guild': (int) Guild identifier.
*
* @return AstroEmpires.Player|false
* Updated AstroEmpires.Player on success, false on failure to set.
*/
AstroEmpires.Player.prototype.set = function(player) {
var success = false;
this.publish(player, 'pre_set', this);
if (typeof(player) != 'undefined' && player.id) {
for(index in player) {
// First create the player object if its not there.
if (!this.exists(player.id)) {
this.players[player.id] = {};
}
this.players[player.id][index] = player[index];
}
success = true;
this.publish(player, 'post_set', this);
}
return (success) ? this.get(player.id) : false;
};