-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
race_event.go
108 lines (82 loc) · 2.08 KB
/
race_event.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
package servermanager
import (
"encoding/json"
"errors"
"fmt"
"time"
)
type RaceEvent interface {
GetRaceConfig() CurrentRaceConfig
GetEntryList() EntryList
IsLooping() bool
IsChampionship() bool
IsRaceWeekend() bool
IsPractice() bool
IsTimeAttack() bool
OverrideServerPassword() bool
ReplacementServerPassword() string
GetForceStopTime() time.Duration
GetForceStopWithDrivers() bool
EventName() string
EventDescription() string
GetURL() string
}
type marshalledRaceEvent struct {
Name string
RaceEvent RaceEvent
}
func nameOf(s interface{}) string {
return fmt.Sprintf("%T", s)
}
func getRaceEvent(name string) (RaceEvent, error) {
var r RaceEvent
switch name {
case nameOf(&QuickRace{}):
r = &QuickRace{}
case nameOf(&CustomRace{}):
r = &CustomRace{}
case nameOf(&ActiveChampionship{}):
r = &ActiveChampionship{}
case nameOf(&ActiveRaceWeekend{}):
r = &ActiveRaceWeekend{}
default:
return nil, fmt.Errorf("servermanager: unknown race event tyre specified: %s", name)
}
return r, nil
}
func marshalRaceEvent(r RaceEvent) ([]byte, error) {
return json.Marshal(marshalledRaceEvent{
Name: nameOf(r),
RaceEvent: r,
})
}
func unmarshalRaceEvent(data []byte) (RaceEvent, error) {
var rawData map[string]json.RawMessage
if err := json.Unmarshal(data, &rawData); err != nil {
return nil, err
}
nameData, ok := rawData["Name"]
if !ok {
return nil, errors.New("servermanager: race event name not found")
}
var name string
if err := json.Unmarshal(nameData, &name); err != nil {
return nil, err
}
raceEventData, ok := rawData["RaceEvent"]
if !ok {
return nil, errors.New("servermanager: race event name not found")
}
raceEvent, err := getRaceEvent(name)
if err != nil {
return nil, err
}
if err := json.Unmarshal(raceEventData, &raceEvent); err != nil {
return nil, err
}
return raceEvent, err
}
func describeRaceEvent(raceEvent RaceEvent) string {
cfg := raceEvent.GetRaceConfig()
return fmt.Sprintf("%d sessions with %d entrants at %s (%s)", len(cfg.Sessions), len(raceEvent.GetEntryList()), cfg.Track, cfg.TrackLayout)
}