From d767ec8d737f383bd1f553b0c95e3f59f3aa33de Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Sun, 26 Jan 2025 14:05:04 +0000 Subject: [PATCH] working on the standings --- esdiTelemetry.go | 22 ++++----- iracingStandings.go | 112 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+), 12 deletions(-) create mode 100644 iracingStandings.go diff --git a/esdiTelemetry.go b/esdiTelemetry.go index 7cbd584..6ce893c 100644 --- a/esdiTelemetry.go +++ b/esdiTelemetry.go @@ -261,19 +261,18 @@ func (e *ESDI) telemetry() { lapDelta := string(lapTimeDeltaRepresentation(lapDeltaToBestLap.(float32))) lastLapTime := string(lapTimeRepresentation(lapLastLapTime.(float32))) - // Relative data - // sessionInfoRaw, _ := e.Source.GetSessionInfo() - // sessionInfo := sessionInfoRaw.(goirsdk.SessionInfoYAML) - // drivers := sessionInfo.DriverInfo.Drivers - // - // selfID := sessionInfo.DriverInfo.DriverCarIdx + // Standings + standings := createStandingsTable(i, 9, relativeStandings) + positions := i.Vars.Vars["CarIdxPosition"].Value.([]int32) + p := positions[9] + standings = standings[p - 3: p + 2] - PlayerCarPosition, err := e.Source.GetData("CarIdxPosition") - if err != nil { - log.Fatalf("could not get field `CarIdxPosition`: %v", err) - } + fmt.Printf("\033[?25l\033[2J\033[H") + for p, v := range standings { + fmt.Printf("[%2d] [%2d]%-30s %3d %10f %s\n", + p+1, v.CarIdx, v.DriverName, v.Lap, v.LapPct, lapTimeRepresentation(v.TimeBehind)) + } - selfPos := int32(PlayerCarPosition.(int)) mu.Lock() data = DataPacket{ @@ -289,7 +288,6 @@ func (e *ESDI) telemetry() { FuelLiters: fuelLiters, FuelPct: fuelPct, FuelTotal: totalFuel, - Position: selfPos, } mu.Unlock() } diff --git a/iracingStandings.go b/iracingStandings.go new file mode 100644 index 0000000..2030ced --- /dev/null +++ b/iracingStandings.go @@ -0,0 +1,112 @@ +package main + +import ( + "fmt" + "sort" + "testing" +) + +type StandingsLine struct { + CarIdx int + LapPct float32 + Lap int32 + DriverName string + EstTime float32 + TimeBehind float32 +} + +type standingsFilter func([]StandingsLine, []float32, int) + +func generalStandings(s []StandingsLine, estTime []float32, id int) { + // This will give the delta to the guy ahead + // P1 0:00:000 + // P2 0:01:000 -> 1s from P1 + // P3 0:01:000 -> 1s from P2 + for p := range s { + theirEstimate := estTime[s[p].CarIdx] + var theThing float32 = 0.0 + if p != id { + theThing = estTime[s[p-1].CarIdx] - theirEstimate + } + + s[p].TimeBehind = theThing + } +} + +func relativeStandings(s []StandingsLine, estTime []float32, id int) { + // Get the delta to a given carId + for p := range s { + curCarEstimate := estTime[s[p].CarIdx] + + var delta float32 = 0.0 + delta = abs(estTime[id] - curCarEstimate) + + s[p].TimeBehind = delta + } +} + +func bestLapTime(i GameSource, id int) float32 { + best := i.Vars.Vars["LapBestLapTime"].Value.(float32) + if best > 0 { + return best + } + + return float32(i.SessionInfo.DriverInfo.Drivers[id].CarClassEstLapTime) +} + +// createStandingsTable will create a table with the standings data +// still working on this +// If the filter applied is generalStandings, the carId has to be 0 +// We can do some dynamicProgramming on this thing I guess, I still haven't +// though about it much yet tbh +func createStandingsTable(i GameSource, carId int, filter standingsFilter) []StandingsLine { + driversLapDistPct := i.Vars.Vars["CarIdxLapDistPct"].Value.([]float32) + driversEstTime := i.Vars.Vars["CarIdxEstTime"].Value.([]float32) + driversLap := i.Vars.Vars["CarIdxLap"].Value.([]int32) + drivers := i.SessionInfo.DriverInfo.Drivers + + standings := make([]StandingsLine, len(drivers)) + + for k := range len(drivers) { + if drivers[k].CarIsPaceCar == 1 || drivers[k].IsSpectator == 1 || drivers[k].UserName == "" { + continue + } + + if driversLap[k] == -1 || drivers[k].UserName == "" { + continue + } + + standings[k] = StandingsLine{ + CarIdx: k, + LapPct: driversLapDistPct[k], + DriverName: drivers[k].UserName, + EstTime: driversEstTime[k], + Lap: driversLap[k], + TimeBehind: 0, + } + } + + // This will sort our standings by lap and LapPct + sort.Slice(standings, func(i int, j int) bool { + if standings[i].Lap == int32(standings[j].Lap) { + return standings[i].LapPct > standings[j].LapPct + } + return standings[i].Lap > standings[j].Lap + }) + + filter(standings, driversEstTime, carId) + + return standings +} + +func abs(v float32) float32 { + if v < 0 { + v = v * -1 + } + return v +} + +func TestStandingsFunctionality(t *testing.T) { + for { + } +}