module Game.Cosanostra.Selectors
    ( alive
    , dead
    , other
    , otherAlive
    , selectorCheck
    ) where

import Game.Cosanostra.Effect
import Game.Cosanostra.Expr
import Game.Cosanostra.Types

import Control.Lens
import Data.Maybe

alive :: Selector
alive = Atom Participant `And` Atom Alive

dead :: Selector
dead = Not alive

other :: Selector
other = Atom Participant `And` Not (Atom Self)

otherAlive :: Selector
otherAlive = other `And` alive

-- | Check if a player fulfills a selector.
selectorCheck :: Players -> Player -> Turn -> Phase -> Selector -> Player -> Bool
selectorCheck players source turn phase expr target = exprEval f expr
  where
    f Participant = target `elem` playerKeys players
    f Self = source == target
    f Alive = isNothing (target ^. to (playerEffects players source turn phase)
                                 . to effectsCauseOfDeath)
    f WasLynched = (target ^. to (playerEffects players source turn phase)
                            . to effectsCauseOfDeath) == Just Lynched
