Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring towards tournament server #1801

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ stan.html

.swarm_history

*.db
*.orig
*.aux
*.log
Expand Down
5 changes: 5 additions & 0 deletions scripts/gen/list-sublibraries.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

cd $(git rev-parse --show-toplevel)

grep '^library \w' swarm.cabal | cut -d' ' -f2
11 changes: 6 additions & 5 deletions src/Swarm/TUI/Controller.hs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import Swarm.Game.ResourceLoading (getSwarmHistoryPath)
import Swarm.Game.Robot
import Swarm.Game.Robot.Concrete
import Swarm.Game.Robot.Context
import Swarm.Game.Scenario.Status (updateScenarioInfoOnFinish)
import Swarm.Game.Scenario.Topography.Structure.Recognition (automatons)
import Swarm.Game.Scenario.Topography.Structure.Recognition.Type (originalStructureDefinitions)
import Swarm.Game.ScenarioInfo
Expand Down Expand Up @@ -319,7 +320,7 @@ handleMainEvent ev = do
-- ctrl-q works everywhere
ControlChar 'q' ->
case s ^. gameState . winCondition of
WinConditions (Won _) _ -> toggleModal $ ScenarioEndModal WinModal
WinConditions (Won _ _) _ -> toggleModal $ ScenarioEndModal WinModal
WinConditions (Unwinnable _) _ -> toggleModal $ ScenarioEndModal LoseModal
_ -> toggleModal QuitModal
VtyEvent (V.EvResize _ _) -> invalidateCache
Expand Down Expand Up @@ -547,7 +548,7 @@ saveScenarioInfoOnFinish p = do
t <- liftIO getZonedTime
wc <- use $ gameState . winCondition
let won = case wc of
WinConditions (Won _) _ -> True
WinConditions (Won _ _) _ -> True
_ -> False
ts <- use $ gameState . temporal . ticks

Expand Down Expand Up @@ -640,7 +641,7 @@ quitGame = do
-- player has won the current one.
wc <- use $ gameState . winCondition
case wc of
WinConditions (Won _) _ -> uiState . uiMenu %= advanceMenu
WinConditions (Won _ _) _ -> uiState . uiMenu %= advanceMenu
_ -> return ()

-- Either quit the entire app (if the scenario was chosen directly
Expand Down Expand Up @@ -932,9 +933,9 @@ doGoalUpdates = do
openModal $ ScenarioEndModal LoseModal
saveScenarioInfoOnFinishNocheat
return True
WinConditions (Won False) x -> do
WinConditions (Won False ts) x -> do
-- This clears the "flag" that the Win dialog needs to pop up
gameState . winCondition .= WinConditions (Won True) x
gameState . winCondition .= WinConditions (Won True ts) x
openModal $ ScenarioEndModal WinModal
saveScenarioInfoOnFinishNocheat
-- We do NOT advance the New Game menu to the next item here (we
Expand Down
18 changes: 13 additions & 5 deletions src/swarm-engine/Swarm/Game/Scenario/Scoring/CodeSize.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Swarm.Game.Scenario.Scoring.CodeSize where

import Control.Monad (guard)
import Data.Aeson
import Data.Data (Data)
import GHC.Generics (Generic)
import Swarm.Language.Module
import Swarm.Language.Pipeline
Expand All @@ -24,12 +25,19 @@ data ScenarioCodeMetrics = ScenarioCodeMetrics
}
deriving (Eq, Ord, Show, Read, Generic, ToJSON, FromJSON)

codeSizeFromDeterminator :: CodeSizeDeterminators -> Maybe ScenarioCodeMetrics
codeSizeFromDeterminator (CodeSizeDeterminators maybeInitialCode usedRepl) = do
guard $ not usedRepl
ProcessedTerm (Module s@(Syntax' srcLoc _ _) _) _ _ <- maybeInitialCode
return $ ScenarioCodeMetrics (charCount srcLoc) (measureAstSize s)
codeMetricsFromSyntax ::
Data a =>
Syntax' a ->
ScenarioCodeMetrics
codeMetricsFromSyntax s@(Syntax' srcLoc _ _) =
ScenarioCodeMetrics (charCount srcLoc) (measureAstSize s)
where
charCount :: SrcLoc -> Int
charCount NoLoc = 0
charCount (SrcLoc start end) = end - start

codeSizeFromDeterminator :: CodeSizeDeterminators -> Maybe ScenarioCodeMetrics
codeSizeFromDeterminator (CodeSizeDeterminators maybeInitialCode usedRepl) = do
guard $ not usedRepl
ProcessedTerm (Module s _) _ _ <- maybeInitialCode
return $ codeMetricsFromSyntax s
1 change: 0 additions & 1 deletion src/swarm-engine/Swarm/Game/ScenarioInfo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ module Swarm.Game.ScenarioInfo (
scenarioPath,
scenarioStatus,
CodeSizeDeterminators (CodeSizeDeterminators),
updateScenarioInfoOnFinish,
ScenarioInfoPair,

-- * Scenario collection
Expand Down
3 changes: 3 additions & 0 deletions src/swarm-engine/Swarm/Game/State.hs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import Control.Effect.State (State)
import Control.Effect.Throw
import Control.Lens hiding (Const, use, uses, view, (%=), (+=), (.=), (<+=), (<<.=))
import Control.Monad (forM, join)
import Data.Aeson (ToJSON)
import Data.Digest.Pure.SHA (sha1, showDigest)
import Data.Foldable (toList)
import Data.Foldable.Extra (allM)
Expand All @@ -97,6 +98,7 @@ import Data.Text qualified as T (drop, take)
import Data.Text.IO qualified as TIO
import Data.Text.Lazy qualified as TL
import Data.Text.Lazy.Encoding qualified as TL
import GHC.Generics (Generic)
import Linear (V2 (..))
import Swarm.Game.CESK (emptyStore, finalValue, initMachine)
import Swarm.Game.Entity
Expand Down Expand Up @@ -140,6 +142,7 @@ import System.Clock qualified as Clock
import System.Random (mkStdGen)

newtype Sha1 = Sha1 String
deriving (Show, Eq, Ord, Generic, ToJSON)

data SolutionSource
= ScenarioSuggested
Expand Down
2 changes: 1 addition & 1 deletion src/swarm-engine/Swarm/Game/State/Substate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ data WinStatus
| -- | The player has won.
-- The boolean indicates whether they have
-- already been congratulated.
Won Bool
Won Bool TickNumber
| -- | The player has completed certain "goals" that preclude
-- (via negative prerequisites) the completion of all of the
-- required goals.
Expand Down
7 changes: 4 additions & 3 deletions src/swarm-engine/Swarm/Game/Step.hs
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,9 @@ hypotheticalWinCheck em g ws oc = do
foldM foldFunc initialAccumulator $
reverse incompleteGoals

ts <- use $ temporal . ticks
let newWinState = case ws of
Ongoing -> getNextWinState $ completions finalAccumulator
Ongoing -> getNextWinState ts $ completions finalAccumulator
_ -> ws

winCondition .= WinConditions newWinState (completions finalAccumulator)
Expand All @@ -347,8 +348,8 @@ hypotheticalWinCheck em g ws oc = do

mapM_ handleException $ exceptions finalAccumulator
where
getNextWinState completedObjs
| WC.didWin completedObjs = Won False
getNextWinState ts completedObjs
| WC.didWin completedObjs = Won False ts
| WC.didLose completedObjs = Unwinnable False
| otherwise = Ongoing

Expand Down
11 changes: 10 additions & 1 deletion src/swarm-scenario/Swarm/Game/Scenario.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Swarm.Game.Scenario (
Scenario (..),
ScenarioLandscape (..),
StaticStructureInfo (..),
ScenarioMetadata (ScenarioMetadata),
staticPlacements,
structureDefs,

Expand Down Expand Up @@ -79,6 +80,7 @@ import Data.Set (Set)
import Data.Set qualified as Set
import Data.Text (Text)
import Data.Text qualified as T
import GHC.Generics (Generic)
import Swarm.Game.Entity
import Swarm.Game.Entity.Cosmetic
import Swarm.Game.Entity.Cosmetic.Assignment (worldAttributes)
Expand Down Expand Up @@ -141,7 +143,14 @@ data ScenarioMetadata = ScenarioMetadata
, _scenarioName :: Text
, _scenarioAuthor :: Maybe Text
}
deriving (Show)
deriving (Show, Generic)

instance ToJSON ScenarioMetadata where
toEncoding =
genericToEncoding
defaultOptions
{ fieldLabelModifier = drop 1 -- drops leading underscore
}

makeLensesNoSigs ''ScenarioMetadata

Expand Down
2 changes: 1 addition & 1 deletion test/integration/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ testScenarioSolutions rs ui =
w <- use winCondition
b <- gets badErrorsInLogs
when (null b) $ case w of
WinConditions (Won _) _ -> return ()
WinConditions (Won _ _) _ -> return ()
_ -> runTimeIO gameTick >> playUntilWin

noBadErrors :: GameState -> Assertion
Expand Down
Loading