Skip to content

Commit

Permalink
misc
Browse files Browse the repository at this point in the history
  • Loading branch information
flober committed Jun 30, 2024
1 parent 596a2a1 commit 31cdcf4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 58 deletions.
6 changes: 4 additions & 2 deletions src/Card.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module Card (module Card) where
import Model


allCards :: [Card]
allCards = [dummy, dumber, triDummy, dumbo, bigDumbo, kingDumbo]
pool :: [Card]
pool = [dummy, dumber, triDummy, dumbo, bigDumbo, kingDumbo]

dummy :: Card
dummy = Card Dummy 1 3 1 1
Expand All @@ -24,3 +24,5 @@ bigDumbo = Card BigDumbo 5 3 5 5
kingDumbo :: Card
kingDumbo = Card KingDumbo 6 3 6 6

dummyWithALongNameItKeepsGoing :: Card
dummyWithALongNameItKeepsGoing = Card DummyWithALongNameItKeepsGoing 1 3 1 1
17 changes: 9 additions & 8 deletions src/Logic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@

module Logic (module Logic) where

import Card (allCards)
import Card (pool)
import Control.Lens ((^.))
import Control.Monad.Random
import Data.UUID
import Model

findCard :: UUID -> [CardInstance] -> CardInstance
findCard cardId instances =
case lookup cardId ([(_cardId cardInstance, cardInstance) | cardInstance <- instances]) of
Nothing -> error "Unexpected path: findCard should always find the target."
deterministicLookup :: Eq a => a -> [(a, b)] -> b
deterministicLookup a xs =
case lookup a xs of
Nothing -> error "Unexpected path: deterministicLookup should always find."
Just c -> c

findCard :: UUID -> [CardInstance] -> CardInstance
findCard cardId instances = deterministicLookup cardId ([(_cardId cardInstance, cardInstance) | cardInstance <- instances])

removeCard :: UUID -> [CardInstance] -> [CardInstance]
removeCard cardId = filter (\ci -> _cardId ci /= cardId)

-- END --

play :: UUID -> PlayerState -> PlayerState
play targetId ps = ps {board = board ps ++ [findCard targetId (hand ps)], hand = removeCard targetId (hand ps)}

Expand Down Expand Up @@ -72,7 +73,7 @@ randomShop t = do
return [CardInstance uuid c | c <- shopCards | uuid <- ids]
where
availableCards :: [Card]
availableCards = filter (\card -> card ^. cardTier <= t) allCards
availableCards = filter (\card -> card ^. cardTier <= t) pool

sampleNFromList :: (MonadRandom m) => Int -> [a] -> m [a]
sampleNFromList _ [] = return []
Expand Down
105 changes: 57 additions & 48 deletions src/View/Terminal.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}

{-# HLINT ignore "Use list literal" #-}
module View.Terminal where
module View.Terminal (render, helpMenu) where

import Data.List (intercalate)
import Data.Map (toList)
import Model (Card (_cardName), CardInstance (..), OppInfo (oppArmor, oppHP), PlayerState (..))
import Model (Card (_cardName), CardInstance (..), OppInfo (oppArmor, oppHP), Phase (..), PlayerState (..))

-- Render creates the following example. In the example, the names and entries are maxed out.
-- I.e., 15 characters is the longest permitting name (Rockpool Hunter and playeracgodman1 have 15 chars). Shop and board have 7 entries max, hand has 10 max.
Expand All @@ -28,56 +25,67 @@ import Model (Card (_cardName), CardInstance (..), OppInfo (oppArmor, oppHP), Pl
-- | playeracgodman5: 35 + 5 | playeracgodman6: 26 + 3 | playeracgodman7: HP 27 + 0 |
-- +-------------------------------------------------------------------------------------------------------------------------------------------+

maxCardNameDisplayLength :: Integer
rowWidth :: Int
rowWidth = 142

maxCardNameDisplayLength :: Int
maxCardNameDisplayLength = 15

maxPlayerNameDisplayLength :: Integer
maxPlayerNameDisplayLength :: Int
maxPlayerNameDisplayLength = 15

maxRowContentWidth :: Int
maxRowContentWidth = length $ intercalate " | " $ replicate 7 "Rockpool Hunter"
maxRowContentWidth = length $ intercalate " | " $ replicate 7 "Rockpool Hunter" -- 123

render :: PlayerState -> String
render = renderRecruit
render ps =
case phase ps of
Recruit -> renderRecruit ps
Blank -> "blank todo"
HeroSelect -> "heroselect todo"
Combat -> "combat todo"

hBorder :: [Char]
hBorder = "+" ++ replicate (rowWidth - 2) '-' ++ "+"

renderRecruit :: PlayerState -> String
renderRecruit ps =
intercalate "\n" $
filter
(not . null)
[ "+-------------------------------------------------------------------------------------------------------------------------------------------+",
"| Recruit |",
"+-------------------------------------------------------------------------------------------------------------------------------------------+",
"| Shop: " ++ alignMid maxRowContentWidth (intercalate " | " shopCardNames) ++ " |",
"+-------------------------------------------------------------------------------------------------------------------------------------------+",
"| Board: " ++ alignMid maxRowContentWidth (intercalate " | " boardCardNames) ++ " |",
"+-------------------------------------------------------------------------------------------------------------------------------------------+",
"| Hand: " ++ alignMid maxRowContentWidth (intercalate " | " $ take 7 handCardNames) ++ " |",
[ hBorder,
"|" ++ alignMid (rowWidth - 2) "Recruit" ++ "|",
hBorder,
"| Shop: " ++ alignMid maxRowContentWidth (intercalate " | " shopCardNames) ++ " |",
hBorder,
"| Board: " ++ alignMid maxRowContentWidth (intercalate " | " boardCardNames) ++ " |",
hBorder,
"| Hand: " ++ alignMid maxRowContentWidth (intercalate " | " $ take 7 handCardNames) ++ " |",
if not (null (drop 7 handCardNames))
then "| " ++ alignMid maxRowContentWidth (intercalate " | " (drop 7 handCardNames)) ++ " |"
then "| " ++ alignMid maxRowContentWidth (intercalate " | " (drop 7 handCardNames)) ++ " |"
else "",
"+-------------------------------------------------------------------------------------------------------------------------------------------+",
"| Tavern: " ++ alignMid maxRowContentWidth (intercalate " | " [tierUpCostText, freezeText, rerollCostText]) ++ " |",
"+-------------------------------------------------------------------------------------------------------------------------------------------+",
"| Player: " ++ alignMid maxRowContentWidth (intercalate " | " [healthText, armorText, goldText]) ++ " |",
"+-------------------------------------------------------------------------------------------------------------------------------------------+",
"| Opps HP: " ++ alignMid maxRowContentWidth (intercalate " | " $ take 4 oppInfoTextList) ++ " |",
hBorder,
"| Tavern: " ++ alignMid maxRowContentWidth (intercalate " | " [tierUpCostText, freezeText, rerollCostText]) ++ " |",
hBorder,
"| Player: " ++ alignMid maxRowContentWidth (intercalate " | " [healthText, armorText, goldText]) ++ " |",
hBorder,
"| Opps HP: " ++ alignMid maxRowContentWidth (intercalate " | " $ take 4 oppInfoTextList) ++ " |",
if not (null (drop 4 oppInfoTextList))
then "| " ++ alignMid maxRowContentWidth (intercalate " | " $ drop 4 oppInfoTextList) ++ " |"
then "| " ++ alignMid maxRowContentWidth (intercalate " | " $ drop 4 oppInfoTextList) ++ " |"
else "",
"+-------------------------------------------------------------------------------------------------------------------------------------------+"
hBorder
]
where
shopCardNames = [(abbrev 15 . show . _cardName . _card) cardInstance | cardInstance <- shop ps]
boardCardNames = [(abbrev 15 . show . _cardName . _card) cardInstance | cardInstance <- board ps]
handCardNames = [(abbrev 15. show . _cardName . _card) cardInstance | cardInstance <- hand ps]
shopCardNames = [(abbrev maxCardNameDisplayLength . show . _cardName . _card) cardInstance | cardInstance <- shop ps]
boardCardNames = [(abbrev maxCardNameDisplayLength . show . _cardName . _card) cardInstance | cardInstance <- board ps]
handCardNames = [(abbrev maxCardNameDisplayLength . show . _cardName . _card) cardInstance | cardInstance <- hand ps]
freezeText = if frozen ps then "Freeze: Yes" else "Freeze: No"
rerollCostText = "Reroll Cost: " ++ show (rerollCost ps)
tierUpCostText = "Upgrade Cost: " ++ show (tierUpCost ps)
healthText = "Health: " ++ show (hp ps)
armorText = "Armor: " ++ show (armor ps)
goldText = "Gold: " ++ show (curGold ps) ++ "/" ++ show (maxGold ps)
oppInfoTextList = [abbrev 15 (show userName) ++ ": " ++ show (oppHP info) ++ " + " ++ show (oppArmor info) | (userName, info) <- toList $ opponentInformation ps]
oppInfoTextList = [abbrev maxPlayerNameDisplayLength userName ++ ": " ++ show (oppHP info) ++ " + " ++ show (oppArmor info) | (userName, info) <- toList $ opponentInformation ps]

abbrev :: Int -> String -> String
abbrev maxLen s =
Expand All @@ -97,21 +105,22 @@ alignMid space s = leftPad ++ s ++ rightPad

helpMenu :: String
helpMenu =
intercalate "\n" $
"+--------------------------------------------+"
: "| HELP MENU |"
: "+--------------------------------------------+"
: "| Command | Description |"
: "+--------------------------------------------+"
: "| buy <n> | Buy card at index <n> |"
: "| b <n> | Shortcut for buy <n> |"
: "| sell <n> | Sell minion at index <n> |"
: "| s <n> | same as sell <n> |"
: "| help | Display this menu |"
: "| h | Shortcut for help |"
: "| endturn | End your turn |"
: "| e | Shortcut for endturn |"
: "+--------------------------------------------+"
: "| Note: number argument <n> starts at 1 |"
: "+--------------------------------------------+"
: []
intercalate
"\n"
[ "+--------------------------------------------+",
"| HELP MENU |",
"+--------------------------------------------+",
"| Command | Description |",
"+--------------------------------------------+",
"| buy <n> | Buy card at index <n> |",
"| b <n> | Shortcut for buy <n> |",
"| sell <n> | Sell minion at index <n> |",
"| s <n> | same as sell <n> |",
"| help | Display this menu |",
"| h | Shortcut for help |",
"| endturn | End your turn |",
"| e | Shortcut for endturn |",
"+--------------------------------------------+",
"| Note: number argument <n> starts at 1 |",
"+--------------------------------------------+"
]

0 comments on commit 31cdcf4

Please sign in to comment.