-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
238 lines (179 loc) · 7.33 KB
/
main.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
let API_CLEF = [
];
let FORMAT = "json";
let START_OFFSET = 1773 // 0 = premier 1 = deuxième
let NBBOUCLE = 100;
// *********************************************
const express = require("express");
const request = require("./requeste");
const game = require("./game");
const fs = require('fs');
const {get} = require("axios");
const app = express();
const port = 3000;
app.use(express.json());
app.listen(port, (req, res) => {
console.log(`[GOOD] Démarrage du script`);
console.log(`Script en cours d'exécution sur http://localhost:${port}`);
console.log(" ");
console.log(" ");
console.log("Nombre de boucle demandé : " + NBBOUCLE + " Sois un nombre de jeux de : " + NBBOUCLE + " à editer");
console.log(`[GOOD] Reset du fichier latest.log`);
fs.writeFileSync('latest.log', "");
let data = {};
data['control'] = {};
const controlReset = JSON.stringify(data, null, 2);
console.log(`[GOOD] Reset du fichier control.json`);
fs.writeFileSync('control.json', controlReset);
// let data2 = {};
// data2['games'] = [];
// const gameReset = JSON.stringify(data2, null, 2);
// console.log(`[GOOD] Reset du fichier games.json`);
// fs.writeFileSync('games.json', gameReset);
console.log(" ");
runLoop(res);
});
async function runLoop(res) {
// let OFFSET_boucle = 0;
for (let i = 0; i < NBBOUCLE; i++) {
console.log("*************************************");
console.log("[START] Boucle numéro : " + (i + 1));
console.log("*************************************");
console.log(" ");
let getResult = await getGameRequest(i+1, res);
if (!getResult.game){
getResult.game = {
guid: "err",
name: "err",
id: "err"
};
}
const result = await makeRequest(getResult.game, res);
console.log(" ");
console.log("*************************************");
console.log(" [" + result.status + "] Boucle numéro : " + (i + 1));
console.log(" - guid : " + getResult.game.guid);
console.log(" - game : " + getResult.game.name);
console.log(" - messsage : " + result.message);
console.log("*************************************");
console.log(" ");
console.log(" ");
console.log('[GOOD] Modification du fichier log');
fs.appendFileSync('latest.log', (i+1) + ": ["+ result.status +"] Game = "+getResult.game.guid+" - "+getResult.game.name+" | Etape n\°"+ (i+1) +" : " + result.message+ '\n');
await updateControleJson(i, result, getResult.game);
console.log(" ");
console.log(" ");
}
console.log("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
console.log("Verification de la modifications de tout les jeux");
let perfect = "NON";
let palierErr = "NON";
let j = 0;
while (perfect !== "OUI") {
j++
console.log(" - Verification n\°" + j);
console.log(" ");
palierErr = "NON";
const controlJson = await getControlJson();
let control = controlJson.control;
fs.appendFileSync('latest.log', "----------> VERIF N\°"+ j +"\n");
for (const key of Object.keys(control)) {
if (control[key].status == "ERR"){
palierErr = "OUI";
console.log(" + " + control[key].nbBoucle + "/" + Object.keys(control).length + " = ERR");
console.log("[ERR] La boucle n\°"+ control[key].nbBoucle + " (" + control[key].guid + " - " + control[key].name +") doit être refaite");
console.log(" ");
console.log("*************************************");
console.log("[START] Boucle numéro : " + (+key + 1));
console.log("*************************************");
console.log(" ");
const getResult = await getGameRequest(+key+1, res);
const resultReVerif = await reRequestPromise(+key, getResult,res);
await updateControleJson(+key, resultReVerif,getResult.game);
console.log(" ");
console.log(" ");
} else {
console.log(" + " + control[key].nbBoucle + "/" + Object.keys(control).length + " = VALID");
}
}
if (palierErr == "NON"){
console.log(" ");
console.log("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
console.log(" ");
console.log('[GOOD] Tout les jeux on été vérifier');
perfect = "OUI";
} else {
console.log("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
}
}
setTimeout(() => {
console.log('[GOOD] Fermeture du Script');
process.exit();
}, 6000);
}
function getGameRequest(i, res) {
return new Promise(resolve => {
let OFFSET_boucle = i + START_OFFSET;
game(OFFSET_boucle, res, resolve);
});
}
function makeRequest(game, res) {
return new Promise(resolve => {
request(API_CLEF, FORMAT, game, res, resolve);
});
}
async function getControlJsonFunct(callback){
fs.readFile('control.json', 'utf8', (err, data) => {
if (err) {
console.error('[ERR] De lecture du fichier JSON de controle');
}
try {
const jsonData = JSON.parse(data);
// console.log('[GOOD] Recuperation du fichier JSON de controle');
callback(jsonData);
} catch (parseError) {
console.error('[ERR] De parsing du fichier JSON de controle');
}
});
}
function getControlJson() {
return new Promise(resolve => {
getControlJsonFunct(resolve);
});
}
async function updateControleJson(id, message, game) {
console.log('[GOOD] Modification du fichier JSON de controle');
const controlJson = await getControlJson();
message['id'] = game.id;
message['id_giant_bomb'] = game.id_giant_bomb;
message['guid'] = game.guid;
message['name'] = game.name;
message['nbBoucle'] = id + 1;
controlJson.control[id] = message;
// console.log(controlJson)
const controlJsonEdit = JSON.stringify(controlJson, null, 2);
fs.writeFileSync('control.json', controlJsonEdit);
}
async function reRequest(i, getResult, res , callback) {
const result = await makeRequest(getResult.game, res);
console.log(" ");
console.log("*************************************");
console.log(" [" + result.status + "] Boucle numéro : " + (i + 1));
console.log(" - guid : " + getResult.game.guid);
console.log(" - game : " + getResult.game.name);
console.log(" - messsage : " + result.message);
console.log("*************************************");
console.log(" ");
console.log(" ");
console.log('[GOOD] Modification du fichier log');
fs.appendFileSync('latest.log', (i+1) + ": ["+ result.status +"] Game = "+getResult.game.guid+" - "+getResult.game.name+" | Etape n\°"+ (i+1) +" : " + result.message+ '\n');
// await updateControleJson(i, result, getResult.game);
// console.log(" ");
// console.log(" ");
callback(result);
}
function reRequestPromise(i, getResult, res) {
return new Promise(resolve => {
reRequest(i, getResult, res, resolve);
});
}