Greasy Fork is available in English.

MLB The Show Nation Profit Reporter

Calculates the current profitability of a card and auto-fills the text box for Buy/Sell Orders with +/- 1 Stub. DOES NOT AUTOMATE ORDERS AND NEVER WILL!

Version au 28/03/2019. Voir la dernière version.

// ==UserScript==
// @name         MLB The Show Nation Profit Reporter
// @namespace    https://greasyfork.org/en/users/8332-sreyemnayr
// @version      2019.3.28.1
// @description  Calculates the current profitability of a card and auto-fills the text box for Buy/Sell Orders with +/- 1 Stub.  DOES NOT AUTOMATE ORDERS AND NEVER WILL!
// @author       sreyemnayr
// @run-at       document-end
// @match        https://mlb19.theshownation.com/community_market/listings/*
// @grant        unsafeWindow
// @require      https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js
// ==/UserScript==


(function() {
    'use strict';

    var page = document;

var card = {
	get buyForm() {
		return page.getElementById("create-buy-order-form");
	},
	get buyFormInput() {
		return page.querySelector("#create-buy-order-form input#price")
	},
	get buyFormButton() {
		return page.querySelector("#create-buy-order-form button")
	},
	get sellForm() {
		return page.getElementById("create-sell-order-form");
	},
	get sellFormInput() {
		return page.querySelector("#create-sell-order-form input#price")
	},
	get sellFormButton() {
		return page.querySelector("#create-sell-order-form button")
	},
	get sellPrice() {
        try {
            return parseInt(page.querySelectorAll('[action*="sell_now"] #price')[0].value);
        }
        catch(e) {
            return this.buyPrice
        }
	},
	get buyPrice() {
		try {
            return parseInt(page.querySelectorAll('[action*="buy_now"] #price')[0].value);
        }
        catch(e) {
            return 0
        }
	},
    get sellable() {
     return parseInt(document.evaluate("/html/body/div/div/div/div/div/div/div/div/div/div/div[contains(.,'Sellable |')]").iterateNext().textContent.replace("Sellable | ",""));
    }

}

function moveBuyForm() {
	page.getElementsByClassName("title-widget-main")[0].prepend(card.buyForm);
    card.buyFormButton.innerHTML = "Create Buy Order";
}

function moveSellForm() {
	page.getElementsByClassName("title-widget-main")[0].prepend(card.sellForm);
    card.sellFormButton.innerHTML = "Create Sell Order";
}

function moveForms() {
	moveBuyForm();
	moveSellForm();
}

function incrementBuy() {
	card.buyFormInput.value = card.sellPrice + 1;
}

function incrementSell() {
	card.sellFormInput.value = card.buyPrice - 1;
}

function sell() {
    if (card.sellable > 0){
	incrementSell();
	card.sellFormButton.click();
    }
}


function buy() {
	incrementBuy();
	card.buyFormButton.click();
}

function doc_keyUp(e) {
    console.log(e.keyCode);
    if (e.target.tagName.toUpperCase() != 'INPUT')
    {
        switch(e.keyCode)
        {
            case 66: // b buys
                buy();
                break;
            case 83: // s sells
                sell();
                break;
            default:
                break;
        }
  }
}
document.addEventListener('keyup', doc_keyUp, false);




    // console.log($('.toastr'));

    //var scatterChart = unsafeWindow.scatterChart;
    var dates = [];


    dates = [];
    for ( let td of document.querySelectorAll("#table-completed-orders tbody tr td:nth-child(2n)") ) {
        // console.log(td.textContent);
       //var thisDate = new Date(td.textContent);
        // 3/25/2019 11:09PM PDT
        var dateStringTemplate = "M/D/YYYY h:mmA Z";
        var thisDate = moment(td.textContent.replace(/PDT/g,"-0700"), dateStringTemplate);
        dates.push(thisDate);
    }
    // console.log(dates);
    // $('.completed-order').each(function(i){var thisDate = new Date($(this).find('.date').text() + ' UTC'); dates.push(thisDate);});

    //for(var i=0,l=scatterChart.data.datasets.length;i<l;i++){ var ds = scatterChart.data.datasets[i]; for(var ii=0, ll=ds.data.length;ii<ll;ii++){ var d = ds.data[ii]; var dte = new Date(d.x); dte.setHours(dte.getHours()+2); dates.push(dte); } }
    var numHour = 0;
    var numThreeHours = 0;
    var numToday = 0;
    var now = moment();
    var today = moment().hours(0);
    
    var OneHourAgo = moment().subtract(1, 'hours');
    var ThreeHoursAgo = moment().subtract(3, 'hours');
    
    for(var iii=0,lll=dates.length;iii<lll;iii++){
        //console.log(dates[iii]);
    if(dates[iii].isAfter(OneHourAgo))
    {
        numHour++;

    }
    if(dates[iii].isAfter(ThreeHoursAgo))
    {
        numThreeHours++;

    }
    if(dates[iii].isSame(moment(), 'day'))
    {
        numToday++;

    }
    }
    console.log("Hour", numHour);
    console.log("3 Hours", numThreeHours);
    console.log("Today", numToday);

    // var buyNowPrice = parseInt($($('.marketplace-card-order-now form button')[0]).text().replace(/,/g,"").match(/\d+/));
    var buyNowPrice = card.buyPrice;
    var sellNowPrice = card.sellPrice;
    // var sellNowPrice = parseInt($($('.marketplace-card-order-now form button')[1]).text().replace(/,/g,"").match(/\d+/));
    var profitMargin = parseInt(buyNowPrice * 0.90 - sellNowPrice);

     var li = document.createElement('div');
                    var btn = document.createElement('button');
                    var dateText = '';

                        now = moment();
                        var minDate=moment.min(dates);
                        //console.log(now);
                        //console.log(minDate);
                        //var diffMs = (Date.now() - minDate);
                        var diffMins = moment().diff(minDate);


                    if (dates.length >= diffMins){
                        var rate = Math.round( (dates.length / diffMins) * 100 ) / 100;
                        dateText = numHour + ' sold in the last ' + diffMins + ' minutes. (' + rate + ' sales per minute)';

                    }
                    else {
                        var rate = Math.round( ( ( diffMins / dates.length )  / 60000) * 100 ) / 100;
                       dateText = numHour + " this hour. ";
                        if (numHour != numToday){
                            dateText += numToday + " today. ";
                        }
                        dateText += "(sale every " + rate + ' minutes)';
                    }
                    dateText += "  -  Current Profit: " + profitMargin;
                    dateText += "  -  Sellable: " + card.sellable;
                    btn.appendChild(
                        document.createTextNode(dateText)
                    );
                    btn.className = 'button button-gray';
                    li.appendChild(btn);

    var mainHeading = document.querySelector(".title-widget-main");
    mainHeading.prepend(li)
    //$('.marketplace-main-heading').append(li);
    // $(li).css('display','flex');
    // $(li).css('float','left');

    moveBuyForm();
    if (card.sellable > 0) {
    moveSellForm(); }

    for ( var button of document.querySelectorAll("button[data-confirm='Are you sure?'") ) { button.dataset["confirm"] = false }

    for ( var table of Array.from(document.querySelectorAll(".title-widget-main table")).slice(0,-2) ) { mainHeading.prepend(table); table.style.backgroundColor = "white"; table.style.color = "black"; }

    mainHeading.prepend(document.querySelector(".currency-widget-inner"));

   incrementBuy();
    incrementSell();

    // var formsDiv = document.createElement('div');
    // mainHeading.append(formsDiv);
    //$(formsDiv).css('display','flex');
    // $(formsDiv).css('float','right');

    // $(formsDiv).append(card.buyForm);
    // $('#create-buy-order-form').css('width','30%');
    // $('#create-buy-order-form').css('float','left');
    // $('#create-buy-order-form #price').val(sellNowPrice + 1);
    // $(formsDiv).append(card.sellForm);
    //$('#create-sell-order-form').css('width','30%');
    //$('#create-sell-order-form').css('float','left');
    //$('#create-sell-order-form #price').val(buyNowPrice - 1);

    // $('.marketplace-card').css('clear','both');
    //if ($('.toast').find('.toast-message')[0].innerHTML == 'reCAPTCHA failed.'){
   // $('#create-buy-order-form').find('button').click();
   // }

    // setTimeout(function(){ window.location.href = 'https://mlb18.theshownation.com/orders'; }, (1000*60*3));



})();