NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript== // @name RingsDB Utils // @namespace http://franco.nom.es // @version 0.4 // @description Utilidades para RingsDB // @author fafranco82 // @match http://ringsdb.com/* // @require https://cdnjs.cloudflare.com/ajax/libs/jquery.rest/1.0.2/jquery.rest.min.js // @require https://cdn.rawgit.com/caolan/async/master/dist/async.min.js // @require https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.min.js // @require https://cdn.jsdelivr.net/lodash/4.13.1/lodash.min.js // @require https://cdn.rawgit.com/padolsey-archive/jquery.fn/master/sortElements/jquery.sortElements.js // @grant none // ==/UserScript== (function($) { 'use strict'; var lo = _.noConflict(); var restapi = new $.RestClient('http://api.franco.nom.es/collections/'); restapi.add('ringsdb', {url: 'ringsdb-es'}); var ringsdbapi = new $.RestClient('http://ringsdb.com/api/public/'); ringsdbapi.add('cards', { stripTrailingSlash: true }); ringsdbapi.add('card', { stripTrailingSlash: true }); var nest = function(seq, keys) { if(!keys.length) { return seq; } else { var first = keys[0]; var rest = keys.slice(1); return lo.mapValues(lo.groupBy(seq, first), function(value) { return nest(value, rest); }); } }; lo.mixin({nest: nest}); $(document).ready(function($) { $('nav.navbar').removeClass('navbar-static-top').addClass('navbar-fixed-top'); $('body').css('margin-top', '90px'); var options = { links: { 'translate': { text: 'Traducir', click: function() { var $d = loading('Traduciendo...'); async.each($('[data-code]'), function(elem, done) { var code = $(elem).data('code'); restapi.ringsdb.read(code).done(function(card) { if(card) $(elem).text(card.name); done(); }); }, function(err) { $d.close(); }); } }, 'position': { text: 'Posicionar', click: function() { var $d = loading('Calculando posiciones...'); var sphere_codes = {'leadership': 'L', 'spirit': 'E', 'tactics': 'T', 'lore': 'S', 'neutral': 'N', 'baggins': 'B', 'fellowship': 'C'}; var type_codes = {'hero': 'H', 'ally': 'A', 'attachment': 'V', 'event': 'E'}; ringsdbapi.cards.read().done(function(cards) { cards = lo(cards).map(function(card) { card.saga = card.code.length == 5 ? 0 : 1; return card; }).orderBy(['sphere_code','type_code','saga','code']).nest(['sphere_code','type_code','saga']).value(); async.each($('[data-code]'), function(elem, done) { var code = $(elem).data('code'); ringsdbapi.card.read(code).done(function(card) { card.saga = card.code.length == 5 ? 0 : 1; var idx = lo.findIndex(cards[card.sphere_code][card.type_code][card.saga], {code: card.code}); var sideSize = card.type_code == 'hero' ? 9 : 3; //var sagaOffset = card.saga ? parseInt(cards[card.sphere_code][card.type_code][0].length / (sideSize*2)) : 0; var sheet = parseInt(idx / (sideSize*2)) + 1;// + sagaOffset; var sheet_idx = idx % (sideSize*2); var side = parseInt(sheet_idx / sideSize); var row = (sheet_idx % sideSize) + 1; var text = '[' + sphere_codes[card.sphere_code]+type_codes[card.type_code]+'-'+sheet+(side ? 'B' : 'A')+row + ']'; $(elem).prevAll('span.position').remove(); var span = $('<span>').addClass('position fg-'+card.sphere_code).css('margin-right','0.1em').text(text).insertBefore(elem); if(card.saga) span.css('text-decoration','underline'); if(card.type_code!='hero' && card.is_unique) span.css('font-weight', 'bold'); if(card.type_code=='hero' && span.closest('.deck-hero').length==1) $('<br>').insertAfter(span); done(); }); }, function(err) { $d.close(); }); }); } }, 'order': { text: 'Ordenar', click: function() { var tw = {'hero': 0, 'ally': 1, 'attachment': 2, 'event': 3, 'treasure': 4}; var sw = ['leadership', 'spirit', 'tactics', 'lore', 'baggins', 'fellowship', 'neutral']; var $d = loading('Ordenando...'); ringsdbapi.cards.read().done(function(cards) { cards = lo(cards).keyBy('code').value(); async.each($('div > [data-code]').closest('div').parent().closest('div'), function(root, done) { $('div > [data-code]', root).sortElements(function(a, b) { var ca = cards[$(a).data('code')]; var cb = cards[$(b).data('code')]; if(ca.sphere_code==cb.sphere_code) { if(ca.type_code==cb.type_code) { return ca.code < cb.code ? -1 : (ca.code > cb.code ? 1 : 0); } else { return tw[ca.type_code] < tw[cb.type_code] ? -1 : 1; } } else { return sw.indexOf(ca.sphere_code) < sw.indexOf(cb.sphere_code) ? -1 : 1; } }, function() { return $(this).closest('div').get(0); }); done(); }, function(err) { $d.close(); }); }); } } } }; var dropdown = Handlebars.compile( '<li> ' + ' <a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false"> ' + ' <span class="fa fa-cog"></span> ' + ' <span class="caret"></span> ' + ' </a> ' + ' <ul class="dropdown-menu"> ' + ' {{#each links}} ' + ' <li> ' + ' <a class="util" href="#" data-link="{{@key}}">{{this.text}}</a> ' + ' </li> ' + ' {{/each}} ' + ' </ul> ' + '</li> '); $(dropdown(options)).appendTo('ul.navbar-right').on('click', 'a.util', function(e) { options.links[$(e.target).data('link')].click(); }); }); var loadingHtml = Handlebars.compile( '<div class="modal"> ' + ' <div class="modal-dialog"> ' + ' <div class="modal-content"> ' + ' <div class="modal-body"> ' + ' <div class="row"> ' + ' <div class="col-xs-12 text-center"> ' + ' <span class="fa fa-spinner fa-spin fa-5x"></span> ' + ' </div> ' + ' </div> ' + ' <div class="row"> ' + ' <div class="col-xs-12 text-center"> ' + ' <h4>{{texto}}</h4> ' + ' </div> ' + ' </div> ' + ' </div> ' + ' </div> ' + ' </div> ' + '</div> '); function loading(text) { var $d = $(loadingHtml({texto:text})).appendTo('body').modal(); return { "close": function() { $d.modal('hide').remove(); } }; } })(jQuery);