-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathendWatcher.js
158 lines (136 loc) · 4.53 KB
/
endWatcher.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
/**
* Watches for auctions/trades/mission that will be ending soon and ensures that they are
* ended at the right time.
*/
var exports = (module.exports = function (client) {
var module = {};
const auctions = require("./auctions.js");
const db = require("./db.js");
const auctionsJS = require("./auctions.js");
const tradesJS = require("./trades.js");
const missionsJS = require("./missions.js");
const utils = require("./utils.js");
const config = require("./config.json");
const BigNumber = require("bignumber.js");
BigNumber.config({ DECIMAL_PLACES: 8 });
BigNumber.config({ EXPONENTIAL_AT: 1e9 });
const ethers = require("ethers");
var intervals = [];
// higher frequency check for the end of an auction
// calls the relevant function when it ends
async function checkAndEndAuction(auctionID) {
try {
var now = Date.now();
var auction = await db.getAuction(auctionID);
if (auction) {
if (now >= auction.endTime.getTime()) {
auctionsJS.endAuction(auctionID, client);
clearInterval(intervals[auctionID]);
}
}
} catch (error) {
console.log(error);
}
}
// higher frequency check for the end of a trade
// calls the relevant function when it ends
async function checkAndEndTrade(tradeID) {
try {
var now = Date.now();
var trade = await db.getTrade(tradeID);
if (trade) {
if (now >= trade.endTime.getTime() && trade.completedTime === null) {
var channel = client.channels.cache.get(config.tradeChannel);
var message = Array.from(
await channel.messages.fetch({ limit: 1 })
)[0][1];
console.log("deleting " + tradeID);
tradesJS.endTrade(message, trade.tradeID, true);
clearInterval(intervals[tradeID]);
}
}
} catch (error) {
console.log(error);
}
}
// higher frequency check for the end of a mission
// calls the relevant function when it ends
async function checkAndEndMission(missionID) {
try {
var now = Date.now();
const provider = new ethers.providers.JsonRpcProvider(config.nevm.rpcUrl);
var mission = await db.getMission(missionID);
if (mission) {
if (now >= mission.endTime.getTime()) {
var channel = client.channels.cache.get(config.missionPayOutsChannel);
var message = Array.from(
await channel.messages.fetch({ limit: 1 })
)[0][1];
console.log("paying mission " + missionID);
missionsJS.payMission(
[mission.missionID],
message,
client,
true,
provider
);
clearInterval(intervals[missionID]);
}
}
} catch (error) {
console.log(error);
}
}
// checks for auctions/trades/missions ending within a certain time and for those that will it
// then adds a higher frequency interval to ensure they end at the right time
async function checkEnding() {
try {
// ending within 2 minutes
var endingAuctions = await auctionsJS.getEndingSoon(120);
if (endingAuctions) {
for (var i = 0; i < endingAuctions.length; i++) {
var interval = intervals[endingAuctions[i].auctionID];
if (interval == undefined) {
intervals[endingAuctions[i].auctionID] = setInterval(
checkAndEndAuction,
5000,
endingAuctions[i].auctionID
);
}
}
}
// ending within 1 minute
var endingTrades = await tradesJS.getEndingSoon(60);
if (endingTrades) {
for (var i = 0; i < endingTrades.length; i++) {
var interval = intervals[endingTrades[i].tradeID];
if (interval == undefined) {
intervals[endingTrades[i].tradeID] = setInterval(
checkAndEndTrade,
5000,
endingTrades[i].tradeID
);
}
}
}
// ending within 2 minutes
var endingMissions = await missionsJS.getEndingSoon(120);
if (endingMissions) {
for (var i = 0; i < endingMissions.length; i++) {
var interval = intervals[endingMissions[i].missionID];
if (interval == undefined) {
intervals[endingMissions[i].missionID] = setInterval(
checkAndEndMission,
5000,
endingMissions[i].missionID
);
}
}
}
} catch (error) {
console.log(error);
}
}
// check every 90 secs
setInterval(checkEnding, 10000);
});