Skip to content

Commit

Permalink
feat: calculate gap every sector
Browse files Browse the repository at this point in the history
  • Loading branch information
y047aka committed Nov 2, 2024
1 parent eda85c4 commit 28a1952
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 23 deletions.
79 changes: 68 additions & 11 deletions src/Motorsport/Gap.elm
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
module Motorsport.Gap exposing (Gap(..), from, toString)
module Motorsport.Gap exposing (Gap(..), at, toString)

import List.Extra
import Motorsport.Car exposing (Car)
import Motorsport.Clock as Clock
import Motorsport.Duration as Duration exposing (Duration)
import Motorsport.Lap exposing (Lap)
import Motorsport.Lap as Lap exposing (Sector(..))
import Time exposing (Month(..))


type Gap
Expand All @@ -10,19 +14,72 @@ type Gap
| Laps Int


from : Lap -> Lap -> Gap
from a b =
case ( a.lap - b.lap, b.elapsed - a.elapsed ) of
( 0, 0 ) ->
None

( 0, seconds ) ->
Seconds seconds
at : Clock.Model -> Car -> Car -> Gap
at clock carA carB =
case lapDiffAt clock carA carB of
0 ->
secondsAt clock carA carB

( laps, _ ) ->
laps ->
Laps laps


lapDiffAt : Clock.Model -> Car -> Car -> Duration
lapDiffAt clock carA carB =
let
diff =
Maybe.map2 (\lapA lapB -> lapA.lap - lapB.lap) carA.currentLap carB.currentLap

raceClock =
{ elapsed = Clock.getElapsed clock }

currentSector =
carB.currentLap
|> Maybe.map (Lap.currentSector raceClock)

isNotLapped =
case ( Maybe.map2 Lap.sectorToElapsed carA.currentLap currentSector, Maybe.map2 Lap.sectorToElapsed carB.currentLap currentSector ) of
( Just a, Just b ) ->
a >= b

_ ->
False
in
case ( diff, isNotLapped ) of
( Just 0, _ ) ->
0

( Just nonzero, True ) ->
nonzero - 1

( Just nonzero, False ) ->
nonzero

( Nothing, _ ) ->
0


secondsAt : Clock.Model -> Car -> Car -> Gap
secondsAt clock carA carB =
let
raceClock =
{ elapsed = Clock.getElapsed clock }

carB_currentSector =
carB.currentLap
|> Maybe.map (Lap.currentSector raceClock)

targetLap =
List.Extra.getAt ((carB.currentLap |> Maybe.map .lap |> Maybe.withDefault 0) - 1) carA.laps
in
case ( carB_currentSector, targetLap, carB.currentLap ) of
( Just sector, Just targetLap_, Just currentLap ) ->
Seconds (Lap.sectorToElapsed currentLap sector - Lap.sectorToElapsed targetLap_ sector)

_ ->
None


toString : Gap -> String
toString gap =
case gap of
Expand Down
1 change: 1 addition & 0 deletions src/Motorsport/Lap.elm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Motorsport.Lap exposing
, completedLapsAt, findLastLapAt, findCurrentLap
, Sector(..)
, currentSector
, sectorToElapsed
)

{-|
Expand Down
14 changes: 2 additions & 12 deletions src/Motorsport/Leaderboard.elm
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,10 @@ init { clock, cars } =
, team = car.team
, lap = lastLap.lap
, gap =
Maybe.map2 calcGap (List.head cars) (Just car)
Maybe.map2 (Gap.at clock) (List.head cars) (Just car)
|> Maybe.withDefault Gap.None
, interval =
Maybe.map2 calcGap (List.Extra.getAt (index - 1) cars) (Just car)
Maybe.map2 (Gap.at clock) (List.Extra.getAt (index - 1) cars) (Just car)
|> Maybe.withDefault Gap.None
, sector_1 = sector_1
, sector_2 = sector_2
Expand All @@ -401,16 +401,6 @@ init { clock, cars } =
)


calcGap : Car -> Car -> Gap
calcGap carA carB =
case ( carA.lastLap, carB.lastLap ) of
( Just carA_lastLap, Just carB_lastLap ) ->
Gap.from carA_lastLap carB_lastLap

_ ->
Gap.None



-- VIEW

Expand Down

0 comments on commit 28a1952

Please sign in to comment.