- // ==UserScript== 
- // @author streaming77 
- // @name Planer 
- // @version        1.0 
- // @description    streaming77. 
- // @include **es45.guerrastribales.es/game.php?*screen=info_village* 
- // ==/UserScript== 
- javascript: (function() { 
-   
-     var Store = function(a) { 
-         var c = {}; 
-         if (typeof(Storage) !== "undefined") { 
-             if (localStorage[a] && localStorage[a] !== 'null' && localStorage[a] !== '') { 
-                 c = JSON.parse(localStorage[a]); 
-             } 
-         } else alert('Browserul dumneavoastra nu este compatibil cu "localStorage"'); 
-         if (!(this instanceof Store)) return { 
-             c: c, 
-             e: a, 
-             Set: function(a, b) { 
-                 if (!b) delete this.c[a]; 
-                 else this.c[a] = b; 
-                 localStorage[this.e] = JSON.stringify(this.c); 
-                 return this; 
-             }, 
-             Get: function(a) { 
-                 if (!a) return this.c; 
-                 else return this.c[a]; 
-             } 
-         } 
-     }; 
-     jQuery.fn.center = function(parent) { 
-         if (parent) parent = this.parent(); 
-         else parent = window; 
-         var pos = ((Store('Loader').Get('SupremeComander') || {}).eclipW || { 
-             top: ((($(parent).height() - this.outerHeight()) / 2) + $(parent).scrollTop()), 
-             left: ((($(parent).width() - this.outerWidth()) / 2) + $(parent).scrollLeft()) 
-         }); 
-         this.css({ 
-             "position": "fixed", 
-             "top": (pos.top + "px"), 
-             "left": (pos.left + "px") 
-         }); 
-         return this; 
-     }; 
-   
-     function getSettings() { 
-             var store = Store('Loader').Get('SupremeComander') || { 
-                 settings: {} 
-             }; 
-             var nou = { 
-                 "units": { 
-                     "spear": 0, 
-                     "sword": 0, 
-                     "axe": 0, 
-                     "archer": 0, 
-                     "spy": 0, 
-                     "light": 0, 
-                     "marcher": 0, 
-                     "heavy": 0, 
-                     "ram": 0, 
-                     "catapult": 0 
-                 }, 
-                 "settings_active": true, 
-                 "building": ['wall'], 
-                 "autoselect": false, 
-                 "force_type": null, 
-                 "reserve": true, 
-                 "random_ms": 0, 
-                 "type": null, 
-                 "ms": 0 
-             }; 
-             var types = ['noble', 'fakes', 'nukes', 'support', 'other', 'reserve']; 
-             for (var i = 0; i < types.length; i++) { 
-                 if (!store.settings[types[i]]) store.settings[types[i]] = nou; 
-                 else { 
-                     types = null; 
-                     break; 
-                 } 
-             } 
-             if (types) { 
-                 Store('Loader').Set('SupremeComander', store); 
-                 return getSettings(); 
-             } else return store.settings; 
-         } 
-         /*if (!Store('Loader')['Get']('Autorizatie')['valida']) {return;}*/ 
-     var obj = Store('Loader').Get('SVPlaner') || {}, 
-         autostart = false, 
-         openin; 
-   
-     function fnAjaxRequest(url, sendMethod, params, type) { 
-         var error = null, 
-             payload = null; 
-         $.ajax({ 
-             "async": false, 
-             "url": url, 
-             "data": params, 
-             "dataType": type, 
-             "type": String(sendMethod || "GET").toUpperCase(), 
-             "error": function(req, status, err) { 
-                 error = "ajax: " + status 
-             }, 
-             "success": function(data, status, req) { 
-                 payload = data 
-             } 
-         }); 
-         if (error) { 
-             throw (error) 
-         } 
-         return payload 
-     } 
-   
-     function fnCreateConfig(name) { 
-         return $(fnAjaxRequest("/interface.php", "GET", { 
-             "func": name 
-         }, "xml")).find("config") 
-     } 
-   
-     function fnCreateUnitConfig() { 
-         return fnCreateConfig("get_unit_info") 
-     } 
-   
-     function addzero(i) { 
-         if (i < 10) { 
-             i = "0" + i; 
-         } 
-         return i; 
-     } 
-   
-     function fnTime(date, time) { 
-         return new Date(date[0] * 1, (date[1] - 1), date[2] * 1, time[0] * 1, time[1] * 1, time[2] * 1); 
-     } 
-   
-     function fnFormatMs(s) { 
-         function addZ(n) { 
-             return (n < 10 ? '0' : '') + n; 
-         } 
-         var ms = s % 1000; 
-         s = (s - ms) / 1000; 
-         var secs = s % 60; 
-         s = (s - secs) / 60; 
-         var mins = s % 60; 
-         var hrs = (s - mins) / 60; 
-         return hrs + ':' + addZ(mins) + ':' + addZ(secs); 
-     } 
-   
-     function fnCalculateLaunchTime(xa, xb, ya, yb, unit, landingTime) { 
-         var unitTime = fnCalculateTimeDistance(xa, xb, ya, yb, unit); 
-         var launchTime = new Date(); 
-         launchTime.setTime(landingTime.getTime() - unitTime); 
-         return launchTime; 
-     } 
-   
-     function fnServerTime() { 
-         var servertime = $("#serverTime").html().match(/\d+/g); 
-         var serverDate = $("#serverDate").html().match(/\d+/g); 
-         return new Date(serverDate[1] + "/" + serverDate[0] + "/" + serverDate[2] + " " + servertime.join(":")) * 1; 
-     } 
-   
-     function fnCalculateTimeDistance(xa, xb, ya, yb, unit) { 
-         var distance = fnCalculateDistance(xa, xb, ya, yb); 
-         var unitTime = (distance * unit); 
-         var hor = Math.floor(unitTime / 60); 
-         var min = Math.floor(unitTime) - (hor * 60); 
-         var seg = Math.round((unitTime - (hor * 60) - min) * 60); 
-         var seg = seg + (min * 60) + (hor * 3600); 
-         var msecsDuration = seg * 1000; 
-         return msecsDuration; 
-     } 
-   
-     function fnCalculateDistance(xa, xb, ya, yb) { 
-         var fields = Math.sqrt(Math.pow(xa - xb, 2) + Math.pow(ya - yb, 2)); 
-         return fields; 
-     } 
-   
-     function refil() { 
-         var a = {}; 
-         if (obj.defaults) { 
-             a = obj.defaults 
-         } else { 
-             var da = document.getElementById('arrivalDate'); 
-             var now = new Date(new Date() + (server_utc_diff * 1000)); 
-             a['date'] = now.getFullYear() + '-' + addzero(now.getMonth() + 1) + '-' + addzero(now.getDate() + 1); 
-         } 
-         return a; 
-     } 
-   
-     function getPlan() { 
-         if (autostart) clearTimeout(autostart); 
-         /*detalii coordinate*/ 
-        [b] var ca = VillageInfoData.coord; 
-         var cb = game_data.village.coord; 
-         var xa = VillageInfoData.x; 
-         var ya = VillageInfoData.y; 
-         var xb = game_data.village.x; 
-         var yb = game_data.village.y;[/b] 
-         /*unitatea selectata*/ 
-         var se = document.getElementById('units'); 
-         var us = se.options[se.selectedIndex].value * 1; 
-         var nm = se.options[se.selectedIndex].text.toUpperCase(); 
-         var outplan = document.getElementById('outplan'); 
-   
-         var da = document.getElementById('arrivalDate').value.match(/\d+/g); 
-         var or = document.getElementById('arrivalTime').value.match(/\d+/g); 
-         if (!da || !or) return; 
-         var lansare = fnTime(da, or); 
-   
-         var durata = fnCalculateTimeDistance(xa, xb, ya, yb, us); 
-         var deschide = ((lansare * 1 - durata) - 20000) - fnServerTime(); 
-         openin = (lansare * 1 - durata) - 20000; 
-         document.getElementById('durata').value = fnFormatMs(durata); 
-         document.getElementById('launchin').value = deschide > 0 ? fnFormatMs(deschide) : 'Depasit'; 
-         obj['defaults'] = { 
-             date: da.join('-'), 
-             time: or.join(':'), 
-             ms: document.getElementById('arrivalMs').value, 
-             unit: se.options[se.selectedIndex].id 
-         }; 
-         Store('Loader').Set('SVPlaner', obj); 
-         var lansare = fnCalculateLaunchTime(xa, xb, ya, yb, us, lansare); 
-   
-         var a = lansare.getFullYear(); 
-         var l = lansare.getMonth() + 1; 
-         var z = lansare.getDate(); 
-         var o = lansare.getHours(); 
-         var m = lansare.getMinutes(); 
-         var s = lansare.getSeconds(); 
-         l = addzero(l); 
-         z = addzero(z); 
-         o = addzero(o); 
-         m = addzero(m); 
-         s = addzero(s); 
-   
-         outplan.value = a + '.' + l + '.' + z + ' [b]' + o + ':' + m + ':' + s + '[/b] ' + nm + ' ' + cb + '  ->  ' + ca; 
-         var auto = document.getElementById('autostart'); 
-         var tip = document.getElementById('type'); 
-         auto.dataset['unit'] = nm; 
-         auto.dataset['sent'] = o + ':' + m + ':' + s; 
-         auto.dataset['type'] = tip.options[tip.selectedIndex].id; 
-         auto.dataset['link'] = VillageContext._urls.place.replace('__village__', VillageInfoData.id); 
-         auto.dataset['ms'] = obj['defaults'].ms; 
-         autostart = false; 
-     } 
-   
-     function append(el, str) { 
-         var div = document.createElement('div'); 
-         div.innerHTML = str; 
-         while (div.children.length > 0) { 
-             if (el['appendChild']) { 
-                 el.appendChild(div.children[0]) 
-             } else { 
-                 setTimeout(function() { 
-                     el.appendChild(div.children[0]) 
-                 }, 500) 
-             } 
-         } 
-     }; 
-     var speed = {}; 
-     var option = ''; 
-     var traducere = { 
-         "Lancier": "spear", 
-         "Spada": "sword", 
-         "Topoare": "axe", 
-         "Arcas": "archer", 
-         "Spion": "spy", 
-         "C.Usoara": "light", 
-         "C.Arcas": "marcher", 
-         "C.Grea": "heavy", 
-         "Berbec": "ram", 
-         "Catapulta": "catapult", 
-         "Paladin": "knight", 
-         "Nobil": "snob" 
-     }; 
-   
-     if (!obj.speed) { 
-         var unitConfig = fnCreateUnitConfig(); 
-         $(unitConfig).children().each(function(i, e) { 
-             speed[e.nodeName] = $(e).find("speed").text() 
-         }); 
-         obj['speed'] = speed; 
-         Store('Loader').Set('SVPlaner', obj); 
-     } else speed = obj['speed']; 
-     $.each(traducere, function(key, value) { 
-         var selected = ''; 
-         if (refil().unit && refil().unit == value) { 
-             selected = 'selected'; 
-         } else if (value == 'ram' && selected != '') { 
-             selected = 'selected'; 
-         } 
-         option += '<option value="' + speed[value] + '" id="' + value + '" ' + selected + '>' + key + '</option>'; 
-     }); 
-     var mainframe = '<div style="padding-top: 10px; clear:both">'; 
-     mainframe += '    <table class="vis" style="width:100%">'; 
-     mainframe += '        <tbody>'; 
-     mainframe += '            <tr>'; 
-     mainframe += '                <th>Sosire</th>'; 
-     mainframe += '                <th>Unitate</th>'; 
-     mainframe += '                <th>Durata</th>'; 
-     mainframe += '                <th>Deschide</th>'; 
-     mainframe += '            </tr>'; 
-     mainframe += '            <tr>'; 
-     mainframe += '                <td>'; 
-     mainframe += '                    <input type="date" id="arrivalDate" style="width: 120px;"  value="' + refil().date + '">'; 
-     mainframe += '                    <input type="text" id="arrivalTime" placeholder="00:00:00" value="' + (refil().time ? refil().time : '00:00:00') + '" style="width:45px;">'; 
-     mainframe += '                    <input type="text" id="arrivalMs" placeholder="000" value="' + (refil().ms ? refil().ms : '000') + '" style="width:20px;">'; 
-     mainframe += '                </td>'; 
-     mainframe += '                <td>'; 
-     mainframe += '                    <select id="units"  style="width:98%;">'; 
-     mainframe += option; 
-     mainframe += '                    </select>'; 
-     mainframe += '                </td>'; 
-     mainframe += '                <td>'; 
-     mainframe += '                    <input type="text" id="durata" placeholder="00:00:00" style="width:98%;" readonly>'; 
-     mainframe += '                </td>'; 
-     mainframe += '                <td>'; 
-     mainframe += '                    <input type="text" id="launchin" placeholder="00:00:00" style="width:98%;" readonly>'; 
-     mainframe += '                </td>'; 
-     mainframe += '            </tr>'; 
-     mainframe += '            <tr>'; 
-     mainframe += '                <td colspan="1">'; 
-     mainframe += '                    <input type="button" id="calculate" value="Calculeaza">'; 
-     mainframe += '                    <input type="button" id="autostart" value="Auto">'; 
-     mainframe += '                    <select id="type">'; 
-     mainframe += '                        <option id="other" selected>Other</option>'; 
-     mainframe += '                        <option id="noble">Noble</option>'; 
-     mainframe += '                        <option id="nukes">Nukes</option>'; 
-     mainframe += '                        <option id="fakes">Fakes</option>'; 
-     mainframe += '                        <option id="support">Support</option>'; 
-     mainframe += '                    </select>'; 
-     mainframe += '                    <input type="button" id="eclipsator" value="E.S">'; 
-     mainframe += '                </td>'; 
-     mainframe += '                <td colspan="3" style="width:60%;">'; 
-     mainframe += '                    <input type="text" id="outplan" style="width:99%;" ondblclick="this.select()">'; 
-     mainframe += '                </td>'; 
-     mainframe += '            </tr>'; 
-     mainframe += '        </tbody>'; 
-     mainframe += '    </table>'; 
-     mainframe += '</div>'; 
-     append(document.querySelector('#content_value > table > tbody > tr > td:nth-child(2)')/*.parentNode*/, mainframe); 
-     $('#arrivalDate, #arrivalTime, #units').on('change', function() { 
-         getPlan() 
-     }); 
-     $('#calculate').on('click', function() { 
-         getPlan() 
-     }); 
-     $('#eclipsator').on('click', function() { 
-         var tip = document.getElementById('type'); 
-         (function SettingsForThisTab(tipu) { 
-             var type = (document.querySelector(".memo-tab-selected") || false), 
-                 tipu = tipu || (type ? (type.innerText || type.textContent).trim() : ''), 
-                 supp = new RegExp(/Spri|spri|Apo|apo|Supp|supp/).test(tipu), 
-                 nobi = new RegExp(/nobil|Nobil|nobl|Nobl/).test(tipu), 
-                 nuke = new RegExp(/Real|real|nuke|Nuke/).test(tipu), 
-                 fake = new RegExp(/Fake|fake/).test(tipu), 
-                 rese = new RegExp(/reserve/).test(tipu), 
-                 conn = (nobi ? 'noble' : (fake ? 'fakes' : (supp ? 'support' : (nuke ? 'nukes' : (rese ? 'reserve' : 'other'))))), 
-                 store = getSettings(), 
-                 src = $('<div class="vis maincontainer" style="margin-bottom:0px;width:280px;padding:0 0 4px 0;border-radius: 6px 6px 0 0;z-index:999999;background: #ECD7AC;position:fixed;left: 545px;top: -274px;"><center style="text-shadow: #F4E4BC 0 0 5px;box-shadow: inset #7D510F 0 0 10px 0;margin: -1px;border: 1px solid #7D510F;padding: 2px;color: #000;border-radius: 6px 6px 0 0;">Setarile vor fi aplicate pentru: <select id="swich"></select></center><table class="vis" width="100%"><tbody></tbody></table></div>'), 
-                 cladiri = ['<option value="main">Cladirea principala</option>']; 
-             cladiri[1] = '<option value="barracks">Cazarma</option>'; 
-             cladiri[2] = '<option value="stable">Grajd</option>'; 
-             cladiri[3] = '<option value="garage">Atelier</option>'; 
-             cladiri[4] = '<option value="snob">Curte nobila</option>'; 
-             cladiri[5] = '<option value="smith">Fierarie</option>'; 
-             cladiri[6] = '<option value="place">Piata centrala</option>'; 
-             cladiri[7] = '<option value="statue">Statuie</option>'; 
-             cladiri[8] = '<option value="market">Tārg</option>'; 
-             cladiri[9] = '<option value="wood">Taietori de lemne</option>'; 
-             cladiri[10] = '<option value="stone">Mina de argila</option>'; 
-             cladiri[11] = '<option value="iron">Mina de fier</option>'; 
-             cladiri[12] = '<option value="farm">Ferma</option>'; 
-             cladiri[13] = '<option value="storage">Magazie</option>'; 
-             cladiri[14] = '<option value="wall">Zid</option>'; 
-             var setari = '<td rowspan="10"><div>Configuratia este activa: <input type="checkbox" class="settings_active"/></div>'; 
-             setari += '<div>Auto selecteaza trupele: <input type="checkbox" class="autoselect"/></div>'; 
-             setari += '<div>Lasa rezerva: <input type="checkbox" class="reserve"/></div>'; 
-             setari += '<div>La milisecundele: <input type="number" class="ms" style="width:64px;"/></div>'; 
-             setari += '<div>Interval random ms: <input type="number" class="random_ms" style="width:44px;"/></div>'; 
-             setari += '<div>Trimite comanda: <select  class="force_type"><option id="null">Auto</option><option id="attack">Attack</option><option id="support">Support</option></select></div>'; 
-             setari += '<div>Catapulte: <select class="building" style="height:140px;" multiple>' + cladiri.join("") + '</select></div></td>'; 
-             setari = $(setari); 
-             for (var tip in store) { 
-                 var s = ''; 
-                 if (conn == tip) { 
-                     s = 'selected'; 
-                     for (var setare in store[tip]) { 
-                         var element = setari.find('.' + setare); 
-                         if (conn == 'reserve') { 
-                             element.prop('disabled', true); 
-                             continue; 
-                         } 
-                         if (element.length > 0) { 
-                             if (setare == 'force_type') element.find('option').filter(function() { 
-                                 return (this.id == store[tip][setare]); 
-                             }).prop('selected', true); 
-                             if (setare == 'building') element.val(store[tip][setare]); 
-                             else if (setare.match(/ms/)) element.val(store[tip][setare]); 
-                             else element.prop('checked', store[tip][setare]); 
-                         } 
-                     } 
-                 } /*de facut: asta trebuie mutat inafara lui for*/ 
-                 src.find('#swich').change(function() { 
-                     var newsetting = $(this).find("option:selected").attr("value"); 
-                     $('.maincontainer').remove(); 
-                     SettingsForThisTab(newsetting); 
-                 }).append('<option value="' + tip + '" ' + s + '>' + tip + '</option>'); 
-             } 
-             for (var unit in store[conn].units) { 
-                 var value = store[conn].units[unit]; 
-                 var style = 'style="background:' + (value < 0 ? '#0F1' : (value > 0 ? '#FFDF00' : '#F00')) + ';border:#B09A2B 1px solid;border-radius:3px"'; 
-                 var tr = $('<tr></tr>'), 
-                     img = $('<td width="18"><img src="/graphic/unit/unit_' + unit + '.png?1" ' + style + '></td>').appendTo(tr), 
-                     input = $('<td width="52"><input style="width:48px;" type="number" min="' + (conn == 'reserve' ? 0 : -1) + '" id="' + unit + '" class="ec_u" value="' + value + '" autocomplete="off"></td>').appendTo(tr); 
-                 tr.appendTo(src.find('tbody')); 
-             } 
-             src.find('tr').first().append(setari); 
-             $('<input class="btn" type="button" value="Salveaza">').on('click', function() { 
-                 var settingsfor = $('#swich').find("option:selected").attr("value"), 
-                     units = $('.ec_u'), 
-                     settings_active = $('.settings_active').is(':checked'), 
-                     autoselect = $('.autoselect').is(':checked'), 
-                     reserve = $('.reserve').is(':checked'), 
-                     force_type = $('.force_type').find("option:selected")[0].id, 
-                     random_ms = $('.random_ms').val(), 
-                     building = $('.building').val(), 
-                     ms = $('.ms').val(); 
-                 for (var unit in store[settingsfor].units) { 
-                     store[settingsfor].units[unit] = units.filter(function() { 
-                         return this.id == unit; 
-                     })[0].value; 
-                 } 
-                 store[settingsfor].settings_active = settings_active; 
-                 store[settingsfor].autoselect = autoselect; 
-                 store[settingsfor].reserve = reserve; 
-                 store[settingsfor].force_type = force_type; 
-                 store[settingsfor].random_ms = random_ms; 
-                 store[settingsfor].ms = ms; 
-                 store[settingsfor].building = building; 
-                 var save_store = Store('Loader').Get('SupremeComander') || {}; 
-                 save_store['settings'] = store; 
-                 Store('Loader').Set('SupremeComander', save_store); 
-             }).appendTo(src); 
-             $('<input class="btn" onclick="$(\'.maincontainer\').remove()" type="button" value="Inchide">').appendTo(src); 
-             src.appendTo('body').draggable({ 
-                 handle: 'center', 
-                 drag: function() { 
-                     var store = Store('Loader').Get('SupremeComander') || {}; 
-                     store['eclipW'] = $(this).position(); 
-                     Store('Loader').Set('SupremeComander', store); 
-                 } 
-             }).center(false); 
-             $('.ec_u').on('change', function() { 
-                 var value = $(this).val() * 1; 
-                 $(this).parent().parent().find('img').css({ 
-                     background: (value < 0 ? '#0F1' : (value > 0 ? '#FFDF00' : '#F00')) 
-                 }); 
-             }); 
-         })(tip.options[tip.selectedIndex].id); 
-         return false; 
-     }); 
-     $('#autostart').on('click', function() { 
-         if (!autostart) { 
-             if (openin && (openin - fnServerTime()) > 0) { 
-                 var that = this; 
-                 var ref = null; 
-                 var opt = 'location=yes,menubar=yes,resizable=yes,toolbar=yes,'; 
-                 opt += 'scrollbars=yes,status=yes,titlebar=yes'; 
-                 opt += ',width=700,height=600'; 
-   
-                 var a = Store('Loader').Get('SupremeComander') || {}; 
-                 var b = a['targets'] ? a['targets'] : {}; 
-                 var default_ms = ((a.settings || {})[this.dataset.type] || {}).ms * 1 || this.dataset.ms * 1; 
-                 if ((this.dataset.ms * 1) > 0) default_ms = this.dataset.ms; 
-                 b[game_data.village.id] = { 
-                     from: 'info', 
-                     unit: this.dataset.unit, 
-                     type: this.dataset.type, 
-                     sent: this.dataset.sent, 
-                     ms: default_ms 
-                 }; 
-                 a['targets'] = b; 
-                 a['last'] = this.dataset.link; 
-                 Store('Loader').Set('SupremeComander', a); 
-                 (function Auto() { 
-                     var durata = openin - fnServerTime(); 
-                     document.getElementById('launchin').value = fnFormatMs(durata); 
-                     if (durata <= 0) { 
-                         autostart = false; 
-                         ref = window.open(that.dataset.link, '_blank', opt); 
-                         window.setTimeout(function() { 
-                             if (ref.closed) window.location.reload(); 
-                         }, 21000) 
-                     } else autostart = setTimeout(Auto, 500); 
-                 })(); 
-             } else if (!openin) { 
-                 getPlan(); 
-             } 
-         } else { 
-             clearTimeout(autostart); 
-             autostart = false; 
-         } 
-     }); 
- })() 
-