//Simulates next event in the season public async Task <string> SimulateNextEvent(Guid id) { Season season = await GetSeason(id); if (season.CurrentEventIndex == season.Events.Count) { throw new OutOfRangeException("Cannot simulate season that has already ended!"); } //If the standings for the next event are empty, populate event standing with drivers from the season if (season.Events[season.CurrentEventIndex].Standings.Count == 0) { await AddToEventStandings(id, season.Drivers); } string returnString = ""; returnString += ("Simulating event " + (season.CurrentEventIndex + 1) + ": " + season.Events[season.CurrentEventIndex].Name + "!\n"); returnString += ("Results of the event were:\n"); List <KeyValuePair <Driver, int> > updatedStandings = CalculateResults(season.Drivers); season.Events[season.CurrentEventIndex].Standings = updatedStandings; // For return print for (int i = 0; i < updatedStandings.Count; i++) { returnString += "\n" + (i + 1) + ". " + updatedStandings[i].Key.Name + " - " + updatedStandings[i].Value + " points"; } // Update events in document var filter = Builders <Season> .Filter.Eq(s => s.Id, id); //Update CurrentLevelIndex in document season.CurrentEventIndex++; //Find every driver in season standings and calculate overall score by adding every event together for (int i = 0; i < season.Standings.Count; i++) { for (int j = 0; j < updatedStandings.Count; j++) { if (updatedStandings[j].Key.Id == season.Standings[i].Key.Id) { season.Standings[i] = new KeyValuePair <Driver, int>(updatedStandings[j].Key, AddDriverStandings(season, updatedStandings[j].Key)); } } } returnString += "\n\nAfter " + season.CurrentEventIndex + " event(s), the season standings are as follows:\n"; //Sort the standings season.Standings.Sort((x, y) => x.Value.CompareTo(y.Value)); season.Standings.Reverse(); //For printing for (int i = 0; i < season.Standings.Count; i++) { returnString += "\n" + (i + 1) + ". " + season.Standings[i].Key.Name + " - " + season.Standings[i].Value + " points"; } // Update whole document await _seasonCollection.ReplaceOneAsync(filter, season); return(returnString); }
//------------------ CRUD-OPERATIONS----------------------- //----CREATE----------------------- public async Task <Season> CreateSeason(Season season) { await _seasonCollection.InsertOneAsync(season); return(season); }