Copyright | (c) Henry J. Wylde, 2016 |
---|---|
License | BSD3 |
Maintainer | public@hjwylde.com |
Safe Haskell | None |
Language | Haskell2010 |
A game is not quite as simple as players! Roughly speaking though, this engine is stateful. The game state only changes when a command is issued (see Game.Werewolf.Command). Thus, this module defines the game data structure and any fields required to keep track of the current state.
It also has a few additional functions for manipulating the game state.
- data Game
- stage :: Lens' Game Stage
- round :: Lens' Game Int
- players :: Lens' Game [Player]
- events :: Lens' Game [Event]
- passes :: Lens' Game [Text]
- allowedVoters :: Lens' Game [Text]
- heal :: Lens' Game Bool
- healUsed :: Lens' Game Bool
- poison :: Lens' Game (Maybe Text)
- poisonUsed :: Lens' Game Bool
- priorProtect :: Lens' Game (Maybe Text)
- protect :: Lens' Game (Maybe Text)
- roleModel :: Lens' Game (Maybe Text)
- scapegoatBlamed :: Lens' Game Bool
- see :: Lens' Game (Maybe Text)
- villageIdiotRevealed :: Lens' Game Bool
- votes :: Lens' Game (Map Text Text)
- data Stage
- allStages :: [Stage]
- stageCycle :: [Stage]
- stageAvailable :: Game -> Stage -> Bool
- data Event
- newGame :: [Player] -> Game
- killPlayer :: Text -> Game -> Game
- setPlayerRole :: Text -> Role -> Game -> Game
- setPlayerAllegiance :: Text -> Allegiance -> Game -> Game
- getAdjacentAlivePlayers :: Text -> Game -> [Player]
- getDevourEvent :: Game -> Maybe Event
- getPassers :: Game -> [Player]
- getPlayerVote :: Text -> Game -> Maybe Text
- getAllowedVoters :: Game -> [Player]
- getPendingVoters :: Game -> [Player]
- getVoteResult :: Game -> [Player]
- isDefendersTurn :: Game -> Bool
- isGameOver :: Game -> Bool
- isScapegoatsTurn :: Game -> Bool
- isSeersTurn :: Game -> Bool
- isSunrise :: Game -> Bool
- isSunset :: Game -> Bool
- isVillagesTurn :: Game -> Bool
- isWerewolvesTurn :: Game -> Bool
- isWildChildsTurn :: Game -> Bool
- isWitchsTurn :: Game -> Bool
- isWolfHoundsTurn :: Game -> Bool
- isFirstRound :: Game -> Bool
- doesPlayerExist :: Text -> Game -> Bool
Game
There are a few key pieces of information that a game always needs to hold. These are:
- the
stage
, - the
round
number, - the
players
and - the
events
.
Any further fields on the game are specific to one or more roles (and their respective turns!).
Some of the additional fields are reset each round (e.g., the Seer's see
) while others are
kept around for the whole game (e.g., the Wild-child's roleModel
).
In order to advance a game's state, a Command
from a user needs to be received. Afterwards
the following steps should be performed:
apply
theCommand
.- run
checkStage
. - run
checkGameOver
.
checkStage
will perform any additional checks and manipulations to the
game state before advancing the game's stage
. It also runs any relevant events
.
checkGameOver
will check to see if any of the win conditions are met and
if so, advance the game's stage
to GameOver
.
allowedVoters :: Lens' Game [Text] Source
Most of these are fairly self-sufficient (the turn stages). Sunrise
and Sunset
are provided
as meaningful breaks between the day and night as, for example, a VillagesTurn
may not always
be available (curse that retched Scapegoat).
Once the game reaches a turn stage, it requires a Command
to help push
it past. Often only certain roles and commands may be performed at any given stage.
stageCycle :: [Stage] Source
An infinite cycle of all stages in the order that they should occur.
stageAvailable :: Game -> Stage -> Bool Source
Checks whether the stage is available for the given game. Most often this just involves checking if there is an applicable role alive, but sometimes it is more complex.
One of the most complex checks here is for the VillagesTurn
. If the Angel is in play, then
the VillagesTurn
is available on the first day rather than only after the first night.
Events occur after a stage is advanced. This is automatically handled in
checkStage
, while an event's specific behaviour is defined by
eventAvailable
and applyEvent
.
For the most part events are used to allow something to happen on a stage different to when it was triggered. E.g., the devour event occurs after the village wakes up rather than when the Werewolves' vote, this gives the Witch a chance to heal the victim.
DevourEvent Text | Werewolves |
NoDevourEvent | Defender, Werewolves and Witch |
PoisonEvent Text | Witch |
newGame :: [Player] -> Game Source
Creates a new game with the given players. No validations are performed here, those are left to
startGame
.
Manipulations
killPlayer :: Text -> Game -> Game Source
Kills the given player! This function should be used carefully as it doesn't clear any state
that the player's role may use. If you're after just removing a player from a game for a test,
try using a quitCommand
instead.
setPlayerRole :: Text -> Role -> Game -> Game Source
Fudges the player's role by completely setting it to something new. This function is useful for roles such as the Angel where they become something else given some trigger.
setPlayerAllegiance :: Text -> Allegiance -> Game -> Game Source
Fudges the player's allegiance. This function is useful for roles such as the Wild-child where they align themselves differently given some trigger.
Searches
getAdjacentAlivePlayers :: Text -> Game -> [Player] Source
getDevourEvent :: Game -> Maybe Event Source
Gets the devour event if it exists.
getPassers :: Game -> [Player] Source
Gets all the passes
in a game (which is names only) and maps them to their player.
getAllowedVoters :: Game -> [Player] Source
Gets all the allowedVoters
in a game (which is names only) and maps them to their player.
getPendingVoters :: Game -> [Player] Source
Gets all alive players that have yet to vote.
getVoteResult :: Game -> [Player] Source
Gets all players that had the highest vote count. This could be 1 or more players depending on whether the votes were in conflict.
Queries
isDefendersTurn :: Game -> Bool Source
isDefendersTurn game = game ^. stage == DefendersTurn
isGameOver :: Game -> Bool Source
isGameOver game = game ^. stage == GameOver
isScapegoatsTurn :: Game -> Bool Source
isScapegoatsTurn game = game ^. stage == ScapegoatsTurn
isSeersTurn :: Game -> Bool Source
isSeersTurn game = game ^. stage == SeersTurn
isVillagesTurn :: Game -> Bool Source
isVillagesTurn game = game ^. stage == VillagesTurn
isWerewolvesTurn :: Game -> Bool Source
isWerewolvesTurn game = game ^. stage == WerewolvesTurn
isWildChildsTurn :: Game -> Bool Source
isWildChildsTurn game = game ^. stage == WildChildsTurn
isWitchsTurn :: Game -> Bool Source
isWitchsTurn game = game ^. stage == WitchsTurn
isWolfHoundsTurn :: Game -> Bool Source
isWolfHoundsTurn game = game ^. stage == WolfHoundsTurn
isFirstRound :: Game -> Bool Source
isFirstRound game = game ^. round == 0
doesPlayerExist :: Text -> Game -> Bool Source
Queries whether the player is in the game.