-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
race_weekend_filter.go
282 lines (205 loc) · 7.53 KB
/
race_weekend_filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package servermanager
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/cj123/ini"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
)
type FilterError string
func (f FilterError) Error() string {
return string(f)
}
type RaceWeekendFilterSplitType string
func (fst RaceWeekendFilterSplitType) String() string {
return string(fst)
}
const (
SplitTypeNumeric RaceWeekendFilterSplitType = "Numeric"
SplitTypeManualDriverSelection RaceWeekendFilterSplitType = "Manual Driver Selection"
SplitTypeChampionshipClass RaceWeekendFilterSplitType = "Championship Class"
)
type RaceWeekendSessionToSessionFilter struct {
// IsPreview indicates that the Filter is for preview only, and will not actually affect a starting grid.
IsPreview bool
// ResultStart is the beginning of the split from the previous session's result
ResultStart int
// ResultEnd is the end of the split from the previous session's result
ResultEnd int
// NumEntrantsToReverse defines how many entrants to reverse. -1 indicates all, 0 indicates none, or N entrants.
NumEntrantsToReverse int
// EntryListStart is where to place the entrants in the starting grid of the next session
EntryListStart int
// SortType defines how the entrants are sorted
SortType string
// ForceUseTyreFromFastestLap forces drivers to start on the same tyre compound as the tyre compound that
// they achieved their fastest lap on in the previous session
ForceUseTyreFromFastestLap bool
// AvailableResultsForSorting are the results files to be used for sorting the entrants.
AvailableResultsForSorting []string
// SplitType defines the kind of splitting that we want to do.
SplitType RaceWeekendFilterSplitType
// SelectedDriverGUIDs is a list of the currently selected driver GUIDs. This is only populated if SplitType == SplitTypeManualDriverSelection
SelectedDriverGUIDs []string
// SelectedChampionshipClassIDs is a list of the currently selected ChampionshipClass IDs. This is only populated if SplitType == SplitTypeChampionshipClass
SelectedChampionshipClassIDs map[uuid.UUID]bool
// Deprecated: ManualDriverSelection indicates that drivers are picked manually from the above results file.
ManualDriverSelection bool
}
func reverseEntrants(numToReverse int, entrants []*RaceWeekendSessionEntrant) {
if numToReverse == 0 {
return
}
if numToReverse > len(entrants) {
numToReverse = len(entrants)
}
var toReverse []*RaceWeekendSessionEntrant
if numToReverse > 0 {
toReverse = entrants[:numToReverse]
} else {
toReverse = entrants
}
for i := len(toReverse)/2 - 1; i >= 0; i-- {
opp := len(toReverse) - 1 - i
toReverse[i], toReverse[opp] = toReverse[opp], toReverse[i]
}
for i := 0; i < len(toReverse); i++ {
entrants[i] = toReverse[i]
}
}
var ErrRaceWeekendUnknownSplitType = errors.New("servermanager: unknown split type")
// Filter takes a set of RaceWeekendSessionEntrants formed by the results of the parent session and filters them into a child session entry list.
func (f RaceWeekendSessionToSessionFilter) Filter(raceWeekend *RaceWeekend, parentSession, childSession *RaceWeekendSession, parentSessionResults []*RaceWeekendSessionEntrant, childSessionEntryList *RaceWeekendEntryList) error {
if parentSession.Completed() || childSession.IsBase() {
sorter := GetRaceWeekendEntryListSort(f.SortType)
parentSession.NumEntrantsToReverse = f.NumEntrantsToReverse
// race weekend session is completed and has a valid sorter, use it to sort results before filtering.
if err := sorter.Sort(raceWeekend, parentSession, parentSessionResults, &f); err != nil {
return err
}
}
entryListStart := f.EntryListStart - 1
var split []*RaceWeekendSessionEntrant
switch f.SplitType {
case SplitTypeNumeric:
resultStart, resultEnd := f.ResultStart, f.ResultEnd
resultStart--
if resultStart > len(parentSessionResults) {
return nil
}
if resultEnd > len(parentSessionResults) {
resultEnd = len(parentSessionResults)
}
split = parentSessionResults[resultStart:resultEnd]
case SplitTypeManualDriverSelection:
for _, driverGUID := range f.SelectedDriverGUIDs {
for _, entrant := range parentSessionResults {
if entrant.Car.GetGUID() == driverGUID {
split = append(split, entrant)
break
}
}
}
case SplitTypeChampionshipClass:
for _, entrant := range parentSessionResults {
class := entrant.ChampionshipClass(raceWeekend)
if _, ok := f.SelectedChampionshipClassIDs[class.ID]; ok {
split = append(split, entrant)
}
}
default:
return ErrRaceWeekendUnknownSplitType
}
if !parentSession.Completed() {
reverseEntrants(f.NumEntrantsToReverse, split)
}
splitIndex := 0
for pitBox := entryListStart; pitBox < entryListStart+len(split); pitBox++ {
entrant := split[splitIndex]
entrant.SessionID = parentSession.ID
if !f.IsPreview && parentSession.Completed() && f.ForceUseTyreFromFastestLap {
// find the tyre from the entrants fastest lap
fastestLap := entrant.SessionResults.GetDriversFastestLap(entrant.Car.GetGUID(), entrant.Car.GetCar())
if fastestLap == nil {
logrus.Warnf("could not find fastest lap for entrant %s (%s). will not lock their tyre choice.", entrant.Car.GetName(), entrant.Car.GetGUID())
} else {
err := raceWeekend.buildLockedTyreSetup(childSession, entrant, fastestLap)
if err != nil {
logrus.WithError(err).Errorf("could not build locked tyre setup for entrant %s (%s)", entrant.Car.GetName(), entrant.Car.GetGUID())
}
}
}
childSessionEntryList.AddInPitBox(entrant, pitBox)
splitIndex++
}
return nil
}
const lockedTyreSetupFolder = "server_manager_locked_tyres"
func (rw *RaceWeekend) buildLockedTyreSetup(session *RaceWeekendSession, entrant *RaceWeekendSessionEntrant, fastestLap *SessionLap) error {
tyreIndex, err := findTyreIndex(entrant.Car.Model, fastestLap.Tyre, session.RaceConfig)
if err != nil {
return err
}
entryList := rw.GetEntryList()
var setup *ini.File
for _, raceWeekendEntrant := range entryList {
if raceWeekendEntrant.GUID == entrant.Car.GetGUID() && raceWeekendEntrant.FixedSetup != "" {
setup, err = ini.Load(filepath.Join(ServerInstallPath, "setups", raceWeekendEntrant.FixedSetup))
if err != nil {
return err
}
break
}
}
if setup == nil {
// no fixed setup was specified
// write out a temp ini setup file for this car + player.
setup = ini.NewFile([]ini.DataSource{nil}, ini.LoadOptions{
IgnoreInlineComment: true,
})
_, err = setup.NewSection("DEFAULT")
if err != nil {
return err
}
car, err := setup.NewSection("CAR")
if err != nil {
return err
}
_, err = car.NewKey("MODEL", entrant.Car.Model)
if err != nil {
return err
}
}
tyres, err := setup.NewSection("TYRES")
if err != nil {
return err
}
_, err = tyres.NewKey("VALUE", fmt.Sprintf("%d", tyreIndex))
if err != nil {
return err
}
raceWeekendSection, err := setup.NewSection("RACE_WEEKEND")
if err != nil {
return err
}
_, err = raceWeekendSection.NewKey("ID", rw.ID.String())
if err != nil {
return err
}
_, err = raceWeekendSection.NewKey("SESSION_ID", entrant.SessionID.String())
if err != nil {
return err
}
setupFilePath := filepath.Join(entrant.Car.Model, lockedTyreSetupFolder, fmt.Sprintf("race_weekend_session_%s_%s.ini", entrant.Car.GetGUID(), entrant.SessionID.String()))
fullSaveFilepath := filepath.Join(ServerInstallPath, "setups", setupFilePath)
if err := os.MkdirAll(filepath.Dir(fullSaveFilepath), 0755); err != nil {
return err
}
if err := setup.SaveTo(fullSaveFilepath); err != nil {
return err
}
entrant.OverrideSetupFile = setupFilePath
return nil
}