<commit_before>package model

import (
	"appengine"
	"appengine/datastore"
	"strconv"
	"time"
)

func deactivateAllSeasons(c appengine.Context) {
	q := datastore.NewQuery("Season").Filter("Active =", true)
	var seasons []Season
	_, err := q.GetAll(c, &seasons)
	if err != nil {
		c.Errorf("Error attempting to get all Seasons from the datastore '%s'", err)
		panic(err)
	}
	for _, s := range seasons {
		s.Active = false
		SaveSeason(c, s)
	}
}

// Creates the schedule and attaches it to the season entity passed in
func generateSchedule(season *SeasonJson) {
	//TODO: Implement this for reals
	season.Weeks = make([]Week, 1)
	season.Weeks[0] = Week {
		Number: 1,
		Date: time.Now(),
		Games: make([]Game, len(season.Players) / 2),
	}
	for i := 0; i < len(season.Players) / 2; i++ {
		season.Weeks[0].Games[i] = Game {
			PlayerIds: make([]string, 2),
			WinnerId: "",
		}
		season.Weeks[0].Games[i].PlayerIds[0] = season.Players[i*2].Name
		season.Weeks[0].Games[i].PlayerIds[1] = season.Players[(i*2)+1].Name
	}
}

// Creates and adds the divisions to the season entity passed in.
func generateDivisions(season *SeasonJson, conferenceCount int, divisionCount int) {
	for i := 0; i < conferenceCount; i++ {
		divisions := make([]Division, divisionCount)
		for j := 0; j < divisionCount; j++ {
			divisions[j] = Division {
				Name: "Division" + strconv.Itoa(j),
				PlayerIds: make([]string, 0),
			}
		}
		season.Conferences[i] = Conference {
			Name: "Conference " + strconv.Itoa(i+1),
			Divisions: divisions,
		}
	}
	playersOrdered := make([]PlayerJson, 0)
	for _, v := range playersByFaction(season.Players) {
		playersOrdered = append(playersOrdered, v...)
	}
	divisions := make([]*Division, conferenceCount * divisionCount)
	for d := 0; d < divisionCount; d++ {
		for c := 0; c < conferenceCount; c++ {
			divisions[c + (d * conferenceCount)] = &(season.Conferences[c].Divisions[d])
		}
	}
	for i, p := range playersOrdered {
		divisionIndex := i % len(divisions)
		divisions[divisionIndex].PlayerIds = append(divisions[divisionIndex].PlayerIds, p.Name)
		divisionIndex++
	}
}

// Creates a map of the players by faction.
func playersByFaction(players []PlayerJson) map[string][]PlayerJson {
	playersByFaction := make(map[string][]PlayerJson)
	for _, p := range players {<commit_msg>Fixed an off-by-one error on season creation<commit_after>package model

import (
	"appengine"
	"appengine/datastore"
	"strconv"
	"time"
)

func deactivateAllSeasons(c appengine.Context) {
	q := datastore.NewQuery("Season").Filter("Active =", true)
	var seasons []Season
	_, err := q.GetAll(c, &seasons)
	if err != nil {
		c.Errorf("Error attempting to get all Seasons from the datastore '%s'", err)
		panic(err)
	}
	for _, s := range seasons {
		s.Active = false
		SaveSeason(c, s)
	}
}

// Creates the schedule and attaches it to the season entity passed in
func generateSchedule(season *SeasonJson) {
	//TODO: Implement this for reals
	season.Weeks = make([]Week, 1)
	season.Weeks[0] = Week {
		Number: 1,
		Date: time.Now(),
		Games: make([]Game, len(season.Players) / 2),
	}
	for i := 0; i < len(season.Players) / 2; i++ {
		season.Weeks[0].Games[i] = Game {
			PlayerIds: make([]string, 2),
			WinnerId: "",
		}
		season.Weeks[0].Games[i].PlayerIds[0] = season.Players[i*2].Name
		season.Weeks[0].Games[i].PlayerIds[1] = season.Players[(i*2)+1].Name
	}
}

// Creates and adds the divisions to the season entity passed in.
func generateDivisions(season *SeasonJson, conferenceCount int, divisionCount int) {
	for i := 0; i < conferenceCount; i++ {
		divisions := make([]Division, divisionCount)
		for j := 0; j < divisionCount; j++ {
			divisions[j] = Division {
				Name: "Division" + strconv.Itoa(j+1),
				PlayerIds: make([]string, 0),
			}
		}
		season.Conferences[i] = Conference {
			Name: "Conference " + strconv.Itoa(i+1),
			Divisions: divisions,
		}
	}
	playersOrdered := make([]PlayerJson, 0)
	for _, v := range playersByFaction(season.Players) {
		playersOrdered = append(playersOrdered, v...)
	}
	divisions := make([]*Division, conferenceCount * divisionCount)
	for d := 0; d < divisionCount; d++ {
		for c := 0; c < conferenceCount; c++ {
			divisions[c + (d * conferenceCount)] = &(season.Conferences[c].Divisions[d])
		}
	}
	for i, p := range playersOrdered {
		divisionIndex := i % len(divisions)
		divisions[divisionIndex].PlayerIds = append(divisions[divisionIndex].PlayerIds, p.Name)
		divisionIndex++
	}
}

// Creates a map of the players by faction.
func playersByFaction(players []PlayerJson) map[string][]PlayerJson {
	playersByFaction := make(map[string][]PlayerJson)
	for _, p := range players {
{'fraction_non_alphanumeric': 0.1560943927256982, 'fraction_numerical': 0.009742368478025547, 'mean_word_length': 4.185185185185185, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1755652', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1638, 'n_words': 406}