-
Notifications
You must be signed in to change notification settings - Fork 10
/
racing_log_scraper.user.js
129 lines (112 loc) · 4.71 KB
/
racing_log_scraper.user.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
// ==UserScript==
// @name Torn: Racing: Log scraper
// @namespace lugburz.racing_log_scraper
// @version 0.1.5
// @description Collect anonymous racing stats data.
// @author Lugburz
// @match https://www.torn.com/loader.php?sid=racing*
// @require https://github.com/f2404/torn-userscripts/raw/8bbe1e8c2120fe3bfa01b374c5646c8e770c2b27/lib/lugburz_lib.js
// @updateURL https://github.com/f2404/torn-userscripts/raw/master/racing_log_scraper.user.js
// @connect script.google.com
// @connect script.googleusercontent.com
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_notification
// @grant GM_xmlhttpRequest
// @run-at document-body
// ==/UserScript==
const SEND_USER_ID = true;
// Convert to 32bit integer
function stringToHash(string) {
var hash = 0;
if (string.length == 0) return hash;
for (let i = 0; i < string.length; i++) {
const char = string.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash;
}
return hash;
}
function anonymizeCarsData(data) {
const my_name = $("#sidebarroot").find("a[class^='menu-value']").html();
let i = 1;
for (let playername in data) {
if (playername == my_name) {
data['racer0'] = data[playername]; // always racer0
} else {
data['racer'+i] = data[playername];
i++;
}
delete data[playername];
}
return data;
}
function parseLog(json) {
if ($('#racingupdatesnew').find('div.title-black').first().text().trim() != 'Race info' || json.timeData.status != 3) {
return;
}
const URL = "https://script.google.com/macros/s/AKfycby2LzfNCfcjFQkDQ2tVE4Kjk_OXLiPlv3wVwhih-pWP9qnc1OE/exec";
const properties = $('#racingupdatesnew').find('ul.properties-wrap').children();
const id = $(properties[0]).find('div.title').text().trim().replace('ID: ', '');
const type = $(properties[2]).find('div.title').text().trim().replace('Type: ', '');
const track = $(properties[6]).find('div.title').text().trim().replace('Track: ', '');
const laps = $(properties[7]).find('div.title').text().trim().replace('Laps: ', '');
const participants = $(properties[8]).find('div.title').text().trim().replace('Participants: ', '');
const length = $('#racingupdatesnew').find('div.track-info').attr('data-length').trim().replace('mi', '');
const wait_time = json.logData.waitTime;
const user_id = json.user.userID;
const intervals_length = json.raceData.trackData.intervals.length;
const carsdata = JSON.stringify(anonymizeCarsData(json.raceData.cars));
// no sidebar in phone mode
const my_name = $("#sidebarroot").find("a[class^='menu-value']").html() || json.user.playername;
let time, car, place;
$('#leaderBoard').find('ul.driver-item').each(function() {
if ($(this).find('li.name').text().split(' ')[0].trim() == my_name) {
time = $(this).find('li.time').text().trim();
car = $(this).find('li.car').find('img').attr('title');
place = Number($(this).parent().attr('lorder')) + 1;
return false;
}
});
if (!time || !car || !place) {
console.log("Log " + id + ": couldn't find some info, is this someone else's log?");
return;
}
const skill_before = GM_getValue('rs_cur_level');
const skill_after = Number(json.user.racinglevel).toFixed(4);
GM_setValue('rs_prev_level', skill_before);
GM_setValue('rs_cur_level', skill_after);
console.log('Uploading log ' + id + '...');
let data = $.param({id: stringToHash(id+user_id), track: track, length: length, laps: laps, type: type, wait_time: wait_time, car: car, time: time, place: place,
participants: participants, skill_before: skill_before, skill_after: skill_after, intervals_length: intervals_length, carsdata: carsdata});
if (SEND_USER_ID) {
data += '&user_id=' + user_id;
}
GM_xmlhttpRequest({
method: "POST",
url: URL,
data: data,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
onload: function(response) {
console.log(response.responseText);
if (response.responseText == "Success") {
GM_notification("Race log has been uploaded.", "Torn: Racing: Log scraper");
} else {
GM_notification(response.responseText, "Torn: Racing: Log scraper");
}
}
});
}
// Your code here...
ajax((page, xhr) => {
if (page != "loader") return;
let json = '';
try {
json = JSON.parse(xhr.responseText);
} catch (e) {}
if (json) {
$("#racingupdatesnew").ready(parseLog(json));
}
});