// ==UserScript==
// @name Sect Misc
// @namespace http://userscripts.org/users/splurov/
// @include http://*sect.com/*
// ==/UserScript==
(function(){
var $ = unsafeWindow.$;
if (! $) {
return;
}
GM_addStyle('\
/* Exp Info, Stash Info */\
.tx6, .tx2 {font-size: 11px;}\
\
/* Fights Players */\
.stats2 {display: none;}\
.ten {font-size: 11px;}\
.ten tr {cursor: pointer;}\
.ten td {padding: 3px 0 !important;}\
.ten a {text-decoration: none !important;}\
.ten a strong {font-weight: normal;}\
.ten tr:hover td {background: url("http://sect.com/img/ten_green.png");}\
.ten .t3, .ten .t5 {font-size: 13px;}\
.ten .t1 span {font-size: 13px; font-weight: bold; color: #efeb77;}\
.ten .t6 {font-size: 13px; font-weight: bold; color: #fc3; text-align: right; padding-right: 10px !important;}\
.ten td:last-child {text-align: right;}\
.attackAll {text-align: right; border: 1px solid #191007; background: #000; padding: 3px 11px; float: right; margin: 5px 0 0;}\
\
/* Fights Gods */\
.inf100 {height: auto !important; padding: 10px 0 !important;}\
.block13 {height: auto !important;}\
.block13 img {display: none;}\
.block13 .tx14 {display: none;}\
\
/* Buildings and Market */\
.ten3 td {padding: 8px 0 0 !important;}\
.ten3 .s7 img {display: none;}\
.ten3 p {padding: 0 !important;}\
.ten3 select {margin: 0 0 10px !important;}\
.ten3 .buy {margin: 0 0 6px !important;}\
');
var $healId = GM_getValue('sect_heal_id');
if (! $healId) {
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState == 4) {
if (httpRequest.status == 200) {
$healId = httpRequest.responseText.match(/HPT_setAction\('([\da-f]+)'/)[1];
GM_setValue('sect_heal_id', $healId);
}
}
};
httpRequest.open('GET', 'http://sect.com/hospital/', true);
httpRequest.send('');
}
var $hideId = GM_getValue('sect_hide_id');
if (! $hideId) {
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState == 4) {
if (httpRequest.status == 200) {
$hideId = httpRequest.responseText.match(/STH_setAction\('([\da-f]+)'/)[1];
GM_setValue('sect_hide_id', $hideId);
}
}
};
httpRequest.open('GET', 'http://sect.com/stash/', true);
httpRequest.send('');
}
var $isAuto = GM_getValue('sect_is_auto'),
$isAutoAttack = GM_getValue('sect_is_auto_attack'),
Game = unsafeWindow.Game,
game = unsafeWindow.game,
$url = unsafeWindow.location.toString();
/* Resources Manager */
var rm = {
getCoins: function () {
return parseInt($('#chr_con').text().replace(/,/g, ''));
},
getHealth: function () {
return parseInt($('#chr_prp_health_val_cur').text());
},
getMaxHealth: function () {
return parseInt(game.char.prp_health);
},
getPhy: function () {
return parseInt($('#chr_prp_physical_val_cur').text());
},
getPsy: function () {
return parseInt($('#chr_prp_psychical_val_cur').text());
},
getRatio: function() {
return parseFloat(game.char.prp_fights_won / game.char.prp_fights_lost);
},
getExp: function() {
return parseInt($('#chr_exp').text().replace(/,/g, ''));
},
getExpLevel: function() {
return parseInt($('#chr_exp_lm').text().replace(/,/g, ''));
},
getStash: function() {
return parseInt(game.char.prp_stash);
},
getHideTax: function() {
return (game.char.specialization_id == '3') ? 0.95 : 0.85;
},
getStamina: function() {
return parseInt($('#chr_prp_stamina_val_cur').text());
},
doHeal: function() {
Game.HPT_setAction($healId, 'heal');
},
doHide: function(number) {
if (! $('#hide_coins').length) {
$(document.body).append('<input type="hidden" id="hide_coins" />');
}
$('#hide_coins').val(number);
Game.STH_setAction($hideId, 'hide');
rm.setStash(rm.getStash() + parseInt(number * rm.getHideTax()));
},
doRetrieve: function(number, callback) {
if (! $('#retrieve_coins').length) {
$(document.body).append('<input type="hidden" id="retrieve_coins" />');
}
if (number <= rm.getStash()) {
$('#retrieve_coins').val(number);
Game.STH_setAction($hideId, 'retrieve');
rm.setStash(rm.getStash() - number);
if (callback) {
var retrieveIntervalAttempts = 0;
retrieveInterval = setInterval(function(){
if (rm.getCoins() >= number) {
clearInterval(retrieveInterval);
callback();
}
else if (retrieveIntervalAttempts > 20) {
clearInterval(retrieveIntervalAttempts);
}
retrieveIntervalAttempts++;
}, 500);
}
}
},
doHealHide: function() {
var coins = rm.getCoins(),
health = rm.getHealth(),
healthMax = rm.getMaxHealth();
if (health < healthMax) {
var healPrice = (healthMax - health) * 2;
if (coins < healPrice) {
rm.doRetrieve(healPrice - coins);
var healIntervalAttempts = 0,
healInterval = setInterval(function(){
if (rm.getCoins() >= coins) {
clearInterval(healInterval);
rm.doHeal();
}
else if (healIntervalAttempts > 20) {
clearInterval(healInterval);
}
healIntervalAttempts++;
}, 500);
}
else {
rm.doHeal();
rm.doHide(coins - healPrice);
}
}
else if (coins >= 1) {
rm.doHide(coins);
}
},
setStash: function(number) {
game.char.prp_stash = number;
$('#coinsStashNumber').text($.number_format(number, Game.nf_params));
},
}
/* Menu Items & Info Blocks */
if ($('.menu').length) {
/* Remove Home & Stash Links */
$('.menu .last, .menu a:first-child').remove();
/* Add Auto Link */
var isAutoLink = document.createElement('a');
isAutoLink.appendChild(document.createTextNode(($isAuto ? 'Off' : 'On') + ' Auto'));
isAutoLink.setAttribute('href', 'http://sect.com/fight/');
isAutoLink.addEventListener('click', function(){
GM_setValue('sect_is_auto', ! $isAuto);
}, false);
$('.menu').append(isAutoLink);
if ($isAuto) {
var isAutoAttackLink = document.createElement('a');
isAutoAttackLink.appendChild(document.createTextNode(($isAutoAttack ? 'Off' : 'On') + ' Attack'));
isAutoAttackLink.setAttribute('href', 'http://sect.com/fight/');
isAutoAttackLink.addEventListener('click', function(){
GM_setValue('sect_is_auto_attack', ! $isAutoAttack);
}, false);
$('.menu').append(isAutoAttackLink);
}
/* Add Heal & Hide Link */
$('.menu').append(
$(document.createElement('a'))
.text('Heal & Hide')
.attr('href', '#')
.addClass('last')
.click(function(e){
rm.doHealHide();
e.preventDefault();
})
);
/* Replace Missions Link */
var missionId = GM_getValue('missionId');
if (missionId) {
$('.menu a:eq(1)').attr({'href': '/missions/' + missionId});
}
/* Exp to Next Level */
$('#chr_exp_lm').after('(<strong id="chr_exp_delta">' + (rm.getExpLevel() - rm.getExp()) + '</strong>)');
setInterval(function(){
$('#chr_exp_delta').text(rm.getExpLevel() - rm.getExp());
}, 2000);
/* Ratio */
var sectantsBlock = $('.tx5');
sectantsBlock.html(sectantsBlock.html().replace(/Sectants:/, rm.getRatio().toFixed(2) + ' | '));
/* Stash */
$('#chr_con').after(' / <span id="coinsStashNumber"></span>');
rm.setStash(rm.getStash());
}
/* Missions Checker */
if ($url.indexOf('missions') != -1) {
var mc = {
phy: rm.getPhy(),
psy: rm.getPsy(),
isLaunched: false,
run: function () {
$('.stats4 tr').each(function(){
var td = $('td:eq(1)', this),
q = $('td:eq(0) p:eq(2)', this).text(),
qDelim = q.indexOf('/');
if (
mc.phy >= mc.get('phy', td)
&& mc.psy >= mc.get('psy', td)
) {
if (parseInt(q.slice(0, qDelim)) != parseInt(q.slice(qDelim + 1, q.length))) {
$(this).css({background: 'url("http://sect.com/img/ten_red.png")'});
}
else {
$(this).css({background: 'url("http://sect.com/img/ten_green.png")'});
}
}
else {
$(this).css({background: 'none'});
}
if (! mc.isLaunched) {
$('td:eq(5) img:eq(0)', this).click(function(){
mc.redraw($(this).parent().parent().parent().children('td:eq(1)'));
});
}
});
if (! mc.isLaunched) {
$('.msn_suc .doitagain a').live('click', function(){
mc.redraw($(this).parent().parent().children('.res_spent'));
});
mc.isLaunched = true;
}
},
redraw: function(element) {
var phy = mc.get('phy', element),
psy = mc.get('psy', element);
if (phy <= mc.phy && psy <= mc.psy) {
mc.phy -= phy;
mc.psy -= psy;
mc.run();
}
},
get: function (what, where) {
var value = $('p span:contains(' + what + ')', where).prev().text();
if (value) {
return parseInt(value);
}
return 0;
}
};
mc.run();
/* Save Missions URL */
var missionId = $url.match(/missions\/(.+)$/);
if (missionId) {
GM_setValue('missionId', missionId[1]);
}
}
/* Storage Manager */
var sm = {
source: unsafeWindow.localStorage,
keys: {
isEnemy: 0,
id: 1,
hash: 2,
defence: 3,
time: 4,
},
get: function(name, what) {
var user = sm.source[name].split('|');
if (! what) {
return user;
}
return user[sm.keys[what]];
},
set: function(name, what, value) {
var user = sm.get(name);
user[sm.keys[what]] = value;
sm.source[name] = user.join('|');
},
setAll: function(name, isEnemy, id, hash, defence, time) {
sm.source[name] = [isEnemy, id, hash, defence, time].join('|');
},
}
doAttack = function(tr) {
Game.FTH_setAttack($(tr).attr('userId'), $(tr).attr('fightId'));
$('td', tr).css('textDecoration', 'line-through');
if (sm.source) {
var userIntervalAttempts = 0,
userInterval = setInterval(function(){
var fightWindow = $('div.msg_fight:eq(0) div.fight_power div.in span.g');
if (fightWindow.length) {
clearInterval(userInterval);
var userName = $('div.msg_fight:eq(0) div.name b a').text();
var userDefence = parseInt(fightWindow.text().replace(/,/g, ''));
sm.set(userName, 'defence', userDefence)
sm.set(userName, 'time', (new Date()).getTime());
}
else if (userIntervalAttempts > 10) {
clearInterval(userInterval);
}
userIntervalAttempts++;
}, 500);
}
}
doAttackAll = function(minStamina) {
if (rm.getStamina() < minStamina) {
return;
}
var currentDelta = 1000;
$('.ten .own').each(function(){
setTimeout('if (parseInt(document.getElementById("chr_prp_stamina_val_cur").textContent) > 0) {Game.FTH_setAttack(' + $(this).attr('userId') + ', "' + $(this).attr('fightId') + '");}', currentDelta);
currentDelta += 2000;
setTimeout(rm.doHealHide, currentDelta + 1000);
});
}
sortFighters = function(a, b) {
var first = parseInt($(a).attr('userDefence')),
second = parseInt($(b).attr('userDefence'));
if (first < second) {
return -1;
}
if (second < first) {
return 1;
}
return 0;
}
/* Fights Helper */
if ($url.indexOf('fight') != -1) {
$('.ten tr').each(function(){
$('td:eq(0) p:eq(0)', this).prepend('<span>' + parseFloat($('td:eq(0) span:eq(0)', this).text().replace(/[^\d.]+/, '')).toFixed(1) + ' </span>');
$('td:eq(0) p.knopa', this).remove();
var code = $('td:eq(5)', this).html().match(/(\d+), '([\da-f]{32})/),
name = $('td:eq(1) a strong', this).text(),
defence = 0;
if (sm.source) {
var name = $('td:eq(1)', this).text(),
time = (new Date()).getTime();
if (! sm.source[name] || sm.source[name].indexOf('|') == -1) {
sm.setAll(name, 0, code[1], code[2], defence, time);
}
else {
defence = sm.get(name, 'defence');
sm.set(name, 'time', time);
}
$('td:eq(5)', this).text($.number_format(defence, Game.nf_params));
}
$(this)
.attr({'userId': code[1], 'fightId': code[2], 'userDefence': defence, 'userName': name})
.removeClass('one')
.click(function(e){
doAttack(this);
e.preventDefault();
})
;
if (sm.source) {
var td = $(document.createElement('td'));
if (sm.get(name, 'isEnemy') == '0') {
td.append(
$(document.createElement('a'))
.attr({href: '#'}).text('[+]').click(function(e){
var userName = $(this).parent().parent().attr('userName');
sm.set(userName, 'isEnemy', '1');
$(this).remove();
e.preventDefault();
e.stopPropagation();
})
);
}
$(this).append(td);
}
});
var rows = $('.ten').find('tbody > tr').get();
rows.sort(sortFighters);
$(rows[0]).addClass('one');
$.each(rows, function(index, row) {
$('.ten tbody').append(row);
});
if (sm.source) {
for (var name in sm.source) {
var u = sm.source[name].split('|');
if (u[0] == '1') {
var remove = $(document.createElement('a'))
.attr({href: '#'})
.html('[−]')
.click(function(e){
var tr = $(this).parent().parent();
var userName = tr.attr('userName');
sm.set(userName, 'isEnemy', '0');
tr.remove();
e.preventDefault();
e.stopPropagation();
});
var tr = $(document.createElement('tr'))
.attr({'userId': u[1], 'fightId': u[2], 'userName': name, 'class': 'own', 'userDefence': u[3]})
.click(function(e){
doAttack(this);
e.preventDefault();
});
for (var i = 0; i <= 6; i++) {
var td = $(document.createElement('td'));
if (i == 1) {
td.text(name).addClass('t2');
}
else if (i == 2) {
var now = (new Date()).getTime();
var delta = (now - u[4]) / 1000;
var minutes = parseInt(delta / 60);
var seconds = parseInt(delta % 60);
td.attr({'colspan': 3}).text('Visible: ' + minutes + ' m ' + seconds + ' s ago');
i += 2;
}
else if (i == 5) {
td.text($.number_format(u[3], Game.nf_params))
.addClass('t6')
.css({'fontWeight': 'normal', 'color': '#8B611D'});
}
else if (i == 6) {
td.append(remove);
}
tr.append(td);
}
$('.ten tbody').append(tr);
}
}
var rows = $('.ten').find('tbody > tr.own').get();
rows.sort(sortFighters);
$.each(rows, function(index, row) {
$('.ten tbody').append(row);
});
$('.ten').after(
$(document.createElement('div'))
.addClass('attackAll')
.append(
$(document.createElement('a'))
.attr({'href': '#'}).text('Attack All').click(function(e){
doAttackAll(1);
e.preventDefault();
})
)
);
}
}
/* Buildings Helper */
if ($url.indexOf('buildings') != -1) {
var foundations = {};
$('.stats6:eq(0) tr').each(function(){
foundations[$('td.s2 strong', this).text()] = parseInt($('td.s4 #building_price', this).text().replace(/,/g, ''));
});
$('.stats6 tr').each(function(){
var reqText = $('td.s2 span.green3 strong', this).text(),
req = 0;
if (reqText) {
req = foundations[reqText];
}
var price = parseInt($('td.s4 #building_price', this).text().replace(/,/g, ''));
$('td:eq(2)', this).append('<p>' + ((price + req) / $('td.s3 span.green2', this).text().replace(/[^\d]+/g, '')).toFixed(2) + '</p>');
$('td.s4 #building_price', this).replaceWith(
$(document.createElement('a'))
.attr({'href': '#', 'price': price}).text($.number_format(price, Game.nf_params)).click(function(e){
var nextTd = $(this).parent().parent().parent().parent().next();
var buildData = nextTd.next().html().match(/Game\.BLD_setAction\((\d+), '([\da-f]+)', 'buy'\)/);
var buildId = buildData[1];
var buildHash = buildData[2];
rm.doRetrieve($(this).attr('price') * nextTd.find('#buy_qty').val(), function(){
Game.BLD_setAction(buildId, buildHash, 'buy');
});
e.preventDefault();
})
);
});
$('.buildings select').each(function(){
$(this).append('<option value="15">15</option><option value="20">20</option><option value="25">25</option><option value="30">30</option><option value="50">50</option><option value="100">100</option>');
});
}
/* Market Helper */
if ($url.indexOf('market') != -1) {
$('.stats6 tr').each(function(){
var price = parseInt($('td.t15 p.tx24 span.col4 strong', this).text().replace(/[^\d]+/g, ''));
$('td.t15 p.tx24 span.col4', this).empty().html('<strong></strong>').find('strong').append(
$(document.createElement('a'))
.attr({'href': '#', 'price': price}).text($.number_format(price, Game.nf_params)).click(function(e){
var nextTd = $(this).parent().parent().parent().parent().next();
var itemData = nextTd.next().html().match(/Game\.ITM_setAction\((\d+), '([\da-f]+)', 'buy'\)/);
var itemId = itemData[1];
var itemHash = itemData[2];
rm.doRetrieve($(this).attr('price') * nextTd.find('#buy_qty').val(), function(){
Game.ITM_setAction(itemId, itemHash, 'buy');
});
e.preventDefault();
})
).append(' coins');
});
}
/* Sectants Helper */
if ($url.indexOf('sectants') != -1) {
var inviteBlock = $('.nd3');
var inviteCode = inviteBlock.html().match(/[\da-f]{32}/)[0];
inviteBlock.find('a').attr('onclick', '').click(function(e){
var invites = $('#friend_invite_code').val().match(/([\da-f]{6})/g);
for (var i in invites) {
$('#friend_invite_code').val(invites[i]);
Game.FRD_setInvite(inviteCode);
}
e.preventDefault();
});
}
if ($isAuto && ! $url.match(/(?:missions|market|buildings|profile)/)) {
rm.doHealHide();
if ($isAutoAttack && $url.indexOf('fight') != -1) {
doAttackAll(5);
}
/*
setTimeout(function(){
location.href = 'http://sect.com/fight/';
}, (Math.floor(Math.random() * 3) + 1) * 60000); // floor(rand() * n - m + 1) + m
*/
}
})();