gilles / Fut Club

// ==UserScript==
// @name         Fut Club
// @namespace    http://zever.eu/
// @version      0.1
// @description  Get better stats!
// @author       G. Maes
// @match        https://www.easports.com/fifa/ultimate-team/web-app/
// @grant        GM_xmlhttpRequest
// @license 	MIT
// ==/UserScript==

(function() {
	'use strict';

	var platform = null;

	var players = [];

	function constructor()
	{
		decorateFutMethods();
		registerEvents();
	}

	function addExportButton()
	{
		gFooter._btnNewScreen = new components.FooterButton();
		gFooter._btnNewScreen.getRootElement().classList.add("btnWorth");
		gFooter.__root.appendChild(gFooter._btnNewScreen.getRootElement());
		gFooter._btnNewScreen.init();
		gFooter._btnNewScreen.setText("Export club");
		gFooter._btnNewScreen.addTarget(gFooter, function () {
			exportClub(0, 60);
		}, enums.Event.TAP);
	}

	function exportClub(start, limit)
	{
		console.log('Getting club players ', start, limit);
		var t = new transferobjects.SearchCriteria();
		t.type = enums.SearchType.PLAYER;

		var o = new communication.ClubSearchDelegate(t, start, limit);
		o._useClickShield = false;
		o.send();

		o.addListener(communication.BaseDelegate.SUCCESS, this, function marketSearch(sender, response) {
			if (response.itemData.length > 0)
			{
				players = players.concat(response.itemData.map(function(item) {
					return {
						'id': item.resourceId,
						'assetId': item.assetId,
						'untradeable': item.untradeable,
						'price': item.lastSalePrice,
						'rating': item.rating,
						'nation': {
							'id': item.nation,
							'name': repositories.TeamConfig.getNation(item.nation).name.replace("'", "'").replace('"', '"')
						},
						'league': {
							'id': item.leagueId,
							'name': repositories.TeamConfig.getLeague(item.leagueId).name.replace("'", "'").replace('"', '"'),
							'abbreviation': repositories.TeamConfig.getLeague(item.leagueId).abbreviation.replace("'", "'").replace('"', '"')
						},
						'team': {
							'id': item.teamid,
							'name': repositories.TeamConfig.getTeam(item.teamid).name.replace("'", "'").replace('"', '"')
						},
						'location': 'club'
					}
				}));
			}

			if (response.itemData.length == limit)
			{
				// Potentially more results, fetch'm!
				exportClub(start + limit, limit);
			}
			else
			{
				exportTransfers();
			}
		});
	}

	function exportTransfers()
	{
		console.log('Getting transfer players');
		var o = new communication.TradePileDelegate();
		o._useClickShield = false;
		o.send();

		o.addListener(communication.BaseDelegate.SUCCESS, this, function marketSearch(sender, response) {
			if (response.auctionInfo.length > 0)
			{
				players = players.concat(response.auctionInfo.map(function(item) {
					if (item.tradeState && item.tradeState == 'closed')
					{
						return null;
					}

					return {
						'id': item.itemData.resourceId,
						'assetId': item.itemData.assetId,
						'untradeable': item.itemData.untradeable,
						'price': item.itemData.lastSalePrice,
						'rating': item.itemData.rating,
						'nation': {
							'id': item.itemData.nation,
							'name': repositories.TeamConfig.getNation(item.itemData.nation).name.replace("'", "'").replace('"', '"')
						},
						'league': {
							'id': item.itemData.leagueId,
							'name': repositories.TeamConfig.getLeague(item.itemData.leagueId).name.replace("'", "'").replace('"', '"'),
							'abbreviation': repositories.TeamConfig.getLeague(item.itemData.leagueId).abbreviation.replace("'", "'").replace('"', '"')
						},
						'team': {
							'id': item.itemData.teamid,
							'name': repositories.TeamConfig.getTeam(item.itemData.teamid).name.replace("'", "'").replace('"', '"')
						},
						'location': 'transfer'

					}
				}).filter(function(item) { return item !== null }));
			}

			sendToFutClub();
		});
	}

	function sendToFutClub()
	{
		var user = {
			'id': repositories.User.getCurrent().getSelectedPersona().id,
			'name': repositories.User.getCurrent().getSelectedPersona().name
		};

		if (repositories.User.getCurrent().getSelectedPersona().isPlaystation)
		{
			user.platform = 'ps';
		}
		else if (repositories.User.getCurrent().getSelectedPersona().isXbox)
		{
			user.platform = 'xbox';
		}
		else
		{
			user.platform = 'pc';
		}

		var form = $("<form method='POST' target='_blank' action='http://zever.eu/fut/import/' />");
		form.append($("<input type='hidden' name='version' value='0.1' />"));
		form.append($("<input type='hidden' name='clubData' value='" + JSON.stringify(players) + "' />"));
		form.append($("<input type='hidden' name='userData' value='" + JSON.stringify(user) + "' />"));
		form.append($("<input type='hidden' name='playerMeta' value='" + gConfigurationModel.getConfigString(models.ConfigurationModel.KEY_RESOURCE_ROOT) + utils.AssetLocator.getPlayerSearchFileURI() + "' />"));
		$('body').append(form);
		form.submit();
	}

	function registerEvents()
	{
		function onPageLoad(e)
		{
			console.log('Loaded <' + e.detail + '>');

			if (e.detail == 'LandingScreen')
			{
				addExportButton();
			}
		}

		function onAction(e)
		{
			console.log('Event <' + e.detail.event + '> was triggered');
		}

		document.addEventListener('futclubPageLoad', onPageLoad, false);
		document.addEventListener('futclubAction', onAction, false);
	}

	function decorateFutMethods()
	{
		var originalOnAnimateInScreenComplete = NavManager.onAnimateInScreenComplete;
		NavManager.onAnimateInScreenComplete = function() {
			originalOnAnimateInScreenComplete.apply(this);
			document.dispatchEvent(new CustomEvent('futclubPageLoad', {'detail': NavManager.getCurrentScreen().getScreenId()}));
		};

		var originalTriggerActions = FUITargetActionView.prototype._triggerActions;
		FUITargetActionView.prototype._triggerActions = function(event, data) {
			originalTriggerActions.apply(this, [event, data]);
			document.dispatchEvent(new CustomEvent('futclubAction', {'detail': {'event': event, 'data': data}}));
		};
	}


	constructor();

})();