From 35bfed90144be43f6d6d3e6caea026c389e2085d Mon Sep 17 00:00:00 2001 From: Callum Jones Date: Mon, 16 Dec 2019 16:18:42 +0000 Subject: [PATCH 1/2] any available car now makes sure there is one of each car in the entrylist before randomising cars --- CHANGELOG.md | 1 + race_manager.go | 43 +++++++++++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 16 deletions(-) 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/race_manager.go b/race_manager.go index cfd3bb862..ac2ef4dca 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.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.randomSkin(entrant.Model) } } @@ -233,6 +230,20 @@ func (rm *RaceManager) applyConfigAndStart(event RaceEvent) error { return nil } +func (rm *RaceManager) randomSkin(model string) string { + car, err := rm.carManager.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))] + } +} + func eventStartPlugin(raceConfig *CurrentRaceConfig, serverOpts *GlobalServerConfig, entryList *EntryList) error { p := &LuaPlugin{} From 4b32e4baea1ec1f010b664088c91813b1ae90f2c Mon Sep 17 00:00:00 2001 From: Callum Jones Date: Mon, 16 Dec 2019 16:27:31 +0000 Subject: [PATCH 2/2] move random skin into car manager --- content_cars.go | 15 +++++++++++++++ race_manager.go | 18 ++---------------- 2 files changed, 17 insertions(+), 16 deletions(-) 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 ac2ef4dca..726a80d19 100644 --- a/race_manager.go +++ b/race_manager.go @@ -186,7 +186,7 @@ func (rm *RaceManager) applyConfigAndStart(event RaceEvent) error { for _, entrant := range entryList { if entrant.Model == AnyCarModel { entrant.Model = car - entrant.Skin = rm.randomSkin(entrant.Model) + entrant.Skin = rm.carManager.RandomSkin(entrant.Model) break } } @@ -196,7 +196,7 @@ func (rm *RaceManager) applyConfigAndStart(event RaceEvent) error { if entrant.Model == AnyCarModel { // cars with 'any car model' become random in the entry list. entrant.Model = finalCars[rand.Intn(len(finalCars))] - entrant.Skin = rm.randomSkin(entrant.Model) + entrant.Skin = rm.carManager.RandomSkin(entrant.Model) } } @@ -230,20 +230,6 @@ func (rm *RaceManager) applyConfigAndStart(event RaceEvent) error { return nil } -func (rm *RaceManager) randomSkin(model string) string { - car, err := rm.carManager.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))] - } -} - func eventStartPlugin(raceConfig *CurrentRaceConfig, serverOpts *GlobalServerConfig, entryList *EntryList) error { p := &LuaPlugin{}