Skip to content

Instantly share code, notes, and snippets.

@chaffeqa
Created April 7, 2016 23:25
Show Gist options
  • Select an option

  • Save chaffeqa/5a1682c463c4ce944a6d12613a2c85e5 to your computer and use it in GitHub Desktop.

Select an option

Save chaffeqa/5a1682c463c4ce944a6d12613a2c85e5 to your computer and use it in GitHub Desktop.

Revisions

  1. chaffeqa created this gist Apr 7, 2016.
    132 changes: 132 additions & 0 deletions generateRunsFor.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,132 @@
    var _generateRunsFor = function( entry ){
    debugGroupCollapsed('_generateRunsFor');
    var
    picks = entry.picks
    ,props = DataHandler.game.props
    ,key = ''
    ,propId, i, prop, pick
    ,runs = [], run, runI = 0
    ,longestI = -1, longestRunCnt = 0
    ,answerWithTie
    ,incorrectTeam
    ,bonusKey
    ,addedBonusKeys = {}
    ,bonus
    ,bonusEndedAt
    ,bonuses = DataHandler.game.bonus
    ,processingBonus
    ;
    // first run
    run = {
    pts: 0
    ,isLongest: false
    ,i: (runI++)
    ,propIds: []
    };
    runs.unshift(run);
    key += '|' + runI + ':';

    for (i = 0; i < props.length; i++) {
    prop = props[i];
    pick = picks[ prop.id ];
    processingBonus = false
    for ( bonusKey in DataHandler.game.bonus ){
    bonusEndedAt = entry[ bonusKey + '_eat' ]
    if (!processingBonus && !addedBonusKeys[ bonusKey ] && bonusEndedAt && (prop.state !== 4 || bonusEndedAt < prop.eat) ) {
    debug("adding bonus before: " + prop.id)
    prop = DataHandler.game.bonus[ bonusKey ];
    pick = picks[ prop.id ] || true;
    answerWithTie = pick
    addedBonusKeys[ bonusKey ] = true
    processingBonus = true
    i--; // so this will redo the current prop
    }
    }
    if (prop.state == 4 && (pick === true || pick === false )) {
    // if its a tie, set answerWithTie to be the users answer so its 'correct'
    answerWithTie = prop.ans == 'tie' ? pick : prop.ans;
    // user picked for this prop! process run logic
    debug("was correct?: " + pick === answerWithTie)
    debug("processing pick for prop.id: " + prop.id)
    // part of a run
    debug("Adding to run: ", run)
    run.pts += (pick === answerWithTie ? prop.tpts : 0);
    run.propIds.unshift(prop.id)
    key += ',' + prop.id;
    if (pick !== answerWithTie){
    debug("Breaking run!")
    incorrectTeam = DataHandler.game.teams[ pick === prop.r.val ? prop.r.id : prop.l.id ]
    run.breaker = incorrectTeam && (incorrectTeam.school + ' Losing Match-up') || ('#' + prop.num)
    run = undefined;
    }
    if (!run) {
    // start of a new run
    debug("Start of a new run!")
    run = {
    pts: 0
    ,isLongest: false
    // ,braker: null
    ,i: (runI++)
    ,propIds: []
    };
    runs.unshift(run);
    key += '|' + runI + ':';
    }
    }
    }
    // close game: no more current runs
    if (DataHandler.game.game_state == 'closed' && runs.length > 0 && runs[0].propIds.length > 0) {
    run = {
    pts: 0
    ,isLongest: false
    // ,braker: null
    ,i: (runI++)
    ,propIds: []
    };
    runs.unshift(run);
    }
    for (i = 0; i < runs.length; i++) {
    if (runs[i].pts > longestRunCnt){
    longestRunCnt = runs[i].pts;
    longestI = i;
    }
    }
    if (longestI >= 0) {
    runs[longestI].isLongest = true;
    runs[0].isCurrent = true;
    }
    runs.sort( _runSortFunction );
    debug("_generateRunsFor: runs: ", runs)
    entry.runs = runs;
    entry.formerRunKey = entry.runKey;
    entry.runKey = key;
    debugGroupEnd();
    return entry;
    }


    // If compareFunction(a, b) is less than 0, sort a to a lower index than b, i.e. a comes first.
    // If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.
    // If compareFunction(a, b) is greater than 0, sort b to a lower index than a.
    // compareFunction(a, b) must always return the same value when given a specific pair of elements a and b as its two arguments. If inconsistent results are returned then the sort order is undefined
    function _runSortFunction(a,b) {
    // current run is first
    if (a.isCurrent) return -1;
    if (b.isCurrent) return 1;
    // then by isLongest per dmac
    if (a.isLongest) return -1;
    if (b.isLongest) return 1;
    // otherwise: the most recent run that is the top run.
    return b.i - a.i;
    // // otherwise: If there is a tie for top runs, it will the most recent run that is the top run.
    // if (a.pts === b.pts) return b.i - a.i;
    // return b.pts - a.pts;
    // if (a is less than b by some ordering criterion) {
    // return -1;
    // }
    // if (a is greater than b by the ordering criterion) {
    // return 1;
    // }
    // // a must be equal to b
    // return 0;
    }