Skip to content

Commit b3caa21

Browse files
committed
Refactored the neighborhood relationship
1 parent 835a11d commit b3caa21

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

Startups/Game.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ initGame = do
4646
playermap . ix pid %= (pCompany .~ profile)
4747
. (pCards .~ [getResourceCard profile Project])
4848
. (pFunds .~ 3)
49-
. (pNeighborhood .~ M.fromList [(NLeft, ln), (NRight, rn)])
49+
. (pNeighborhood .~ (ln, rn))
5050
. (pCompanyStage .~ Project)
5151

5252
-- | A simple wrapper for getting random numbers.
@@ -104,7 +104,7 @@ resolveExchange pid exch = mconcat . M.elems <$> itraverse resolveExchange' exc
104104
stt <- use playermap
105105
let cost = getSum $ reslist ^. folded . to (Sum . getExchangeCost pid neigh stt)
106106
playermoney = fromMaybe 0 (stt ^? ix pid . pFunds)
107-
neighname = stt ^. ix pid . pNeighborhood . ix neigh
107+
neighname = stt ^. ix pid . neighbor neigh
108108
neigresources = stt ^. ix neighname . to (availableResources Exchange)
109109
when (cost > playermoney) (throwError "A player tried to perform an exchange without enough funding")
110110
unless (any (reslist `MS.isSubsetOf`) neigresources) (throwError "The neighbor doesn't have enough resources")
@@ -218,7 +218,7 @@ rotateHands age cardmap = itraverse rotatePlayer cardmap
218218
where
219219
rotatePlayer pid _ = do
220220
-- get the identifier of the correct neighbor
221-
neighid <- use (playermap . ix pid . pNeighborhood . ix direction)
221+
neighid <- use (playermap . ix pid . neighbor direction)
222222
-- get his hand
223223
return (cardmap ^. ix neighid)
224224
direction = if age == Age2

Startups/GameTypes.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ data GameState = GameState { _playermap :: M.Map PlayerId PlayerState
2323
, _rnd :: StdGen
2424
}
2525

26+
type Neighborhood = (PlayerId, PlayerId)
27+
2628
data PlayerState = PlayerState { _pCompany :: CompanyProfile
2729
, _pCompanyStage :: CompanyStage
2830
, _pCards :: [Card]
2931
, _pFunds :: Funding
30-
, _pNeighborhood :: M.Map Neighbor PlayerId
32+
, _pNeighborhood :: Neighborhood
3133
, _pPoachingResults :: [PoachingOutcome]
3234
}
3335

@@ -40,6 +42,10 @@ cardEffects = pCards . traverse . cEffect . traverse
4042
playerEffects :: PlayerId -> Traversal' GameState Effect
4143
playerEffects pid = playermap . ix pid . cardEffects
4244

45+
neighbor :: Neighbor -> Lens' PlayerState PlayerId
46+
neighbor NLeft = pNeighborhood . _1
47+
neighbor NRight = pNeighborhood . _2
48+
4349
type Message = String
4450

4551
data PlayerAction = PlayerAction ActionType Card

Startups/Utils.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ getTargets pid targets stt = targets ^.. folded . to getTargets' . traverse
106106
where
107107
getPlayer p = stt ^. at p
108108
getTargets' Own = getPlayer pid
109-
getTargets' (Neighboring n) = getPlayer pid >>= view (pNeighborhood . at n) >>= getPlayer
109+
getTargets' (Neighboring n) = getPlayer pid >>= Just . view (neighbor n) >>= getPlayer
110110

111111
-- | Given a condition, counts how often it hits. It can return an
112112
-- arbitrary Num because we will multiply this results with Funding or

tests/tests.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ testState = GameState (M.fromList players) discard (mkStdGen 5)
3232
ppam = CompanyProfile Apple B
3333
ppoum = CompanyProfile Google A
3434
pbob = CompanyProfile Twitter A
35-
pim = PlayerState ppim Project pimcards 1 (M.fromList [(NLeft, "pam"), (NRight, "bob")]) []
36-
pam = PlayerState ppam Project pamcards 3 (M.fromList [(NLeft, "poum"), (NRight, "pim")]) []
37-
poum = PlayerState ppoum Project poumcards 6 (M.fromList [(NLeft, "bob"), (NRight, "pam")]) []
38-
bob = PlayerState pbob Project bobcards 5 (M.fromList [(NLeft, "pim"), (NRight, "poum")]) []
35+
pim = PlayerState ppim Project pimcards 1 ("pam", "bob") []
36+
pam = PlayerState ppam Project pamcards 3 ("poum", "pim") []
37+
poum = PlayerState ppoum Project poumcards 6 ("bob", "pam") []
38+
bob = PlayerState pbob Project bobcards 5 ("pim", "poum") []
3939
pimcards = map (getResourceCard ppim) [Project .. Stage1] <> map getCard [ "Cloud Servers"
4040
, "Marketroid"
4141
, "Company Nerf Battles"

0 commit comments

Comments
 (0)