-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommonaccount.js
177 lines (152 loc) · 6.04 KB
/
commonaccount.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
var logutil = require("./logutil");
var pppoeutil = require("./pppoeutil");
var events = require('events');
var CommonAccount = function (user, source) {
this.user = user;
this.source = source;
this.createdTime = new Date();
this.consumeHistory = [];
this.capability = {
consume: true,
leverage: 2.375,
schedule: false
};
//events.EventEmitter.call(this);
};
CommonAccount.prototype.__proto__ = events.EventEmitter.prototype;
CommonAccount.prototype.createdTime = null;
CommonAccount.prototype.user = "";
CommonAccount.prototype.password = "";
CommonAccount.prototype.tradePassword = "";
CommonAccount.prototype.source = "";
CommonAccount.prototype.cookieJar = null;
CommonAccount.prototype.loginTime = null;
CommonAccount.prototype.loginExtendInterval = 5 * 60 * 1000;
CommonAccount.prototype.loginExtendedTime = null;
CommonAccount.prototype.locked = false;
CommonAccount.prototype.interestLevelMin = 100;
CommonAccount.prototype.interestLevelMax = 100;
CommonAccount.prototype.availableBalance = 0;
CommonAccount.prototype.reservedBalance = 0;
CommonAccount.prototype.stopConsumeBalance = 0;
CommonAccount.prototype.pricePerBidMin = 0;
CommonAccount.prototype.pricePerBidMax = 10000;
CommonAccount.prototype.lastConsumingTime = null;
CommonAccount.prototype.consumeHistory = null;
CommonAccount.prototype.infoUpdateTime = null;
CommonAccount.prototype.scheduleObj = null;
CommonAccount.prototype.capability = null;
CommonAccount.prototype.config = function (obj) {
var leverageChanged = obj.capability.leverage !== this.capability.leverage;
for (var att in obj) {
if (obj[att] instanceof Object && this[att] instanceof Object) {
for (var subatt in obj[att]) {
this[att][subatt] = obj[att][subatt];
}
} else {
this[att] = obj[att];
}
}
if (leverageChanged && this.scheduleObj && this.scheduleObj.transferingTotal === 0) {
delete this.scheduleObj;
}
return this;
};
CommonAccount.prototype.loggedIn = function () {
return !!this.cookieJar;
}
CommonAccount.prototype.JSONInfo = function () {
return {
user: this.user,
loginTime: this.loginTime ? this.loginTime.getTime() : "",
availableBalance: this.availableBalance,
reservedBalance: this.reservedBalance,
pricePerBidMin: this.pricePerBidMin,
pricePerBidMax: this.pricePerBidMax,
stopConsumeBalance: this.stopConsumeBalance,
interestLevelMin: this.interestLevelMin,
interestLevelMax: this.interestLevelMax,
capability: this.capability,
source: this.source
}
}
CommonAccount.prototype.getUpdateInfo = function (lastTime) {
if (this.infoUpdateTime > lastTime) {
var consumes = this.consumeHistory.map(function(item){
return {
productId: item.productId,
price: item.price,
interest: item.interest,
producedTime: item.producedTime
}
});
return {
updateTime: this.infoUpdateTime,
consume: consumes,
schedule: {
EXables: this.scheduleObj.EXables.length,
appliedEX: this.scheduleObj.appliedEX.length,
transferingTotal: this.scheduleObj.transferingTotal,
expectedEXAmount: this.scheduleObj.expectedEXAmount,
scheduleTime: this.scheduleObj.scheduleTime
}
}
}
return null;
}
CommonAccount.prototype.addToConsumeHistory = function (obj) {
this.consumeHistory.push(obj);
//this.emit('consumeHistory', this, obj);
this.markInfoUpdate();
}
CommonAccount.prototype.markInfoUpdate = function () {
this.infoUpdateTime = new Date();
}
CommonAccount.prototype.isActive = function () {
var lgt = this.loginExtendedTime ? this.loginExtendedTime : this.loginTime;
return this.capability.consume || this.capability.schedule || new Date() - lgt < this.loginExtendInterval + 60000;
//return this.ableToConsume() || this.loginTime ? (new Date() - this.loginTime < this.loginExtendInterval) : (new Date() - this.createdTime < 1000*60*15)
}
CommonAccount.prototype.needNewSchedule = function () {
if (!this.scheduleObj) return true;
var scheduleTime = this.scheduleObj.scheduleTime;
var now = new Date();
var hours = now.getHours() + now.getMinutes() / 60;
var shours = scheduleTime.getHours() + scheduleTime.getMinutes() / 60;
if (scheduleTime.getDate() !== now.getDate() && hours >= 7.5) return true;
if (scheduleTime.getDate() === now.getDate() && hours < 7.5 && hours - shours>5) return true;
return false;
}
CommonAccount.prototype.ableToSchedule = function () {
if (!this.scheduleObj) return false;
if (this.scheduleObj.expectedEXAmount === 0 && this.scheduleObj.transferingTotal === 0) return false;
var scheduleTime = this.scheduleObj.scheduleTime
var now = new Date();
if (scheduleTime.getDate() !== now.getDate() || (now - scheduleTime) > 24 * 60 * 60 * 1000) return false;
var hours = now.getHours() + now.getMinutes() / 60;
return this.capability.schedule && hours >= 8.25 && hours <= 20;
}
CommonAccount.prototype.ableToConsume = function () {
if (!this.cookieJar || !this.capability.consume) return false;
var total = this.availableBalance;
if (total < this.stopConsumeBalance) return false;
if (this.scheduleObj) {
total = this.availableBalance + this.scheduleObj.expectedEXAmount + this.scheduleObj.transferingTotal;
var afterRepay = total - this.ongoingTodayBuyBackAmount;
if (afterRepay < this.stopConsumeBalance) return false;
}
return true;
}
CommonAccount.prototype.lock = function (info) {
this.locked = true;
this.lockedTime = new Date();
pppoeutil.lock(true);
logutil.log("lock account:", this.user, info);
};
CommonAccount.prototype.unlock = function (info) {
this.locked = false;
this.unlockedTime = new Date();
pppoeutil.lock(false);
logutil.log("unlock account:", this.user, info);
};
module.exports = CommonAccount;