This repository has been archived by the owner on Dec 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
177 lines (155 loc) · 4.65 KB
/
server.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
"use strict";
// npm modules
const snoowrap = require('snoowrap');
// custom modules
const config = require('./src/config/config.js');
// command line arguments
const commandLineArgs = require('command-line-args');
// parse the arguments
const options = commandLineArgs([
{
name: 'gui',
alias: 'g',
type: Boolean,
defaultValue: false
},
]);
// holds some stats for the server
var genericInfo = {
// timer duration for the polling
nextTimer: 0,
// most recent username and server
recentUser: 'none',
// amount of times we request unread messages and how many we received
timesPolled: 0,
timesReceived: 0,
// amount of users we parsed from messages
foundUsers: 0,
// reply count
sentResponses: 0,
// list of errors
errorList: [],
// list of generic messages
messageList: [],
},
// server list storage, is fetched from api
servers = false,
// champion list storage, is fetched from api
champions = false;
// helpers and classes
var RequestHandler = require('./src/RequestHandler.js');
var DatabaseHandler = require('./src/DatabaseHandler.js')();
var ConsoleTemplate = require('./src/ConsoleTemplate');
var Logging = require('./src/Logging')();
// start sqlite
DatabaseHandler.init(config.sqliteDb);
// snoowrap setup for reddit api calls
const r = new snoowrap({
user_agent: config.user_agent,
client_id: config.client_id,
client_secret: config.client_secret,
username: config.username,
password: config.password,
// don't continue api queue if rate limit reached
continueAfterRatelimitError: false
});
// helper objects
var Responder = require('./src/Responder')(r, DatabaseHandler, {
sentResponse: () => {
genericInfo.sentResponses += 1;
},
});
var Fetcher = require('./src/Fetcher')(r, DatabaseHandler, {servers: servers, champions: champions}, {
hasPolled: () => {
genericInfo.timesPolled += 1;
},
hasReceived: () => {
genericInfo.timesReceived += 1;
},
hasFound: (username) => {
genericInfo.foundUsers += 1;
genericInfo.recentUser = username;
},
});
var loadChampions = new Promise((resolve, reject)=> {
if (!champions) {
Logging('cyan', 'Loading champions');
RequestHandler.request(
config.api_base + '/static/champions',
(result) => {
var championsJson = JSON.parse(result);
if (championsJson.champions) {
champions = championsJson.champions;
}
// reformat champions
for (var key in champions) {
champions[champions[key]['champkey']] = champions[key];
champions[champions[key]['pretty_name']] = champions[key];
}
Fetcher.updateChampions(champions);
resolve();
},
(err) => {
champions = false;
Logging('red', 'Error!');
Logging('red', err);
reject();
}
);
} else {
resolve();
}
});
var loadServers = new Promise((resolve, reject)=> {
if (!servers) {
Logging('cyan', 'Loading servers');
RequestHandler.request(
config.api_base + '/static/servers',
(result) => {
var serversJson = JSON.parse(result);
if (serversJson.servers) {
servers = serversJson.servers;
}
Fetcher.updateServers(servers);
resolve();
},
(err) => {
servers = false;
Logging('red', 'Error!');
Logging('red', err);
reject();
}
);
} else {
resolve();
}
});
// check if we have the servers and champions
function runFetcher() {
Promise.all([loadChampions, loadServers]).then((result) => {
Fetcher.checkMessages();
});
}
// 1 second timer to keep track of the main timer progress. used in the gui
setInterval(() => {
genericInfo.nextTimer = genericInfo.nextTimer + 1000;
}, 1000);
// if the gui parameter is given ,start showing the gui
if (options.gui) {
// refresh the guid for the console
const showGui = () => {
ConsoleTemplate(genericInfo, config);
};
// Show the console gui
showGui();
setInterval(showGui, 1000);
}
// run the initial round
runFetcher();
Responder.getResponses();
// main timer
setInterval(()=> {
runFetcher();
Responder.getResponses();
genericInfo.nextTimer = 0;
}, config.pollTimer);