Last active
August 29, 2015 14:05
-
-
Save boertel/16a5c30755e9f0d0b7ac to your computer and use it in GitHub Desktop.
ESPN Pick'em Bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
window._benbot = window._benbot || {}; | |
var config = { | |
method: window._benbot.method || 100, | |
submission: window._benbot.submission || false, | |
diff: window._benbot.diff || 20 | |
}; | |
function r (n) { n = n || 35; return Math.floor(Math.random() * n) } | |
function Match (away, home) { | |
this.away = away; | |
this.home = home; | |
} | |
Match.prototype.method = function () { | |
/* config.method is a int between 0 and 100, which determined the chance to choose the | |
winner randomly or depending on the percentage. */ | |
var n = r(100/config.method); | |
/* config.diff is forcing the method to be random if the difference between the two percentages | |
is close (the definition of close is the value of config.diff) */ | |
if (config.diff && Math.abs(this.home.percentage - this.away.percentage) <= config.diff) { | |
n = 1; | |
} | |
return (n === 0) ? 'percent' : 'random'; | |
}; | |
Match.prototype.winner = function () { | |
var method = this.method(); | |
if (method === 'random') { | |
if (typeof this._winner === 'undefined') { | |
this._winner = (r(2) === 0) ? 'home' : 'away'; | |
} | |
return this._winner; | |
} else { | |
if (this.home.percentage >= this.away.percentage) { | |
return 'home'; | |
} | |
return 'away'; | |
} | |
}; | |
Match.prototype.pick = function () { | |
this[this.winner()].check(); | |
}; | |
Match.prototype.score = function () { | |
var score = {}, | |
winner = this.winner(), | |
other = (winner === "home") ? "away" : "home"; | |
do { | |
score = { | |
away: r(), | |
home: r() | |
} | |
} while (score[winner] <= score[other]); | |
return score; | |
}; | |
function Team(node) { | |
this.name = node.find('.pickem-teamName a').text(); | |
this.percentage = parseFloat(node.find('.games-greenbar-pickem').attr('title')); | |
this.checkbox = node.find('.entryPick'); | |
} | |
Team.prototype.check = function () { | |
console.log(this.toString()); | |
this.checkbox.trigger('click') | |
}; | |
Team.prototype.toString = function () { | |
return this.name + ' (' + this.percentage + ')'; | |
}; | |
// Pick winning team for each match | |
var teams = jQuery('tr.matchupRow'); | |
window.matches = []; | |
for (var i = 0; i < teams.length - 1; i += 2) { | |
var node = { | |
away: jQuery(teams[i]), | |
home: jQuery(teams[i + 1]) | |
}; | |
var away = new Team(node.away), | |
home = new Team(node.home), | |
match = new Match(away, home); | |
window.matches.push(match); | |
} | |
window.matches.forEach(function (m) { | |
window.setTimeout(function () { | |
m.pick(); | |
}, 1); | |
}); | |
// Update Tiebreaker final score | |
var form = jQuery('form[name=pickemForm]'), | |
inputs = form.find('input.tieBreaker'), | |
tieBreaker = matches[matches.length - 1]; | |
var score = tieBreaker.score(); | |
jQuery(inputs.get(0)).val(score.away); | |
jQuery(inputs.get(1)).val(score.home); | |
if (config.submission) { | |
window.onbeforeunload = undefined; | |
form.find('.submit-button:not(.disabled)').trigger('click'); | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment