// ==UserScript==
// @name SyndAnalyser
// @namespace https://github.com/MyRequiem/comfortablePlayingInGW
// @description Анализ активности синдиката.
// @id comfortablePlayingInGW@MyRequiem
// @updateURL https://raw.githubusercontent.com/MyRequiem/comfortablePlayingInGW/master/separatedScripts/SyndAnalyser/syndAnalyser.meta.js
// @downloadURL https://raw.githubusercontent.com/MyRequiem/comfortablePlayingInGW/master/separatedScripts/SyndAnalyser/syndAnalyser.user.js
// @include https://*gwars*/syndicate.php?id=*
// @grant none
// @license MIT
// @version 2.27-140522
// @author MyRequiem [https://www.gwars.io/info.php?id=2095458]
// ==/UserScript==
/*global unsafeWindow */
/*jslint browser: true, maxlen: 80, vars: true, nomen: true, regexp: true,
plusplus: true, continue: true, devel: true
*/
/*eslint-env browser */
/*eslint no-useless-escape: 'warn', linebreak-style: ['error', 'unix'],
quotes: ['error', 'single'], semi: ['error', 'always'],
eqeqeq: 'error', curly: 'error'
*/
/*jscs:disable requireMultipleVarDecl, requireVarDeclFirst */
/*jscs:disable disallowKeywords, disallowDanglingUnderscores */
/*jscs:disable validateIndentation */
(function () {
'use strict';
/**
* @class General
* @constructor
*/
var General = function () {
/**
* @property root
* @type {Object}
*/
this.root = this.getRoot();
/**
* @property doc
* @type {Object}
*/
this.doc = this.root.document;
/**
* @property loc
* @type {String}
*/
this.loc = this.root.location.href;
/**
* @property imgPath
* @type {String}
*/
this.imgPath = 'https://raw.githubusercontent.com/MyRequiem/' +
'comfortablePlayingInGW/master/imgs/';
/**
* @property domain
* @type {String}
*/
this.domain = this.doc.domain;
};
/**
* @lends General.prototype
*/
General.prototype = {
/**
* @method getRoot
* @return {Object}
*/
getRoot: function () {
var rt = typeof unsafeWindow;
return rt !== 'undefined' ? unsafeWindow : window;
},
/**
* @method $
* @param {String} id
* @return {HTMLElement|null}
*/
$: function (id) {
return this.doc.querySelector('#' + id);
}
};
var general = new General();
/**
* @class AjaxQuery
* @constructor
*/
var AjaxQuery = function () {
/**
* @method init
* @param {String} url
* @param {Function} onsuccess
* @param {Function} onfailure
*/
this.init = function (url, onsuccess, onfailure) {
var xmlHttpRequest = new XMLHttpRequest();
if (!xmlHttpRequest) {
general.root.console.log('Error create xmlHttpRequest !!!');
return;
}
xmlHttpRequest.open('GET', url, true);
xmlHttpRequest.send(null);
var timeout = general.root.setTimeout(function () {
xmlHttpRequest.abort();
}, 10000);
xmlHttpRequest.onreadystatechange = function () {
if (xmlHttpRequest.readyState === 4) {
clearTimeout(timeout);
if (xmlHttpRequest.status === 200) {
onsuccess(xmlHttpRequest);
} else {
onfailure();
}
}
};
};
};
/**
* @class GetStrDateNow
* @constructor
*/
var GetStrDateNow = function () {
/**
* @method init
* @return {String}
*/
this.init = function () {
var date = new Date(),
month = date.getMonth() + 1,
day = date.getDate();
return (day < 10 ? '0' + day : day) + '.' +
(month < 10 ? '0' + month : month) + '.' +
/20(\d+)/.exec(date.getFullYear().toString())[1];
};
};
/**
* @class GetTimestamp
* @constructor
*/
var GetTimestamp = function () {
/**
* @method init
* @param {String} val
* @return {int}
*/
this.init = function (val) {
var date = /(\d\d)\.(\d\d)\.(\d\d)/.exec(val);
if (!date) {
return 0;
}
var d = +date[1],
m = +date[2],
y = +date[3];
if (!d || d > 31 || !m || m > 12 || !y) {
return 0;
}
return new Date(2000 + y, m - 1, d).getTime();
};
};
/**
* @class SetPoints
* @constructor
*/
var SetPoints = function () {
/**
* @method init
* @param {String|int} num
* @param {String} separator
* @param {Boolean} flagSign
* @return {String}
*/
this.init = function (num, separator, flagSign) {
var x = +num,
sign = x > 0 && flagSign ? '+' : '',
i;
if (isNaN(x)) {
return 'NaN';
}
x = x.toString().split('').reverse();
for (i = 2; i < x.length; i += 3) {
if (x[i] === '-' || !x[i + 1] || x[i + 1] === '-') {
break;
}
x[i] = separator + x[i];
}
return sign + x.reverse().join('');
};
};
/**
* @class SyndAnalyser
* @constructor
*/
var SyndAnalyser = function () {
/**
* @property syndId
* @type {String}
*/
this.syndId = /\?id=(\d+)/.exec(general.loc)[1];
/**
* @property mainTable
* @type {Element}
*/
this.mainTable = general.doc.querySelector('center+br+table') ||
general.doc.querySelector('center+br+script+table');
/**
* @property tm
* @type {int}
*/
this.tm = 1500;
/**
* @property lastDate
* @type {String}
*/
this.lastDate = '';
/**
* @property from
* @type {int}
*/
this.from = 0;
/**
* @property to
* @type {int}
*/
this.to = 0;
/**
* @property mainData
* @type {Object|null}
*/
this.mainData = null;
/**
* @class MainData
* @constructor
*/
var MainData = function () {
/**
* @property pers
* @type {Array}
*/
this.pers = [];
/**
* @property allProtect
* @type {int}
*/
this.allProtect = 0;
/**
* @property allAttaks
* @type {int}
*/
this.allAttaks = 0;
/**
* @property allTake
* @type {int}
*/
this.allTake = 0;
/**
* @property allPut
* @type {int}
*/
this.allPut = 0;
/**
* @property takenSynd
* @type {Array}
*/
this.takenSynd = [];
/**
* @property dismissedSynd
* @type {Array}
*/
this.dismissedSynd = [];
/**
* @property realEstate
* @type {Object}
*/
this.realEstate = {
es: {mMoney: 0, pMoney: 0, mPTS: 0, pPTS: 0},
uran: {mMoney: 0, pMoney: 0, mPTS: 0, pPTS: 0},
bars: {mMoney: 0, pMoney: 0, mPTS: 0, pPTS: 0},
another: {mMoney: 0, pMoney: 0, mPTS: 0, pPTS: 0}
};
};
/**
* @method getLastDate
* @param {String} url
*/
this.getLastDate = function (url) {
var _url = url || 'https://' + general.domain +
'/syndicate.log.php?id=' + this.syndId + '&page_id=100500',
counter = general.$('syndAnalyseCounter'),
_this = this;
if (!url) {
counter.innerHTML = '2/1';
}
new AjaxQuery().init(_url, function (xml) {
var spanContent = general.doc.createElement('span');
spanContent.innerHTML = xml.responseText;
if (!url) {
counter.innerHTML = '2/2';
general.root.setTimeout(function () {
_this.getLastDate(spanContent.
querySelector('tr>td[class="greenbg"]' +
'[onclick*="window.location=\'' +
'/syndicate.log.php?"]:last-child>' +
'a[href*="/syndicate.log.php?id="]').href);
}, _this.tm);
} else {
var fonts = spanContent.
querySelectorAll('nobr>font[color="green"]');
_this.lastDate = /\d+.\d+.\d+/.
exec(fonts[fonts.length - 1].innerHTML)[0];
var inpFrom = general.$('inpDateFrom');
inpFrom.value = _this.lastDate;
inpFrom.disabled = false;
general.$('inpDateTo').disabled = false;
general.$('goSAnalyse').disabled = false;
general.$('syndAnalysePreloader').style.display = 'none';
}
}, function () {
var preloader = general.$('syndAnalysePreloader');
preloader.style.display = 'none';
preloader.parentNode.innerHTML += '
Ошибка ответа сервера...';
});
};
/**
* @method enterPress
* @param {Object} e
*/
this.enterPress = function (e) {
var ev = e || general.root.event;
if (ev.keyCode === 13) {
general.$('goSAnalyse').click();
}
};
/**
* @method getPers
* @param {Object} link
* @return {Object}
*/
this.getPers = function (link) {
var i;
for (i = 0; i < this.mainData.pers.length; i++) {
if (this.mainData.pers[i].link === link.href) {
return this.mainData.pers[i];
}
}
var pers = {
link: link.href,
name: link.firstElementChild.innerHTML,
attaks: 0,
putMoney: 0,
takeMoney: 0
};
this.mainData.pers.push(pers);
return pers;
};
/**
* @method getTypeLine
* @param {String} str
* @return {int|Object}
*/
this.getTypeLine = function (str) {
if (/инициировал нападение/i.test(str)) {
return 1;
}
if (/На контролируемый объект/i.test(str)) {
return 2;
}
if (/снято со счета/i.test(str)) {
return {takeOff: +/\$(\d+)/.exec(str)[1]};
}
if (/переведено на счет/i.test(str)) {
return {takeOn: +/\$(\d+)/.exec(str)[1]};
}
if (/на нападения на электростанции/i.test(str)) {
return 3;
}
if (/на нападения на рудники/i.test(str)) {
return 4;
}
if (/нападениях на остальную/i.test(str)) {
return 5;
}
if (/принят/i.test(str)) {
return 6;
}
if (/вышел/i.test(str)) {
return 7;
}
if (/в нападениях за контроль баров/i.test(str)) {
return 8;
}
// выгнали
if (/покинул синдикат/i.test(str)) {
return 9;
}
return 0;
};
/**
* @method getPersLink
* @param {Node} node
* @return {Element}
*/
this.getPersLink = function (node) {
// noinspection JSUnresolvedVariable
return node.parentNode.nextElementSibling.querySelector('a');
};
/**
* @method parseLine
* @param {String} str
* @param {Object} obj
*/
this.parseLine = function (str, obj) {
var rez = /\$(\d+).*и (\d+).*\$(\d+).*и (\d+)/.exec(str);
if (rez) {
obj.mMoney += +rez[1].replace(/,/g, '');
obj.mPTS += +rez[2].replace(/,/g, '');
obj.pMoney += +rez[3].replace(/,/g, '');
obj.pPTS += +rez[4].replace(/,/g, '');
}
};
/**
* @method getRealEstateStr
* @param {Object} obj
* @param {String} name
* @param {String} title
* @param {String} str1
* @param {String} str2
* @param {String} str3
* @return {String} str
*/
this.getRealEstateStr = function (obj, name, title, str1, str2, str3) {
var setPoints = new SetPoints().init;
return title + name + str3 +
str1 + 'Потрачено гб' + str2 + '$' +
setPoints(obj.mMoney, '\'', false) + str3 + str1 +
'Заработано гб' + str2 + '$' +
setPoints(obj.pMoney, '\'', false) + str3 +
str1 + 'Итого:' + str2 +
'$' +
setPoints(obj.pMoney - obj.mMoney, '\'', true) + '' +
str3 + str1 + 'Потрачено PTS' + str2 +
setPoints(obj.mPTS, '\'', false) + str3 +
str1 + 'Заработано PTS' + str2 +
setPoints(obj.pPTS, '\'', false) + str3 + str1 +
'Итого:' + str2 +
'' +
setPoints(obj.pPTS - obj.mPTS, '\'', true) + '' +
str3;
};
/**
* @method showRezult
*/
this.showRezult = function () {
this.sortPers();
var setPoints = new SetPoints().init,
main = this.mainData,
str1 = '