Free javascript Hosting


trainfake.js

Uploaded on Nov 10 2021 20:34 by alexandrustruta

/*
* Script Name: Evolved Fake Train
* Version: v1.0
* Last Updated: 2021-10-23
* Author: RedAlert
* Author URL: https://twscripts.dev/
* Author Contact: RedAlert#9859 (Discord)
* Approved: N/A
* Approved Date: 2021-11-05
* Mod: JawJaw
*/

/*--------------------------------------------------------------------------------------
* This script can NOT be cloned and modified without permission from the script author.
--------------------------------------------------------------------------------------*/

// User Input
if (typeof DEBUG !== 'boolean') DEBUG = false;

// Script Config
var scriptConfig = {
scriptData: {
prefix: 'evolvedFakeTrain',
name: 'Evolved Fake Train',
version: 'v1.0',
author: 'RedAlert',
authorUrl: 'https://twscripts.dev/',
helpLink: 'https://forum.tribalwars.net/index.php?threads/evolved-fake-train.287900/',
},
translations: {
en_DK: {
'Evolved Fake Train': 'Evolved Fake Train',
Help: 'Help',
'This script can only be run on command confirmation screen!':
'This script can only be run on command confirmation screen!',
'Fixed units': 'Fixed units',
'Dynamically calculated amounts': 'Dynamically calculated amounts',
All: 'All',
'Set all dynamic units': 'Set all dynamic units',
},
},
allowedMarkets: [],
allowedScreens: ['place'],
allowedModes: [],
isDebug: DEBUG,
enableCountApi: true,
};

$.getScript('https://twscripts.dev/scripts/twSDK.js', async function () {
// Initialize Library
twSDK.init(scriptConfig);
const scriptInfo = twSDK.scriptInfo();
const isValidScreen = twSDK.checkValidLocation('screen');
const gameMode = twSDK.getParameterByName('try');

// Script business logic
(async function () {
if (isValidScreen && gameMode === 'confirm') {
const unitsToggle = buildUnitsToggle();

const customStyle = `
.ra-dflex { display: flex; flex-flow: row wrap; }

.ra-btn-set-units { font-size: 15px; width: 60px; height: auto !important; }
.ra-btn-set-units span { margin: 4px; display: block; }

.ra-btn-set-all-units { margin-bottom: 5px; }

.ra-label { font-weight: bold; display: block; margin-bottom: 8px; }
.ra-dflex { display: flex; align-items: flex-start; }

.ra-evolved-fake-train-body .btn-confirm-yes { padding: 3px; }
`;

const content = `
<div class="ra-mb15">
<a href="javascript:void(0);" class="btn" id="raResetUnitAmountsBtn">
<span>Reset</span>
</a>
<a href="javascript:void(0);" class="btn" id="raSetAllDynamicUnitsBtn">
<span>${twSDK.tt('Set all dynamic units')}</span>
</a>
</div>
<div class="ra-mb15">
<label class="ra-label">${twSDK.tt('Fixed units')}</label>
<div class="ra-dflex">
<a href="javascript:void(0);" class="btn ra-btn-set-units" data-units-amounts='{"axe": 85, "light": 41, "snob": 1}'>
<span>1 <img src="/graphic/unit/tiny/ram.png"></span>
<span>1 <img src="/graphic/unit/tiny/spy.png"></span>
<span>1 <img src="/graphic/unit/tiny/catapult.png"></span>
</a>
<a href="javascript:void(0);" class="btn ra-btn-set-units" data-units-amounts='{"catapult": 1, "spy": 1}'>
<span>1 <img src="/graphic/unit/tiny/catapult.png"></span>
<span>1 <img src="/graphic/unit/tiny/spy.png"></span>
</a>
<a href="javascript:void(0);" class="btn ra-btn-set-units" data-units-amounts='{"spy": 1, "catapult": 20}'>
<span>1 <img src="/graphic/unit/tiny/spy.png"></span>
<span>20 <img src="/graphic/unit/tiny/catapult.png"></span>
</a>
</div>
</div>
<div class="ra-mb15">
<label class="ra-label">${twSDK.tt('Dynamically calculated amounts')}</label>
<div class="ra-dflex">
${unitsToggle}
</div>
</div>
`;

twSDK.renderFixedWidget(content, 'raEvolvedFakeTrain', 'ra-evolved-fake-train', customStyle);

const villageUnitAmounts = collectUnitAmounts();

// register action handler
onClickSetUnitAmounts(villageUnitAmounts);
onClickSetDynamicallyCalculatedUnits(villageUnitAmounts);
onClickSetAllDynamicallyCalculatedUnits(villageUnitAmounts);
onClickResetUnitAmounts();
} else {
UI.ErrorMessage(twSDK.tt('This script can only be run on command confirmation screen!'));
}
})();

// Action Handler: Handle click on set unit amounts button
function onClickSetUnitAmounts(villageUnitAmounts) {
jQuery('.ra-btn-set-units').on('click', function (e) {
e.preventDefault();

addTroopConfigurator();
toggleCurrentActiveButtonState(this);

const currentChosenUnits = JSON.parse(jQuery(this).attr('data-units-amounts'));

for (let unit in currentChosenUnits) {
jQuery(`.units-row input[type="number"][data-unit="${unit}"]`).val(currentChosenUnits[unit]);
}

Place.confirmScreen.updateUnitsSum();
Place.confirmScreen.checkForUnprotectedSnobs();
Place.confirmScreen.checkAndShowCatapultTargetSelection();
});
}

// Action Handler: Set dynamically calculated units
function onClickSetDynamicallyCalculatedUnits(villageUnitAmounts) {
jQuery('.ra-btn-set-all-units').on('click', function (e) {
e.preventDefault();

addTroopConfigurator();
toggleCurrentActiveButtonState(this);

unitAutoBalancer([jQuery(this).data('unit')]);
});
}

// Action Handler: Set all dynamically calculated units
function onClickSetAllDynamicallyCalculatedUnits(villageUnitAmounts) {
jQuery('#raSetAllDynamicUnitsBtn').on('click', function (e) {
e.preventDefault();

addTroopConfigurator();
toggleCurrentActiveButtonState(this);

unitAutoBalancer(['axe', 'light', 'marcher', 'spy', 'ram', 'catapult']);
});
}

// Action Handler: Reset unit amounts
function onClickResetUnitAmounts() {
jQuery('#raResetUnitAmountsBtn').on('click', function (e) {
e.preventDefault();

toggleCurrentActiveButtonState(this);

game_data.units.forEach((unit) => jQuery(`.units-row input[type="number"][data-unit="${unit}"]`).val(''));
jQuery('#place_confirm_units img.float_right').trigger('click');

Place.confirmScreen.updateUnitsSum();
});
}

// Helper: Build the units toggle
function buildUnitsToggle() {
let allUnitsToggles = ``;
game_data.units.forEach((unit) => {
if (unit !== 'militia' && unit !== 'knight') {
allUnitsToggles += `
<a href="javascript:void(0);" class="btn ra-btn-set-all-units" data-unit="${unit}">
<span><img src="/graphic/unit/unit_${unit}.png"></span>
</a>
`;
}
});
return allUnitsToggles;
}

// Helper: Units auto balancer. Code heavily based from Place.confirmScreen.autoBalance()
function unitAutoBalancer(units) {
var send = Place.confirmScreen.getSendUnits();
var avail = Place.confirmScreen.getAvailableUnits();

$.each(units, function (k, unit_id) {
var pool = avail[unit_id] - send[unit_id] - Place.confirmScreen.getDirtyUnitCount(unit_id);
var pool_used = 0;

var $inputs = $('#place_confirm_units').find('input[name*=\\[' + unit_id + '\\]]:not(.dirty)');

$inputs.each(function (k) {
var units = k === $inputs.length - 1 ? pool - pool_used : Math.floor(pool / $inputs.length);
$(this).val(units > 0 ? units : '');
pool_used += units;
});
});

Place.confirmScreen.updateUnitsSum();
Place.confirmScreen.checkForUnprotectedSnobs();
Place.confirmScreen.checkAndShowCatapultTargetSelection();
}

// Helper: Unselect all buttons and highlight current active one
function toggleCurrentActiveButtonState(currentButton) {
jQuery('.ra-evolved-fake-train-body .btn').removeClass('btn-confirm-yes');
jQuery(currentButton).addClass('btn-confirm-yes');
}

// Helper: Show the unit amount configurator
function addTroopConfigurator() {
Place.confirmScreen.addAdditionalAttack();
Place.confirmScreen.applyTemplate('fake');
jQuery(`.units-row input[type="number"]`).val('');
}

// Helper: Collect the troops in village
function collectUnitAmounts() {
const amounts = [];
let unitAmounts = {};

jQuery('#place_confirm_units tbody tr.train-ui:eq(0) td').each(function () {
const amount = jQuery(this).text().trim();
amounts.push(parseInt(amount));
});

game_data.units.forEach((unit, index) => {
if (unit !== 'militia') {
unitAmounts = {
...unitAmounts,
[unit]: amounts[index],
};
}
});

return unitAmounts;
}
});

Back to list