Xorboo / ConsulEarthBonuses

// ==UserScript==
// @name        ConsulEarthBonuses
// @namespace   consulwar.ru
// @description Temporary fix for missing Earth Zones bonuses
// @require     https://code.jquery.com/jquery-1.12.4.js
// @version     0.1.1
// @author      Xorboo
// @license     MIT
// @match       *://consulwar.ru/*
// ==/UserScript==

/* RELEASE NOTES
  0.1.1
    + basic bonuses
*/

$(function() {
    log('============ Start');

    Template.earthZonePopup.onRendered(function() {
        const zoneName = this.data.name;
        const zone = Game.EarthZones.Collection.find({ name: zoneName }).fetch()[0];

        let bonus = zone.bonus.id;
        const bonusFormat = getBonusNameFormat(bonus);
        bonus = bonus.slice(Math.max(bonus.lastIndexOf('.'), bonus.lastIndexOf('/')) + 1);
        const bonusText = bonusFormat.format(bonus);

        const resourcesDiv = $('#map-earth .leaflet-popup-pane .point-popup div:first');
        resourcesDiv.after(`<div><font color="cyan">{0}: {1}</font></div>`.format(bonusText, zone.bonus.count));
    });


    if (!String.prototype.format) {
        String.prototype.format = function () {
            var args = arguments;
            return this.replace(/{(\d+)}/g, function (match, number) {
                return typeof args[number] != 'undefined' ? args[number] : match;
            });
        };
    }

    log('============ End');


    const bonusFormats = [
        { name: 'resources', format: 'Ресурс "<b>{0}</b>"' },
        { name: 'containers.Container/Fleet', format: 'Контейнер "<b>{0}</b>"' },

        { name: 'units.Unit/Human/Space', format: 'Корабль "<b>{0}</b>"' },
        { name: 'Unit/Human/Space', format: 'Корабль "<b>{0}</b>"' },

        { name: 'units.Unit/Human/Defense', format: 'Оборона "<b>{0}</b>"' },
        { name: 'Unit/Human/Defense', format: 'Оборона "<b>{0}</b>"' },

        { name: 'units.Unit/Human/Ground', format: 'Армия "<b>{0}</b>"' },
        { name: 'Unit/Human/Ground', format: 'Армия "<b>{0}</b>"' }
    ];

    function getBonusNameFormat(bonus) {
        for (let i = 0; i < bonusFormats.length; i++) {
            const formatData = bonusFormats[i];
            if (bonus.startsWith(formatData.name)) {
                return formatData.format;
            }
        }

        return 'Unknown "<b>{0}</b>"'.format(bonus);
    }


    // #region Logging
    function log(text) {
        console.log('ConsulEarthBonuses: ' + text);
    }

    function logObj(obj, obj_name='[Object]') {
        log('\'' + obj_name + '\' data:');
        console.dir(obj);
    }
    // #endregion
});