-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimporter.js
188 lines (127 loc) · 4.38 KB
/
importer.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
185
186
const Importer = require('./api/importer/importer');
const Message = require('./api/message');
const mysql = require('./api/database');
const config = require('./config.json');
new Message('Node UTStats 2 Importer module started.','note');
ftpServers = [];
bCurrentImportFinished = false;
async function setFTPSettings(){
try{
ftpServers = [];
const query = "SELECT * FROM nstats_ftp ORDER BY id ASC";
const result = await mysql.simpleFetch(query);
for(let i = 0; i < result.length; i++){
ftpServers.push(result[i]);
}
}catch(err){
console.trace(err);
new Message(err, "error");
}
}
async function getLogsFolderSettings(){
const query = "SELECT ignore_duplicates,ignore_bots,min_players,min_playtime,import_ace,use_ace_player_hwid FROM nstats_logs_folder ORDER BY id DESC LIMIT 1";
const result = await mysql.simpleQuery(query);
if(result.length === 0){
throw new Error("Logs folder settings have not been found, you have most likely missed an upgrade step from a previous version of node utstats 2.");
}
return result[0];
}
async function startNewImport(ftpServer){
if(ftpServer !== null){
const f = ftpServer;
if(f.enabled == 0){
new Message(`${f.host}:${f.port} has been disabled, skipping import.`,"note");
return true;
}
const importer = new Importer(
f.id,
f.host,
f.port,
f.user,
f.password,
f.target_folder,
f.delete_after_import,
f.delete_tmp_files,
f.ignore_bots,
f.ignore_duplicates,
f.min_players,
f.min_playtime,
f.sftp,
f.import_ace,
f.delete_ace_logs,
f.delete_ace_screenshots,
false//f.use_ace_player_hwid
);
return await importer.import();
}else{
const logsSettings = await getLogsFolderSettings();
const importer = new Importer(
-1,
null,
null,
null,
null,
null,
null,
null,
logsSettings.ignore_bots,
logsSettings.ignore_duplicates,
logsSettings.min_players,
logsSettings.min_playtime,
false,
false,
false,
false,
false,//logsSettings.use_ace_player_hwid,
true
);
return await importer.import();
}
}
async function main(){
try{
const start = process.uptime();
bCurrentImportFinished = false;
await setFTPSettings();
if(ftpServers.length > 0){
let currentServerIndex = 0;
while(currentServerIndex < ftpServers.length){
try{
await startNewImport(ftpServers[currentServerIndex]);
}catch(err){
new Message(err.toString(),"error");
}
currentServerIndex++;
}
}
new Message(`Checking for leftover logs in/Logs folder.`,'note');
//console.log(fs.readdirSync("./Logs"));
await startNewImport(null);
new Message(`Import process completed.`,'pass');
bCurrentImportFinished = true;
const end = process.uptime();
new Message(`----------------------------------------------`, "note");
new Message(`Import completed in ${(end - start).toFixed(4)} seconds`,"note");
new Message(`----------------------------------------------`, "note");
}catch(err){
console.trace(err);
new Message(`There was a problem connecting to the mysql server.`,"error");
new Message(err, "error");
bCurrentImportFinished = true;
}
}
(async () =>{
await main();
if(config.importInterval > 0){
setInterval(async () =>{
if(bCurrentImportFinished){
await main();
}else{
new Message("Previous import has not finished, skipping until next check interval.", "note");
}
}, config.importInterval * 1000);
}else{
new Message("Import Interval is set to 0, this means the importer will only run once.", "note");
process.exit(0);
}
})();