Skip to content

Commit 6aa7339

Browse files
committed
Make getCard* pure
1 parent fd7c343 commit 6aa7339

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

Diff for: Startups/Game.hs

+4-20
Original file line numberDiff line numberDiff line change
@@ -111,23 +111,6 @@ resolveExchange pid exch = mconcat . M.elems <$> itraverse resolveExchange' exc
111111
playermap . ix pid . pFunds -= cost
112112
pure (reslist, AddMap (M.singleton neighname cost))
113113

114-
-- | Compute the money that a card gives to a player
115-
getCardFunding :: GameStateOnly m => PlayerId -> Card -> m Funding
116-
getCardFunding pid card = do
117-
stt <- use playermap
118-
-- note how we exploit the fact that ^. behaves like foldMap here
119-
let funding = card ^. cEffect . traverse . _GainFunding . to computeFunding
120-
computeFunding (n, cond) = countConditionTrigger pid cond stt * n
121-
return funding
122-
123-
-- | Compute the victory points a card awards.
124-
getCardVictory :: GameStateOnly m => PlayerId -> Card -> m [(VictoryType, VictoryPoint)]
125-
getCardVictory pid card = do
126-
stt <- use playermap
127-
let victory = card ^.. cEffect . traverse . _AddVictory . to computeVictory
128-
computeVictory (vtype, vpoints, vcond) = (vtype, countConditionTrigger pid vcond stt * vpoints)
129-
return victory
130-
131114
-- | Try to play a card, with some extra resources, provided that the
132115
-- player has enough.
133116
playCard :: NonInteractive m => Age -> PlayerId -> MS.MultiSet Resource -> Card -> m ()
@@ -225,7 +208,7 @@ playTurn age turn rawcardmap = do
225208
-- then add the money gained from cards
226209
ifor results $ \pid (hand, _, card) -> do
227210
void $ for card $ \c -> do
228-
f <- getCardFunding pid c
211+
f <- getCardFunding pid c <$> use playermap
229212
playermap . ix pid . pFunds += f
230213
return hand
231214
-- TODO recycling capability
@@ -279,8 +262,9 @@ victoryPoints = use playermap >>= itraverse computeScore
279262
scienceTypes = playerState ^.. cardEffects . _RnD
280263
scienceJokers = length (playerState ^.. cardEffects . _ScientificBreakthrough)
281264
research = (RnDVictory, scienceScore scienceTypes scienceJokers)
282-
cardPoints <- mapM (getCardVictory pid) (playerState ^. pCards)
283-
return $ M.fromListWith (+) $ poaching : funding : research : concat cardPoints
265+
stt <- use playermap
266+
let cardPoints = playerState ^.. pCards . traverse . to (\c -> getCardVictory pid c stt) . folded
267+
return $ M.fromListWith (+) $ poaching : funding : research : cardPoints
284268

285269
-- | The main game function, runs a game. The state must be initialized in
286270
-- the same way as the 'initGame' function.

Diff for: Startups/Utils.hs

+13
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,16 @@ scienceScore rt 0 =
136136
let eachtypes = map (\t -> length (filter (== t) rt)) [Scaling, Programming, CustomSolution]
137137
in fromIntegral $ sum (map (\x -> x * x) eachtypes) + minimum eachtypes * 7
138138
scienceScore rt jokers = maximum [ scienceScore (t : rt) (jokers -1) | t <- [Scaling, Programming, CustomSolution] ]
139+
140+
-- | Compute the money that a card gives to a player. Note how we exploit the fact that ^. behaves like foldMap here.
141+
getCardFunding :: PlayerId -> Card -> M.Map PlayerId PlayerState -> Funding
142+
getCardFunding pid card stt = card ^. cEffect . traverse . _GainFunding . to computeFunding
143+
where
144+
computeFunding (n, cond) = countConditionTrigger pid cond stt * n
145+
146+
-- | Compute the victory points a card awards.
147+
getCardVictory :: PlayerId -> Card -> M.Map PlayerId PlayerState -> [(VictoryType, VictoryPoint)]
148+
getCardVictory pid card stt = card ^.. cEffect . traverse . _AddVictory . to computeVictory
149+
where
150+
computeVictory (vtype, vpoints, vcond) = (vtype, countConditionTrigger pid vcond stt * vpoints)
151+

0 commit comments

Comments
 (0)