-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
race_custom.go
414 lines (321 loc) · 10.5 KB
/
race_custom.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package servermanager
import (
"fmt"
"net/http"
"time"
"4d63.com/tz"
"github.com/go-chi/chi"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
)
type CustomRace struct {
ScheduledEventBase
Name string
HasCustomName, OverridePassword bool
ReplacementPassword string
Created time.Time
Updated time.Time
Deleted time.Time
UUID uuid.UUID
Starred bool
// Deprecated: Replaced by LoopServer
Loop bool
LoopServer map[ServerID]bool
TimeAttackCombinedResultFile string
ForceStopTime int
ForceStopWithDrivers bool
RaceConfig CurrentRaceConfig
EntryList EntryList
ScheduledEvents map[ServerID]*ScheduledEventBase
}
func (cr *CustomRace) GetRaceConfig() CurrentRaceConfig {
return cr.RaceConfig
}
func (cr *CustomRace) GetEntryList() EntryList {
return cr.EntryList
}
func (cr *CustomRace) IsLooping() bool {
if cr.LoopServer == nil {
return false
}
return cr.LoopServer[serverID]
}
func (cr *CustomRace) EventName() string {
if cr.HasCustomName {
return cr.Name
}
return trackSummary(cr.RaceConfig.Track, cr.RaceConfig.TrackLayout)
}
func (cr *CustomRace) OverrideServerPassword() bool {
return cr.OverridePassword
}
func (cr *CustomRace) ReplacementServerPassword() string {
return cr.ReplacementPassword
}
func (cr *CustomRace) IsChampionship() bool {
return false
}
func (cr *CustomRace) IsPractice() bool {
return false
}
func (cr *CustomRace) IsRaceWeekend() bool {
return false
}
func (cr *CustomRace) IsTimeAttack() bool {
return cr.RaceConfig.TimeAttack
}
func (cr *CustomRace) HasSignUpForm() bool {
return false
}
func (cr *CustomRace) GetID() uuid.UUID {
return cr.UUID
}
func (cr *CustomRace) GetScheduledServerID() ServerID {
return cr.ScheduledServerID
}
func (cr *CustomRace) GetRaceSetup() CurrentRaceConfig {
return cr.RaceConfig
}
func (cr *CustomRace) GetSummary() string {
return ""
}
func (cr *CustomRace) GetURL() string {
return ""
}
func (cr *CustomRace) EventDescription() string {
return ""
}
func (cr *CustomRace) ReadOnlyEntryList() EntryList {
return cr.EntryList
}
func (cr *CustomRace) GetForceStopTime() time.Duration {
return time.Minute * time.Duration(cr.ForceStopTime)
}
func (cr *CustomRace) GetForceStopWithDrivers() bool {
return cr.ForceStopWithDrivers
}
type CustomRaceHandler struct {
*BaseHandler
raceManager *RaceManager
championshipManager *ChampionshipManager
raceWeekendManager *RaceWeekendManager
store Store
}
func NewCustomRaceHandler(base *BaseHandler, raceManager *RaceManager, store Store, championshipManager *ChampionshipManager, raceWeekendManager *RaceWeekendManager) *CustomRaceHandler {
return &CustomRaceHandler{
BaseHandler: base,
raceManager: raceManager,
store: store,
championshipManager: championshipManager,
raceWeekendManager: raceWeekendManager,
}
}
type customRaceListTemplateVars struct {
BaseTemplateVars
Recent, Starred, Loop, Scheduled []*CustomRace
}
func (crh *CustomRaceHandler) list(w http.ResponseWriter, r *http.Request) {
recent, starred, looped, scheduled, err := crh.raceManager.ListCustomRaces()
if err != nil {
logrus.WithError(err).Errorf("couldn't list custom races")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
crh.viewRenderer.MustLoadTemplate(w, r, "custom-race/index.html", &customRaceListTemplateVars{
Recent: recent,
Starred: starred,
Loop: looped,
Scheduled: scheduled,
})
}
type eventDetailsTemplateVars struct {
BaseTemplateVars
EventConfig CurrentRaceConfig
EntryList EntryList
EventName string
IsChampionship bool
}
func (crh *CustomRaceHandler) view(w http.ResponseWriter, r *http.Request) {
var (
eventConfig CurrentRaceConfig
eventName string
entryList EntryList
isChampionship bool
)
if customRaceID := r.URL.Query().Get("custom-race"); customRaceID != "" {
race, err := crh.store.FindCustomRaceByID(customRaceID)
if err != nil {
http.NotFound(w, r)
return
}
eventConfig = race.RaceConfig
eventName = race.Name
entryList = race.EntryList
} else if championshipID := r.URL.Query().Get("championshipID"); championshipID != "" {
championship, err := crh.championshipManager.LoadChampionship(championshipID)
if err != nil {
http.NotFound(w, r)
return
}
eventID := r.URL.Query().Get("eventID")
event, _, err := championship.EventByID(eventID)
if err != nil {
http.NotFound(w, r)
return
}
eventName = "Championship Event"
eventConfig, entryList = crh.championshipManager.FinalEventConfigurationFiles(championship, event, false)
isChampionship = true
} else if raceWeekendID := r.URL.Query().Get("raceWeekendID"); raceWeekendID != "" {
raceWeekend, err := crh.raceWeekendManager.LoadRaceWeekend(raceWeekendID)
if err != nil {
http.NotFound(w, r)
return
}
sessionID := r.URL.Query().Get("sessionID")
session, err := raceWeekend.FindSessionByID(sessionID)
if err != nil {
http.NotFound(w, r)
return
}
eventConfig = session.RaceConfig
eventName = fmt.Sprintf("%s (%s)", session.Name(), raceWeekend.Name)
rwe, err := session.GetRaceWeekendEntryList(raceWeekend, nil, "")
if err != nil {
http.NotFound(w, r)
return
}
entryList = rwe.AsEntryList()
isChampionship = raceWeekend.HasLinkedChampionship()
}
if eventConfig.Track == "" {
http.NotFound(w, r)
return
}
crh.viewRenderer.MustLoadPartial(w, r, "custom-race/popups/view.html", &eventDetailsTemplateVars{
EventConfig: eventConfig,
EventName: eventName,
EntryList: entryList,
IsChampionship: isChampionship,
})
}
func (crh *CustomRaceHandler) createOrEdit(w http.ResponseWriter, r *http.Request) {
customRaceData, err := crh.raceManager.BuildRaceOpts(r)
if err != nil {
logrus.WithError(err).Errorf("couldn't build custom race")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
crh.viewRenderer.MustLoadTemplate(w, r, "custom-race/new.html", customRaceData)
}
func (crh *CustomRaceHandler) submit(w http.ResponseWriter, r *http.Request) {
err := crh.raceManager.SetupCustomRace(r)
if err != nil {
logrus.WithError(err).Errorf("couldn't apply custom race")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
action := r.FormValue("action")
if action == "justSave" {
AddFlash(w, r, "Custom race saved!")
http.Redirect(w, r, "/custom", http.StatusFound)
} else if action == "schedule" {
AddFlash(w, r, "Custom race scheduled!")
http.Redirect(w, r, "/custom", http.StatusFound)
} else {
AddFlash(w, r, "Custom race started!")
if config.Server.PerformanceMode {
http.Redirect(w, r, "/", http.StatusFound)
} else {
http.Redirect(w, r, "/live-timing", http.StatusFound)
}
}
}
func (crh *CustomRaceHandler) schedule(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
logrus.WithError(err).Errorf("couldn't parse schedule race form")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
raceID := chi.URLParam(r, "uuid")
dateString := r.FormValue("event-schedule-date")
timeString := r.FormValue("event-schedule-time")
timezone := r.FormValue("event-schedule-timezone")
location, err := tz.LoadLocation(timezone)
if err != nil {
logrus.WithError(err).Errorf("could not find location: %s", location)
location = time.Local
}
// Parse time in correct time zone
date, err := time.ParseInLocation("2006-01-02-15:04", dateString+"-"+timeString, location)
if err != nil {
logrus.WithError(err).Errorf("couldn't parse schedule race date")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
err = crh.raceManager.ScheduleRace(raceID, date, r.FormValue("action"), r.FormValue("event-schedule-recurrence"))
if err != nil {
logrus.WithError(err).Errorf("couldn't schedule race")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
AddFlash(w, r, fmt.Sprintf("We have scheduled the race to begin at %s", date.Format(time.RFC1123)))
http.Redirect(w, r, r.Referer(), http.StatusFound)
}
func (crh *CustomRaceHandler) removeSchedule(w http.ResponseWriter, r *http.Request) {
err := crh.raceManager.ScheduleRace(chi.URLParam(r, "uuid"), time.Time{}, "remove", "")
if err != nil {
logrus.WithError(err).Errorf("couldn't remove scheduled race")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
http.Redirect(w, r, r.Referer(), http.StatusFound)
}
func (crh *CustomRaceHandler) start(w http.ResponseWriter, r *http.Request) {
_, err := crh.raceManager.StartCustomRace(chi.URLParam(r, "uuid"), false)
if err != nil {
logrus.WithError(err).Errorf("couldn't apply custom race")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
AddFlash(w, r, "Custom race started!")
if config.Server.PerformanceMode {
http.Redirect(w, r, "/", http.StatusFound)
} else {
http.Redirect(w, r, "/live-timing", http.StatusFound)
}
}
func (crh *CustomRaceHandler) delete(w http.ResponseWriter, r *http.Request) {
err := crh.raceManager.DeleteCustomRace(chi.URLParam(r, "uuid"))
if err != nil {
logrus.WithError(err).Errorf("couldn't delete custom race")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
AddFlash(w, r, "Custom race deleted!")
http.Redirect(w, r, r.Referer(), http.StatusFound)
}
func (crh *CustomRaceHandler) star(w http.ResponseWriter, r *http.Request) {
err := crh.raceManager.ToggleStarCustomRace(chi.URLParam(r, "uuid"))
if err != nil {
logrus.WithError(err).Errorf("couldn't star custom race")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
AddFlash(w, r, "Custom race starred")
http.Redirect(w, r, r.Referer(), http.StatusFound)
}
func (crh *CustomRaceHandler) loop(w http.ResponseWriter, r *http.Request) {
loopStatus, err := crh.raceManager.ToggleLoopCustomRace(chi.URLParam(r, "uuid"))
if err != nil {
logrus.WithError(err).Errorf("couldn't add custom race to loop")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if loopStatus {
AddFlash(w, r, "Custom race added to loop")
} else {
AddFlash(w, r, "Custom race removed from loop")
}
http.Redirect(w, r, r.Referer(), http.StatusFound)
}