marceloorigoni.com.ar / Neuron Ball on Br-Ba

// ==UserScript==
// @name        Neuron Ball on Br-Ba
// @namespace   nb
// @description Extra functionality missing in the game, and discussed in the forums
// @include     http://www.neuronball.com/*/my-team/*
// @grant    none
// @version     1.1.0
// ==/UserScript==

// Functions

var players = my_team.players;
for(p = 0;p < players.length; p++){
    players[p].canvas = {};
}

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function set_neurons(){
		var cdnImg = 'http://cdn.neuronball.com.s3.amazonaws.com/img';
		var sort_options;
		var sort_mode = $('#sort_mode').attr('data-estate') * 1;
        switch(sort_type) {
            case "number":
                sort_options = function(a,b) { return (a.number-b.number) * sort_mode }; break;
            case "name":
                sort_options = function(a,b) { return (b.name < a.name ? 1 : (a.name < b.name ? -1 : 0)) * sort_mode }; break;
            case "charge":
                sort_options = function(a,b) { return (a.charge()-b.charge()) * sort_mode }; break;
            case "level":
                sort_options = function(a,b) { return (b.xp-a.xp) * sort_mode }; break;
            case "engaged":
                sort_options = function(a,b) { return (b.selected-a.selected) * sort_mode }; break;
            case "health":
                sort_options = function(a,b) { return (a.health-b.health) * sort_mode }; break;
			case "type":
				sort_options = function(a,b) { return (a.type_id-b.type_id) * sort_mode }; break;
			case "ai":
				sort_options = function(a,b) { return (a.ai_image.split('-')[1]-b.ai_image.split('-')[1]) * sort_mode }; break;
			case "price":
				sort_options = function(a,b) { return (a.recycle_value-b.recycle_value) * sort_mode }; break;
			case "capacity":
				sort_options = function(a,b) { return (a.capacity * a._charge - b.capacity * b._charge) * sort_mode }; break;
			case "level-up":
				sort_options = function(a,b) { return (a.level_completion_ratio-b.level_completion_ratio) * sort_mode }; break;				
            default:
                return; break;
        }
        my_team.players.sort(sort_options);

        $("#neurons_overview").empty();
        // boxes inclusion
        var boxes = 0;
        var $p1 = $("<div class='innerpage'/>");
        var $p2 = $("<div class='innerpage'/>");
        for(idx in my_team.players)
        {
            var nbox = $("#neuron_overview_template").html()
                .replace(/pid/g, my_team.players[idx].id)
                .replace("#up#", my_team.players[idx].points_to_spend > 0 ? " up" : "");
            
            if(boxes<10) { $p1.append(nbox); } else { $p2.append(nbox); }
            boxes++;
        }
        for(x=0; x<my_team.free_slots; x++) {
            var d = $("<div>").html('<a href="/es/market/" class="neuron-container"><span class="free-slot">Libre</span></a>').addClass("neuron-box").addClass("free-slot-box");
            if(boxes<10) { $p1.append(d); } else { $p2.append(d); }
            boxes++;
        }
        for(x=0; x<20-boxes; x++) {
            var d = $("<div>").html('<a href="/es/headquarters/" class="neuron-container"><img src="http://nbstatic.s3.amazonaws.com/img/lock26.png"></a>').addClass("neuron-box").addClass("locked-slot-box");
            $p2.append(d);
        }
        $("#neurons_overview").append($p1);
        $("#neurons_overview").append($p2);

        // neurons canvases
        for(idx in my_team.players)
        {
            my_team.players[idx].canvas = new PlayerView(document.getElementById('neuron_view_'+my_team.players[idx].id), new ViewSize(16));
            my_team.players[idx].canvas.setParams(my_team.players[idx]);
            my_team.players[idx].canvas.anim = true;

            $("#neuron_name_"+my_team.players[idx].id).html(my_team.players[idx].name);
            $("#neuron_level_"+my_team.players[idx].id).html("Nivel: "+my_team.players[idx].level);
            $("#neuron_advancement_"+my_team.players[idx].id).css("width", my_team.players[idx].level_completion_ratio*100+"%");
            $("#neuron_bat_"+my_team.players[idx].id).css("width", my_team.players[idx].charge()*100+"%");
			      $cpu = $('<img src="' + cdnImg + "/cpu/" + my_team.players[idx].cpu_image + '" style="z-index: 999; width: 24px; height: 24px; position: relative; opacity: 1; left: 24px; top: 71px;" />');
			      $bat = $('<img src="' + cdnImg + "/battery/" + my_team.players[idx].battery_image + '" style="z-index: 999; width: 24px; height: 24px; position: relative; opacity: 1; left: 24px; top: 71px;" />');
			      $ai = $('<img src="' + cdnImg + "/ai/" + my_team.players[idx].ai_image + '" style="z-index: 999; width: 24px; height: 24px; position: relative; opacity: 1; left: 24px; top: 71px;" />');
            $('#neuron_container_' + my_team.players[idx].id).append($bat);
			      $('#neuron_container_' + my_team.players[idx].id).append($ai);
            $('#neuron_container_' + my_team.players[idx].id).append($cpu);
		}			
        
        // neuron construction
        if(neuron_progress && $(".free-slot-box").length > 0) {
            $(".free-slot-box:first").removeClass("free-slot-box").addClass("in-progress-box");
            var tpl = $("#in_progress_neuron").html().replace("#title#", neuron_progress.name);
            $(".in-progress-box").html(tpl).on("click", function(evt){
                document.location.href = "/es/headquarters/";
                evt.stopPropagation();
            });
            clearInterval(neuron_progress.timer);
            neuron_progress.timer = setInterval(set_progress, 1000);
            
            neuron_progress.canvas.anim = false;
            neuron_progress.canvas = new PlayerView($(".in-progress-box").find("canvas").get(0), new ViewSize(16));
            neuron_progress.canvas.setParams({
                pattern: 20,
                color1: "#FFF",
                color2: "#FFF",
                number: 1,
                eyes: 1,
                booster_active: 5,
                type_id: neuron_progress.type|0
            });
            neuron_progress.canvas.anim = true;
        }
        
        $(".pageicon").removeClass("selected");
        $(".pageicon.p1").addClass("selected");
    };

/* Old function Only removes visually, can't save formation */
NB_Management.clear_all = function(){
   var p = 0;
   var players = my_team.players;
   for(p = 0;p < players.length; p++){
     if(players[p].selected){
       this.unselect_player(players[p].id);   
     }     
  }
}

/** New function, removes all but 1 **/

NB_Management.remove_all = function(){
  var p; 
  var players = my_team.players;
   for(p = 0;p < players.length; p++){
     if(players[p].selected && this.update_selected() > 1){       
       selected_player = players[p].id;
       this.select_player(players[p].id, true);
       this.unselect_player(players[p].id);   
     }     
  }
  this.update_positions();
  this.autosave_formation(1);
}

NB_Management.readPositions = function(){
  json_to_post = [];
   for (idx in my_team.players) {
     if (my_team.players[idx].selected) {
        if (my_team.players[idx].on_sale) {
            my_team.players[idx].on_sale = false;
            $("#autoremove_warning").fadeIn().delay(3000).fadeOut();
            NB_Management.show_neuron_details(my_team.players[idx].id);
        }
        json_to_post.push({
            id: my_team.players[idx].id,
            x: my_team.players[idx].position_x,
            y: my_team.players[idx].position_y,
        });
    }
  };
  return json_to_post;
}

NB_Management.setPositions = function(j){
  for (idx in my_team.players) {
    var s = false;
    var pid = my_team.players[idx].id
    for (sel in j) {  
      if(j[sel].id == pid){
       s = true;
       x = j[sel].x;
       y = j[sel].y;
      }
    }
    my_team.players[idx].selected = s;
    if(s){
        my_team.players[idx].position_x = x;
        my_team.players[idx].position_y = y;
        this.select_player(pid, true);
    }
  }
  this.update_positions();
  this.autosave_formation(this.update_selected());  
}

// Elements Creation

var $rem_all = $('<a href="#" id="remove_player_all" class="unselect-player" style="display: block;left: 0">Remove All</a>');
var $sm = $('<div id="sort_menu_ext" class="order-zone pull-left"></div>');
var $tm = $('<div id="tactis_menu"></div>');
var $tt = $('<span>Tactic:</span>');
var $t1 = $('<a href="#" class="tactic_btn" id="tactic_one" data-id="1">1</a>');
var $t2 = $('<a href="#" class="tactic_btn" id="tactic_two" data-id="2">2</a>');
var $t3 = $('<a href="#" class="tactic_btn" id="tactic_three" data-id="3">3</a>');
var $t4 = $('<a href="#" class="tactic_btn" id="tactic_four" data-id="4">4</a>');
var $t5 = $('<a href="#" id="tactic_save" data-estate="1"><i class="fa fa-floppy-o"></i></a>');
var $st = $('<span class="title">Adv Sort</span>');
var $sortMode = $('<div id="sort_mode" data-estate="1"><i class="fa fa-sort-numeric-asc"></i></div>');
var $sortType = $('<div class="ordericon advordericon" data-sort="type"><i class="fa fa-info-circle"></i></div>');
var $sortIA = $('<div class="ordericon advordericon" data-sort="ai"><i class="fa fa-tag"></i></div>');
var $sortPrice = $('<div class="ordericon advordericon" data-sort="price"><i class="fa fa-btc"></i></div>');
var $sortCapacity = $('<div class="ordericon advordericon" data-sort="capacity"><i class="fa fa-bolt"></i></div>');
var $sortLevelUp = $('<div class="ordericon advordericon" data-sort="level-up"><i class="fa fa-plus-circle"></i></div>');

var estate = readCookie("save_estate");
if(estate == "0" || estate == 0){
	$t5.attr('data-estate', 0);
	$t5.html('<i class="fa fa-folder-open-o"></i>');
}

// Elements adoption

$('#position_zone').append($rem_all);
$tm.prepend($tt);
$tm.prepend($t1);
$tm.prepend($t2);
$tm.prepend($t3);
$tm.prepend($t4);
$tm.prepend($t5);

$sm.prepend($st);
$sm.append($sortMode);
$sm.append($sortType);
$sm.append($sortIA);
$sm.append($sortPrice);
$sm.append($sortCapacity);
$sm.append($sortLevelUp);

$('#_team_management').prepend($sm);
$('#_team_management').prepend($tm);

//Styles

$sm.css({
  'float':'left',
  'width': '510px',
  'height': '25px',
  'margin': '0 0 4px 0'
});

$tm.css({
  'float':'right',
  'width': '440px',
  'height': '25px',
  'margin': '0 0 4px 0'
});

$tt.css({
    'background': '#222931 none repeat scroll 0 0',
    'border-radius': '20px',
    'color': '#5bc0de',
    'display': 'inline-block',
    'font-family': 'Orbitron,Verdana',
    'line-height': '25px',
    'text-align': 'center',
  'float':'left',
  'width': '180px',
  'height': '25px'
});

$('#_team_management #tactis_menu a').css({
    'background': '#222931 none repeat scroll 0 0',
    'border-radius': '20px',
    'color': '#5bc0de',
    'display': 'inline-block',
    'font-family': 'Orbitron,Verdana',
  'float': 'right',
    'line-height': '25px',
    'text-align': 'center',
  'width': '50px'});

$('#position_warning').css({
  'z-index': 99999, 
  'left': '60px',
  'bottom': '-20px'});

$st.css({
    'background': 'rgba(0, 0, 0, 0.5) none repeat scroll 0 0',
    'border': '2px solid transparent',
    'border-radius': '5px 0 0 5px',
    'float': 'left',
    'margin-right': '14px',
    'padding': '4px 10px',
    'position': 'relative',
    'text-align': 'center',
    'width': '100px'
});

$sortMode.css({
    'background': 'rgba(0, 0, 0, 0.5) none repeat scroll 0 0',
    'border': '2px solid transparent',
    'border-radius': '5px',
    'cursor': 'pointer',
    'display': 'inline-block',
    'margin-right': '1px',
    'padding': '4px 5px',
    'text-align': 'center',
    'width': '35px'
});

$('#sort_menu_ext .advordericon').css({
    'background': 'rgba(0, 0, 0, 0.5) none repeat scroll 0 0',
    'border': '2px solid transparent',
    'border-radius': '5px',
    'cursor': 'pointer',
    'display': 'inline-block',
    'margin-right': '1px',
    'padding': '4px 5px',
    'text-align': 'center',
    'width': '35px'
});

$( "<style>.advordericon.selected{border: 2px solid #b6ffff !important;color: #5bc0de !important;}</style>" ).appendTo( "head" )

//Events

$rem_all.click(function(e){e.preventDefault();NB_Management.remove_all()});

$t5.click(function(e){
  e.preventDefault();
  var estate = $(this).attr('data-estate');
  if(estate == "1" || estate == 1){
    $(this).attr('data-estate', 0);
    $(this).html('<i class="fa fa-folder-open-o"></i>');
	 estate = 0;
  }else{
    $(this).attr('data-estate',1);
    $(this).html('<i class="fa fa-floppy-o"></i>')
	 estate = 1;
  }
  
  createCookie("save_estate", estate, 365);
});	


$('.tactic_btn').click(function(e){
  e.preventDefault();
  var id = $(this).attr('data-id');
  if($("#tactic_save").attr('data-estate') == "1"){
     var j = NB_Management.readPositions();
    createCookie("BrBa_tactic_" + id, JSON.stringify(j), 365);
  }else{
    var j = readCookie("BrBa_tactic_" + id);
    NB_Management.setPositions(JSON.parse(j));
  }
});	

$sortMode.click(function(e){
  var estate = $(this).attr('data-estate');
  if(estate == "1" || estate == 1){
    $(this).attr('data-estate', -1);
    $(this).html('<i class="fa fa-sort-numeric-desc"></i>');
  }else{
    $(this).attr('data-estate',1);
    $(this).html('<i class="fa fa-sort-numeric-asc"></i>')
  }
    $(".ordericon.selected").click();
});

$(".ordericon").off("click");
$(".ordericon").on("click", function() {
  //if($(this).hasClass("selected")) { return; }
  $(".ordericon").removeClass("selected");
  $(this).addClass("selected");

  $("#neuron_informations").hide();
  sort_type = $(this).attr("data-sort");
  localStorage.setItem("neuronsortAdv", sort_type);
  for(idx in my_team.players) { my_team.players[idx].canvas.anim = false; }
  set_neurons();
  NB_Management.update_selected();
});

var sort_type = localStorage.getItem("neuronsortAdv") || "number";
$(".ordericon").removeClass("selected");
$(".ordericon[data-sort="+sort_type+"]").addClass("selected");
set_neurons();