Skip to content

Commit

Permalink
Merge pull request #681 from cj123/any-available-car-distribution
Browse files Browse the repository at this point in the history
any available car now makes sure there is one of each car in the entrylist before randomising cars
  • Loading branch information
cj123 authored Dec 16, 2019
2 parents 72868d6 + 4b32e4b commit f2e467d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
15 changes: 15 additions & 0 deletions content_cars.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"io/ioutil"
"math"
"math/rand"
"mime/multipart"
"net/http"
"net/url"
Expand Down Expand Up @@ -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()
Expand Down
29 changes: 13 additions & 16 deletions race_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit f2e467d

Please sign in to comment.