Source: site.view [edit]
Function name: chooseRound
Arguments: Players,stat,round,date
Description: Choose two random players to compare on stat. As round gets higher, the challenge should get harder, namely the two stat values are closer together.
Page type: webl
Render function:  
Module: sportstreak

Page source:

  
  // Return Max of A vs. B
  var Max = fun(a, b)
     if a > b then
        return a
     else
        return b
     end
  end;

  // Return Min of A vs. B
  var Min = fun(a, b)
     if a < b then
        return a
     else
        return b
     end
  end;

  // Return Abs of A
  var Abs = fun(a)
     if a < 0 then
        return -a
     else
        return a
     end
  end;


  // The higher the better usually, except for ESPN ranking.
  var getStat = fun(player, stat, reverseESPN)
      var s = (ToReal(player[stat]) ? 0);
      if reverseESPN and (stat == "ESPN Ranking") then
         s = 76 - s
      end;
      return s
  end;

  var getOriginalIndex = fun(sp)
     var i = 0;
     while (i < Size(Players)) do
        if sp["Player Name"] == Players[i]["Player Name"] then
           return i
        else
           i = i + 1
        end;
     end;
     return 0
  end;

  var MAXRND = 503;

  // Returns a random number mod max
  // Seed number makes random number static or not
  var getRndWithDateSeed = fun(date, max, seed)
     var rnd = WubCall("sportstreak.getRandom", [date, MAXRND]);
     seed = seed + 1;
     if (seed > MAXRND) then
        seed = 0
     end;
     return rnd[seed] mod max
  end;

  // Sort the players based on Stat
  var sp = Sort(Players, fun(p1, p2)
            var s1 = getStat(p1, stat, true);
            var s2 = getStat(p2, stat, true);
            if s1 > s2 then return 1
            elsif s1 == s2 then return 0
            else return -1
            end
         end);
  var sortedPlayers = [];
  every p in sp do
     if getStat(p, stat, true) > 0 then
        sortedPlayers = sortedPlayers + [p]
     end
  end;


  // SELECT PLAYERS 
    var turn = round + 1;
    // Assuming turn starts from 1 and increases.
    // Adjust the range based on the turn, making the game harder.

    // Get random first player out of full list
    var firstPlayerIndex = getRndWithDateSeed(date, Size(sortedPlayers), turn);
    var secondPlayerIndex;

    var numPartitions = 8;
    var getsHarderEveryNRounds = 3;
    var sizePartitions = ToInt(Size(sortedPlayers) / numPartitions);
    var distance = Max(ToInt(4 - (round / getsHarderEveryNRounds)), 0) * sizePartitions;
    if firstPlayerIndex < (Size(sortedPlayers) / 2) then
        secondPlayerIndex = firstPlayerIndex + distance
    else
        secondPlayerIndex = firstPlayerIndex - distance
    end;
       
    // Calculate the range for the second player.
    var rangeStart = Max(secondPlayerIndex - sizePartitions, 0);
    var rangeEnd = Min(secondPlayerIndex + sizePartitions, Size(sortedPlayers) - 1);
    
    var done = false;
    while !done do

        // Select the second player within this range.
        secondPlayerIndex = getRndWithDateSeed(date, (rangeEnd - rangeStart + 1), turn) + rangeStart;
        done = (secondPlayerIndex != firstPlayerIndex);

        // increase random seed
        turn = turn + 1
    end;


  [. player1=getOriginalIndex(sortedPlayers[firstPlayerIndex]), player2=getOriginalIndex(sortedPlayers[secondPlayerIndex]), delta = Abs(firstPlayerIndex - secondPlayerIndex), s1 = getStat(sortedPlayers[firstPlayerIndex],stat, false), s2 = getStat(sortedPlayers[secondPlayerIndex],stat,false) .];