Skip to content

Instantly share code, notes, and snippets.

@danabrey
Created April 11, 2016 22:44
Show Gist options
  • Save danabrey/ddae0d10b387dd8dad9c3612dbdc7da1 to your computer and use it in GitHub Desktop.
Save danabrey/ddae0d10b387dd8dad9c3612dbdc7da1 to your computer and use it in GitHub Desktop.
(function() {
'use strict';
var data = {};
var $rosterTableRows = $('#single_roster').find('table.report').find('tr');
var config = {
$tableHeaderRow: $rosterTableRows.slice(0,1),
$playerRows: $rosterTableRows.slice(1,-1),
currentYear: 2016,
maxContractYears: 5,
annualPlayerSalaryIncrease: 1.15,
annualSalaryCapIncrease: 1.05
};
function augmentPlayerRows() {
config.$playerRows.each(function() {
var contractYearsRemaining = parseInt($(this).find('td.contractyear').text(), 10);
if (contractYearsRemaining > 0) {
var $salaryTD = $(this).find('td.salary');
var currentSalary = parseInt($salaryTD.text().replace('$','') * 100, 10);
var playerSalariesByYear = [currentSalary];
for (var i = 1; i < config.maxContractYears; i++) {
if (playerSalariesByYear[playerSalariesByYear.length-1] !== 0 && contractYearsRemaining > i) {
var salary = parseInt(playerSalariesByYear[playerSalariesByYear.length-1] * config.annualPlayerSalaryIncrease, 10);
playerSalariesByYear.push(salary);
data[i] = data[i] || {};
data[i].totalSalary = data[i].totalSalary ? data[i].totalSalary + salary : salary;
} else {
playerSalariesByYear.push(0);
}
}
var salariesHTML = playerSalariesByYear.map(function(value) {
return '<td>' + (value > 0 ? '$' + value / 100 : '') + '</td>';
});
$salaryTD.replaceWith(salariesHTML);
}
});
}
function augmentHeaderRow() {
var $salaryHeaderTD = config.$tableHeaderRow.find('th.salary');
var salaryHeaderHTML;
$salaryHeaderTD.text(config.currentYear);
for (var i = 1; i < config.maxContractYears; i++) {
salaryHeaderHTML += '<th>' + (config.currentYear + i) + '</th>';
}
$salaryHeaderTD.after(salaryHeaderHTML);
}
function augmentTotalSalariesRow() {
var $totalSalaryTD = $($('.total_salary_row').find('th')[1]);
var totalSalariesHTML;
for (var i = 1; i < config.maxContractYears; i++) {
totalSalariesHTML += '<th>$' + (data[i].totalSalary ? data[i].totalSalary / 100 : '0.00') + '</th>';
}
$totalSalaryTD.after(totalSalariesHTML);
}
function augmentSalaryCapRow() {
var $salaryCapTD = $($('.salary_cap_row').find('th')[1]);
var salaryCapHTML;
for (var i = 1, cap = $salaryCapTD.text().replace('$',''); i < config.maxContractYears; i++) {
cap = (cap * config.annualSalaryCapIncrease).toFixed(2);
salaryCapHTML += '<th>$' + cap + '</th>';
}
$salaryCapTD.after(salaryCapHTML);
}
augmentPlayerRows();
augmentHeaderRow();
augmentTotalSalariesRow();
augmentSalaryCapRow();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment