{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -Wno-orphans #-}

-- |
-- SPDX-License-Identifier: BSD-3-Clause
--
-- High-level status of scenario play.
-- Representation of progress, logic for updating.
module Swarm.Game.Scenario.Status where

import Control.Lens hiding (from, (<.>))
import Data.Aeson (
  genericParseJSON,
  genericToEncoding,
  genericToJSON,
 )
import Data.Function (on)
import Data.String (IsString (..))
import Data.Time (ZonedTime, diffUTCTime, zonedTimeToUTC)
import Data.Yaml as Y
import GHC.Generics (Generic)
import Swarm.Game.Scenario
import Swarm.Game.Scenario.Scoring.Best
import Swarm.Game.Scenario.Scoring.CodeSize
import Swarm.Game.Scenario.Scoring.ConcreteMetrics
import Swarm.Game.Scenario.Scoring.GenericMetrics
import Swarm.Game.Tick (TickNumber)
import Swarm.Game.World.Gen (Seed)
import Swarm.Util.Lens (makeLensesNoSigs)

-- | These launch parameters are used in a number of ways:
--
-- * Serializing the seed/script path for saves
-- * Holding parse status from form fields, including error info
-- * Carrying fully-validated launch parameters.
--
-- Type parameters are utilized to support all of these use cases.
data ParameterizableLaunchParams code f = LaunchParams
  { forall code (f :: * -> *).
ParameterizableLaunchParams code f -> f (Maybe Seed)
seedVal :: f (Maybe Seed)
  , forall code (f :: * -> *).
ParameterizableLaunchParams code f -> f (Maybe code)
initialCode :: f (Maybe code)
  }

type SerializableLaunchParams = ParameterizableLaunchParams FilePath Identity
deriving instance Eq SerializableLaunchParams
deriving instance Ord SerializableLaunchParams
deriving instance Show SerializableLaunchParams
deriving instance Read SerializableLaunchParams
deriving instance Generic SerializableLaunchParams
deriving instance FromJSON SerializableLaunchParams
deriving instance ToJSON SerializableLaunchParams

-- | A 'ScenarioStatus' stores the status of a scenario along with
--   appropriate metadata: 'NotStarted', or 'Played'.
--   The 'Played' status has two sub-states: 'Attempted' or 'Completed'.
data ScenarioStatus
  = NotStarted
  | Played
      SerializableLaunchParams
      ProgressMetric
      BestRecords
  deriving (ScenarioStatus -> ScenarioStatus -> Bool
(ScenarioStatus -> ScenarioStatus -> Bool)
-> (ScenarioStatus -> ScenarioStatus -> Bool) -> Eq ScenarioStatus
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ScenarioStatus -> ScenarioStatus -> Bool
== :: ScenarioStatus -> ScenarioStatus -> Bool
$c/= :: ScenarioStatus -> ScenarioStatus -> Bool
/= :: ScenarioStatus -> ScenarioStatus -> Bool
Eq, Eq ScenarioStatus
Eq ScenarioStatus =>
(ScenarioStatus -> ScenarioStatus -> Ordering)
-> (ScenarioStatus -> ScenarioStatus -> Bool)
-> (ScenarioStatus -> ScenarioStatus -> Bool)
-> (ScenarioStatus -> ScenarioStatus -> Bool)
-> (ScenarioStatus -> ScenarioStatus -> Bool)
-> (ScenarioStatus -> ScenarioStatus -> ScenarioStatus)
-> (ScenarioStatus -> ScenarioStatus -> ScenarioStatus)
-> Ord ScenarioStatus
ScenarioStatus -> ScenarioStatus -> Bool
ScenarioStatus -> ScenarioStatus -> Ordering
ScenarioStatus -> ScenarioStatus -> ScenarioStatus
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: ScenarioStatus -> ScenarioStatus -> Ordering
compare :: ScenarioStatus -> ScenarioStatus -> Ordering
$c< :: ScenarioStatus -> ScenarioStatus -> Bool
< :: ScenarioStatus -> ScenarioStatus -> Bool
$c<= :: ScenarioStatus -> ScenarioStatus -> Bool
<= :: ScenarioStatus -> ScenarioStatus -> Bool
$c> :: ScenarioStatus -> ScenarioStatus -> Bool
> :: ScenarioStatus -> ScenarioStatus -> Bool
$c>= :: ScenarioStatus -> ScenarioStatus -> Bool
>= :: ScenarioStatus -> ScenarioStatus -> Bool
$cmax :: ScenarioStatus -> ScenarioStatus -> ScenarioStatus
max :: ScenarioStatus -> ScenarioStatus -> ScenarioStatus
$cmin :: ScenarioStatus -> ScenarioStatus -> ScenarioStatus
min :: ScenarioStatus -> ScenarioStatus -> ScenarioStatus
Ord, Seed -> ScenarioStatus -> ShowS
[ScenarioStatus] -> ShowS
ScenarioStatus -> FilePath
(Seed -> ScenarioStatus -> ShowS)
-> (ScenarioStatus -> FilePath)
-> ([ScenarioStatus] -> ShowS)
-> Show ScenarioStatus
forall a.
(Seed -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Seed -> ScenarioStatus -> ShowS
showsPrec :: Seed -> ScenarioStatus -> ShowS
$cshow :: ScenarioStatus -> FilePath
show :: ScenarioStatus -> FilePath
$cshowList :: [ScenarioStatus] -> ShowS
showList :: [ScenarioStatus] -> ShowS
Show, ReadPrec [ScenarioStatus]
ReadPrec ScenarioStatus
Seed -> ReadS ScenarioStatus
ReadS [ScenarioStatus]
(Seed -> ReadS ScenarioStatus)
-> ReadS [ScenarioStatus]
-> ReadPrec ScenarioStatus
-> ReadPrec [ScenarioStatus]
-> Read ScenarioStatus
forall a.
(Seed -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Seed -> ReadS ScenarioStatus
readsPrec :: Seed -> ReadS ScenarioStatus
$creadList :: ReadS [ScenarioStatus]
readList :: ReadS [ScenarioStatus]
$creadPrec :: ReadPrec ScenarioStatus
readPrec :: ReadPrec ScenarioStatus
$creadListPrec :: ReadPrec [ScenarioStatus]
readListPrec :: ReadPrec [ScenarioStatus]
Read, (forall x. ScenarioStatus -> Rep ScenarioStatus x)
-> (forall x. Rep ScenarioStatus x -> ScenarioStatus)
-> Generic ScenarioStatus
forall x. Rep ScenarioStatus x -> ScenarioStatus
forall x. ScenarioStatus -> Rep ScenarioStatus x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ScenarioStatus -> Rep ScenarioStatus x
from :: forall x. ScenarioStatus -> Rep ScenarioStatus x
$cto :: forall x. Rep ScenarioStatus x -> ScenarioStatus
to :: forall x. Rep ScenarioStatus x -> ScenarioStatus
Generic)

instance FromJSON ScenarioStatus where
  parseJSON :: Value -> Parser ScenarioStatus
parseJSON = Options -> Value -> Parser ScenarioStatus
forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON Options
scenarioOptions

instance ToJSON ScenarioStatus where
  toEncoding :: ScenarioStatus -> Encoding
toEncoding = Options -> ScenarioStatus -> Encoding
forall a.
(Generic a, GToJSON' Encoding Zero (Rep a)) =>
Options -> a -> Encoding
genericToEncoding Options
scenarioOptions
  toJSON :: ScenarioStatus -> Value
toJSON = Options -> ScenarioStatus -> Value
forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON Options
scenarioOptions

emptyLaunchParams :: Applicative f => ParameterizableLaunchParams a f
emptyLaunchParams :: forall (f :: * -> *) a.
Applicative f =>
ParameterizableLaunchParams a f
emptyLaunchParams = f (Maybe Seed) -> f (Maybe a) -> ParameterizableLaunchParams a f
forall code (f :: * -> *).
f (Maybe Seed)
-> f (Maybe code) -> ParameterizableLaunchParams code f
LaunchParams (Maybe Seed -> f (Maybe Seed)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Seed
forall a. Maybe a
Nothing) (Maybe a -> f (Maybe a)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
forall a. Maybe a
Nothing)

getLaunchParams :: ScenarioStatus -> SerializableLaunchParams
getLaunchParams :: ScenarioStatus -> SerializableLaunchParams
getLaunchParams = \case
  ScenarioStatus
NotStarted -> SerializableLaunchParams
forall (f :: * -> *) a.
Applicative f =>
ParameterizableLaunchParams a f
emptyLaunchParams
  Played SerializableLaunchParams
x ProgressMetric
_ BestRecords
_ -> SerializableLaunchParams
x

-- | The normalized path to a scenario, amenable to lookup
newtype ScenarioPath = ScenarioPath
  { ScenarioPath -> FilePath
getScenarioPath :: FilePath
  }
  deriving (ScenarioPath -> ScenarioPath -> Bool
(ScenarioPath -> ScenarioPath -> Bool)
-> (ScenarioPath -> ScenarioPath -> Bool) -> Eq ScenarioPath
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ScenarioPath -> ScenarioPath -> Bool
== :: ScenarioPath -> ScenarioPath -> Bool
$c/= :: ScenarioPath -> ScenarioPath -> Bool
/= :: ScenarioPath -> ScenarioPath -> Bool
Eq, Seed -> ScenarioPath -> ShowS
[ScenarioPath] -> ShowS
ScenarioPath -> FilePath
(Seed -> ScenarioPath -> ShowS)
-> (ScenarioPath -> FilePath)
-> ([ScenarioPath] -> ShowS)
-> Show ScenarioPath
forall a.
(Seed -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Seed -> ScenarioPath -> ShowS
showsPrec :: Seed -> ScenarioPath -> ShowS
$cshow :: ScenarioPath -> FilePath
show :: ScenarioPath -> FilePath
$cshowList :: [ScenarioPath] -> ShowS
showList :: [ScenarioPath] -> ShowS
Show)

instance IsString ScenarioPath where
  fromString :: FilePath -> ScenarioPath
fromString = FilePath -> ScenarioPath
ScenarioPath

data ScenarioWith a = ScenarioWith
  { forall a. ScenarioWith a -> Scenario
_getScenario :: Scenario
  , forall a. ScenarioWith a -> a
_getScenarioInfo :: a
  }
  deriving ((forall x. ScenarioWith a -> Rep (ScenarioWith a) x)
-> (forall x. Rep (ScenarioWith a) x -> ScenarioWith a)
-> Generic (ScenarioWith a)
forall x. Rep (ScenarioWith a) x -> ScenarioWith a
forall x. ScenarioWith a -> Rep (ScenarioWith a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (ScenarioWith a) x -> ScenarioWith a
forall a x. ScenarioWith a -> Rep (ScenarioWith a) x
$cfrom :: forall a x. ScenarioWith a -> Rep (ScenarioWith a) x
from :: forall x. ScenarioWith a -> Rep (ScenarioWith a) x
$cto :: forall a x. Rep (ScenarioWith a) x -> ScenarioWith a
to :: forall x. Rep (ScenarioWith a) x -> ScenarioWith a
Generic, (forall a b. (a -> b) -> ScenarioWith a -> ScenarioWith b)
-> (forall a b. a -> ScenarioWith b -> ScenarioWith a)
-> Functor ScenarioWith
forall a b. a -> ScenarioWith b -> ScenarioWith a
forall a b. (a -> b) -> ScenarioWith a -> ScenarioWith b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> ScenarioWith a -> ScenarioWith b
fmap :: forall a b. (a -> b) -> ScenarioWith a -> ScenarioWith b
$c<$ :: forall a b. a -> ScenarioWith b -> ScenarioWith a
<$ :: forall a b. a -> ScenarioWith b -> ScenarioWith a
Functor)

makeLenses ''ScenarioWith

-- | A 'ScenarioInfo' record stores metadata about a scenario: its
-- canonical path and status.
-- By way of the 'ScenarioStatus' record, it stores the
-- most recent status and best-ever status.
data ScenarioInfoT a = ScenarioInfo
  { forall a. ScenarioInfoT a -> a
_scenarioPath :: a
  , forall a. ScenarioInfoT a -> ScenarioStatus
_scenarioStatus :: ScenarioStatus
  }
  deriving (ScenarioInfoT a -> ScenarioInfoT a -> Bool
(ScenarioInfoT a -> ScenarioInfoT a -> Bool)
-> (ScenarioInfoT a -> ScenarioInfoT a -> Bool)
-> Eq (ScenarioInfoT a)
forall a. Eq a => ScenarioInfoT a -> ScenarioInfoT a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => ScenarioInfoT a -> ScenarioInfoT a -> Bool
== :: ScenarioInfoT a -> ScenarioInfoT a -> Bool
$c/= :: forall a. Eq a => ScenarioInfoT a -> ScenarioInfoT a -> Bool
/= :: ScenarioInfoT a -> ScenarioInfoT a -> Bool
Eq, Eq (ScenarioInfoT a)
Eq (ScenarioInfoT a) =>
(ScenarioInfoT a -> ScenarioInfoT a -> Ordering)
-> (ScenarioInfoT a -> ScenarioInfoT a -> Bool)
-> (ScenarioInfoT a -> ScenarioInfoT a -> Bool)
-> (ScenarioInfoT a -> ScenarioInfoT a -> Bool)
-> (ScenarioInfoT a -> ScenarioInfoT a -> Bool)
-> (ScenarioInfoT a -> ScenarioInfoT a -> ScenarioInfoT a)
-> (ScenarioInfoT a -> ScenarioInfoT a -> ScenarioInfoT a)
-> Ord (ScenarioInfoT a)
ScenarioInfoT a -> ScenarioInfoT a -> Bool
ScenarioInfoT a -> ScenarioInfoT a -> Ordering
ScenarioInfoT a -> ScenarioInfoT a -> ScenarioInfoT a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (ScenarioInfoT a)
forall a. Ord a => ScenarioInfoT a -> ScenarioInfoT a -> Bool
forall a. Ord a => ScenarioInfoT a -> ScenarioInfoT a -> Ordering
forall a.
Ord a =>
ScenarioInfoT a -> ScenarioInfoT a -> ScenarioInfoT a
$ccompare :: forall a. Ord a => ScenarioInfoT a -> ScenarioInfoT a -> Ordering
compare :: ScenarioInfoT a -> ScenarioInfoT a -> Ordering
$c< :: forall a. Ord a => ScenarioInfoT a -> ScenarioInfoT a -> Bool
< :: ScenarioInfoT a -> ScenarioInfoT a -> Bool
$c<= :: forall a. Ord a => ScenarioInfoT a -> ScenarioInfoT a -> Bool
<= :: ScenarioInfoT a -> ScenarioInfoT a -> Bool
$c> :: forall a. Ord a => ScenarioInfoT a -> ScenarioInfoT a -> Bool
> :: ScenarioInfoT a -> ScenarioInfoT a -> Bool
$c>= :: forall a. Ord a => ScenarioInfoT a -> ScenarioInfoT a -> Bool
>= :: ScenarioInfoT a -> ScenarioInfoT a -> Bool
$cmax :: forall a.
Ord a =>
ScenarioInfoT a -> ScenarioInfoT a -> ScenarioInfoT a
max :: ScenarioInfoT a -> ScenarioInfoT a -> ScenarioInfoT a
$cmin :: forall a.
Ord a =>
ScenarioInfoT a -> ScenarioInfoT a -> ScenarioInfoT a
min :: ScenarioInfoT a -> ScenarioInfoT a -> ScenarioInfoT a
Ord, Seed -> ScenarioInfoT a -> ShowS
[ScenarioInfoT a] -> ShowS
ScenarioInfoT a -> FilePath
(Seed -> ScenarioInfoT a -> ShowS)
-> (ScenarioInfoT a -> FilePath)
-> ([ScenarioInfoT a] -> ShowS)
-> Show (ScenarioInfoT a)
forall a. Show a => Seed -> ScenarioInfoT a -> ShowS
forall a. Show a => [ScenarioInfoT a] -> ShowS
forall a. Show a => ScenarioInfoT a -> FilePath
forall a.
(Seed -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Seed -> ScenarioInfoT a -> ShowS
showsPrec :: Seed -> ScenarioInfoT a -> ShowS
$cshow :: forall a. Show a => ScenarioInfoT a -> FilePath
show :: ScenarioInfoT a -> FilePath
$cshowList :: forall a. Show a => [ScenarioInfoT a] -> ShowS
showList :: [ScenarioInfoT a] -> ShowS
Show, ReadPrec [ScenarioInfoT a]
ReadPrec (ScenarioInfoT a)
Seed -> ReadS (ScenarioInfoT a)
ReadS [ScenarioInfoT a]
(Seed -> ReadS (ScenarioInfoT a))
-> ReadS [ScenarioInfoT a]
-> ReadPrec (ScenarioInfoT a)
-> ReadPrec [ScenarioInfoT a]
-> Read (ScenarioInfoT a)
forall a. Read a => ReadPrec [ScenarioInfoT a]
forall a. Read a => ReadPrec (ScenarioInfoT a)
forall a. Read a => Seed -> ReadS (ScenarioInfoT a)
forall a. Read a => ReadS [ScenarioInfoT a]
forall a.
(Seed -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall a. Read a => Seed -> ReadS (ScenarioInfoT a)
readsPrec :: Seed -> ReadS (ScenarioInfoT a)
$creadList :: forall a. Read a => ReadS [ScenarioInfoT a]
readList :: ReadS [ScenarioInfoT a]
$creadPrec :: forall a. Read a => ReadPrec (ScenarioInfoT a)
readPrec :: ReadPrec (ScenarioInfoT a)
$creadListPrec :: forall a. Read a => ReadPrec [ScenarioInfoT a]
readListPrec :: ReadPrec [ScenarioInfoT a]
Read, (forall x. ScenarioInfoT a -> Rep (ScenarioInfoT a) x)
-> (forall x. Rep (ScenarioInfoT a) x -> ScenarioInfoT a)
-> Generic (ScenarioInfoT a)
forall x. Rep (ScenarioInfoT a) x -> ScenarioInfoT a
forall x. ScenarioInfoT a -> Rep (ScenarioInfoT a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (ScenarioInfoT a) x -> ScenarioInfoT a
forall a x. ScenarioInfoT a -> Rep (ScenarioInfoT a) x
$cfrom :: forall a x. ScenarioInfoT a -> Rep (ScenarioInfoT a) x
from :: forall x. ScenarioInfoT a -> Rep (ScenarioInfoT a) x
$cto :: forall a x. Rep (ScenarioInfoT a) x -> ScenarioInfoT a
to :: forall x. Rep (ScenarioInfoT a) x -> ScenarioInfoT a
Generic, (forall a b. (a -> b) -> ScenarioInfoT a -> ScenarioInfoT b)
-> (forall a b. a -> ScenarioInfoT b -> ScenarioInfoT a)
-> Functor ScenarioInfoT
forall a b. a -> ScenarioInfoT b -> ScenarioInfoT a
forall a b. (a -> b) -> ScenarioInfoT a -> ScenarioInfoT b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> ScenarioInfoT a -> ScenarioInfoT b
fmap :: forall a b. (a -> b) -> ScenarioInfoT a -> ScenarioInfoT b
$c<$ :: forall a b. a -> ScenarioInfoT b -> ScenarioInfoT a
<$ :: forall a b. a -> ScenarioInfoT b -> ScenarioInfoT a
Functor)

type ScenarioInfo = ScenarioInfoT FilePath

instance (FromJSON a) => FromJSON (ScenarioInfoT a) where
  parseJSON :: Value -> Parser (ScenarioInfoT a)
parseJSON = Options -> Value -> Parser (ScenarioInfoT a)
forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON Options
scenarioOptions

instance (ToJSON a) => ToJSON (ScenarioInfoT a) where
  toEncoding :: ScenarioInfoT a -> Encoding
toEncoding = Options -> ScenarioInfoT a -> Encoding
forall a.
(Generic a, GToJSON' Encoding Zero (Rep a)) =>
Options -> a -> Encoding
genericToEncoding Options
scenarioOptions
  toJSON :: ScenarioInfoT a -> Value
toJSON = Options -> ScenarioInfoT a -> Value
forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON Options
scenarioOptions

makeLensesNoSigs ''ScenarioInfoT

-- | The path of the scenario, relative to @data/scenarios@.
scenarioPath :: Lens' ScenarioInfo FilePath

-- | The status of the scenario.
scenarioStatus :: Lens' ScenarioInfo ScenarioStatus

-- | Check if a given scenario has been completed.
scenarioIsCompleted :: ScenarioInfo -> Bool
scenarioIsCompleted :: ScenarioInfo -> Bool
scenarioIsCompleted (ScenarioInfo FilePath
_ (Played SerializableLaunchParams
_ ProgressMetric
_ BestRecords
best)) =
  BestRecords
best BestRecords -> Getting Progress BestRecords Progress -> Progress
forall s a. s -> Getting a s a -> a
^. (ProgressMetric -> Const Progress ProgressMetric)
-> BestRecords -> Const Progress BestRecords
Lens' BestRecords ProgressMetric
scenarioBestByTime ((ProgressMetric -> Const Progress ProgressMetric)
 -> BestRecords -> Const Progress BestRecords)
-> ((Progress -> Const Progress Progress)
    -> ProgressMetric -> Const Progress ProgressMetric)
-> Getting Progress BestRecords Progress
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Progress -> Const Progress Progress)
-> ProgressMetric -> Const Progress ProgressMetric
forall a (f :: * -> *).
Functor f =>
(Progress -> f Progress) -> Metric a -> f (Metric a)
metricProgress Progress -> Progress -> Bool
forall a. Eq a => a -> a -> Bool
== Progress
Completed
scenarioIsCompleted ScenarioInfo
_ = Bool
False

-- | Update the current 'ScenarioInfo' record when quitting a game.
--
-- Note that when comparing \"best\" times, shorter is not always better!
-- As long as the scenario is not completed (e.g. some do not have win condition)
-- we consider having fun /longer/ to be better.
updateScenarioInfoOnFinish ::
  CodeSizeDeterminators ->
  ZonedTime ->
  TickNumber ->
  Bool ->
  ScenarioInfo ->
  ScenarioInfo
updateScenarioInfoOnFinish :: CodeSizeDeterminators
-> ZonedTime -> TickNumber -> Bool -> ScenarioInfo -> ScenarioInfo
updateScenarioInfoOnFinish
  CodeSizeDeterminators
csd
  ZonedTime
z
  TickNumber
ticks
  Bool
completed
  si :: ScenarioInfo
si@(ScenarioInfo FilePath
p ScenarioStatus
prevPlayState) = case ScenarioStatus
prevPlayState of
    Played SerializableLaunchParams
launchParams (Metric Progress
_ (ProgressStats ZonedTime
start AttemptMetrics
_currentPlayMetrics)) BestRecords
prevBestRecords ->
      FilePath -> ScenarioStatus -> ScenarioInfo
forall a. a -> ScenarioStatus -> ScenarioInfoT a
ScenarioInfo FilePath
p (ScenarioStatus -> ScenarioInfo) -> ScenarioStatus -> ScenarioInfo
forall a b. (a -> b) -> a -> b
$
        SerializableLaunchParams
-> ProgressMetric -> BestRecords -> ScenarioStatus
Played SerializableLaunchParams
launchParams ProgressMetric
newPlayMetric (BestRecords -> ScenarioStatus) -> BestRecords -> ScenarioStatus
forall a b. (a -> b) -> a -> b
$
          ProgressMetric -> BestRecords -> BestRecords
updateBest ProgressMetric
newPlayMetric BestRecords
prevBestRecords
     where
      el :: NominalDiffTime
el = (UTCTime -> UTCTime -> NominalDiffTime
diffUTCTime (UTCTime -> UTCTime -> NominalDiffTime)
-> (ZonedTime -> UTCTime)
-> ZonedTime
-> ZonedTime
-> NominalDiffTime
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` ZonedTime -> UTCTime
zonedTimeToUTC) ZonedTime
z ZonedTime
start
      cs :: Maybe ScenarioCodeMetrics
cs = CodeSizeDeterminators -> Maybe ScenarioCodeMetrics
codeSizeFromDeterminator CodeSizeDeterminators
csd
      newCompletionFlag :: Progress
newCompletionFlag = if Bool
completed then Progress
Completed else Progress
Attempted
      newPlayMetric :: ProgressMetric
newPlayMetric =
        Progress -> ProgressStats -> ProgressMetric
forall a. Progress -> a -> Metric a
Metric Progress
newCompletionFlag (ProgressStats -> ProgressMetric)
-> ProgressStats -> ProgressMetric
forall a b. (a -> b) -> a -> b
$
          ZonedTime -> AttemptMetrics -> ProgressStats
ProgressStats ZonedTime
start (AttemptMetrics -> ProgressStats)
-> AttemptMetrics -> ProgressStats
forall a b. (a -> b) -> a -> b
$
            DurationMetrics -> Maybe ScenarioCodeMetrics -> AttemptMetrics
AttemptMetrics (NominalDiffTime -> TickNumber -> DurationMetrics
DurationMetrics NominalDiffTime
el TickNumber
ticks) Maybe ScenarioCodeMetrics
cs
    ScenarioStatus
_ -> ScenarioInfo
si