-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…1809) The only reason the `swarm-integration` test had `swarm-tournament` as a dependency was because of a few validation functions in `Swarm.Web.Tournament.Validate`. However, this seemed to me like a strange place for those validation functions to live, since they had nothing to do with running a tournament in particular, and it meant that running the integration tests required having postgresql installed even though no databases are used in the integration tests at all. This PR moves those validation functions to two more generic places: the first function to extract text from a sequence of log messages now lives in `Swarm.Log`, and the other two functions to check for bad errors and run a `GameState` to completion now live in a new module `Swarm.Game.Step.Validate`.
- Loading branch information
Showing
5 changed files
with
68 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
-- | | ||
-- SPDX-License-Identifier: BSD-3-Clause | ||
-- Description: Validation of gameplay. | ||
-- | ||
-- Facilities for running a game state until completion, checking for | ||
-- any errors encountered. This is not used for normal gameplay but | ||
-- can be used by /e.g./ integration tests. | ||
module Swarm.Game.Step.Validate where | ||
|
||
import Control.Lens (use, (^.)) | ||
import Control.Monad.State (StateT, gets) | ||
import Data.List.NonEmpty qualified as NE | ||
import Data.Text qualified as T | ||
import Swarm.Effect.Time (runTimeIO) | ||
import Swarm.Game.Robot.Concrete (robotLog) | ||
import Swarm.Game.State (GameState, messageInfo, robotInfo, winCondition) | ||
import Swarm.Game.State.Robot (robotMap) | ||
import Swarm.Game.State.Substate (WinCondition (..), WinStatus (..), messageQueue) | ||
import Swarm.Game.Step (gameTick) | ||
import Swarm.Game.Tick (TickNumber) | ||
import Swarm.Log (logToText) | ||
|
||
-- | Keep stepping a 'GameState' until completion, returning the | ||
-- number of ticks taken if successful, or any bad error messages | ||
-- encountered. | ||
playUntilWin :: StateT GameState IO (Either (NE.NonEmpty T.Text) TickNumber) | ||
playUntilWin = do | ||
w <- use winCondition | ||
b <- gets badErrorsInLogs | ||
case NE.nonEmpty b of | ||
Just badErrs -> return $ Left badErrs | ||
Nothing -> case w of | ||
WinConditions (Won _ ts) _ -> return $ Right ts | ||
_ -> runTimeIO gameTick >> playUntilWin | ||
|
||
-- | Extract any bad error messages from robot logs or the global | ||
-- message queue, where "bad" errors are either fatal errors or | ||
-- ones referring to issues in the issue tracker. | ||
badErrorsInLogs :: GameState -> [T.Text] | ||
badErrorsInLogs g = | ||
concatMap | ||
(\r -> filter isBad (logToText $ r ^. robotLog)) | ||
(g ^. robotInfo . robotMap) | ||
<> filter isBad (logToText $ g ^. messageInfo . messageQueue) | ||
where | ||
isBad m = "Fatal error:" `T.isInfixOf` m || "swarm/issues" `T.isInfixOf` m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters