FlokiTV / Eldarya Leilão

// ==UserScript==
// @name         Eldarya Leilão
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  -----
// @author       FlokiTV
// @match        http://www.eldarya.com.br/marketplace
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==


var att = false;
var lance = false;
$(document).ready(function() {
    barra();
    attClick();
    lanceClick();
    setInterval(function(){
        if(att){
            atualizar();
            if(lance){
                fazerLance();
            }
        }
    }, 2000);
});

//constroi a barra
function barra(){
    $('body')
        .append("<style>.dwRed {background: #ff6d6d} .dwGreen {background: #89ffa7} #dw_bar button{ border: 0; border-radius: 3px; padding: 3px 7px; height: 30px}</style>")
        .append("<div id='dw_bar'></div>")
        .find('#dw_bar')
        .css({
        'position':'fixed',
        'bottom': '0',
        'padding':'15px',
        'background':'rgba(255,255,255,.9)'
    });
    $('#dw_bar')
        .append('<button id="on_off" style="float:left">Atualizar <span>OFF</span></button>')
        .append('<div id="itemdetalhe"></div>')
        .find("#itemdetalhe")
        .css({
        'margin-left':'10px',
        'float':'left'
        });
    $("#itemdetalhe")
        .append('<button id="itemid">ItemId: <span></span></button>')
        .find("#itemid")
        .css({
        'margin-left':'10px',
        'float':'left'
        });
    $("#itemdetalhe")
        .append('<button id="name">Nome: <span></span></button>')
        .find("#name")
        .css({
        'margin-left':'10px',
        'float':'left'
        });
    $("#itemdetalhe")
        .append('<button id="atual">Atual: <span></span></button>')
        .find("#atual")
        .css({
        'margin-left':'10px',
        'float':'left'
        });
    $("#itemdetalhe")
        .append('<button id="maximo">Máximo: <input type="number"></button>')
        .find("#maximo")
        .css({
        'margin-left':'10px',
        'float':'left'
        });
    $("#itemdetalhe")
        .append('<button id="lance">Lance <span>OFF</span></button>')
        .find("#lance")
        .addClass('dwRed')
        .css({
        'margin-left':'10px',
        'float':'left'
        });
    $("#dw_bar")
        .append('<br><p id="status">Status: <span></span></p>')
        .find("#status")
        .css({
        'margin-top':'5px',
        'float':'left',
        'padding':'10px'
        });
}

function attClick(){
    $('#on_off').click(function(){
        if(att){
            att = false;
            $('#on_off span').text('OFF');
        }else{
            att = true;
            $('#on_off span').text('ON');
        }
    });
}

function lanceClick(){
    $('#lance').click(function(){
        if(lance){
            lance = false;
            $('#lance').removeClass('dwGreen').addClass('dwRed');
            $('#lance span').text('OFF');
        }else{
            lance = true;
            $('#lance').removeClass('dwRed').addClass('dwGreen');
            $('#lance span').text('ON');
        }
    });
}

function fazerLance(){
    bidId = parseInt($('#dw_bar #itemid span').text());
    bidPrice = parseInt($('#dw_bar #atual span').text()) + 1;
    bidMax = parseInt($("#maximo input").val());
    if(bidPrice <= bidMax){
        //$("#dw_bar #status span").text("id:"+bidId+" bidPrice:"+bidPrice+" Max:"+bidMax);
        $.post( "marketplace/newBid", { id: bidId, bidPrice: bidPrice } )
            .done(function(data){
            if(data.result == "error"){
                $("#dw_bar #status").removeClass('dwGreen').addClass('dwRed');
                $("#dw_bar #status span").text(data.data);
            }else{
                $('#dw_bar #status').removeClass('dwRed').addClass('dwGreen');
                $("#dw_bar #status span").text(data.data);
            }
            //console.log(data);
        });
    }else{
        $("#dw_bar #status span").text("Valor acima do permitido");
    }
}

function atualizar(){
    $('.marketplace-search-items').load(
        'http://www.eldarya.com.br/marketplace/ajax_search?type=&bodyLocation=&category=&rarity=&price=&guard=&from=0&to=6&page=1&name=',
        function(){
            getID();
            console.log('Atualizado');
        }
    );
}

function getID(){
    $(".marketplace-search-items li").click(function(){
        id = $(this).attr('data-itemid');
        $('#dw_bar #itemid span').text(id);
        $('#dw_bar #name span').text(ltrim($(this).find(".abstract-name").text()));
    });
    price_current = $("#currentPrice-item-"+$('#dw_bar #itemid span').text()).text();
    $('#dw_bar #atual span').text(ltrim(price_current));
}

function ltrim(str) {
		return str.replace(/^\s+/,"");
	}