-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
entrylist_ini.go
356 lines (276 loc) · 7.54 KB
/
entrylist_ini.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package servermanager
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/cj123/ini"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
)
const (
AnyCarModel = "any_car_model"
entryListFilename = "entry_list.ini"
driverSwapEntrantSeparator = ";"
maxEntryListSize = 255
)
type EntryList map[string]*Entrant
// Write the EntryList to the server location
func (e EntryList) Write() error {
setupDirectory := filepath.Join(ServerInstallPath, "setups")
// belt and braces check to make sure setup file exists
for _, entrant := range e.AsSlice() {
if entrant.FixedSetup != "" {
if _, err := os.Stat(filepath.Join(setupDirectory, entrant.FixedSetup)); os.IsNotExist(err) {
return err
}
}
}
for i, entrant := range e.AsSlice() {
entrant.PitBox = i
}
f := ini.NewFile([]ini.DataSource{nil}, ini.LoadOptions{
IgnoreInlineComment: true,
})
// making and throwing away a default section due to the utter insanity of ini or assetto. i don't know which.
_, err := f.NewSection("DEFAULT")
if err != nil {
return err
}
for _, v := range e.AsSlice() {
s, err := f.NewSection(fmt.Sprintf("CAR_%d", v.PitBox))
if err != nil {
return err
}
err = s.ReflectFrom(&v)
if err != nil {
return err
}
}
return f.SaveTo(filepath.Join(ServerInstallPath, ServerConfigPath, entryListFilename))
}
func (e EntryList) ReadString() (string, error) {
content, err := ioutil.ReadFile(filepath.Join(ServerInstallPath, ServerConfigPath, entryListFilename))
if err != nil {
return "", err
}
return string(content), nil
}
// Add an Entrant to the EntryList
func (e EntryList) AddToBackOfGrid(entrant *Entrant) {
e.AddInPitBox(entrant, len(e))
}
// AddInPitBox adds an Entrant in a specific pitbox - overwriting any entrant that was in that pitbox previously.
func (e EntryList) AddInPitBox(entrant *Entrant, pitBox int) {
pitBoxKey := fmt.Sprintf("CAR_%d", pitBox)
if existingEntrant, ok := e[pitBoxKey]; ok {
logrus.Warnf("Car already present in pitbox: %d! Driver: %s (%s) in %s will be overwritten!", pitBox, existingEntrant.Name, existingEntrant.GUID, existingEntrant.Model)
}
entrant.PitBox = pitBox
e[pitBoxKey] = entrant
}
// Remove an Entrant from the EntryList
func (e EntryList) Delete(entrant *Entrant) {
for k, v := range e {
if v == entrant {
delete(e, k)
return
}
}
}
func (e EntryList) AsSlice() []*Entrant {
var entrants []*Entrant
for _, x := range e {
entrants = append(entrants, x)
}
// note: pitbox sorting here is crucial
sort.Slice(entrants, func(i, j int) bool {
return entrants[i].PitBox < entrants[j].PitBox
})
return entrants
}
func (e EntryList) AlphaSlice() []*Entrant {
var entrants []*Entrant
for _, x := range e {
entrants = append(entrants, x)
}
sort.Slice(entrants, func(i, j int) bool {
return entrants[i].Name < entrants[j].Name
})
return entrants
}
func (e EntryList) PrettyList() []*Entrant {
var entrants []*Entrant
numOpenSlots := 0
for _, x := range e {
if x.GUID == "" {
numOpenSlots++
continue
}
if x.Model == AnyCarModel {
continue
}
entrants = append(entrants, x)
}
sort.Slice(entrants, func(i, j int) bool {
return entrants[i].Name < entrants[j].Name
})
entrants = append(entrants, &Entrant{
Name: fmt.Sprintf("%d open slots", numOpenSlots),
GUID: "OPEN_SLOTS",
})
return entrants
}
func (e EntryList) Entrants() string {
var entrants []string
numOpenSlots := 0
for _, x := range e {
if x.Name == "" {
numOpenSlots++
} else {
entrants = append(entrants, driverName(x.Name))
}
}
if numOpenSlots > 0 {
entrants = append(entrants, fmt.Sprintf("%d open slots", numOpenSlots))
}
return strings.Join(entrants, ", ")
}
func (e EntryList) FindEntrantByInternalUUID(internalUUID uuid.UUID) *Entrant {
for _, entrant := range e {
if entrant.InternalUUID == internalUUID {
return entrant
}
}
return &Entrant{}
}
// CarIDs returns a unique list of car IDs used in the EntryList
func (e EntryList) CarIDs() []string {
cars := make(map[string]bool)
for _, entrant := range e {
cars[entrant.Model] = true
}
var out []string
for car := range cars {
out = append(out, car)
}
return out
}
// returns the greatest ballast set on any entrant
func (e EntryList) FindGreatestBallast() int {
var greatest int
for _, entrant := range e {
if entrant.Ballast > greatest {
greatest = entrant.Ballast
}
}
return greatest
}
func NewEntrant() *Entrant {
return &Entrant{
InternalUUID: uuid.New(),
}
}
type Entrant struct {
InternalUUID uuid.UUID `ini:"-"`
PitBox int `ini:"-"`
Name string `ini:"DRIVERNAME"`
Team string `ini:"TEAM"`
GUID string `ini:"GUID"`
Model string `ini:"MODEL"`
Skin string `ini:"SKIN"`
Ballast int `ini:"BALLAST"`
SpectatorMode int `ini:"SPECTATOR_MODE"`
Restrictor int `ini:"RESTRICTOR"`
FixedSetup string `ini:"FIXED_SETUP"`
TransferTeamPoints bool `ini:"-" json:"-"`
OverwriteAllEvents bool `ini:"-" json:"-"`
IsPlaceHolder bool `ini:"-"`
}
func (e Entrant) ID() string {
if e.GUID != "" {
return e.GUID
}
return e.Name
}
func (e *Entrant) OverwriteProperties(other *Entrant) {
e.FixedSetup = other.FixedSetup
e.Restrictor = other.Restrictor
e.SpectatorMode = other.SpectatorMode
e.Ballast = other.Ballast
e.Skin = other.Skin
e.PitBox = other.PitBox
}
func (e *Entrant) SwapProperties(other *Entrant, entrantRemainedInClass bool) {
if entrantRemainedInClass {
e.Model, other.Model = other.Model, e.Model
e.Skin, other.Skin = other.Skin, e.Skin
e.FixedSetup, other.FixedSetup = other.FixedSetup, e.FixedSetup
e.Restrictor, other.Restrictor = other.Restrictor, e.Restrictor
e.Ballast, other.Ballast = other.Ballast, e.Ballast
}
e.Team, other.Team = other.Team, e.Team
e.InternalUUID, other.InternalUUID = other.InternalUUID, e.InternalUUID
e.PitBox, other.PitBox = other.PitBox, e.PitBox
}
func (e *Entrant) AssignFromResult(result *SessionResult, car *SessionCar) {
e.Name = result.DriverName
e.Team = car.Driver.Team
e.GUID = result.DriverGUID
e.Model = result.CarModel
e.Skin = car.Skin
e.Restrictor = car.Restrictor
e.Ballast = car.BallastKG
}
func (e *Entrant) AsSessionCar() *SessionCar {
return &SessionCar{
BallastKG: e.Ballast,
CarID: e.PitBox,
Driver: SessionDriver{
GUID: e.GUID,
GuidsList: []string{e.GUID},
Name: e.Name,
Team: e.Team,
},
Model: e.Model,
Restrictor: e.Restrictor,
Skin: e.Skin,
}
}
func (e *Entrant) AsSessionResult() *SessionResult {
return &SessionResult{
BallastKG: e.Ballast,
CarID: e.PitBox,
CarModel: e.Model,
DriverGUID: e.GUID,
DriverName: e.Name,
Restrictor: e.Restrictor,
}
}
var guidCleanupRegex = regexp.MustCompile(`[^0-9]+`)
func CleanGUIDs(guids []string) []string {
var cleaned []string
for _, guid := range guids {
g := guidCleanupRegex.ReplaceAllLiteralString(guid, "")
if len(g) > 0 {
cleaned = append(cleaned, g)
}
}
return cleaned
}
// NormaliseEntrantGUID takes a guid which may have driverSwapEntrantSeparators in it,
// sorts all GUIDs in the string and then rejoins them by driverSwapEntrantSeparator
func NormaliseEntrantGUID(guid string) string {
split := CleanGUIDs(strings.Split(guid, driverSwapEntrantSeparator))
sort.Strings(split)
return strings.Join(split, driverSwapEntrantSeparator)
}
// NormaliseEntrantGUIDs takes a list of guids, sorts them and joins them by driverSwapEntrantSeparator
func NormaliseEntrantGUIDs(guids []string) string {
guids = CleanGUIDs(guids)
sort.Strings(guids)
return strings.Join(guids, driverSwapEntrantSeparator)
}