TechplexEngineer
3/12/2017 - 9:17 PM

TBA

TBA

var base = "https://www.thebluealliance.com/api/v2";
var options = {
  headers: {
    "X-TBA-App-Id": "blakeb:fantasyfrcsheet:1.0",
  }
};

//Docs: https://www.thebluealliance.com/apidocs#team-events-request
// This function takes a team number and returns a pipe delimited list of the weeks they are competing
function weeksAttending(teamNum) {
  var url = base+'/team/frc'+teamNum+'/2017/events';
  
  var response = UrlFetchApp.fetch(url, options);
  
  var json = response.getContentText();
  var data = JSON.parse(json);
  var events = data.map(function(val) {
    //Chris thinks we should change this to all events, not just districts, but if you do, it includes week 0
    if (val.event_district != 0)
      return val.week+1;
    return undefined;
  });
//  Browser.msgBox(events);
  return events.join('|');
}

function scoreCalc(teamNum){
  
}

//https://www.thebluealliance.com/apidocs#district-rankings-request
//@note not sure how often this will update...
function getRankings() {
  var url = base+'/district/ne/2017/rankings';
  
  var response = UrlFetchApp.fetch(url, options);
  
  var json = response.getContentText();
  var data = JSON.parse(json);
  
  var map = getDistrictEventsMap();
  
  var result = [];
  
  data.map(function(val) {
    for (var key in val.event_points) {
      result.push([val.team_key.replace('frc',''), key, val.event_points[key].total, map[key].week])
    }
  });
  return result;
}

 function _compare(a, b) {
   if (a[2] < b[2]) return -1;
   if (a[2] > b[2]) return 1;
   return 0;
 }

function getDistrictEvents() {
  var url = base + "/district/ne/2017/events";
  
  var response = UrlFetchApp.fetch(url, options);
  
  var json = response.getContentText();
  var data = JSON.parse(json);
  data = data.map(function(val) {
    return [val.short_name, val.key, val.week+1]
  });
  
  return data.sort(_compare);
  
}

function getDistrictEventsMap() {
  var url = base + "/district/ne/2017/events";
  
  var response = UrlFetchApp.fetch(url, options);
  
  var json = response.getContentText();
  var data = JSON.parse(json);
  var map = {
    
  };
  
  data.map(function(val) {
    map[val.key] = {
      week: val.week+1,
      short_name: val.short_name
    }
  });
  
  return map;
  
}