forked from JustaPenguin/assetto-server-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
penalties.go
296 lines (227 loc) · 7.97 KB
/
penalties.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
package servermanager
import (
"fmt"
"net/http"
"sort"
"strconv"
"strings"
"time"
"github.com/go-chi/chi"
"github.com/sirupsen/logrus"
)
type PenaltiesHandler struct {
*BaseHandler
penaltiesManager *PenaltiesManager
}
func NewPenaltiesHandler(baseHandler *BaseHandler, penaltiesManager *PenaltiesManager) *PenaltiesHandler {
return &PenaltiesHandler{
BaseHandler: baseHandler,
penaltiesManager: penaltiesManager,
}
}
func (ph *PenaltiesHandler) managePenalty(w http.ResponseWriter, r *http.Request) {
jsonFileName := chi.URLParam(r, "sessionFile")
guid := chi.URLParam(r, "driverGUID")
carModel := r.URL.Query().Get("model")
err := r.ParseForm()
if err != nil {
AddErrorFlash(w, r, "Could not parse penalty form")
http.Redirect(w, r, r.Referer(), http.StatusFound)
return
}
add := r.FormValue("action") == "add"
penalty := 0.0
if add {
penaltyString := r.FormValue("time-penalty")
if penaltyString == "" {
penalty = 0
} else {
pen, err := strconv.ParseFloat(penaltyString, 64)
if err != nil {
logrus.WithError(err).Errorf("could not parse penalty time")
AddErrorFlash(w, r, "Could not parse penalty time")
http.Redirect(w, r, r.Referer(), http.StatusFound)
return
}
penalty = pen
}
}
err = ph.penaltiesManager.applyPenalty(jsonFileName, guid, carModel, penalty, add)
if err != nil {
AddErrorFlash(w, r, "Could not add/remove penalty")
http.Redirect(w, r, r.Referer(), http.StatusFound)
return
}
if !add {
AddFlash(w, r, "Penalty Removed!")
} else {
AddFlash(w, r, "Penalty Added!")
}
http.Redirect(w, r, r.Referer(), http.StatusFound)
}
type PenaltiesManager struct {
store Store
}
func NewPenaltiesManager(store Store) *PenaltiesManager {
return &PenaltiesManager{
store: store,
}
}
func (pm *PenaltiesManager) applyPenalty(jsonFileName, guid, carModel string, penalty float64, add bool) error {
var results *SessionResults
var fullFileName string
if !strings.HasSuffix(jsonFileName, ".json") {
fullFileName = jsonFileName + ".json"
} else {
fullFileName = jsonFileName
jsonFileName = strings.TrimSuffix(jsonFileName, ".json")
}
results, err := LoadResult(fullFileName)
if err != nil {
logrus.WithError(err).Errorf("could not load session result file")
return err
}
for _, result := range results.Result {
if result.DriverGUID == guid && result.CarModel == carModel {
if !add {
result.HasPenalty = false
result.Disqualified = false
result.PenaltyTime = 0
result.LapPenalty = 0
logrus.Infof("All penalties cleared from Driver: %s", guid)
} else {
if penalty == 0 {
result.Disqualified = true
result.HasPenalty = false
result.LapPenalty = 0
logrus.Infof("Driver: %s disqualified", guid)
} else {
result.HasPenalty = true
result.Disqualified = false
timeParsed, err := time.ParseDuration(fmt.Sprintf("%.1fs", penalty))
if err != nil {
logrus.WithError(err).Errorf("could not parse penalty time")
return err
}
result.PenaltyTime = timeParsed
// If penalty time is greater than a lap then add a lap penalty and change penalty time by one lap
lastLapTime := results.GetLastLapTime(result.DriverGUID, result.CarModel)
if result.PenaltyTime > lastLapTime {
result.LapPenalty = int(result.PenaltyTime / lastLapTime)
}
logrus.Infof("%s penalty applied to driver: %s", timeParsed.String(), guid)
}
}
break
}
}
switch results.Type {
case SessionTypePractice, SessionTypeQualifying:
sort.Slice(results.Result, func(i, j int) bool {
if (!results.Result[i].Disqualified && !results.Result[j].Disqualified) || (results.Result[i].Disqualified && results.Result[j].Disqualified) {
if results.Result[i].BestLap == 0 {
return false
}
if results.Result[j].BestLap == 0 {
return true
}
// if both drivers are/aren't disqualified
return results.GetTime(results.Result[i].BestLap, results.Result[i].DriverGUID, results.Result[i].CarModel, true) <
results.GetTime(results.Result[j].BestLap, results.Result[j].DriverGUID, results.Result[j].CarModel, true)
}
// driver i is closer to the front than j if they are not disqualified and j is
return results.Result[j].Disqualified
})
case SessionTypeRace:
// sort results.Result, if disqualified go to back, if time penalty sort by laps completed then lap time
sort.Slice(results.Result, func(i, j int) bool {
if !results.Result[i].Disqualified && !results.Result[j].Disqualified {
// if both drivers aren't disqualified
if results.GetNumLaps(results.Result[i].DriverGUID, results.Result[i].CarModel) == results.GetNumLaps(results.Result[j].DriverGUID, results.Result[j].CarModel) {
// if their number of laps are equal, compare lap times
return results.GetTime(results.Result[i].TotalTime, results.Result[i].DriverGUID, results.Result[i].CarModel, true) <
results.GetTime(results.Result[j].TotalTime, results.Result[j].DriverGUID, results.Result[j].CarModel, true)
}
return results.GetNumLaps(results.Result[i].DriverGUID, results.Result[i].CarModel) >= results.GetNumLaps(results.Result[j].DriverGUID, results.Result[j].CarModel)
} else if results.Result[i].Disqualified && results.Result[j].Disqualified {
// if both drivers ARE disqualified, compare their lap times / num laps
if results.GetNumLaps(results.Result[i].DriverGUID, results.Result[i].CarModel) == results.GetNumLaps(results.Result[j].DriverGUID, results.Result[j].CarModel) {
// if their number of laps are equal, compare lap times
return results.GetTime(results.Result[i].TotalTime, results.Result[i].DriverGUID, results.Result[i].CarModel, true) <
results.GetTime(results.Result[j].TotalTime, results.Result[j].DriverGUID, results.Result[j].CarModel, true)
}
return results.GetNumLaps(results.Result[i].DriverGUID, results.Result[i].CarModel) >= results.GetNumLaps(results.Result[j].DriverGUID, results.Result[j].CarModel)
} else {
// driver i is closer to the front than j if they are not disqualified and j is
return results.Result[j].Disqualified
}
})
}
err = saveResults(fullFileName, results)
if err != nil {
logrus.WithError(err).Errorf("could not encode to session result file")
return err
}
if results.ChampionshipID != "" {
championship, err := pm.store.LoadChampionship(results.ChampionshipID)
if err != nil {
logrus.WithError(err).Errorf("Couldn't load championship with ID: %s", results.ChampionshipID)
return err
}
champEvents:
for i, event := range championship.Events {
if event.IsRaceWeekend() {
raceWeekend, err := pm.store.LoadRaceWeekend(event.RaceWeekendID.String())
if err != nil {
return err
}
for key, session := range raceWeekend.Sessions {
if !session.Completed() {
continue
}
if session.Results.SessionFile == jsonFileName {
raceWeekend.Sessions[key].Results = results
break champEvents
}
}
} else {
for key, session := range event.Sessions {
if !session.Completed() {
continue
}
if session.Results.SessionFile == jsonFileName {
championship.Events[i].Sessions[key].Results = results
break champEvents
}
}
}
}
err = pm.store.UpsertChampionship(championship)
if err != nil {
logrus.WithError(err).Errorf("Couldn't save championship with ID: %s", results.ChampionshipID)
return err
}
}
if results.RaceWeekendID != "" {
raceWeekend, err := pm.store.LoadRaceWeekend(results.RaceWeekendID)
if err != nil {
logrus.WithError(err).Errorf("Couldn't load race weekend with id: %s", results.RaceWeekendID)
return err
}
for _, session := range raceWeekend.Sessions {
if !session.Completed() {
continue
}
if session.Results.SessionFile == jsonFileName {
session.Results = results
break
}
}
err = pm.store.UpsertRaceWeekend(raceWeekend)
if err != nil {
logrus.WithError(err).Errorf("Could not update race weekend: %s", raceWeekend.ID.String())
return err
}
}
return nil
}