This repository was archived by the owner on Nov 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.js
184 lines (160 loc) · 5.03 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
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
const util = require("./utilities");
const $s = require("./simple-seconds");
module.exports = function (config, db) {
function IdlePlayer() {
var name, pass, clazz; // player name, password, class
var online = false; // Is the user online?
var level = 1, next = config.base, idled; // Current level, time until next level, total time idled
var penalties = 0;
var lastLogin; // last login time
var items = {}; // Current equipment
util.item_types.forEach(type => items[type] = 0);
var users = []; // Keep a record of identified users
var stored = false;
// *** Method declarations
this.update = function update(time) {
// Is owner online and idling?
if (!online) return false;
idled += time;
return this.adjust(time);
}
this.isOnline = function isOnline() {
return online;
}
this.getName = function getName() {
return name;
}
this.getNext = function getNext() {
return next;
}
this.getClass = function getClass() {
return clazz;
}
this.getLevel = function getLevel() {
return level;
}
this.getItem = function getItem(type) {
return items[type];
}
this.setItem = function setItem(type, level) {
if (items.hasOwnProperty(type)) items[type] = level;
}
this.getItemCount = function getItemCount() {
var count = 0;
util.forEach(items, (key, val) => count += val);
return count;
}
this.isPassword = function isPassword(password) {
// TODO: crypt password
return password === pass;
}
this.setPassword = function (password) {
// TODO: crypt password
pass = password;
};
// Mark as online
this.login = function login(server_nick) {
// Already online?
if (online && users.includes(server_nick)) {
return false;
}
online = true;
lastLogin = $s.time();
users.push(server_nick);
return true;
}
this.logout = function logout(server_nick) {
if (!server_nick || !users.includes(server_nick)) return online;
users.splice(users.indexOf(uid));
return online = users.length > 0;
}
this.getUsers = function () {
return users;
};
// Penalize a user for X base * variable amount
this.penalize = function penalize(time) {
time = Math.floor(time * Math.pow(config.pStep, level));
var limit = config.penaltyLimit;
if (limit) time = Math.min(time, limit);
next += time;
penalties += time;
return time;
}
// +time = lower time, -time = increase time
// Return true if leveled
this.adjust = function adjust(time) {
next -= time;
if (next <= 0) {
next += Math.floor(config.base * Math.pow(config.step, level++));
return true;
}
return false;
}
this._load = function _load(data) {
// Don't load if there's no data, or a name is already set
if (!data || name) return this;
if (data.name) name = data.name;
if (data.pass) pass = data.pass;
if (data.clazz) clazz = data.clazz;
if (data.online) online = data.online;
if (data.level) level = data.level;
if (data.next) next = data.next;
if (data.idled) idled = data.idled;
if (data.penalties) penalties = data.penalties;
if (data.lastLogin) lastLogin = data.lastLogin;
if (data.items) {
// Loop through item keys, only set what exists
var $items = JSON.parse(data.items);
Object.keys($items).forEach(function (item) {
this.setItem(item, $items[item]);
}, this);
}
if (data.fromDB) stored = true;
return this;
}
this.isStored = function() {
return stored;
};
this.save = function save() {
return {
$name: name,
$pass: pass,
$class: clazz,
$online: online,
$level: level,
$next: next,
$idled: idled,
$penalties: penalties,
$lastLogin: lastLogin,
$itemSum: this.getItemCount(),
$items: JSON.stringify(items)
};
}
}
IdlePlayer.prototype.toString = function() {
var types = {
helm: "Helmet",
shirt: "Shirt",
pants: "Pants",
shoes: "Shoes",
gloves: "Gloves",
weapon: "Weapon",
shield: "Shield",
ring: "Ring",
amulet: "Amulet",
charm: "Charm",
};
var items = [];
util.forEach(types, (key, val) => items.push(`${val}(${this.getItem(key)})`), this);
return `${this.getName()}(${this.getLevel()}) the ${this.getClass()}, will level in ${util.duration(player.getNext())}. Items(${this.getItemCount()}): ${items.join(", ")}`;
};
IdlePlayer.createPlayer = function (data) {
if (!data) return null;
return new IdlePlayer()._load(data);
};
IdlePlayer.newPlayer = function (name, password, $class) {
// TODO: Crypt password
return new IdlePlayer()._load({name: name, pass: password, clazz: $class});
};
return IdlePlayer;
};