Skip to content

Instantly share code, notes, and snippets.

@acconrad
Created August 31, 2012 18:13
Show Gist options
  • Save acconrad/3556780 to your computer and use it in GitHub Desktop.
Save acconrad/3556780 to your computer and use it in GitHub Desktop.
Troublesome JS functions
function addDailyPlayer (element) {
if (logged_in) {
var $row = $(element).closest('li')
, $myTeam = $('#player_confirmation_table').children('tbody')
, playerSlotAvailableNew = 0
, useFlex = false
, $playerInfo
, $playerSlot
, playerId
, gameId
, position
, price
, confirmHtml
, playerSlotAvailable
, playerSlotNumber
, playerSlotPosition
, slotsFilled
if ($row.hasClass('green-selected')) {
removeDailyPlayer($myTeam.children('tr[data-player-table-row-id="' + $row.attr('id') + '"]').find('.js-remove-confirmation'))
return false
}
//add to team table
gameId = $row.attr('data-game-id')
playerId = $row.attr('data-player-id')
price = $row.children().children('a.li-daily-player').children('div.left').children('p:first').children('span.js-daily-price').text()
$playerInfo = $row.children().children('a.js-player-name')
position = $row.attr('data-slot-position')
if (IS_FOOTBALL &&
!$myTeam.children('.slot-' + position.toLowerCase() + '.slot-available').length &&
!$myTeam.children('.slot-flex.slot-available').length) return false
if (IS_FOOTBALL) {
//add to correct slot
playerSlotAvailable = parseInt($('#' + position.toLowerCase() + '-remaining').text(), 10)
useFlex = playerSlotAvailable === 0 && position !== 'QB' && $('.js-confirm-total').length !== 9
if ($('#game_entry_holding_slot_' + playerId + '_game_' + gameId).length || (playerSlotAvailable < 1 && !useFlex)) {
return false
} else {
CAN_LEAVE = false
$row.addClass('green-selected').find('i').removeClass('icon-chevron-right').addClass('icon-ok')
if (useFlex) {
playerSlotPosition = 'flex'
playerSlotAvailable = parseInt($('#flex-remaining').text(), 10)
} else {
playerSlotPosition = position.toLowerCase()
}
confirmHtml = '<td><strong>' + playerSlotPosition.toUpperCase() + '</strong>' +
'<td><a href="#" class="js-player-name-confirm">' + $playerInfo.attr('data-first-name').substr(0, 1) + '. ' + $playerInfo.attr('data-last-name') + '</a>' +
'<td class="js-confirm-total">' + price +
'<td><a href="#" class="js-remove-confirmation"><i class="icon-remove"></i></a>'
$playerSlot = $('.slot-' + playerSlotPosition + '.slot-available:first')
$playerSlot.html(confirmHtml)
.attr('data-player-table-row-id', $row.attr('id'))
.removeClass('slot-available')
playerSlotAvailableNew = playerSlotAvailable - 1
$('#' + playerSlotPosition + '-remaining').html(playerSlotAvailableNew)
$('.game_entry_form').append('<input id="game_entry_holding_player_' + playerId + '_game_' + gameId + '" type="hidden" name="holding_entries[holding_' + playerId + '_game_' + gameId + '[master_player_id]" value="' + playerId + '">')
.append('<input id="game_entry_holding_slot_' + playerId + '_game_' + gameId + '" type="hidden" name="holding_entries[holding_' + playerId + '_game_' + gameId + '[slot]" value="' + $playerSlot.attr('id').split('-')[0] + '">')
.append('<input id="game_entry_holding_game_' + playerId + '_game_' + gameId + '" type="hidden" name="holding_entries[holding_' + playerId + '_game_' + gameId + '[game_id]" value="' + gameId + '">')
}
} else {
$row.addClass('green-selected').find('i').removeClass('icon-chevron-right').addClass('icon-ok')
CAN_LEAVE = false
confirmHtml = '<tr data-player-table-row-id="' + $row.attr('id') + '">' +
'<td><a href="#" class="js-player-name-confirm">' + $playerInfo.attr('data-first-name').substr(0, 1) + '. ' + $playerInfo.attr('data-last-name') + '</a>' +
'<td>' +
'<div class="range range-shares"> ' +
'<input class="js-daily-shares" type="text" size="3" value="1">' +
'<div class="btn-group">' +
'<a class="btn hidden"></a>' +
'<a class="btn increment_up" href="#"><i class="icon-chevron-up"></i></a>' +
'<a class="btn increment_down" href="#"><i class="icon-chevron-down"></i></a>' +
'</div>' +
'</div>' +
'<td>' + price +
'<td class="js-confirm-total">' + price +
'<td><a href="#" class="js-remove-confirmation"><i class="icon-remove"></i></a>'
if (!$('#sample_player_1').hasClass('hidden')) {
$('#sample_player_1').addClass('hidden').after(confirmHtml)
if ($('#js_daily').hasClass('js-show-shares-tooltip')) $('#shares_tooltip').tooltip('show')
} else if (!$('#sample_player_3').hasClass('hidden')) {
$('#sample_player_3').addClass('hidden')
$('#sample_player_2').before(confirmHtml)
} else if (!$('#sample_player_2').hasClass('hidden')) {
$('#sample_player_2').addClass('hidden').after(confirmHtml)
} else {
$myTeam.append(confirmHtml)
}
if ($('#game_entry_holding_shares_' + playerId + '_game_' + gameId).length) {
$('#game_entry_holding_shares_' + playerId + '_game_' + gameId).val(1)
} else {
$('.game_entry_form').append('<input id="game_entry_holding_player_' + playerId + '_game_' + gameId + '" type="hidden" name="holding_entries[holding_' + playerId + '_game_' + gameId + '[master_player_id]" value="' + playerId + '">')
.append('<input id="game_entry_holding_shares_' + playerId + '_game_' + gameId + '" type="hidden" name="holding_entries[holding_' + playerId + '_game_' + gameId + '[shares]" value="1">')
.append('<input id="game_entry_holding_game_' + playerId + '_game_' + gameId + '" type="hidden" name="holding_entries[holding_' + playerId + '_game_' + gameId + '[game_id]" value="' + gameId + '">')
}
}
track('Daily', 'Add Player Main', typeof(logged_in_user_email) !== 'undefined' ? logged_in_user_email : null)
updateDailyTeamCap(parseInt($row.attr('data-player-price').replace(',', ''), 10))
gameEntryValidation(false)
} else {
openSignupPopup(false, false, null, false, 'Sign Up For StarStreet & Add Your Players', 'StarStreet offers fantasy sports games that last just one day. Sign up and make real money today.')
$('#daily-mobile-back, #daily-mobile-forward').addClass('hidden')
track('Daily', 'Logged Out Add Player')
}
}
function removeDailyPlayer (element) {
var $row = element.closest('tr')
, rowIdArr = $row.attr('data-player-table-row-id').split('_')
, playerId = rowIdArr[2]
, gameId = rowIdArr[3]
, position = $row.children('td:first').text().trim()
, priceToRemove = -1 * parseInt($row.find('.js-confirm-total').text().substring(1).replace(',', ''), 10)
, positionCap = 0
, $filter
, rowNew
, teamSize
$('#' + $row.attr('data-player-table-row-id')).removeClass('green-selected').find('i').removeClass('icon-ok').addClass('icon-chevron-right')
if (IS_FOOTBALL) {
$filter = $('#' + position.toLowerCase() + '-remaining')
positionCap = parseInt($filter.text(), 10) + 1
$filter.text(positionCap)
$('#game_entry_holding_slot_' + playerId + '_game_' + gameId + ', #game_entry_holding_player_' + playerId + '_game_' + gameId + ', #game_entry_holding_game_' + playerId + '_game_' + gameId).remove()
$row.html(position === 'FLEX' ? '<td><strong class="black-faded">' + position + '</strong><td colspan="3"><p class="center no-margin"><small>RB/WR/TE</small>' : '<td><strong class="black-faded">' + position + '</strong><td colspan="3"><p class="center no-margin"><small>' + position + '</small>')
.addClass('slot-available')
} else {
$('#game_entry_holding_shares_' + playerId + '_game_' + gameId).val(0)
$row.remove()
teamSize = $('#player_confirmation_table').children('tbody').children('tr').length
if (teamSize === 5)
$('#sample_player_2').removeClass('hidden')
else if (teamSize === 4)
$('#sample_player_3').removeClass('hidden')
else if (teamSize === 3)
$('#sample_player_1').removeClass('hidden')
}
updateDailyTeamCap(priceToRemove)
gameEntryValidation(false)
}
function gameEntryValidation (show) {
var valid = true
, instantShow = false
, entryErrorMsg = ''
, accountValue = $('#account_free_funds').length ? parseFloat($('#account_free_funds').html().substr(1).replace(',', '')) : 0.0
//show on click of enter
if ($('#remaining_total').hasClass('red')) { //under the salary cap
valid = false
entryErrorMsg = 'You are over your budget!'
} else if ((!IS_FOOTBALL && $('input#[id^="game_entry_holding_shares"]').filter(function() { return $(this).val() > 0 }).length < 3) ||
(IS_FOOTBALL && $('.js-confirm-total').length !== 9)) { //enough players
valid = false
entryErrorMsg = 'You need to add more players!'
} else if ($('#total_entry_fee').length && parseFloat($('#prize_total').html().slice(1)) === 0 && parseInt($('#fee_0').val(),10) === 0 && $("input[name^=freerolls]:checked").length === 0) {
valid = false
entryErrorMsg = 'You must enter at least one contest!'
}
//show immediatley
if ($('#fee_0').val() > 3) {
valid = false
instantShow = true
entryErrorMsg = 'You cannot enter more than 3 free games each night!'
} else if ($('#fee_108').val() > DAILY_GAMES_LIMIT || $('#fee_538').val() > DAILY_GAMES_LIMIT || $('#fee_1070').val() > DAILY_GAMES_LIMIT || $('#fee_2130').val() > DAILY_GAMES_LIMIT || $('#fee_5300').val() > DAILY_GAMES_LIMIT || $('#fee_1070_6').val() > DAILY_GAMES_LIMIT || $('#fee_108_6').val() > DAILY_GAMES_LIMIT) {
valid = false
instantShow = true
entryErrorMsg = 'You cannot enter more than ' + DAILY_GAMES_LIMIT + ' games per price level each night!'
} else if (logged_in && $('#total_entry_fee').length && parseFloat($('#total_entry_fee').html().substr(1)) > accountValue) {
valid = false
instantShow = true
entryErrorMsg = 'You need to <a href="#" class="js-add-funds">deposit more money</a> to enter those contests'
}
if (!valid && (show || instantShow)) $('#entry_error').html(entryErrorMsg).removeClass('hidden')
else if (valid || !instantShow) $('#entry_error').html('').addClass('hidden')
return valid;
}
function updateDailyTeamCap (amount) {
var $salary = $('#remaining_total, #remaining_total_mobile')
, $salaryAvgPlayer = $('#remaining_player')
, $myTeam = $('#player_confirmation_table').children('tbody')
, $availablePlayers = $myTeam.children('.slot-available')
, newSalary = parseInt($salary.text().substring(1).replace(',', ''), 10) - amount
, newSalaryStr = '$' + formatMoney(newSalary, 0, '.', ',')
, newSalaryAvg = !$availablePlayers.length ? 0 : parseFloat(newSalary / $availablePlayers.length)
, newSalaryAvgStr = '$' + formatMoney(newSalaryAvg, 0, '.', ',')
, isSlotAvail = []
, $listItems
, $item
, position
$salary.html(newSalaryStr)
$salaryAvgPlayer.html(newSalaryAvgStr)
if (newSalary >= 0 && $salary.hasClass('red')) $salary.removeClass('red')
else if (newSalary < 0 && !$salary.hasClass('red')) $salary.addClass('red')
if (newSalaryAvg >= 5000 && $salaryAvgPlayer.hasClass('red')) $salaryAvgPlayer.removeClass('red')
else if (newSalaryAvg < 5000 && !$salaryAvgPlayer.hasClass('red') && $availablePlayers.length > 0) $salaryAvgPlayer.addClass('red')
if (!isDevice()) {
$listItems = $('#players_list').children('li')
if (IS_FOOTBALL) {
isSlotAvail ['qb'] = $myTeam.children('.slot-qb.slot-available').length
isSlotAvail ['rb'] = $myTeam.children('.slot-rb.slot-available').length
isSlotAvail ['wr'] = $myTeam.children('.slot-wr.slot-available').length
isSlotAvail ['te'] = $myTeam.children('.slot-te.slot-available').length
isSlotAvail ['flex'] = $myTeam.children('.slot-flex.slot-available').length
}
$listItems.each(function () {
$item = $(this)
position = $item.attr('data-slot-position').toLowerCase()
if ((parseInt($item.attr('data-player-price'), 10) > newSalary) ||
(IS_FOOTBALL && ((position === 'qb' && !isSlotAvail['qb']) ||
(!isSlotAvail[position]) && !isSlotAvail['flex'])))
$item.addClass('greyed-out')
else if (!IS_FOOTBALL || (IS_FOOTBALL && ((position === 'qb' && isSlotAvail['qb'] ||
(isSlotAvail[position] || isSlotAvail['flex'])))))
$item.removeClass('greyed-out')
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment