smmerz / Pigskin Pick'em

// ==UserScript==
// @name        Pigskin Pick'em
// @namespace   Pigskin Pick'em
// @include     *google.com/
// @include     *.profootball.upickem.net/profootball/framed/football/*
// @include     http://www.vegasinsider.com/nfl/odds/las-vegas/*
// @version     3
// @grant       none
// ==/UserScript==

var location=window.location.href;
var ProbabilitySet = [[0.5,51.5],[1,53],[1.5,54.4],[2,55.9],[2.5,57.4],[3,58.8],[3.5,60.3],[4,61.7],[4.5,63.1],[5,64.5],[5.5,65.9],[6,67.2],[6.5,68.5],[7,69.9],[7.5,71.1],[8,72.4],[8.5,73.6],[9,74.8],[9.5,76],[10,77.1],[10.5,78.2],[11,79.3],[11.5,80.4],[12,81.4],[12.5,82.3],[13,83.3],[13.5,84.2],[14,85.1],[0,50],[-0.5,51.5],[-1,53],[-1.5,54.4],[-2,55.9],[-2.5,57.4],[-3,58.8],[-3.5,60.3],[-4,61.7],[-4.5,63.1],[-5,64.5],[-5.5,65.9],[-6,67.2],[-6.5,68.5],[-7,69.9],[-7.5,71.1],[-8,72.4],[-8.5,73.6],[-9,74.8],[-9.5,76],[-10,77.1],[-10.5,78.2],[-11,79.3],[-11.5,80.4],[-12,81.4],[-12.5,82.3],[-13,83.3],[-13.5,84.2],[-14,85.1]]
var ScoreSet = [['28', '21'], ['31', '23'], ['27', '20'], ['31', '10'], ['27', '14'], ['17', '10'], ['20', '09'], ['34', '31'], ['31', '19'], ['33', '28'], ['42', '14'], ['33', '13'], ['19', '13'], ['27', '26'], ['26', '24'], ['20', '03'], ['31', '24'], ['24', '17'], ['43', '18'], ['26', '19'], ['26', '16'], ['48', '23'], ['40', '32'], ['24', '19'], ['28', '14'], ['24', '20'], ['24', '10'], ['23', '20'], ['37', '33'], ['20', '10'], ['27', '17'], ['20', '07'], ['32', '21'], ['39', '28'], ['35', '33'], ['27', '20'], ['28', '24'], ['51', '17'], ['27', '22'], ['24', '17'], ['19', '09'], ['31', '14'], ['12', '06'], ['47', '07'], ['41', '14'], ['26', '00'], ['24', '12'], ['38', '28'], ['23', '20'], ['27', '14'], ['16', '13'], ['24', '10'], ['37', '23'], ['23', '20'], ['22', '20'], ['48', '21'], ['36', '21'], ['30', '27'], ['17', '03'], ['24', '22'], ['23', '20'], ['26', '20'], ['13', '10'], ['27', '20'], ['18', '17'], ['27', '24'], ['25', '19'], ['38', '31'], ['39', '17'], ['33', '30'], ['24', '10'], ['14', '13'], ['42', '17'], ['30', '06'], ['16', '10'], ['30', '27'], ['24', '20'], ['31', '21'], ['34', '20'], ['25', '13'], ['16', '10'], ['34', '21'], ['37', '34'], ['26', '23'], ['31', '20'], ['38', '10'], ['27', '23'], ['27', '20'], ['25', '20'], ['34', '27'], ['27', '07'], ['20', '03'], ['34', '31'], ['31', '30'], ['10', '07'], ['27', '21'], ['28', '19'], ['23', '13'], ['24', '06'], ['44', '26'], ['30', '23'], ['37', '29'], ['27', '20'], ['27', '16'], ['26', '18'], ['36', '07'], ['45', '10'], ['23', '20'], ['34', '20'], ['27', '06'], ['52', '49'], ['23', '20'], ['29', '26'], ['16', '10'], ['20', '06'], ['34', '20'], ['13', '12'], ['29', '10'], ['29', '26'], ['31', '10'], ['37', '29'], ['27', '10'], ['34', '28'], ['33', '17'], ['21', '18'], ['28', '23'], ['38', '35'], ['32', '18'], ['17', '16'], ['27', '24'], ['33', '27'], ['22', '19'], ['22', '17'], ['18', '16'], ['10', '06'], ['27', '10'], ['37', '13'], ['47', '14'], ['20', '19'], ['30', '09'], ['22', '20'], ['30', '14'], ['27', '26'], ['29', '13'], ['39', '32'], ['10', '06'], ['19', '13'], ['18', '13'], ['24', '21'], ['16', '13'], ['45', '17'], ['17', '15'], ['24', '17'], ['44', '16'], ['24', '14'], ['33', '03'], ['30', '13'], ['29', '13'], ['34', '31'], ['20', '13'], ['45', '14'], ['33', '14'], ['17', '13'], ['24', '06'], ['31', '07'], ['20', '10'], ['20', '14'], ['25', '12'], ['30', '22'], ['24', '21'], ['31', '25'], ['38', '20'], ['19', '13'], ['39', '30'], ['30', '24'], ['33', '27'], ['27', '23'], ['23', '20'], ['27', '03'], ['23', '19'], ['38', '07'], ['26', '20'], ['42', '39'], ['30', '21'], ['15', '13'], ['37', '03'], ['34', '20'], ['17', '03'], ['35', '28'], ['41', '38'], ['45', '10'], ['19', '16'], ['23', '20'], ['23', '20'], ['35', '06'], ['24', '10'], ['21', '14'], ['30', '08'], ['33', '20'], ['51', '16'], ['10', '03'], ['24', '21'], ['38', '00'], ['24', '17'], ['15', '12'], ['28', '07'], ['27', '06'], ['31', '24'], ['31', '23'], ['19', '16'], ['38', '35'], ['33', '16'], ['35', '25'], ['34', '14'], ['16', '10'], ['23', '17'], ['38', '17'], ['30', '20'], ['30', '13'], ['24', '14'], ['30', '14'], ['34', '27'], ['40', '17'], ['35', '27'], ['23', '20'], ['38', '24'], ['32', '17'], ['20', '17'], ['16', '06'], ['26', '21'], ['20', '13'], ['18', '12'], ['26', '20'], ['34', '06'], ['17', '13'], ['38', '27'], ['38', '08'], ['23', '17'], ['49', '17'], ['20', '17'], ['22', '17'], ['20', '10'], ['20', '17'], ['24', '20'], ['35', '30'], ['34', '23'], ['30', '24'], ['24', '16'], ['28', '12'], ['30', '06'], ['23', '17'], ['27', '20'], ['38', '10'], ['36', '06'], ['19', '16'], ['20', '13']]
var WinningTeams=[]
var WinnerSpots=[]
var CopyMessage='=SPLIT("'+location.toString()
    
function randomIntFromInterval(min,max)
{
    return Math.floor(Math.random()*(max-min+1)+min);
};

function randomNumberFromInterval(min,max)
{
    return Math.random()*(max-min)+min;
};

function setCookie(cname, cvalue, exdays, cpath, cdomain) {
//    console.log(cname,cvalue,exdays,cpath,cdomain)
    if (cpath == undefined){
//        console.log('True')
        cpath = ""
    }
    if (cdomain == undefined){
//        console.log('true')
        cdomain = ""
    }
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = 'expires='+d.toUTCString();
    document.cookie = cname + '=' + cvalue + '; ' + cpath + cdomain + expires;
};

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
    }
    return "";
};

function PickWinner(away,home,odds,game_status){
    if (game_status == "Completed"){
        WinnerSpots.push("Completed");
        CopyMessage += "," + "Miami"
        return "Completed"
    }
    else if (game_status != "Completed"){
        var random = randomNumberFromInterval(0,100);
        for (i=0; i<ProbabilitySet.length;i++){
            if (odds == ProbabilitySet[i][0]){
                if (random <= ProbabilitySet[i][1]){
                    if (odds < 0){
                        WinningTeams.push(home);
                        CopyMessage += "," + home
                        WinnerSpots.push("H");
                        return "H  "+home
                    }else if (odds > 0){
                        WinningTeams.push(away);
                        CopyMessage += "," + away
                        WinnerSpots.push("A");
                        return "A  "+away
                    }else if (odds == 0){
                        WinningTeams.push(home);
                        CopyMessage += "," + home
                        WinnerSpots.push("H");
                        return "H  "+home
                    }

                }
                else if (random > ProbabilitySet[i][1]){
                    if (odds < 0){
                        WinningTeams.push(away);
                        CopyMessage += "," + away
                        WinnerSpots.push("A");
                        return "A  "+away
                    }else if (odds > 0){
                        WinningTeams.push(home);
                        CopyMessage += "," + home
                        WinnerSpots.push("H");
                        return "H  "+home
                    }else if (odds == 0){
                        WinningTeams.push(away);
                        CopyMessage += "," + away
                        WinnerSpots.push("A");
                        return "A  "+away
                    }
                }  
            }    
        }
    }
    setCookie("PigskinWinnerSpots",WinnerSpots,3)
}

ScoreGame=13
//If away is favorite, odds are positive. If home is favorite, odds are negative.
Game1=PickWinner("Arizona","San Francisco",4,"Not Completed")//
Game2=PickWinner("New England","Cleveland",10.5,"Not Completed")
Game3=PickWinner("Philadelphia","Detroit",3,"Not Completed")//
Game4=PickWinner("Chicago","Indianapolis",-4.5,"Not Completed")//
Game5=PickWinner("Tennessee","Miami",-3.5,"Not Completed")//
Game6=PickWinner("Washington","Baltimore",-3.5,"Not Completed")
Game7=PickWinner("Houston","Minnesota",-6,"Not Completed")
Game8=PickWinner("New York Jets","Pittsburgh",-7,"Not Completed")
Game9=PickWinner("Atlanta","Denver",-6,"Not Completed")
Game10=PickWinner("Cincinnati","Dallas",1,"Not Completed")
Game11=PickWinner("Buffalo","Los Angeles",-3,"Not Completed")
Game12=PickWinner("San Diego","Oakland",-4,"Not Completed")
Game13=PickWinner("New York Giants","Green Bay",-7.5,"Not Completed")//
Game14=PickWinner("Tampa Bay","Carolina",-6,"Not Completed")//
//Game15=PickWinner("NY Giants","Minnesota",-4.5,"Not Completed")
//Game16=PickWinner("Green Bay","Philadelphia",-3,"Not Completed")
Game15=""
Game16=""
GameScore=ScoreSet[randomIntFromInterval(0,255)]
WinnerScore=GameScore[0]
LoserScore=GameScore[1]
CopyMessage+=","+GameScore.toString()
CopyMessage+='",",")'

var Survivor=WinningTeams[randomIntFromInterval(0,WinningTeams.length-1)]
var index = WinningTeams.indexOf(Survivor);
WinningTeams.splice(WinningTeams[index],1)
var Survivor2=WinningTeams[randomIntFromInterval(0,WinningTeams.length-1)]

var ifrm = document.createElement('iframe');
ifrm.setAttribute('id', 'ifrm'); // assign an id
ifrm.width = "180px" ;
ifrm.height = "400px" ;
ifrm.style.position = "fixed";
ifrm.style.top = "10px";
ifrm.style.left = "10px";
ifrm.style.border = "solid 1px green";
ifrm.scrolling = 'no';

document.body.appendChild(ifrm);
var doc = document.getElementById('ifrm').contentWindow.document;
doc.open();

//var setCookie_code = "<script>function setCookie(cname, cvalue, exdays, cpath, cdomain) {console.log(cname,cvalue,exdays,cpath,cdomain); if (cpath == undefined){ cpath = '/' }; if (cdomain == undefined){ cdomain = '' }; var d = new Date(); d.setTime(d.getTime() + (exdays*24*60*60*1000)); var expires = 'expires='+d.toUTCString(); document.cookie = cname + '=' + cvalue + '; ' + cpath + cdomain + expires;};</script>";
var copyButton_code = "<script>function copyToClipboard(text) {window.prompt('Copy it, Hit Enter then Paste into Google Spreadsheets:', document.getElementById('CopyMessage').innerHTML)}</script>"
//var newsimbutton = "<button onclick=setCookie('storedsim','',100)>New Simulation</button>";
//var refreshbutton = "<button onclick=window.top.location.reload()>Refresh</button>";
var copybutton = '<button onclick=copyToClipboard()>Copy</button>';
var autofillbutton = "<button onClick=parent.autoFill()>Auto Fill</button> "
//var roundSelect = '<select id="round_number" onchange="changed_round()"><option'+round_a+' value="R64">Round of 64</option><option'+round_b+' value="R32">Round of 32</option><option'+round_c+' value="R16">Sweet 16</option><option'+round_d+' value="R8">Elite 8</option><option'+round_e+' value="R4">Final 4</option><option'+round_f+' value="R2">Championship</option></select>'
//var fillStyleSelect = '<select id="fill_style" onchange="changed_fill_style()"><option'+fill_style_a+' value="Upfront">Upfront</option><option'+fill_style_b+' value="Round By Round">Round By Round</option></select>'
//var changed_fill_style_code = "<script>function changed_fill_style(){var x = document.getElementById('fill_style').value; setCookie('fill_style',x,100,'','Domain=.upickem.net; ')}</script>";
//var changed_round_code = "<script>function changed_round(){var x = document.getElementById('round_number').value; setCookie('round_number',x,100,'','Domain=.upickem.net; ')}</script>";
//var clearcookiesbutton = "<button onclick=clearCookies()>Clear Cookies</button>";
//var clearCookies_code = '<script>function clearCookies(){setCookie("storedsim","",100);setCookie("results_r1","",100);setCookie("results_r2","",100);setCookie("results_r3","",100);setCookie("results_r4","",100);setCookie("results_r5","",100);setCookie("txtFirstName","",100);setCookie("txtLastName","",100);setCookie("txtUserNameReg","",100);setCookie("txtEmailAddress","",100);setCookie("txtConfirmEmail","",100);setCookie("txtPhone","",100);setCookie("txtCity","",100);setCookie("txtPostalCode","",100);setCookie("txtExtraField","",100);setCookie("txtPasswordReg","",100);setCookie("txtConfirmPasswordReg","",100);}</script>';


var AutoFillScript = "<!DOCTYPE html><html><body>"+copyButton_code+autofillbutton+copybutton+"</br>S:"+Survivor+" "+Survivor2+"</br>"+Game1+"</br>"+Game2+"</br>"+Game3+"</br>"+Game4+"</br>"+Game5+"</br>"+Game6+"</br>"+Game7+"</br>"+Game8+"</br>"+Game9+"</br>"+Game10+"</br>"+Game11+"</br>"+Game12+"</br>"+Game13+"</br>"+Game14+"</br>"+Game15+"</br>"+Game16+"</br>"+GameScore+"</br><p id='CopyMessage'>"+CopyMessage+"</p></body></html>";

doc.write(AutoFillScript);


doc.body.style.background= "white";
doc.close();



var newDiv2 = document.createElement("script");
newDiv2.setAttribute('Id', 'AFT');
var newContent2 = document.createTextNode("function autoFill(){document.getElementById('AutoFillReady').innerHTML='Yes'}"); 
newDiv2.appendChild(newContent2); //add the text node to the newly created div. 
// add the newly created element and its content into the DOM 
var currentDiv = document.getElementById("div1"); 
document.body.insertBefore(newDiv2, currentDiv); 

var newDiv2 = document.createElement("P");
newDiv2.setAttribute('Id', 'AutoFillReady');
var newContent2 = document.createTextNode('No'); 
newDiv2.appendChild(newContent2); //add the text node to the newly created div. 
// add the newly created element and its content into the DOM 
var currentDiv = document.getElementById("div1"); 
document.body.insertBefore(newDiv2, currentDiv); 

var newDiv2 = document.createElement("P");
newDiv2.setAttribute('Id', 'PigskinWinnerSpots');
var newContent2 = document.createTextNode(WinnerSpots); 
newDiv2.appendChild(newContent2); //add the text node to the newly created div. 
// add the newly created element and its content into the DOM 
var currentDiv = document.getElementById("div1"); 
document.body.insertBefore(newDiv2, currentDiv); 




window.onload = function Checking(){
    if (document.getElementById('AutoFillReady').innerHTML=='No'){
        setTimeout(Checking, 5000);
    }else if (document.getElementById('AutoFillReady').innerHTML=='Yes'){
        alert('Winner Score is: '+WinnerScore);
        var Bottom = document.getElementById("PigskinWinnerSpots").innerHTML;
        BottomAdj = Bottom.split(",");
        console.log(BottomAdj);
        AdjSpots=BottomAdj;
        //AdjSpots=Spots.split(",")
        
        for (var i = 0; i < AdjSpots.length; i++) {
            if (AdjSpots[i] == "A"){
                gamenumber=i+1
                gamenumberstring=gamenumber.toString()
                if (ScoreGame-1 == i){
                   //document.getElementById("txtTopScore").value = WinnerScore;
                   document.getElementById("txtBottomScore").value = LoserScore;    
                }
                document.getElementById("ssTeam"+gamenumberstring+"-"+"0").click()                
            }
            else if (AdjSpots[i] == "H"){
                gamenumber=i+1
                gamenumberstring=gamenumber.toString()  
                if (ScoreGame-1 == i){
                   document.getElementById("txtTopScore").value = LoserScore;
                   //document.getElementById("txtBottomScore").value = WinnerScore;    
                }      
                document.getElementById("ssTeam"+gamenumberstring+"-"+"1").click()                
            }            
        
                    
            
        }

        
        
        
        
        
        
    }
}
//alert(WinnerSpots)