#!/usr/bin/env ruby -w require 'date' require 'json' def load_races # get data from json file and convert to ruby types all_races = JSON.parse(File.read('races.json'))["races"].map do |race| { :date => Date.parse(race["date"]), :priority => race["priority"], :name => race["name"] } end # select all non-zero priority races all_races.select { |r| r[:priority] > 0 }.sort_by { |r| r[:date] } end def load_plan # get data from json file JSON.parse(File.read('plan.json'))["weeks"] end def mondays(first,last) (first..last).map { |d| Date.commercial(d.year, d.cweek, 1) }.sort.uniq end def get_plan_week(start_date, test_date, plan) days_from_start = (test_date - start_date).to_i (days_from_start / 7) % plan.size end def score(start_date, plan, races) races.inject(0.0) do |sum, race| weeks_from_start = get_plan_week(start_date, race[:date], plan) week_value = plan[weeks_from_start] sum + (week_value * (race[:priority].to_f ** 3.0)) end end # TODO hacky. should be in the data. def get_week_desc(wnum) case wnum when 0..6 "building" when 7..10 "peaking" when 11..14 "recovery" end end def dump(start_date, plan, races) puts "================================================================================" puts "starting on #{start_date}" puts "in week #{get_plan_week(start_date, Date.today, plan)+1} as of today (#{Date.today})" puts "--------------------------------------------------------------------------------" races.each do |race| weeks_from_start = get_plan_week(start_date, race[:date], plan) puts "week #{weeks_from_start+1} #{get_week_desc(weeks_from_start)} #{race[:date]} #{race[:priority]} #{race[:name]}" end puts "================================================================================" puts end # load data races = load_races plan = load_plan # find all the mondays from a few months before the first date, through the end first_race_date = races[0][:date] m = mondays(first_race_date - ((plan.size-1)*7), first_race_date) # score the plan beginning on each monday scores = m.map do |monday| [score(monday,plan,races), monday] end.sort.reverse # output the best dates dump(scores[0][1], plan, races)