-
Notifications
You must be signed in to change notification settings - Fork 1
/
player.js
104 lines (97 loc) · 2.07 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
const fetch = require('./fetch');
class Player {
/**
* Create a new Pactify player store
* @param {string} id User ID
* @param {boolean} [partial] Wether the player is partial
*/
constructor(id, partial = true) {
/**
* @type {string} User ID
*/
this.id = id;
/**
* @type {boolean} Wether the player is partial
*/
this.partial = partial;
/**
* @type {string} Username
*/
this.name;
/**
* @type {Date} Player registration date
*/
this.registrationDate;
/**
* @type {Date} Last player activity date
*/
this.lastActivityDate;
/**
* @type {Date} Last faction activity date - optionnal
*/
this.factionLastActivityDate;
/**
* @type {number} activityTime
*/
this.activityTime;
/**
* @type {string} Player rank - optionnal
*/
this.rank;
/**
* @type {number} Player power - optionnal
*/
this.power;
/**
* @type {string} Player role - optionnal
*/
this.role;
/**
* @type {import('./faction')} Player faction - optionnal
*/
this.faction;
/**
* @type {boolean} Wether the player is online
*/
this.online;
/**
* @type {string} Server where the player is connected - optionnal
*/
this.onlineServer;
}
/**
* Fetch player infos
*/
async fetch() {
const result = await fetch.playerInfos(this.id);
this._loadObject(result);
this.partial = false;
}
/**
* Assign result object to Player
* @param {object} result
*/
_loadObject(result) {
for (let key of Object.keys(result)) {
if (key !== 'statusCode' && key !== 'error')
this[key] = result[key];
}
if (this.faction) {
this.faction = new (require('./faction'))(this.faction.id, true);
this.faction._loadObject(result.faction);
}
}
}
/**
* Get a player by his name
* @returns {Promise<Player>}
*/
async function fromName(username) {
const playerID = await fetch.searchPlayer(username);
const result = await fetch.playerInfos(playerID.current);
const player = new Player(playerID.current, false);
player._loadObject(result);
return player;
}
Player.fromName = fromName;
module.exports = Player;