Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Company assistant
- // @namespace Company assistant
- // @author --
- // @description Various features to assist a company director
- // @include *.torn.com/companies.php*
- // @version 1.8.3
- // @grant none
- // @require http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js
- // @require http://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js
- // ==/UserScript==
- this.$ = this.jQuery = jQuery.noConflict(true);
- (function(XMLHttpRequest) {
- 'use strict';
- var COMPANIES = {
- 'Oil Rig': {
- positions: {
- 'Driller': {'primary': {'stat': 'man', 'cap': 150000}, 'secondary': {'stat': 'int', 'cap': 75000}},
- 'Roughneck': {'primary': {'stat': 'man', 'cap': 75000}, 'secondary': {'stat': 'end', 'cap': 37500}},
- 'Derrick Hand': {'primary': {'stat': 'man', 'cap': 94000}, 'secondary': {'stat': 'end', 'cap': 47000}},
- 'Secretary': {'primary': {'stat': 'end', 'cap': 112500}, 'secondary': {'stat': 'int', 'cap': 56250}},
- 'Inspector': {'primary': {'stat': 'int', 'cap': 225000}, 'secondary': {'stat': 'end', 'cap': 112500}},
- 'Sales Executive': {'primary': {'stat': 'int', 'cap': 131500}, 'secondary': {'stat': 'end', 'cap': 65750}},
- 'Motor Hand': {'primary': {'stat': 'man', 'cap': 112500}, 'secondary': {'stat': 'int', 'cap': 56250}}
- }
- },
- 'Television Network': {
- positions: {
- 'Producer': {'primary': {'stat': 'int', 'cap': 99000}, 'secondary': {'stat': 'end', 'cap': 49500}},
- 'Programmer': {'primary': {'stat': 'int', 'cap': 66000}, 'secondary': {'stat': 'end', 'cap': 33000}},
- 'Camera Operator': {'primary': {'stat': 'int', 'cap': 49500}, 'secondary': {'stat': 'man', 'cap': 24750}},
- 'Sales Executive': {'primary': {'stat': 'end', 'cap': 49500}, 'secondary': {'stat': 'int', 'cap': 24750}},
- 'Cleaner': {'primary': {'stat': 'man', 'cap': 33000}, 'secondary': {'stat': 'end', 'cap': 16500}},
- 'Attorney': {'primary': {'stat': 'int', 'cap': 132000}, 'secondary': {'stat': 'end', 'cap': 66000}},
- 'Secretary': {'primary': {'stat': 'end', 'cap': 99000}, 'secondary': {'stat': 'int', 'cap': 49500}},
- 'Marketer': {'primary': {'stat': 'int', 'cap': 132000}, 'secondary': {'stat': 'end', 'cap': 66000}},
- 'Writer': {'primary': {'stat': 'int', 'cap': 115500}, 'secondary': {'stat': 'end', 'cap': 57570}},
- 'Stagehand': {'primary': {'stat': 'man', 'cap': 33000}, 'secondary': {'stat': 'end', 'cap': 16500}},
- 'Anchor': {'primary': {'stat': 'int', 'cap': 132000}, 'secondary': {'stat': 'end', 'cap': 66000}},
- 'Reporter': {'primary': {'stat': 'int', 'cap': 82500}, 'secondary': {'stat': 'end', 'cap': 41250}}
- }
- },
- 'Property Broker': {
- positions: {
- 'Property Broker': {'primary': {'stat': 'end', 'cap': 1500}, 'secondary': {'stat': 'int', 'cap': 750}},
- 'Valuation Specialist': {'primary': {'stat': 'int', 'cap': 3000}, 'secondary': {'stat': 'end', 'cap': 1500}},
- 'Associate Broker': {'primary': {'stat': 'end', 'cap': 500}, 'secondary': {'stat': 'int', 'cap': 250}},
- 'Cleaner': {'primary': {'stat': 'man', 'cap': 1000}, 'secondary': {'stat': 'end', 'cap': 500}},
- 'Team Manager': {'primary': {'stat': 'end', 'cap': 3000}, 'secondary': {'stat': 'int', 'cap': 1500}},
- 'Receptionist': {'primary': {'stat': 'end', 'cap': 2500}, 'secondary': {'stat': 'int', 'cap': 1250}},
- 'Graphic Designer': {'primary': {'stat': 'int', 'cap': 3000}, 'secondary': {'stat': 'end', 'cap': 1500}},
- 'Broker Support': {'primary': {'stat': 'int', 'cap': 4500}, 'secondary': {'stat': 'end', 'cap': 2250}}
- }
- },
- 'Restaurant': {
- positions: {
- 'Waiter': {'primary': {'stat': 'end', 'cap': 2500}, 'secondary': {'stat': 'man', 'cap': 1250}},
- 'Sous Chef': {'primary': {'stat': 'int', 'cap': 4000}, 'secondary': {'stat': 'end', 'cap': 2000}},
- 'Head Chef': {'primary': {'stat': 'end', 'cap': 5000}, 'secondary': {'stat': 'int', 'cap': 2500}},
- 'Cleaner': {'primary': {'stat': 'man', 'cap': 1500}, 'secondary': {'stat': 'end', 'cap': 750}},
- 'Kitchen Assistant': {'primary': {'stat': 'end', 'cap': 3000}, 'secondary': {'stat': 'int', 'cap': 1500}},
- 'Head Waiter': {'primary': {'stat': 'end', 'cap': 4000}, 'secondary': {'stat': 'int', 'cap': 2000}},
- 'Line Cook': {'primary': {'stat': 'int', 'cap': 2500}, 'secondary': {'stat': 'man', 'cap': 1250}},
- 'Chef': {'primary': {'stat': 'int', 'cap': 3000}, 'secondary': {'stat': 'man', 'cap': 1500}},
- 'Apprentice Chef': {'primary': {'stat': 'int', 'cap': 1500}, 'secondary': {'stat': 'man', 'cap': 750}},
- 'Dishwasher': {'primary': {'stat': 'man', 'cap': 1500}, 'secondary': {'stat': 'end', 'cap': 750}}
- }
- },
- 'Sweet Shop': {
- positions: {
- 'Confectionist': {'primary': {'stat': 'int', 'cap': 2500}, 'secondary': {'stat': 'end', 'cap': 1250}},
- 'Packager': {'primary': {'stat': 'end', 'cap': 1500}, 'secondary': {'stat': 'man', 'cap': 750}},
- 'Cleaner': {'primary': {'stat': 'man', 'cap': 1000}, 'secondary': {'stat': 'end', 'cap': 500}},
- 'Manager': {'primary': {'stat': 'end', 'cap': 4000}, 'secondary': {'stat': 'int', 'cap': 2000}},
- 'Bookkeeper': {'primary': {'stat': 'end', 'cap': 3000}, 'secondary': {'stat': 'int', 'cap': 1500}},
- 'Marketer': {'primary': {'stat': 'int', 'cap': 4000}, 'secondary': {'stat': 'end', 'cap': 2000}},
- 'Clerk': {'primary': {'stat': 'end', 'cap': 2000}, 'secondary': {'stat': 'man', 'cap': 1000}}
- }
- },
- 'Ladies Strip Club': {
- positions: {
- 'Male Stripper': {'primary': {'stat': 'end', 'cap': 14500}, 'secondary': {'stat': 'man', 'cap': 7250}},
- 'Security': {'primary': {'stat': 'man', 'cap': 29000}, 'secondary': {'stat': 'end', 'cap': 14500}},
- 'Cleaner': {'primary': {'stat': 'man', 'cap': 8500}, 'secondary': {'stat': 'end', 'cap': 4250}},
- 'Manager': {'primary': {'stat': 'end', 'cap': 33000}, 'secondary': {'stat': 'int', 'cap': 16500}},
- 'Bookkeeper': {'primary': {'stat': 'end', 'cap': 25000}, 'secondary': {'stat': 'int', 'cap': 12500}},
- 'Photographer': {'primary': {'stat': 'int', 'cap': 33000}, 'secondary': {'stat': 'end', 'cap': 16500}},
- }
- },
- 'Television Network ': {
- positions: {
- 'Producer': {'primary': {'stat': 'int', 'cap': 99000}, 'secondary': {'stat': 'end', 'cap': 49500}},
- 'Programmer': {'primary': {'int': 'man', 'cap': 66000}, 'secondary': {'stat': 'end', 'cap': 33000}},
- 'Camera Operator': {'primary': {'stat': 'int', 'cap': 49500}, 'secondary': {'stat': 'man', 'cap': 24750}},
- 'Sales Executive': {'primary': {'stat': 'end', 'cap': 49500}, 'secondary': {'stat': 'int', 'cap': 24750}},
- 'Cleaner': {'primary': {'stat': 'man', 'cap': 33000}, 'secondary': {'stat': 'end', 'cap': 16500}},
- 'Attorney': {'primary': {'stat': 'int', 'cap': 132000}, 'secondary': {'stat': 'end', 'cap': 66000}},
- 'Secretary': {'primary': {'stat': 'end', 'cap': 99000}, 'secondary': {'stat': 'int', 'cap': 49500}},
- 'Marketer': {'primary': {'stat': 'int', 'cap': 132000}, 'secondary': {'stat': 'end', 'cap': 66000}},
- 'Writer': {'primary': {'stat': 'int', 'cap': 115500}, 'secondary': {'stat': 'end', 'cap': 57750}},
- 'Stagehand': {'primary': {'stat': 'man', 'cap': 33000}, 'secondary': {'stat': 'end', 'cap': 16500}},
- 'Anchor': {'primary': {'stat': 'int', 'cap': 132000}, 'secondary': {'stat': 'end', 'cap': 66000}},
- 'Reporter': {'primary': {'stat': 'int', 'cap': 82500}, 'secondary': {'stat': 'end', 'cap': 41250}},
- }
- },
- 'Game Shop': {
- positions: {
- 'Game Advisor': {'primary': {'stat': 'int', 'cap': 4500}, 'secondary': {'stat': 'end', 'cap': 2250}},
- 'Cleaner': {'primary': {'stat': 'man', 'cap': 1500}, 'secondary': {'stat': 'end', 'cap': 750}},
- 'Store Manager': {'primary': {'stat': 'end', 'cap': 6000}, 'secondary': {'stat': 'int', 'cap': 3000}},
- 'Accountant': {'primary': {'stat': 'end', 'cap': 4500}, 'secondary': {'stat': 'int', 'cap': 2250}},
- 'Marketer': {'primary': {'stat': 'int', 'cap': 4000}, 'secondary': {'stat': 'end', 'cap': 2000}},
- 'Clerk': {'primary': {'stat': 'end', 'cap': 3000}, 'secondary': {'stat': 'man', 'cap': 1500}}
- }
- },
- };
- // set value of header 'X-Requested-With' to an empty string (necessary to access some pages)
- $.ajaxSettings.beforeSend = function(xhr){
- xhr.setRequestHeader('X-Requested-With', {toString: function(){ return ''; }});
- };
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- // Utilities
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * Adds CSS to the HEAD of the document
- * @param {string} css
- */
- function addCss(css) {
- var head = document.head,
- style = document.createElement('style');
- style.type = 'text/css';
- style.appendChild(document.createTextNode(css));
- head.appendChild(style);
- }
- /**
- * Returns information about a player:
- * - last action
- * @param {string} id
- * @return {object}
- */
- function getPlayerInformation(id) {
- return $.ajax({
- url: 'profiles.php?XID=' + id
- }).then(function(data) {
- data = $(data);
- var info = {
- lastAction: {
- text: data.find('.basic-list > li:nth-child(12)')[0].childNodes[2].nodeValue.trim()
- }
- };
- var time = parseFloat(info.lastAction.text, 10);
- if (info.lastAction.text.indexOf('minute') !== -1) {
- info.lastAction.minutes = time;
- } else if (info.lastAction.text.indexOf('hour') !== -1) {
- info.lastAction.hours = time;
- } else if (info.lastAction.text.indexOf('day') !== -1) {
- info.lastAction.days = time;
- }
- return info;
- });
- }
- /**
- * Parses one working stat in the format used on the company page
- * @param {string} text i.e.: "Manual: 11.2k" or "Intelligence: 718"
- * @return {int}
- */
- function parseWorkingStat(text) {
- text = text.slice(text.indexOf(':') + 1).trim();
- var multiplyByThousand = text.indexOf('k') !== -1;
- var stat = parseFloat(text, 10);
- if (multiplyByThousand) {
- return stat * 1000;
- } else {
- return stat;
- }
- }
- var Efficiency = (function() {
- //0-25% red 26%-50 orange 51%-75 yellow %76%-99 green %100+ blue
- var colors = [
- [150, 'rgba(144, 216, 245, 1)'],
- [100, 'rgba(178, 245, 144, 1)'],
- [75, 'rgba(245, 237, 144, 1)'],
- [50, 'rgba(245, 190, 144, 1)'],
- [25, 'rgba(245, 144, 144, 1)']
- ];
- function getColor(efficiency) {
- var color = 'transparent';
- colors.forEach(function(colorLevel) {
- if (efficiency <= colorLevel[0]) {
- color = colorLevel[1];
- }
- });
- return color;
- }
- return {
- getColor: getColor
- };
- }());
- /**
- * Returns the efficiency of a position drop down item
- * @param {String} positionText i.e.: "Driller (83%)"
- * @return {int}
- */
- function parsePositionEfficiency(positionText) {
- var match = positionText.match(/\((\d+)/);
- // there can be positions without an efficiency ("Unassigned")
- return match ? parseInt(match[1], 10) : 0;
- }
- /**
- * Returns the position name of a position drop down item
- * @param {String} positionText i.e.: "Driller (83%)"
- * @return {string}
- */
- function parsePositionName(positionText) {
- var posParenthesis = positionText.indexOf(' (');
- if (posParenthesis === -1) {
- return positionText;
- } else {
- return positionText.slice(0, posParenthesis);
- }
- }
- /**
- * Returns the information about the current company
- * @return {object}
- */
- function getInformationForCurrentCompany() {
- var companyType = $('div.details-wrap').get(0).lastChild.textContent.trim();
- return COMPANIES[companyType];
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- // MAIN
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * Basic initialisation after page load
- */
- function init() {
- addCss(
- '#vinkuun-companyStatusTable { margin: 0 10px }' +
- '#vinkuun-companyStatusTable thead td { color: #222; font-weight: bold; vertical-align: middle }' +
- '#vinkuun-companyStatusTable table { width: 100% }' +
- '#vinkuun-companyStatusTable tbody { background-color: #f2f2f2 }' +
- '#vinkuun-companyStatusTable tbody td:nth-child(1) { width: 220px }' +
- '#vinkuun-companyStatusTable td { border: 1px solid #ccc; text-align: center; padding: 4px 6px }' +
- '#vinkuun-companyStatusTable .statusOk { background-color: rgb(196, 255, 185) }' +
- '#vinkuun-companyStatusTable .statusWarning { background-color: rgb(253, 255, 185) }' +
- '#vinkuun-companyStatusTable .statusCritical { background-color: rgb(236, 66, 66) }' +
- '#vinkuun-companyCalculatorPanel { padding: 5px 10px; margin: 5px 10px 0; border: 1px solid rgba(176, 176, 176, 1); background: linear-gradient(to bottom, #FFF 0, #E4E4E4 100%) repeat 0 0; box-shadow: 0 0 5px rgba(189, 189, 189, 1) } ' +
- '#vinkuun-companyCalculatorPanel input { border: 1px solid #BBB; padding: 2px 4px; width: 50px; line-height: 16px }' +
- '#vinkuun-companyCalculatorPanel select, #vinkuun-companyCalculatorPanel input { margin-left: 5px; }' +
- '#vinkuun-companyCalculatorPanel label { margin-right: 6px }' +
- '#vinkuun-companyCalculatorPanel h5 { margin: 5px 0px 10px }'
- );
- }
- /**
- * Calculates the efficiency based on primary and secondary working stats
- * @param {int} primaryStat
- * @param {int} secondaryStat
- * @param {int} primaryStatCap
- * @param {int} secondaryStatCap
- * @return {int}
- */
- function calculateEfficiency(workingStats, positionName) {
- if (!COMPANY.positions[positionName]) {
- throw new Error('Invalid position name: ' + positionName);
- }
- var pos = COMPANY.positions[positionName];
- var primaryStat = workingStats[pos.primary.stat];
- var secondaryStat = workingStats[pos.secondary.stat];
- var primaryStatCap = pos.primary.cap;
- var secondaryStatCap = pos.secondary.cap;
- var primaryStatOverCap = primaryStat - primaryStatCap;
- if (primaryStatOverCap < 0) {
- primaryStatOverCap = 0;
- }
- var secondaryStatOverCap = secondaryStat - secondaryStatCap;
- if (secondaryStatOverCap < 0) {
- secondaryStatOverCap = 0;
- }
- var statsOverCap = primaryStatOverCap + secondaryStatOverCap;
- var efficiency = ((primaryStat + secondaryStat) - statsOverCap / 2) / (primaryStatCap + secondaryStatCap);
- // efficiency is capped
- if (efficiency > 1.5) {
- efficiency = 1.5;
- }
- // return in percent
- return Math.round(efficiency * 100);
- }
- /**
- * Adds an efficiency calculator after the company information block
- */
- function addEfficiencyCalculator(applicationsTab) {
- var calculatorPanel = $('<div>', {'class': 'border-round', 'id': 'vinkuun-companyCalculatorPanel'});
- var defaultEfficiency = 'N/A';
- var statMan = $('<input>', {type: 'text', rel: 'man'}).on('keyup', recalculateEfficiency);
- var statInt = $('<input>', {type: 'text', rel: 'int'}).on('keyup', recalculateEfficiency);
- var statEnd = $('<input>', {type: 'text', rel: 'end'}).on('keyup', recalculateEfficiency);
- var currentEfficiency = $('<span>', {text: defaultEfficiency, style: 'border: 1px solid #bbb; padding: 2px 5px'});
- var positionSelect = $('<select>').on('change keyup', recalculateEfficiency);
- // populate position drop down
- for (var position in COMPANY.positions) {
- positionSelect.append($('<option>', {text: position}));
- }
- function recalculateEfficiency() {
- if (statMan.val() !== '' && statInt.val() !== '' && statEnd.val() !== '') {
- var workingStats = {
- 'man': parseInt(statMan.val(), 10),
- 'int': parseInt(statInt.val(), 10),
- 'end': parseInt(statEnd.val(), 10)
- };
- var position = positionSelect.find('option:selected').text();
- var eff = calculateEfficiency(workingStats, position);
- currentEfficiency.text(eff + '%');
- currentEfficiency.css('background-color', Efficiency.getColor(eff));
- } else {
- currentEfficiency.text(defaultEfficiency);
- currentEfficiency.css('background-color', '');
- }
- }
- calculatorPanel
- .append($('<h5>', {text: 'Efficiency calcuator'}))
- .append($('<label>', {text: 'MAN:'}).append(statMan))
- .append($('<label>', {text: 'INT:'}).append(statInt))
- .append($('<label>', {text: 'END:'}).append(statEnd))
- .append($('<label>', {text: 'Position:'}).append(positionSelect))
- .append($('<label>', {text: 'Efficiency = '}).append(currentEfficiency));
- applicationsTab.prepend(calculatorPanel);
- // close the gap between "Manage Company" and the tabs
- applicationsTab.css('padding-top', '5px');
- }
- /**
- * Appends the efficiency to an element
- * @param {HTMLNode} element
- * @param {Object} workingStats
- */
- function appendEfficiencyToElement(element, workingStats) {
- var position = element.textContent.trim();
- if (position !== 'Unassigned') {
- var eff = calculateEfficiency(workingStats, position);
- element.textContent = element.textContent + ' (' + eff + '%)';
- }
- }
- /**
- * Adds efficiencies to the position dropdown
- * @param {jQuery-Object} employeesTab
- */
- function addEmployeeEfficiency(employeesTab) {
- employeesTab.find('.ui-selectmenu-status').each(function() {
- var $this = $(this);
- var rankCell = $this.parents('.rank');
- // get current employee stats
- var statElement = $this.parents('.acc-body').find('.stats')[0];
- var employeeStats = {
- 'man': parseWorkingStat(statElement.children[0].textContent),
- 'int': parseWorkingStat(statElement.children[2].textContent),
- 'end': parseWorkingStat(statElement.children[4].textContent)
- };
- // add position efficiency to position name
- appendEfficiencyToElement(this, employeeStats);
- // add color to the cell
- rankCell.css('background-color', Efficiency.getColor(parsePositionEfficiency(this.textContent)));
- // find ui select box items and add efficiency to each option
- rankCell.find('.employee-rank-list li a').each(function() {
- appendEfficiencyToElement(this, employeeStats);
- });
- // observe a rank change
- new MutationObserver(function(mutations) {
- mutations.forEach(function(mutation) {
- // update background color
- var positionEfficiency = parsePositionEfficiency(mutation.addedNodes[0].textContent);
- rankCell.css('background-color', Efficiency.getColor(positionEfficiency));
- // re-sort the table after a rank was changed
- sortEmployeesByRank(employeesTab);
- });
- }).observe(this, { childList: true });
- });
- }
- /**
- * Adds a status table after the existing employee table
- * @param {jQuery-Object} employeesTab
- */
- function addEmployeeLastAction(employeesTab) {
- var blockTitle = $('<div>', {'class': 'title-black border-round', 'text': 'Employee status', 'style': 'margin: 10px 10px 0 10px; cursor: pointer', 'title': 'Show/hide status'})
- .on('click', function() {
- var $this = $(this);
- var existingTable = $('#vinkuun-companyStatusTable');
- if (existingTable.length === 1) {
- $this.removeClass('top-round').addClass('border-round');
- existingTable.remove();
- } else {
- $this.removeClass('border-round').addClass('top-round');
- loadEmployeeStatus();
- }
- }).appendTo(employeesTab);
- function loadEmployeeStatus() {
- var container = $('<div>', {'id': 'vinkuun-companyStatusTable', 'class': 'cont-gray bottom-round'}).appendTo(employeesTab);
- var table = $('<table>').appendTo(container);
- // add header
- $('<thead class="white-grad"><tr><td>Company Employees</td><td>Last Action</td></tr></thead>').appendTo(table);
- var body = $('<tbody>').appendTo(table);
- employeesTab.find('.user.name').each(function() {
- var employeeId = this.href.match(/XID=(\d+)/)[1];
- var userbar = this.children[0].cloneNode();
- var row = $('<tr>').appendTo(body);
- // add name as first column
- $('<td>').append(userbar).appendTo(row);
- var lastAction = $('<td>').appendTo(row);
- getPlayerInformation(employeeId).then(function(info) {
- lastAction.text(info.lastAction.text);
- if (info.lastAction.days) {
- if (info.lastAction.days < 3) {
- lastAction.addClass('statusWarning');
- } else {
- lastAction.addClass('statusCritical');
- }
- } else {
- lastAction.addClass('statusOk');
- }
- });
- });
- }
- }
- /**
- * Sorts the employees ascending by their current position and their efficiency
- * @param {jQuery-Object} employeesTab
- */
- function sortEmployeesByRank(employeesTab) {
- var list = employeesTab.find('ul.employee-list');
- var rows = list.find('> li').get();
- rows.sort(function(a,b) {
- var positionEffA = $(a).find('.ui-selectmenu-status').text();
- var positionEffB = $(b).find('.ui-selectmenu-status').text();
- // extract position name
- var positionA = parsePositionName(positionEffA);
- var positionB = parsePositionName(positionEffB);
- // sort by position name
- if (positionA < positionB) {
- return -1;
- } else if (positionA > positionB) {
- return 1;
- } else {
- // sort descending by efficiency
- var effA = parsePositionEfficiency(positionEffA);
- var effB = parsePositionEfficiency(positionEffB);
- if (effA < effB) {
- return 1;
- } else if (effA > effB) {
- return -1;
- }
- }
- return 0;
- });
- rows.forEach(function(row) {
- list.append(row);
- });
- }
- /**
- * Adds a block after the employee list which shows how many employees are assigned to the company's ranks
- * @param {jQuery-Object} employeesTab
- */
- function addWorkersPerRankInformation(employeesTab) {
- var panel = $('<p>').appendTo(employeesTab.find('form div').eq(0));
- var text = 'Your employees are currently working in the following positions: ';
- for (var position in COMPANY.positions) {
- var count = employeesTab.find('.ui-selectmenu-status:contains("' + position + '")').length;
- text += count + 'x ' + position + ', ';
- }
- // remove last ', '
- text = text.slice(0, text.length - 2);
- text += '.';
- panel.text(text);
- }
- /**
- * Adds a link after the working stats of an applicant.
- * Clicking that link enters the stats into the calculator.
- *
- * @param {jQuery-Object} applicationsTab
- */
- function addEnterApplicationStatsIntoCalculator(applicationsTab) {
- var calculator = $('#vinkuun-companyCalculatorPanel');
- var calcIntField = calculator.find('input[rel="int"]');
- var calcManField = calculator.find('input[rel="man"]');
- var calcEndField = calculator.find('input[rel="end"]');
- applicationsTab.find('div.effectiveness').each(function() {
- var $this = $(this);
- var stats = {
- 'man': parseInt($this.find('.manual')[0].lastChild.textContent.replace(',', ''), 10),
- 'int': parseInt($this.find('.intelligence')[0].lastChild.textContent.replace(',', ''), 10),
- 'end': parseInt($this.find('.endurance')[0].lastChild.textContent.replace(',', ''), 10)
- };
- var enterLink = $('<a>', {text: 'Enter into calculator', style: 'cursor: pointer'})
- .on('click', function (e) {
- e.preventDefault();
- enterStatsIntoCalculator(stats);
- });
- $this.append(enterLink);
- });
- function enterStatsIntoCalculator(stats) {
- calcIntField.val(stats['int']);
- calcManField.val(stats['man']);
- calcEndField.val(stats['end']);
- calcEndField.trigger('keyup');
- }
- }
- /**
- * Executes the provided watchers after the employee tab was loaded
- * @param {array} watchers
- */
- function watchEmployeesTab(watchers) {
- var employeesTab = $('#employees');
- new MutationObserver(function(mutations) {
- mutations.forEach(function(mutation) {
- if (mutation.addedNodes.length === 3) {
- watchers.forEach(function(watcher) {
- watcher(employeesTab);
- });
- }
- });
- }).observe(employeesTab[0], { childList: true });
- }
- /**
- * Executes the provided watchers after the employee tab was loaded
- * @param {array} watchers
- */
- function watchApplicationsTab(watchers) {
- var tab = $('#applications');
- new MutationObserver(function(mutations) {
- mutations.forEach(function(mutation) {
- if (mutation.addedNodes.length === 9) {
- watchers.forEach(function(watcher) {
- watcher(tab);
- });
- }
- });
- }).observe(tab[0], { childList: true });
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- // Main
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- init();
- var COMPANY = getInformationForCurrentCompany();
- // exit if the company is not supported
- if (!COMPANY) {
- alert('Unsupported company.');
- return;
- }
- watchEmployeesTab([
- addWorkersPerRankInformation,
- addEmployeeEfficiency,
- addEmployeeLastAction,
- sortEmployeesByRank
- ]);
- watchApplicationsTab([
- addEfficiencyCalculator,
- addEnterApplicationStatsIntoCalculator
- ]);
- }(XMLHttpRequest));
Add Comment
Please, Sign In to add comment