forked from JustaPenguin/assetto-server-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
194 lines (149 loc) · 4.4 KB
/
init.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
package servermanager
import (
"math/rand"
"os"
"os/signal"
"time"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
type ServerID string
var (
serverID ServerID
serverIDMetaKey = "server_id"
)
func initServerID(store Store) error {
err := store.GetMeta(serverIDMetaKey, &serverID)
if err == ErrValueNotSet {
serverID = ServerID(uuid.New().String())
err = store.SetMeta(serverIDMetaKey, serverID)
if err != nil {
return err
}
} else if err != nil {
return err
}
OpenAccount = &Account{
Name: "Free Access",
Groups: map[ServerID]Group{serverID: GroupRead},
LastSeenVersion: BuildVersion,
Theme: ThemeDefault,
}
return nil
}
func InitWithResolver(resolver *Resolver) error {
store := resolver.ResolveStore()
err := store.GetMeta(serverAccountOptionsMetaKey, &accountOptions)
if err != nil && err != ErrValueNotSet {
return err
}
if err := initServerID(store); err != nil {
return err
}
logrus.Infof("Server manager instance identifies as: %s", serverID)
opts, err := store.LoadServerOptions()
if err != nil && err != ErrValueNotSet {
return err
}
UseShortenedDriverNames = opts != nil && opts.UseShortenedDriverNames == 1
UseFallBackSorting = opts != nil && opts.FallBackResultsSorting == 1
process := resolver.resolveServerProcess()
championshipManager := resolver.resolveChampionshipManager()
raceWeekendManager := resolver.resolveRaceWeekendManager()
notificationManager := resolver.resolveNotificationManager()
raceControl := resolver.ResolveRaceControl()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
// ^C, handle it
if process.IsRunning() {
event := process.Event()
opts, err := store.LoadServerOptions()
if err == nil && opts.RestartEventOnServerManagerLaunch == 1 {
// save the event so it can be started again next time server-manager starts
err := store.UpsertLastRaceEvent(event)
if err != nil {
logrus.WithError(err).Error("Could not save last server event")
}
}
if event.IsChampionship() && !event.IsPractice() {
if err := championshipManager.StopActiveEvent(); err != nil {
logrus.WithError(err).Errorf("Error stopping Championship event")
}
} else if event.IsRaceWeekend() && !event.IsPractice() {
if err := raceWeekendManager.StopActiveSession(); err != nil {
logrus.WithError(err).Errorf("Error stopping Race Weekend session")
}
} else {
if err := process.Stop(); err != nil {
logrus.WithError(err).Errorf("Could not stop server")
}
}
}
if err := notificationManager.Stop(); err != nil {
logrus.WithError(err).Errorf("Could not stop notification manager")
}
raceControl.persistTimingData()
os.Exit(0)
}
}()
raceManager := resolver.resolveRaceManager()
go panicCapture(raceManager.LoopRaces)
err = raceManager.InitScheduledRaces()
if err != nil {
return err
}
err = championshipManager.InitScheduledChampionships()
if err != nil {
return err
}
err = raceWeekendManager.WatchForScheduledSessions()
if err != nil {
return err
}
carManager := resolver.resolveCarManager()
go func() {
err = carManager.CreateOrOpenSearchIndex()
if err != nil {
logrus.WithError(err).Error("Could not open search index")
}
}()
if opts.RestartEventOnServerManagerLaunch == 1 {
if lastEvent, err := store.LoadLastRaceEvent(); err == nil && lastEvent != nil {
var err error
switch a := lastEvent.(type) {
case *ActiveChampionship:
if !a.IsPractice() {
err = championshipManager.applyConfigAndStart(a)
} else {
err = raceManager.applyConfigAndStart(a)
}
case *ActiveRaceWeekend:
if !a.IsPractice() {
err = raceWeekendManager.applyConfigAndStart(a)
} else {
err = raceManager.applyConfigAndStart(a)
}
default:
err = raceManager.applyConfigAndStart(a)
}
if err != nil {
logrus.WithError(err).Errorf("Could not start last running event")
}
}
}
if config.Store.ScheduledEventCheckLoop > time.Duration(0) {
logrus.Infof("Experimental Scheduled Event Check loop feature enabled. Checking for new scheduled events every %s", config.Store.ScheduledEventCheckLoop)
go func() {
ticker := time.NewTicker(config.Store.ScheduledEventCheckLoop)
for range ticker.C {
err = raceManager.InitScheduledRaces()
}
}()
}
return nil
}