forked from JustaPenguin/assetto-server-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
race_control_driver.go
198 lines (147 loc) · 4.63 KB
/
race_control_driver.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
package servermanager
import (
"context"
"sort"
"sync"
"time"
"github.com/JustaPenguin/assetto-server-manager/pkg/udp"
"github.com/sirupsen/logrus"
)
func NewRaceControlDriver(carInfo udp.SessionCarInfo) *RaceControlDriver {
driver := &RaceControlDriver{
CarInfo: carInfo,
Cars: make(map[string]*RaceControlCarLapInfo),
LastSeen: time.Now(),
}
driver.Cars[carInfo.CarModel] = NewRaceControlCarLapInfo(carInfo.CarModel)
return driver
}
func NewRaceControlCarLapInfo(carModel string) *RaceControlCarLapInfo {
return &RaceControlCarLapInfo{
CarName: prettifyName(carModel, true),
}
}
type RaceControlDriver struct {
CarInfo udp.SessionCarInfo `json:"CarInfo"`
TotalNumLaps int `json:"TotalNumLaps"`
ConnectedTime time.Time `json:"ConnectedTime" ts:"date"`
LoadedTime time.Time `json:"LoadedTime" ts:"date"`
Position int `json:"Position"`
Split string `json:"Split"`
LastSeen time.Time `json:"LastSeen" ts:"date"`
LastPos udp.Vec `json:"LastPos"`
Collisions []Collision `json:"Collisions"`
driverSwapContext context.Context
driverSwapCfn context.CancelFunc
// Cars is a map of CarModel to the information for that car.
Cars map[string]*RaceControlCarLapInfo `json:"Cars"`
mutex sync.Mutex
}
func (rcd *RaceControlDriver) CurrentCar() *RaceControlCarLapInfo {
if car, ok := rcd.Cars[rcd.CarInfo.CarModel]; ok {
return car
}
logrus.Warnf("Could not find current car for driver: %s (current car: %s)", rcd.CarInfo.DriverGUID, rcd.CarInfo.CarModel)
return &RaceControlCarLapInfo{}
}
type RaceControlCarLapInfo struct {
TopSpeedThisLap float64 `json:"TopSpeedThisLap"`
TopSpeedBestLap float64 `json:"TopSpeedBestLap"`
BestLap time.Duration `json:"BestLap"`
NumLaps int `json:"NumLaps"`
LastLap time.Duration `json:"LastLap"`
LastLapCompletedTime time.Time `json:"LastLapCompletedTime" ts:"date"`
TotalLapTime time.Duration `json:"TotalLapTime"`
CarName string `json:"CarName"`
}
type DriverMap struct {
Drivers map[udp.DriverGUID]*RaceControlDriver `json:"Drivers"`
GUIDsInPositionalOrder []udp.DriverGUID `json:"GUIDsInPositionalOrder"`
driverSortLessFunc driverSortLessFunc
driverGroup RaceControlDriverGroup
rwMutex sync.RWMutex
}
type RaceControlDriverGroup int
const (
ConnectedDrivers RaceControlDriverGroup = 0
DisconnectedDrivers RaceControlDriverGroup = 1
)
type driverSortLessFunc func(group RaceControlDriverGroup, driverA, driverB *RaceControlDriver) bool
func NewDriverMap(driverGroup RaceControlDriverGroup, driverSortLessFunc driverSortLessFunc) *DriverMap {
return &DriverMap{
Drivers: make(map[udp.DriverGUID]*RaceControlDriver),
driverSortLessFunc: driverSortLessFunc,
driverGroup: driverGroup,
}
}
func (d *DriverMap) Each(fn func(driverGUID udp.DriverGUID, driver *RaceControlDriver) error) error {
d.rwMutex.RLock()
defer d.rwMutex.RUnlock()
for _, guid := range d.GUIDsInPositionalOrder {
driver, ok := d.Drivers[guid]
if !ok {
continue
}
err := fn(guid, driver)
if err != nil {
return err
}
}
return nil
}
func (d *DriverMap) Get(driverGUID udp.DriverGUID) (*RaceControlDriver, bool) {
d.rwMutex.RLock()
defer d.rwMutex.RUnlock()
driver, ok := d.Drivers[driverGUID]
return driver, ok
}
func (d *DriverMap) Add(driverGUID udp.DriverGUID, driver *RaceControlDriver) {
d.rwMutex.Lock()
defer d.rwMutex.Unlock()
defer d.sort()
d.Drivers[driverGUID] = driver
for _, guid := range d.GUIDsInPositionalOrder {
if guid == driverGUID {
return
}
}
d.GUIDsInPositionalOrder = append(d.GUIDsInPositionalOrder, driverGUID)
}
func (d *DriverMap) sort() {
sort.Slice(d.GUIDsInPositionalOrder, func(i, j int) bool {
driverA, ok := d.Drivers[d.GUIDsInPositionalOrder[i]]
if !ok {
return false
}
driverB, ok := d.Drivers[d.GUIDsInPositionalOrder[j]]
if !ok {
return false
}
return d.driverSortLessFunc(d.driverGroup, driverA, driverB)
})
// correct positions
for pos, guid := range d.GUIDsInPositionalOrder {
driver, ok := d.Drivers[guid]
if !ok {
continue
}
driver.Position = pos + 1
}
}
func (d *DriverMap) Del(driverGUID udp.DriverGUID) {
d.rwMutex.Lock()
defer d.rwMutex.Unlock()
delete(d.Drivers, driverGUID)
for index, guid := range d.GUIDsInPositionalOrder {
if guid == driverGUID {
d.GUIDsInPositionalOrder = append(d.GUIDsInPositionalOrder[:index], d.GUIDsInPositionalOrder[index+1:]...)
break
}
}
d.sort()
}
func (d *DriverMap) Len() int {
d.rwMutex.RLock()
defer d.rwMutex.RUnlock()
return len(d.Drivers)
}