-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexport-dotabuff-to-liquipedia-format.js
189 lines (169 loc) · 5.24 KB
/
export-dotabuff-to-liquipedia-format.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
// ==UserScript==
// @name Export Dotabuff match to Liquipedia format
// @description Exports a match to Liquipedia format. Useful when the Valve API goes down.
// @version 1
// @grant none
// @include https://www.dotabuff.com/matches/*
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js
// ==/UserScript==
// Returns "radiant" or "dire"
var getWinner = function() {
return $("section > header > span.victory-icon").closest("section").attr("class");
}
var generateOutput = function(flip) {
var i = 0;
var sides = [];
var mapNumber = 1;
var winner = getWinner();
var teamIndex = 1;
var pickIndex = 1;
var banIndex = 1;
// Map number
if ($("div.game-link.active").length > 0) {
mapNumber = $("div.game-link.active a").text().split(" ")[1]
}
$("section").each(function() {
var clazz = $(this).attr("class")
if (clazz === "radiant" || clazz === "dire") {
sides.push(clazz);
}
});
var s = "|map" + mapNumber + "={{Map\n";
var generateDraft = function(n, side, winner) {
var output = ""
var picks = [];
var bans = [];
$("div.picks-inline:eq(" + n + ") div div div a[href^='/heroes/'] img[src^='/assets/']").each(function() {
var ban = $(this).parent().parent().parent().parent().attr("class") === "ban";
var hero = $(this).attr("alt").toLowerCase();
if (ban) {
bans.push(hero);
} else {
picks.push(hero);
}
});
return { side, picks, bans, winner };
}
var convertMatchDuration = function(duration) {
var minutes = -1;
var seconds = -1;
var components = duration.split(":");
if (components.length === 3) {
minutes = parseInt(components[0]) * 60 + parseInt(components[1]);
seconds = parseInt(components[2]).toString().padStart(2, '0');
} else {
minutes = parseInt(components[0]);
seconds = parseInt(components[1]).toString().padStart(2, '0');
}
return minutes + "m" + seconds + "s";
}
var outputTop = generateDraft(0, sides[0], winner === sides[0]);
var outputBottom = generateDraft(1, sides[1], winner === sides[1]);
var generateDraftOutput = function(n, draft) {
var output = "|team" + n + "side=" + draft.side;
output += "\n";
for (var j = 0; j < draft.picks.length; j++) {
output += "|t" + n + "h" + (j + 1) + "=" + draft.picks[j];
}
// Sometimes there's no bans...
if (draft.bans.length > 0) {
output += "\n";
for (var j = 0; j < draft.bans.length; j++) {
output += "|t" + n + "b" + (j + 1) + "=" + draft.bans[j];
}
}
return output;
}
if (flip) {
s += generateDraftOutput(1, outputBottom);
} else {
s += generateDraftOutput(1, outputTop);
}
s += "\n";
if (flip) {
s += generateDraftOutput(2, outputTop);
} else {
s += generateDraftOutput(2, outputBottom);
}
s += "\n";
s += "|length=" + convertMatchDuration($("div.match-victory-subtitle span.duration").text());
if (flip) {
if (winner === "radiant") {
winnerIndex = 2;
} else {
winnerIndex = 1;
}
} else {
if (winner === "radiant") {
winnerIndex = 1;
} else {
winnerIndex = 2;
}
}
s += "|winner=" + winnerIndex;
s += "\n";
s += "}}"
navigator.clipboard.writeText(s);
}
$(document).ready(function() {
var getTeams = function() {
var teamA = "Radiant";
var teamB = "Dire";
var aOrB = 0;
var teamSelector = function(side) {
return "div.team-results > section." + side + " > header > a.team-link > span.team-text";
}
if ($(teamSelector("radiant")).length > 0) {
teamA = $(teamSelector("radiant")).text();
}
if ($(teamSelector("dire")).length > 0) {
teamB = $(teamSelector("dire")).text();
}
return [ teamA, teamB ];
}
let teamA, teamB;
[teamA, teamB] = getTeams();
var winner = getWinner();
console.log(winner);
var winnerIcon = " <i class=\"fa fa-trophy\"></i>";
if (winner === "radiant") {
teamA += winnerIcon;
} else {
teamB += winnerIcon;
}
var normal = $("<a>", {
class: "esports-team esports-link team-link",
html: "Copy Liquipedia output for <span style=\"color: #92A525;\">" + teamA + "</span> vs. <span style=\"color: #C23C2A;\">" + teamB + "</span> to clipboard",
href: "#",
click: function(event) {
event.preventDefault();
generateOutput(false);
$("#normalTick").show();
$("#flippedTick").hide();
}
});
var normalTick = $("<span>", {
id: "normalTick",
html: "✓"
});
var flipped = $("<a>", {
class: "esports-team esports-link team-link",
html: "Copy Liquipedia output for <span style=\"color: #C23C2A;\">" + teamB + "</span> vs. <span style=\"color: #92A525;\">" + teamA + "</span> to clipboard",
href: "#",
click: function(event) {
event.preventDefault();
generateOutput(true);
$("#normalTick").hide();
$("#flippedTick").show();
}
});
var flippedTick = $("<span>", {
id: "flippedTick",
html: "✓"
});
normalTick.hide();
flippedTick.hide();
$("div.header-content-title").append(normal).append(" ").append(normalTick);
$("div.header-content-title").append("<br/>");
$("div.header-content-title").append(flipped).append(" ").append(flippedTick);
});