Skip to content
This repository has been archived by the owner on Apr 22, 2021. It is now read-only.

Commit

Permalink
fix: select correct Futbin player when there are multiple results
Browse files Browse the repository at this point in the history
also look at club to do a more exact search for the Futbin link
  • Loading branch information
Tim Klingeleers committed Sep 28, 2018
2 parents c184d6f + 113f13a commit 76b8224
Showing 1 changed file with 34 additions and 30 deletions.
64 changes: 34 additions & 30 deletions app/futbin/futbin-player-links.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* globals
enums
window $ document
*/
import { analytics, BaseScript, Database } from '../core';
Expand Down Expand Up @@ -42,32 +41,37 @@ export class FutbinPlayerLinks extends BaseScript {
mutationRecords.forEach(function (mutation) {
if ($(mutation.target).hasClass('DetailView') && $(mutation.target)
.find('.DetailPanel') && mutation.addedNodes.length > 0) {
if ($(mutation.target).find('#futbinPlayerLink').length === 0 && this.getSettings()['show-link-to-player'] === 'true') {
let selectedItem = this._getSelectedItem();
if (this.getSettings()['show-link-to-player'] !== 'true') {
return;
}

if (selectedItem == null || selectedItem.resourceId === 0) {
return;
}
$(mutation.target).find('.DetailPanel .ut-button-group').append(`<button id="futbinPlayerLink" data-resource-id="${selectedItem.resourceId}" class="list"><span class="btn-text">View on Futbin</span><span class="btn-subtext"></span></button>`);

$('#futbinPlayerLink').bind('click', async () => {
let btn = $('#futbinPlayerLink');
btn.find('.btn-text').html('Searching on Futbin ...');
const futbinLink = await FutbinPlayerLinks._getFutbinPlayerUrl(selectedItem);

selectedItem = this._getSelectedItem();
btn = $('#futbinPlayerLink');
if (btn.data('resource-id') === selectedItem.resourceId) {
if (futbinLink) {
btn.find('.btn-text').html('View on Futbin');
analytics.trackEvent('Futbin', 'Show player on Futbin', btn.data('resource-id'));
window.open(futbinLink);
} else {
btn.find('.btn-text').html('No exact Futbin player found');
}
}
});
let selectedItem = this._getSelectedItem();
if (selectedItem == null || selectedItem.resourceId === 0) {
return;
}

const futbinPlayerLink = $(mutation.target).find('#futbinPlayerLink');
futbinPlayerLink.remove();

$(mutation.target).find('.DetailPanel > .ut-button-group').prepend(`<button id="futbinPlayerLink" data-resource-id="${selectedItem.resourceId}" class="list"><span class="btn-text">View on Futbin</span><span class="btn-subtext"></span></button>`);

$('#futbinPlayerLink').bind('click', async () => {
let btn = $('#futbinPlayerLink');
btn.find('.btn-text').html('Searching on Futbin ...');
const futbinLink = await FutbinPlayerLinks._getFutbinPlayerUrl(selectedItem);

selectedItem = this._getSelectedItem();
btn = $('#futbinPlayerLink');
if (btn.data('resource-id') === selectedItem.resourceId) {
if (futbinLink) {
btn.find('.btn-text').html('View on Futbin');
analytics.trackEvent('Futbin', 'Show player on Futbin', btn.data('resource-id'));
window.open(futbinLink);
} else {
btn.find('.btn-text').html('No exact Futbin player found');
}
}
});
}
}, this);
}
Expand Down Expand Up @@ -97,12 +101,9 @@ export class FutbinPlayerLinks extends BaseScript {
let exactPlayers = players.filter(p =>
parseInt(p.rating, 10) === parseInt(item.rating, 10));
if (exactPlayers.length > 1) {
let version = Object.keys(enums.ItemRareType)[item.rareflag];
if (item.rareflag < 3) {
version = 'normal';
}
exactPlayers = exactPlayers.filter(p =>
p.version.toLowerCase() === version.toLowerCase());
p.rare_type === item.rareflag.toString() &&
p.club_image.endsWith(`/${item.teamId}.png`));
}
if (exactPlayers.length === 1) {
futbinPlayerIds = Database.getJson('futbin-player-ids', []);
Expand All @@ -114,6 +115,9 @@ export class FutbinPlayerLinks extends BaseScript {
}
Database.setJson('futbin-player-ids', futbinPlayerIds);
return resolve(`https://www.futbin.com/19/player/${exactPlayers[0].id}`);
} else if (exactPlayers.length > 1) {
// Take first one, several players are returned more than once
return resolve(`https://www.futbin.com/19/player/${exactPlayers[0].id}`);
}

return resolve(null); // TODO: what should we do if we find more than one?
Expand Down

0 comments on commit 76b8224

Please sign in to comment.