public void Save(SQLiteConnection db) { RaceHeat re = db.Find <RaceHeat>(x => x.HeatID == this.HeatID); if (re == null) { db.Insert(this); } else { db.Update(this); } foreach (HeatTime ht in this.HeatTimes) { ht.Save(db); } }
public void CreateHeats(SQLiteConnection db) { // each race must run on each lane once. // x lanes are available to race on simultaneously // heatcount = racers * x / x = racers int HeatCount = this.Racers.Count; int iTracks = this.TrackCount; //generate shuffled list of racers Random rng = new Random(); List <Racer> shuffledRacers = this.Racers.Shuffle(rng).ToList(); // create the heats + heat times for (int i = 0; i < HeatCount; i++) { RaceHeat h = new RaceHeat(); h.EventID = this.EventID; h.HeatID = Guid.NewGuid(); h.HeatNumber = (i + 1); this.Heats.Add(h); for (int j = 0; j < iTracks; j++) { HeatTime ht = new HeatTime(this); ht.HeatTimeID = Guid.NewGuid(); ht.HeatID = h.HeatID; ht.TrackNumber = j + 1; ht.RaceTime = null; ht.RacerID = null; h.HeatTimes.Add(ht); } } for (int i = 0; i < iTracks; i++) { using (IEnumerator <Racer> ienumShuffled = shuffledRacers.GetEnumerator()) { foreach (RaceHeat heat in this.Heats) { if (!ienumShuffled.MoveNext()) { break; } HeatTime ht = heat.HeatTimes.First(x => x.TrackNumber == (i + 1)); ht.RacerID = ienumShuffled.Current.RacerID; } } Racer[] rgShuffledRacers = shuffledRacers.ToArray(); int RotateBy = HeatCount / iTracks; // if less tracks than racers, we'll run into an issue where we won't rotate if (RotateBy == 0) { RotateBy = 1; } rgShuffledRacers.RotateLeft(RotateBy); shuffledRacers = new List <Racer>(rgShuffledRacers); } #if DEBUG StringBuilder sb = new StringBuilder(); foreach (RaceHeat rh in this.Heats) { sb.AppendFormat("Heat {0}: ", rh.HeatNumber.ToString().PadLeft(2)); foreach (HeatTime ht in rh.HeatTimes) { sb.AppendFormat("({0}): {1} {2}. ", ht.TrackNumber, ht.Racer.FirstName, ht.Racer.LastName.Substring(0, 1)); } sb.AppendLine(); } //System.Diagnostics.Debug.Print(sb.ToString()); #endif }