Skip to content
This repository has been archived by the owner on May 20, 2024. It is now read-only.

Commit

Permalink
(#11) moved Player to internal module
Browse files Browse the repository at this point in the history
  • Loading branch information
hjwylde committed Feb 17, 2016
1 parent bafd85d commit 5379805
Show file tree
Hide file tree
Showing 15 changed files with 50 additions and 86 deletions.
14 changes: 4 additions & 10 deletions app/Werewolf/Commands/End.hs
Expand Up @@ -16,32 +16,26 @@ module Werewolf.Commands.End (
handle,
) where

import Control.Lens
import Control.Monad.Extra
import Control.Monad.IO.Class

import Data.Maybe
import Data.Text (Text)
import qualified Data.Text as T

import Game.Werewolf.Engine
import Game.Werewolf.Engine hiding (doesPlayerExist)
import Game.Werewolf.Game
import Game.Werewolf.Player
import Game.Werewolf.Response

import Werewolf.Messages

handle :: MonadIO m => Text -> m ()
handle callerName = do
unlessM doesGameExist $ exitWith failure
{ messages = [noGameRunningMessage callerName]
}
unlessM doesGameExist $ exitWith failure { messages = [noGameRunningMessage callerName] }

game <- readGame

when (isNothing $ findByName callerName (game ^. players)) $ exitWith failure
{ messages = [playerCannotDoThatMessage callerName]
}
unless (doesPlayerExist callerName game) $
exitWith failure { messages = [playerCannotDoThatMessage callerName] }

deleteGame

Expand Down
6 changes: 4 additions & 2 deletions app/Werewolf/Messages.hs
Expand Up @@ -16,7 +16,8 @@ module Werewolf.Messages (
engineVersionMessage,

-- ** Error messages
noGameRunningMessage, gameAlreadyRunningMessage, roleDoesNotExistMessage, playerCannotDoThatMessage,
noGameRunningMessage, gameAlreadyRunningMessage, roleDoesNotExistMessage,
playerCannotDoThatMessage,
) where

import Data.Text (Text)
Expand All @@ -26,7 +27,8 @@ import Data.Version
import Game.Werewolf.Response

engineVersionMessage :: Text -> Version -> Message
engineVersionMessage to version = privateMessage to $ T.unwords ["Version", T.pack $ showVersion version]
engineVersionMessage to version =
privateMessage to $ T.unwords ["Version", T.pack $ showVersion version]

noGameRunningMessage :: Text -> Message
noGameRunningMessage to = privateMessage to "No game is running."
Expand Down
4 changes: 2 additions & 2 deletions src/Game/Werewolf/Command.hs
Expand Up @@ -39,11 +39,11 @@ import qualified Data.Text as T
import Game.Werewolf.Engine
import Game.Werewolf.Game hiding (getAllowedVoters, getDevourEvent, getPendingVoters,
getPlayerVote, isDefendersTurn, isGameOver,
isScapegoatsTurn, isSeersTurn, isVillagesTurn,
isScapegoatsTurn, isSeersTurn, isVillagesTurn, doesPlayerExist,
isWerewolvesTurn, isWildChildsTurn, isWitchsTurn,
isWolfHoundsTurn, killPlayer, setPlayerRole)
import Game.Werewolf.Messages
import Game.Werewolf.Player hiding (doesPlayerExist)
import Game.Werewolf.Internal.Player
import Game.Werewolf.Response
import Game.Werewolf.Role hiding (name)
import qualified Game.Werewolf.Role as Role
Expand Down
7 changes: 3 additions & 4 deletions src/Game/Werewolf/Engine.hs
Expand Up @@ -68,14 +68,13 @@ import qualified Data.Text as T

import Game.Werewolf.Game hiding (getAllowedVoters, getDevourEvent, getPassers,
getPendingVoters, getPlayerVote, getVoteResult,
isDefendersTurn, isGameOver, isScapegoatsTurn, isSeersTurn,
isDefendersTurn, isGameOver, isScapegoatsTurn, isSeersTurn, doesPlayerExist,
isVillagesTurn, isWerewolvesTurn, isWildChildsTurn,
isWitchsTurn, isWolfHoundsTurn, killPlayer,
setPlayerAllegiance, setPlayerRole)
import qualified Game.Werewolf.Game as Game
import Game.Werewolf.Messages
import Game.Werewolf.Player hiding (doesPlayerExist)
import qualified Game.Werewolf.Player as Player
import Game.Werewolf.Internal.Player
import Game.Werewolf.Response
import Game.Werewolf.Role hiding (name)
import qualified Game.Werewolf.Role as Role
Expand Down Expand Up @@ -365,7 +364,7 @@ createPlayers :: MonadIO m => [Text] -> [Role] -> m [Player]
createPlayers playerNames extraRoles = zipWith newPlayer playerNames <$> randomiseRoles extraRoles (length playerNames)

doesPlayerExist :: MonadState Game m => Text -> m Bool
doesPlayerExist name = uses players $ Player.doesPlayerExist name
doesPlayerExist name = gets $ Game.doesPlayerExist name

isPlayerDefender :: MonadState Game m => Text -> m Bool
isPlayerDefender name = isDefender <$> findPlayerByName_ name
Expand Down
6 changes: 5 additions & 1 deletion src/Game/Werewolf/Game.hs
Expand Up @@ -22,6 +22,7 @@ module Game.Werewolf.Game (

-- ** Queries
isFirstRound,
doesPlayerExist,
getPassers, getPlayerVote, getAllowedVoters, getPendingVoters, getVoteResult,

-- * Stage
Expand All @@ -48,7 +49,7 @@ import qualified Data.Map as Map
import Data.Maybe
import Data.Text (Text)

import Game.Werewolf.Player
import Game.Werewolf.Internal.Player
import Game.Werewolf.Role hiding (name)

import Prelude hiding (round)
Expand Down Expand Up @@ -119,6 +120,9 @@ setPlayerAllegiance name' allegiance' game = game & players %~ map (\player -> i
isFirstRound :: Game -> Bool
isFirstRound game = game ^. round == 0

doesPlayerExist :: Text -> Game -> Bool
doesPlayerExist name = isJust . findByName name . view players

getPassers :: Game -> [Player]
getPassers game = map (`findByName_` players') passes'
where
Expand Down
@@ -1,5 +1,5 @@
{-|
Module : Game.Werewolf.Player
Module : Game.Werewolf.Internal.Player
Description : Simplistic player data structure with functions for searching, filtering and querying
lists of players.
Copyright : (c) Henry J. Wylde, 2016
Expand All @@ -14,7 +14,7 @@ attributes.

{-# LANGUAGE TemplateHaskell #-}

module Game.Werewolf.Player (
module Game.Werewolf.Internal.Player (
-- * Player
Player, name, role, state,

Expand All @@ -31,7 +31,6 @@ module Game.Werewolf.Player (
filterAlive, filterDead,

-- ** Queries
doesPlayerExist,
isAngel, isDefender, isScapegoat, isSeer, isSimpleVillager, isSimpleWerewolf, isVillageIdiot,
isVillagerVillager, isWildChild, isWitch, isWolfHound,
isVillager, isWerewolf,
Expand Down Expand Up @@ -110,10 +109,6 @@ filterAlive = filter isAlive
filterDead :: [Player] -> [Player]
filterDead = filter isDead

-- | @doesPlayerExist name = isJust . findByName name@
doesPlayerExist :: Text -> [Player] -> Bool
doesPlayerExist name = isJust . findByName name

-- | @isAngel player = player ^. role == 'angelRole'@
isAngel :: Player -> Bool
isAngel player = player ^. role == angelRole
Expand Down
2 changes: 1 addition & 1 deletion src/Game/Werewolf/Messages.hs
Expand Up @@ -88,7 +88,7 @@ import Data.Text (Text)
import qualified Data.Text as T

import Game.Werewolf.Game
import Game.Werewolf.Player
import Game.Werewolf.Internal.Player
import Game.Werewolf.Response
import Game.Werewolf.Role hiding (name)
import qualified Game.Werewolf.Role as Role
Expand Down
6 changes: 5 additions & 1 deletion src/Game/Werewolf/Response.hs
Expand Up @@ -81,6 +81,10 @@ failure = Response False []
exitWith :: MonadIO m => Response -> m ()
exitWith response = liftIO $ T.putStrLn (T.decodeUtf8 $ encode response) >> Exit.exitSuccess

-- | A message may be either public or private, indicated by it's @to@ field.
--
-- Each message contains a single text field. This field is permitted to contain special
-- characters such as new lines and tabs.
data Message = Message
{ to :: Maybe Text -- ^ The message recipient: 'Nothing' for a public message,
-- 'Just' for a private message.
Expand All @@ -105,7 +109,7 @@ publicMessage = Message Nothing
privateMessage :: Text -> Text -> Message
privateMessage to = Message (Just to)

-- | @groupmessages tos message@
-- | @groupMessages tos message@
--
-- Creates multiple private messages (1 to each recipient) with the given text.
groupMessages :: [Text] -> Text -> [Message]
Expand Down
3 changes: 1 addition & 2 deletions test/app/Main.hs
Expand Up @@ -15,7 +15,6 @@ module Main (
import Game.Werewolf.Test.Command
import Game.Werewolf.Test.Engine
import Game.Werewolf.Test.Game
import Game.Werewolf.Test.Player

import Test.Tasty

Expand All @@ -24,4 +23,4 @@ main = defaultMain =<< tests

tests :: IO TestTree
tests = return . testGroup "Tests" $ concat
[allCommandTests, allEngineTests, allGameTests, allPlayerTests]
[allCommandTests, allEngineTests, allGameTests]
2 changes: 1 addition & 1 deletion test/src/Game/Werewolf/Test/Arbitrary.hs
Expand Up @@ -50,7 +50,7 @@ import qualified Data.Text as T
import Game.Werewolf.Command
import Game.Werewolf.Engine (checkStage)
import Game.Werewolf.Game
import Game.Werewolf.Player
import Game.Werewolf.Internal.Player
import Game.Werewolf.Role hiding (name)
import Game.Werewolf.Test.Util

Expand Down
38 changes: 19 additions & 19 deletions test/src/Game/Werewolf/Test/Command.hs
Expand Up @@ -24,7 +24,7 @@ import qualified Data.Text as T
import Game.Werewolf.Command
import Game.Werewolf.Engine (checkStage)
import Game.Werewolf.Game
import Game.Werewolf.Player
import Game.Werewolf.Internal.Player
import Game.Werewolf.Role hiding (name)
import Game.Werewolf.Test.Arbitrary
import Game.Werewolf.Test.Util
Expand Down Expand Up @@ -158,7 +158,7 @@ prop_chooseAllegianceCommandErrorsWhenCallerDoesNotExist :: GameAtWolfHoundsTurn
prop_chooseAllegianceCommandErrorsWhenCallerDoesNotExist (GameAtWolfHoundsTurn game) caller allegiance = do
let command = chooseAllegianceCommand (caller ^. name) (T.pack $ show allegiance)

not (doesPlayerExist (caller ^. name) (game ^. players))
not (doesPlayerExist (caller ^. name) game)
==> verbose_runCommandErrors game command

prop_chooseAllegianceCommandErrorsWhenCallerIsDead :: GameAtWolfHoundsTurn -> Allegiance -> Property
Expand Down Expand Up @@ -213,15 +213,15 @@ prop_choosePlayerCommandErrorsWhenCallerDoesNotExist (GameAtWildChildsTurn game)
forAll (arbitraryPlayer game) $ \target -> do
let command = choosePlayerCommand (caller ^. name) (target ^. name)

not (doesPlayerExist (caller ^. name) (game ^. players))
not (doesPlayerExist (caller ^. name) game)
==> verbose_runCommandErrors game command

prop_choosePlayerCommandErrorsWhenTargetDoesNotExist :: GameAtWildChildsTurn -> Player -> Property
prop_choosePlayerCommandErrorsWhenTargetDoesNotExist (GameAtWildChildsTurn game) target = do
let wildChild = findByRole_ wildChildRole (game ^. players)
let command = choosePlayerCommand (wildChild ^. name) (target ^. name)

not (doesPlayerExist (target ^. name) (game ^. players))
not (doesPlayerExist (target ^. name) game)
==> verbose_runCommandErrors game command

prop_choosePlayerCommandErrorsWhenCallerIsDead :: GameAtWildChildsTurn -> Property
Expand Down Expand Up @@ -283,15 +283,15 @@ prop_choosePlayersCommandErrorsWhenCallerDoesNotExist (GameAtScapegoatsTurn game
forAll (NonEmpty <$> sublistOf (filterAlive $ game ^. players)) $ \(NonEmpty targets) -> do
let command = choosePlayersCommand (caller ^. name) (map (view name) targets)

not (doesPlayerExist (caller ^. name) (game ^. players))
not (doesPlayerExist (caller ^. name) game)
==> verbose_runCommandErrors game command

prop_choosePlayersCommandErrorsWhenAnyTargetDoesNotExist :: GameAtScapegoatsTurn -> Player -> Property
prop_choosePlayersCommandErrorsWhenAnyTargetDoesNotExist (GameAtScapegoatsTurn game) target = do
let scapegoat = findByRole_ scapegoatRole (game ^. players)
let command = choosePlayersCommand (scapegoat ^. name) [target ^. name]

not (doesPlayerExist (target ^. name) (game ^. players))
not (doesPlayerExist (target ^. name) game)
==> verbose_runCommandErrors game command

prop_choosePlayersCommandErrorsWhenAnyTargetIsDead :: GameAtScapegoatsTurn -> Property
Expand Down Expand Up @@ -342,7 +342,7 @@ prop_healCommandErrorsWhenGameIsOver (GameAtGameOver game) = do

prop_healCommandErrorsWhenCallerDoesNotExist :: GameWithDevourEvent -> Player -> Property
prop_healCommandErrorsWhenCallerDoesNotExist (GameWithDevourEvent game) caller =
not (doesPlayerExist (caller ^. name) (game ^. players))
not (doesPlayerExist (caller ^. name) game)
==> verbose_runCommandErrors game (healCommand (caller ^. name))

prop_healCommandErrorsWhenCallerIsDead :: GameWithDevourEvent -> Property
Expand Down Expand Up @@ -397,7 +397,7 @@ prop_passCommandErrorsWhenGameIsOver (GameAtGameOver game) =

prop_passCommandErrorsWhenCallerDoesNotExist :: GameAtWitchsTurn -> Player -> Property
prop_passCommandErrorsWhenCallerDoesNotExist (GameAtWitchsTurn game) caller =
not (doesPlayerExist (caller ^. name) (game ^. players))
not (doesPlayerExist (caller ^. name) game)
==> verbose_runCommandErrors game (passCommand (caller ^. name))

prop_passCommandErrorsWhenCallerIsDead :: GameAtWitchsTurn -> Property
Expand Down Expand Up @@ -426,7 +426,7 @@ prop_poisonCommandErrorsWhenGameIsOver (GameAtGameOver game) =

prop_poisonCommandErrorsWhenCallerDoesNotExist :: GameAtWitchsTurn -> Player -> Property
prop_poisonCommandErrorsWhenCallerDoesNotExist (GameAtWitchsTurn game) caller =
not (doesPlayerExist (caller ^. name) (game ^. players))
not (doesPlayerExist (caller ^. name) game)
==> forAll (arbitraryPlayer game) $ \target -> do
let command = poisonCommand (caller ^. name) (target ^. name)

Expand All @@ -437,7 +437,7 @@ prop_poisonCommandErrorsWhenTargetDoesNotExist (GameAtWitchsTurn game) target =
let witch = findByRole_ witchRole (game ^. players)
let command = poisonCommand (witch ^. name) (target ^. name)

not (doesPlayerExist (target ^. name) (game ^. players))
not (doesPlayerExist (target ^. name) game)
==> verbose_runCommandErrors game command

prop_poisonCommandErrorsWhenCallerIsDead :: GameAtWitchsTurn -> Property
Expand Down Expand Up @@ -507,7 +507,7 @@ prop_protectCommandErrorsWhenGameIsOver (GameAtGameOver game) =

prop_protectCommandErrorsWhenCallerDoesNotExist :: GameAtDefendersTurn -> Player -> Property
prop_protectCommandErrorsWhenCallerDoesNotExist (GameAtDefendersTurn game) caller =
not (doesPlayerExist (caller ^. name) (game ^. players))
not (doesPlayerExist (caller ^. name) game)
==> forAll (arbitraryPlayer game) $ \target -> do
let command = protectCommand (caller ^. name) (target ^. name)

Expand All @@ -518,7 +518,7 @@ prop_protectCommandErrorsWhenTargetDoesNotExist (GameAtDefendersTurn game) targe
let defender = findByRole_ defenderRole (game ^. players)
let command = protectCommand (defender ^. name) (target ^. name)

not (doesPlayerExist (target ^. name) (game ^. players))
not (doesPlayerExist (target ^. name) game)
==> verbose_runCommandErrors game command

prop_protectCommandErrorsWhenCallerIsDead :: GameAtDefendersTurn -> Property
Expand Down Expand Up @@ -579,7 +579,7 @@ prop_quitCommandErrorsWhenGameIsOver (GameAtGameOver game) =

prop_quitCommandErrorsWhenCallerDoesNotExist :: Game -> Player -> Property
prop_quitCommandErrorsWhenCallerDoesNotExist game caller =
not (doesPlayerExist (caller ^. name) (game ^. players))
not (doesPlayerExist (caller ^. name) game)
==> verbose_runCommandErrors game (quitCommand $ caller ^. name)

prop_quitCommandErrorsWhenCallerIsDead :: Game -> Property
Expand Down Expand Up @@ -674,7 +674,7 @@ prop_seeCommandErrorsWhenGameIsOver (GameAtGameOver game) =

prop_seeCommandErrorsWhenCallerDoesNotExist :: GameAtSeersTurn -> Player -> Property
prop_seeCommandErrorsWhenCallerDoesNotExist (GameAtSeersTurn game) caller =
not (doesPlayerExist (caller ^. name) (game ^. players))
not (doesPlayerExist (caller ^. name) game)
==> forAll (arbitraryPlayer game) $ \target -> do
let command = seeCommand (caller ^. name) (target ^. name)

Expand All @@ -685,7 +685,7 @@ prop_seeCommandErrorsWhenTargetDoesNotExist (GameAtSeersTurn game) target = do
let seer = findByRole_ seerRole (game ^. players)
let command = seeCommand (seer ^. name) (target ^. name)

not (doesPlayerExist (target ^. name) (game ^. players))
not (doesPlayerExist (target ^. name) game)
==> verbose_runCommandErrors game command

prop_seeCommandErrorsWhenCallerIsDead :: GameAtSeersTurn -> Property
Expand Down Expand Up @@ -732,15 +732,15 @@ prop_voteDevourCommandErrorsWhenGameIsOver (GameAtGameOver game) =

prop_voteDevourCommandErrorsWhenCallerDoesNotExist :: GameAtWerewolvesTurn -> Player -> Property
prop_voteDevourCommandErrorsWhenCallerDoesNotExist (GameAtWerewolvesTurn game) caller =
not (doesPlayerExist (caller ^. name) (game ^. players))
not (doesPlayerExist (caller ^. name) game)
==> forAll (arbitraryPlayer game) $ \target -> do
let command = voteDevourCommand (caller ^. name) (target ^. name)

verbose_runCommandErrors game command

prop_voteDevourCommandErrorsWhenTargetDoesNotExist :: GameAtWerewolvesTurn -> Player -> Property
prop_voteDevourCommandErrorsWhenTargetDoesNotExist (GameAtWerewolvesTurn game) target =
not (doesPlayerExist (target ^. name) (game ^. players))
not (doesPlayerExist (target ^. name) game)
==> forAll (arbitraryWerewolf game) $ \caller -> do
let command = voteDevourCommand (caller ^. name) (target ^. name)

Expand Down Expand Up @@ -804,15 +804,15 @@ prop_voteLynchCommandErrorsWhenGameIsOver (GameAtGameOver game) =

prop_voteLynchCommandErrorsWhenCallerDoesNotExist :: GameAtVillagesTurn -> Player -> Property
prop_voteLynchCommandErrorsWhenCallerDoesNotExist (GameAtVillagesTurn game) caller =
not (doesPlayerExist (caller ^. name) (game ^. players))
not (doesPlayerExist (caller ^. name) game)
==> forAll (arbitraryPlayer game) $ \target -> do
let command = voteLynchCommand (caller ^. name) (target ^. name)

verbose_runCommandErrors game command

prop_voteLynchCommandErrorsWhenTargetDoesNotExist :: GameAtVillagesTurn -> Player -> Property
prop_voteLynchCommandErrorsWhenTargetDoesNotExist (GameAtVillagesTurn game) target =
not (doesPlayerExist (target ^. name) (game ^. players))
not (doesPlayerExist (target ^. name) game)
==> forAll (arbitraryPlayer game) $ \caller -> do
let command = voteLynchCommand (caller ^. name) (target ^. name)

Expand Down
2 changes: 1 addition & 1 deletion test/src/Game/Werewolf/Test/Engine.hs
Expand Up @@ -30,7 +30,7 @@ import Game.Werewolf.Engine hiding (doesPlayerExist, getDevour
isWerewolvesTurn, isWildChildsTurn, isWitchsTurn,
isWolfHoundsTurn, killPlayer)
import Game.Werewolf.Game
import Game.Werewolf.Player
import Game.Werewolf.Internal.Player
import Game.Werewolf.Role hiding (name)
import qualified Game.Werewolf.Role as Role
import Game.Werewolf.Test.Arbitrary
Expand Down
2 changes: 1 addition & 1 deletion test/src/Game/Werewolf/Test/Game.hs
Expand Up @@ -18,7 +18,7 @@ import qualified Data.Map as Map
import Data.Maybe

import Game.Werewolf.Game
import Game.Werewolf.Player
import Game.Werewolf.Internal.Player
import Game.Werewolf.Test.Arbitrary ()

import Prelude hiding (round)
Expand Down

0 comments on commit 5379805

Please sign in to comment.