-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
96 lines (79 loc) · 2.08 KB
/
index.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
var initial = require('./config/default.json');
var hpManager = function (config){
this.db = config.db;/* || throw new Error("DB information not found.");*/
this.hpKey = config.hpKey || initial.hpKey;
this.max = config.max || initial.max;
this.min = config.min || initial.min;
};
hpManager.prototype.getMax = function (){
return this.max;
};
hpManager.prototype.getMin = function (){
return this.min;
};
hpManager.prototype.attack = function (key,val){
val = val || initial.attack;
var hpObj = this.db.get(this.hpKey) || {};
hpObj[key] = hpObj[key] || this.max;
hpObj[key] = Math.max(hpObj[key] - val, this.min);
this.db.set(this.hpKey,hpObj);
return hpObj[key];
};
hpManager.prototype.care = function (key,val){
val = val || initial.care;
var hpObj = this.db.get(this.hpKey) || {};
hpObj[key] = hpObj[key] || this.max;
hpObj[key] = Math.min(hpObj[key] + 10, this.max);
this.db.set(this.hpKey,hpObj);
return hpObj[key];
};
hpManager.prototype.full_care = function (key){
key = key || '';
var hpObj = this.db.get(this.hpKey) || {};
if(key === ''){
for(var name in hpObj){
hpObj[name] = this.max;
}
this.db.set(this.hpKey,hpObj);
return hpObj;
} else{
hpObj[key] = hpObj[key] || this.max;
hpObj[key] = this.max;
this.db.set(this.hpKey,hpObj);
var data = {};
data[key] = hpObj[key];
return data;
}
return {};
};
hpManager.prototype.status = function (key){
var status = new Array();
key = key || '';
var listObj = this.db.get(this.hpKey) || {};
if(key === ''){
return listObj;
} else{
if(listObj[key]){
var data = {};
data[key] = listObj[key];
return data;
} else {
listObj[key] = this.max;
this.db.set(this.hpKey,listObj);
var data = {};
data[key] = listObj[key];
return data;
}
}
return {};
};
hpManager.prototype.delete = function (key){
key = key || '';
var listObj = this.db.get(this.hpKey) || {};
if(listObj[key]){
delete listObj[key];
this.db.set(this.hpKey,listObj);
}
return listObj;
};
module.exports = hpManager;