diff --git a/CHANGELOG.md b/CHANGELOG.md index 544646e92..3ae5f2191 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Added: * The UDP message receiver now detects if it has fallen behind while handling messages. If it has, it reduces the refresh rate that the Assetto Corsa Server sends messages at, so it can catch up. If you see a lot of "can't keep up!" messages in the Server Logs, you probably need to increase your 'refresh_interval_ms' in the config.yml. * Added configurable Open Graph images to both the manager as a whole and championships, with these you can link to images that will be shown whenever you share a link of the manager/championship pages on social media (premium). * Optimised the handling of UDP messages to improve performance. +* When using "Any Available Car", one of each car is added to the EntryList (so long as there are enough entrants!) and then after that Entrant cars are randomised. Fixes: diff --git a/content_cars.go b/content_cars.go index cf2ebbdf1..cf98c3d7c 100644 --- a/content_cars.go +++ b/content_cars.go @@ -7,6 +7,7 @@ import ( "io" "io/ioutil" "math" + "math/rand" "mime/multipart" "net/http" "net/url" @@ -405,6 +406,20 @@ func (cm *CarManager) LoadCar(name string, tyres Tyres) (*Car, error) { }, nil } +func (cm *CarManager) RandomSkin(model string) string { + car, err := cm.LoadCar(model, nil) + + if err != nil { + logrus.WithError(err).Errorf("Could not load car %s. No skin will be specified", model) + return "" + } else if len(car.Skins) == 0 { + logrus.Warnf("Car %s has no skins uploaded. No skin will be specified", model) + return "" + } else { + return car.Skins[rand.Intn(len(car.Skins))] + } +} + // ResultsForCar finds results for a given car. func (cm *CarManager) ResultsForCar(car string) ([]SessionResults, error) { results, err := ListAllResults() diff --git a/race_manager.go b/race_manager.go index cfd3bb862..726a80d19 100644 --- a/race_manager.go +++ b/race_manager.go @@ -181,25 +181,22 @@ func (rm *RaceManager) applyConfigAndStart(event RaceEvent) error { return err } + // any available car should make sure you have one of each before randomising (#678) + for _, car := range finalCars { + for _, entrant := range entryList { + if entrant.Model == AnyCarModel { + entrant.Model = car + entrant.Skin = rm.carManager.RandomSkin(entrant.Model) + break + } + } + } + for _, entrant := range entryList { if entrant.Model == AnyCarModel { // cars with 'any car model' become random in the entry list. - cars := strings.Split(config.CurrentRaceConfig.Cars, ";") - - entrant.Model = cars[rand.Intn(len(cars))] - - // generate a random skin too - car, err := rm.carManager.LoadCar(entrant.Model, nil) - - if err != nil { - logrus.WithError(err).Errorf("Could not load car %s. No skin will be specified", entrant.Model) - entrant.Skin = "" - } else if len(car.Skins) == 0 { - logrus.Warnf("Car %s has no skins uploaded. No skin will be specified", entrant.Model) - entrant.Skin = "" - } else { - entrant.Skin = car.Skins[rand.Intn(len(car.Skins))] - } + entrant.Model = finalCars[rand.Intn(len(finalCars))] + entrant.Skin = rm.carManager.RandomSkin(entrant.Model) } }