-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
275 lines (241 loc) · 9.13 KB
/
app.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
const UPDATE_INTERVAL_MS = 3000;
const API_URL = 'https://api3.elevenvr.com';
const LAST_MATCHES_SHOWN_COUNT = 1;
const QUERY_API_KEY = 'apiKey';
const QUERY_PARAM_USERID = 'user';
const QUERY_PARAM_ROWS_REVERSED = 'rowsReversed';
const QUERY_PARAM_AUTO_FLAGS = 'autoFlags';
const QUERY_PARAM_BESTOF = 'bestOf';
const QUERY_PARAM_COUNTRY_FLAG_1 = 'flag_1';
const QUERY_PARAM_COUNTRY_FLAG_2 = 'flag_2';
const INTERNAL_ENABLE_ACCOUNTS_CACHE = true;
const INTERNAL_ACCOUNTS_CACHE = {}; // Cache account data for user ID
const TEAM_NAME_1 = 'home';
const TEAM_NAME_2 = 'away';
let updateHandle = 0;
const urlParams = new URLSearchParams(window.location.search);
const apiKey = urlParams.get(QUERY_API_KEY);
const userID = urlParams.get(QUERY_PARAM_USERID);
const rowsReversed = urlParams.get(QUERY_PARAM_ROWS_REVERSED);
const bestOf = urlParams.get(QUERY_PARAM_BESTOF);
const flag_1 = urlParams.get(QUERY_PARAM_COUNTRY_FLAG_1);
const flag_2 = urlParams.get(QUERY_PARAM_COUNTRY_FLAG_2);
const autoFlags = urlParams.get(QUERY_PARAM_AUTO_FLAGS);
const homeWinsOffset = urlParams.get('home-offset') || 0;
const awayWinOffset = urlParams.get('away-offset') || 0;
const getValAsBoolean = (val) => !!val && val !== '0' && val !== 'false' ? true : false
const App = {
data() {
return {
counter: 0,
apiKey: apiKey || '',
userID: userID || '60531',
matches: [],
homeMatchesWon: 0,
awayMatchesWon: 0,
rowsReversed: getValAsBoolean(rowsReversed),
teams: [TEAM_NAME_1, TEAM_NAME_2],
bestOf: bestOf || '',
countries: countries,
flag_1: flag_1 || '',
flag_2: flag_2 || '',
autoFlags: getValAsBoolean(autoFlags),
apiFetchError: false,
}
},
computed: {
teamsReversed() {
return this.teams.slice().reverse();
}
},
watch: {
rowsReversed: function (val) {
if (val) {
urlParams.set(QUERY_PARAM_ROWS_REVERSED, 1);
} else {
urlParams.delete(QUERY_PARAM_ROWS_REVERSED);
}
this.updateUrlParams();
},
apiKey: function (val) {
urlParams.set(QUERY_API_KEY, val);
this.updateUrlParams();
this.updateMatches();
},
userID: function (val) {
urlParams.set(QUERY_PARAM_USERID, val);
this.updateUrlParams();
this.updateMatches();
},
bestOf: function (val) {
if (val) {
urlParams.set(QUERY_PARAM_BESTOF, val);
} else {
urlParams.delete(QUERY_PARAM_BESTOF);
}
this.updateUrlParams();
},
flag_1: function (val) {
if (val) {
urlParams.set(QUERY_PARAM_COUNTRY_FLAG_1, val);
} else {
urlParams.delete(QUERY_PARAM_COUNTRY_FLAG_1);
}
this.updateUrlParams();
},
flag_2: function (val) {
if (val) {
urlParams.set(QUERY_PARAM_COUNTRY_FLAG_2, val);
} else {
urlParams.delete(QUERY_PARAM_COUNTRY_FLAG_2);
}
this.updateUrlParams();
},
autoFlags: function (val) {
if (val) {
urlParams.set(QUERY_PARAM_AUTO_FLAGS, 1);
} else {
urlParams.delete(QUERY_PARAM_AUTO_FLAGS);
}
this.updateUrlParams();
this.loadAccountsAndSetFlags();
},
},
methods: {
getAPIURL(path) {
return `${API_URL}${path}?api-key=${this.apiKey}`;
},
updateUrlParams() {
history.pushState(null, null, "?" + urlParams.toString());
},
getFlag(teamName) {
return teamName === TEAM_NAME_1 ? this.flag_1 : this.flag_2;
},
loadAccountsAndSetFlags() {
if (!this.matches.length) {
console.error('No matches found');
return;
}
if (!this.autoFlags) {
return;
}
const match = this.matches[0];
const users = [match.attributes['home-user-id'], match.attributes['away-user-id']];
const setFlagForAccount = (data, idx) => {
const countryCode = data?.data?.attributes?.['country-code'];
if (countryCode) {
this['flag_' + (idx + 1)] = countryCode.toLowerCase();
}
}
for (const [idx, userID] of users.entries()) {
if (INTERNAL_ENABLE_ACCOUNTS_CACHE && INTERNAL_ACCOUNTS_CACHE[userID]) {
setFlagForAccount(INTERNAL_ACCOUNTS_CACHE[userID], idx);
} else {
fetch(this.getAPIURL(`/accounts/${userID}`))
.then(res => res.json())
.then(data => {
INTERNAL_ACCOUNTS_CACHE[userID] = data;
setFlagForAccount(data, idx);
})
}
}
},
updateMatches() {
clearTimeout(updateHandle);
fetch(this.getAPIURL(`/accounts/${this.userID}/matches`))
.then(res => res.json())
.then(data => {
this.apiFetchError = false;
// Count matches score for those opponents
let otherIDPrevious = -1;
this.homeMatchesWon = homeWinsOffset;
this.awayMatchesWon = awayWinOffset;
try {
for (const match of data.data) {
const user1 = match.attributes['home-user-id'];
const user2 = match.attributes['away-user-id'];
const otherID = user1 == this.userID ? user2 : user1;
if (otherIDPrevious === -1) {
otherIDPrevious = otherID
}
if (otherIDPrevious !== otherID) break; // End counting if no more games between those two
if (match.attributes.state !== 1) continue; // Don't count matches that are not over
if (match.attributes.winner === 0) {
this.homeMatchesWon++;
} else {
this.awayMatchesWon++;
}
}
} catch (e) {
}
const newMatches = data.data.slice(0, LAST_MATCHES_SHOWN_COUNT);
this.matches = newMatches.map((m) => {
let scores = m.relationships.rounds.data.map((r) => {
return data.included.find((inclRound) => inclRound.id == r.id && inclRound.type === r.type).attributes;
});
return {
...m,
scores
}
})
// TODO: We do this every tick, maybe we could cache account info
// But what if account changes? How do we force refresh? Re-add OBS source?
this.loadAccountsAndSetFlags();
}).catch(() => {
this.apiFetchError = true;
});
updateHandle = setTimeout(() => this.updateMatches(), UPDATE_INTERVAL_MS);
},
},
mounted() {
this.updateMatches();
}
}
var app = Vue.createApp(App);
app.component('player', {
props: ['player'],
template: `
<div class="player">
<h3>
<img v-if="player.flag" :src="'flags/'+player.flag + '.svg'" class="flag"/>{{ player.UserName }}
</h3>
<div class="player__info">
<div>
WR#{{ player.Rank }} {{ Math.round(player.ELO) }}ELO
</div>
</div>
</div>
`
});
app.component('player-row', {
props: ['match', 'teamName', 'matchesWon', 'flag'],
methods: {
isMatchOver(score) {
return (score['home-score'] >= 11 || score['away-score'] >= 11) && Math.abs(score['home-score'] - score['away-score']) > 1;
},
},
computed: {
otherTeamName: function () {
return this.teamName === TEAM_NAME_1 ? TEAM_NAME_2 : TEAM_NAME_1;
},
playerData: function () {
return {
...this.match.attributes[this.teamName + '-team'][0],
flag: this.flag,
}
}
},
template: `
<div class="player-row">
<player :player="playerData" />
<div class="score score--matches">
{{matchesWon}}
</div>
<div v-for="score in match.scores" class="score"
:class="[isMatchOver(score) ? 'ended': 'running', score[teamName + '-score'] > score[otherTeamName + '-score'] ? 'win' :'lose']">
{{score[teamName + '-score']}}
</div>
</div>
`
});
app.mount('#app')