Skip to content

Commit

Permalink
WIP: Record best code size
Browse files Browse the repository at this point in the history
  • Loading branch information
kostmo committed Feb 3, 2023
1 parent 6f1a8c1 commit d8c65b7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
20 changes: 18 additions & 2 deletions src/Swarm/Game/ScenarioInfo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,29 @@ data ScenarioStatus
}
deriving (Eq, Ord, Show, Read, Generic)

-- TODO Define a semigroup instance that encodes the
-- "best" precedence logic, factoring in the completion state.

instance FromJSON ScenarioStatus where
parseJSON = genericParseJSON scenarioOptions

instance ToJSON ScenarioStatus where
toEncoding = genericToEncoding scenarioOptions
toJSON = genericToJSON scenarioOptions

data ScenarioCodeMetrics = ScenarioCodeMetrics {
astSize :: Int
, sourceTextLength :: Int
}

-- | A @ScenarioInfo@ record stores metadata about a scenario: its
-- canonical path, most recent status, and best-ever status.
data ScenarioInfo = ScenarioInfo
{ _scenarioPath :: FilePath
, _scenarioStatus :: ScenarioStatus
, _scenarioBestTime :: ScenarioStatus
, _scenarioBestTicks :: ScenarioStatus
, _scenarioBestCodeSize :: Maybe ScenarioCodeMetrics
}
deriving (Eq, Ord, Show, Read, Generic)

Expand Down Expand Up @@ -158,11 +167,18 @@ scenarioBestTicks :: Lens' ScenarioInfo ScenarioStatus
-- 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.
updateScenarioInfoOnQuit :: ZonedTime -> Integer -> Bool -> ScenarioInfo -> ScenarioInfo
updateScenarioInfoOnQuit z ticks completed (ScenarioInfo p s bTime bTicks) = case s of
updateScenarioInfoOnQuit
:: Bool
-> ZonedTime
-> Integer
-> Bool
-> ScenarioInfo
-> ScenarioInfo
updateScenarioInfoOnQuit usedRepl z ticks completed (ScenarioInfo p s bTime bTicks) = case s of
InProgress start _ _ ->
let el = (diffUTCTime `on` zonedTimeToUTC) z start
cur = (if completed then Complete else InProgress) start el ticks
-- TODO Offload this logic to a Semigroup instance of ScenarioStatus
best f b = case b of
Complete {} | not completed || f b <= f cur -> b -- keep faster completed
InProgress {} | not completed && f b > f cur -> b -- keep longer progress (fun!)
Expand Down
5 changes: 4 additions & 1 deletion src/Swarm/TUI/Controller.hs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,10 @@ saveScenarioInfoOnQuit = do
ts <- use $ gameState . ticks
let currentScenarioInfo :: Traversal' AppState ScenarioInfo
currentScenarioInfo = gameState . scenarios . scenarioItemByPath p . _SISingle . _2
currentScenarioInfo %= updateScenarioInfoOnQuit t ts won

replHist <- use $ uiState . uiREPL . replHistory
liftIO $ writeFile "flarg.txt" $ show replHist
currentScenarioInfo %= updateScenarioInfoOnQuit (hasExecutedREPL replHist) t ts won
status <- preuse currentScenarioInfo
case status of
Nothing -> return ()
Expand Down
6 changes: 5 additions & 1 deletion src/Swarm/TUI/Model/Repl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module Swarm.TUI.Model.Repl (
replLength,
replSeq,
newREPLHistory,
hasExecutedREPL,
addREPLItem,
restartREPLHistory,
getLatestREPLHistoryItems,
Expand Down Expand Up @@ -95,7 +96,7 @@ data REPLHistory = REPLHistory
{ _replSeq :: Seq REPLHistItem
, _replIndex :: Int
, _replStart :: Int
}
} deriving (Show)

makeLensesWith (lensRules & generateSignatures .~ False) ''REPLHistory

Expand All @@ -121,6 +122,9 @@ newREPLHistory xs =
, _replIndex = length s
}

hasExecutedREPL :: REPLHistory -> Bool
hasExecutedREPL h = _replIndex h > _replStart h

-- | Point the start of REPL history after current last line. See 'replStart'.
restartREPLHistory :: REPLHistory -> REPLHistory
restartREPLHistory h = h & replStart .~ replLength h
Expand Down

0 comments on commit d8c65b7

Please sign in to comment.