Skip to content

Instantly share code, notes, and snippets.

@0x6e
Last active January 30, 2016 19:59
Show Gist options
  • Save 0x6e/692ab447cd7f60b44054 to your computer and use it in GitHub Desktop.
Save 0x6e/692ab447cd7f60b44054 to your computer and use it in GitHub Desktop.
/*
* Expects a JSON array of teams in the same format as byeTeam
*/
const fs = require('fs');
const util = require('util');
var teamFile = 'teams.json';
const byeTeam = { coach: '', race: '', teamName: 'BYE' };
// Parse teams:
var teams;
try {
teams = JSON.parse(fs.readFileSync(teamFile, 'utf8'));
}
catch (e) {
console.log(util.format("Got error: %s\nDoes '%s' exist?", e.message, teamFile));
return;
}
// Setup indexes:
if (teams.length % 2 != 0)
teams.push("BYE");
var indexes = [];
for (var i = 0; i < teams.length; ++i)
indexes.push(i);
// Randomise the indexes (a good old Fisher–Yates Shuffle):
var currentIndex = indexes.length, temporaryIndex, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryIndex = indexes[currentIndex];
indexes[currentIndex] = indexes[randomIndex];
indexes[randomIndex] = temporaryIndex;
}
// Schedule the league:
var top = indexes.slice(0, indexes.length / 2);
var bottom = indexes.slice(indexes.length / 2, indexes.length);
var weeks = indexes.length - 1;
var league = {
teams: teams,
schedule: []
}
for (var i = 0; i < weeks; ++i) {
temporaryIndex = top.shift();
bottom.push(top.pop());
top.unshift(bottom.shift());
top.unshift(temporaryIndex);
var week = {day: i + 1, games:[]};
for (var j = 0; j < top.length; ++j) {
week.games.push( {a:top[j], b:bottom[j], played:false});
}
league.schedule.push(week)
}
fs.writeFileSync("schedule.json", JSON.stringify(league, null, " "), "utf8");
/*
* Loads a schedule.json file output by league-scheduler.js and prints it in a
* couple of useful formats.
*/
var scheduleFile = 'schedule.json';
var startDate = new Date("2016-01-31"); // ISO 8601: "YYYY-MM-DD"
const fs = require('fs');
const util = require('util');
// Parse schedule:
var schedule;
try {
schedule = JSON.parse(fs.readFileSync(scheduleFile, 'utf8'));
}
catch (e) {
console.log( "Got error: {0}\nDoes {1} exist?".format(e.message, teamFile));
return;
}
var teams = schedule.teams;
schedule = schedule.schedule;
// Print the schedule:
var date = new Date(startDate);
var csv = fs.createWriteStream('schedule.csv');
var forum = fs.createWriteStream('schedule.forum');
schedule.forEach(function (day, dayIndex, dayArray) {
csv.write(util.format("Day %d,%d/%d/%d,", day.day, date.getMonth() + 1, date.getDate(), date.getFullYear()))
forum.write(util.format("Day %d:\n", day.day));
day.games.forEach(function (game, gameIndex, gameArray) {
csv.write(util.format("%s,vs,%s,", teams[game.a].teamName, teams[game.b].teamName));
forum.write(util.format("%s [b]vs[/b] %s\n", teams[game.a].teamName, teams[game.b].teamName));
});
date.setDate(date.getDate() + 7);
csv.write("\n");
forum.write("\n\n");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment