-
Notifications
You must be signed in to change notification settings - Fork 0
/
serveurOri.js
91 lines (76 loc) · 2.8 KB
/
serveurOri.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
const body = require('body-parser');
const express = require('express');
const fs = require('fs');
const https = require('https');
const WebSocket = require('ws');
const axios = require('axios');
token = "";
const app = express();
const options = {
key: fs.readFileSync('selfsigned.key'),
cert: fs.readFileSync('selfsigned.crt')
};
eval(fs.readFileSync('twitchBot.js')+'');
const bodyParser=require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json({limit: '100mb'}));
// Parse the request body as JSON
var lastFinish = "";
const fileOptions = {root: '.'};
//Fonction pour récupérer le token pour l'utilisation de l'API de gestion de races.
function getToken(){
axios.post("https://racetime.gg/o/token", {
client_id: clientId,
client_secret: secret,
grant_type: "client_credentials",
rejectUnauthorized: false
}).then(
function(response){
token = response.access_token;
}
).catch(
function (error) {
console.log(error);
});
}
app.use(express.static("."));
app.all('/', function(request, response){
response.sendFile("./index.html", fileOptions);
})
//Api permettant de récupérer les informations de la race entrée.
app.all('/race', function(request, response){
console.log(request.body);
axios.get(request.body.urlRace+"/data", {}).then(
function(res){
response.send(res.data);
var wsRace = new WebSocket("wss://racetime.gg"+res.data.websocket_bot_url);
wsRace.on('message', function(data, flags) {
let donne = JSON.parse(data);
//console.log(donne);
if(donne.type=="race.data"){
if(donne.race.status.value == 'in_progress' || donne.race.status.value =='finished'){
console.log(donne.race.entrants);
if(donne.race.entrants_count_finished == 0){
return;
}
let player = donne.race.entrants[donne.race.entrants_count_finished-1]
if(player.status.value == 'done' && player.user.id != lastFinish){
lastFinish = player.user.id;
let time = player.finish_time.match(/(\d+)H(\d+)M(\d+)/g)[0];
time = time.replace(/[HM]/g, ":");
startBot("blabla");
sendInfo(player.user.name+" "+player.status.verbose_value+" "+player.place_ordinal+" in "+time);
}
}
}
});
}
).catch(function (error){
console.log(error);
})
})
app.all('/twitch', function(request, response){
console.log(request.body.urlChannel);
setChannel(request.body.urlChannel);
})
const server = https.createServer(options, app).listen(3123);