-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.js
99 lines (83 loc) · 2.98 KB
/
tools.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
/*
currentTime: make GMT more readable
IsJsonString: checks if a string is json
leftDisjoin: efficient ES6 function to find difference between 2 arrays
*/
module.exports = {
currentTime: () => {
_time = new Date().toISOString().
replace(/T/, ' '). // replace T with a space
replace(/\..+/, '');
return _time;
},
IsJsonString: (str) => {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
},
leftDisjoin: (newArr, oldArr) => {
var oldSet = new Set(oldArr);
return newArr.filter(function(x) { return !oldSet.has(x); });
},
sendTweet: (msg) => {
const CircularJSON = require('circular-json');
const twit = require('twit');
// staging will send from nxt335_lg_01, production from nxt335_lg_02
const config = require('./config_twit.js');
var T = new twit(config);
var msg = JSON.parse(msg);
var send = false;
// decide whether to send tweet
if (msg["team1_win_percentage_live"] === undefined) {
console.log('WARNING', 'cannot parse tweet');
return;
}
if (msg["team1_score"] == 1 || msg["team2_score"] == 1) { send = true;}
if (Math.abs(msg["team1_score"] - msg["team2_score"]) > 1) { send = true;}
if ( msg["team1_win_percentage_live"].toFixed(3) > 0.98 || msg["team2_win_percentage_live"].toFixed(3) > 0.98 ) { send = false;}
console.log('send tweet', send);
if (!send) {return;}
// HACK remove this when the model is updated
var t1 = unescape(msg["team1_name"]);
var t2 = unescape(msg["team2_name"]);
var tweet = '';
if (parseFloat(msg["team1_win_percentage_live"]).toFixed(4) > parseFloat(msg["team2_win_percentage_live"]).toFixed(4))
{
tweet = tweet + t1 + " are a " + parseFloat((msg["team1_win_percentage_live"])*100).toFixed(2) + "% favorite";
tweet = tweet + " over " + t2 ;
tweet = tweet + ', score ' + msg["team1_score"] +" to "+ msg["team2_score"] ;
}
else
{
tweet = tweet + t2 + " are a " + parseFloat((msg["team2_win_percentage_live"])*100).toFixed(2) + "% favorite";
tweet = tweet + " over " + t1 ;
tweet = tweet + ', score ' + msg["team2_score"] + " to " + msg["team1_score"] ;
}
tweet = tweet + ", in match " + msg["match_number"] + " of " + msg["bestof"] ;
if (process.env.NODE_ENV === 'production') {
tweet = tweet + '. http://' + msg["csgogame_id"];
}
else {
tweet = tweet + '. http://' + msg["csgogame_id"];
}
tweet = tweet.substring(0,139);
try {
T.post('statuses/update', { status: tweet }, function(err, data, response) {
reply = CircularJSON.stringify(data);
if (reply.indexOf('"errors":')>0) {
console.log(data); // probably an error
}
else {
// console.log('OK');
}
});
}
catch (e) {
console.log(e);
}
return send;
}
};