Files
training-planner/create-plan.rb
T
2014-12-21 18:48:14 -08:00

72 lines
1.8 KiB
Ruby
Executable File

#!/usr/bin/env ruby -w
require 'date'
require 'json'
require 'pp'
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)
end
end
def dump(start_date, plan, races)
puts "starting on #{start_date}"
puts "in week #{get_plan_week(start_date, Date.today, plan)+1} as of today (#{Date.today})"
races.each do |race|
weeks_from_start = get_plan_week(start_date, race[:date], plan)
week_value = plan[weeks_from_start]
puts "week #{weeks_from_start+1} (#{week_value}) #{race[:date]} #{race[:priority]} #{race[:name]}"
end
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 - 180, first_race_date)
# score the plan beginning on each monday
scores = m.map do |monday|
[score(monday,plan,races), monday]
end.sort.reverse
# output three best dates
3.times do |i|
starting = scores[i][1]
dump(starting, plan, races)
end