Created
May 21, 2017 04:06
-
-
Save rezich/6d0fb1bd538a2743686bf083c60caa33 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 11matches | |
module App exposing (..) | |
import Html exposing (..) | |
import Html.Attributes exposing (class, disabled) | |
import Html.Events exposing (onClick, onMouseDown, onMouseUp) | |
import Time exposing (Time, second) | |
-- MODEL | |
type alias Money = | |
Int | |
type alias Wood = | |
Int | |
type alias Phosphorus = | |
Int | |
type alias Cardboard = | |
Int | |
type alias Boxes = | |
Int | |
type alias Matches = | |
Int | |
type alias Matchboxes = | |
Int | |
type alias Model = | |
{ money : Money | |
, wood : Wood | |
, phosphorus : Phosphorus | |
, cardboard : Cardboard | |
, boxes : Boxes | |
, matches : Matches | |
, matchboxes : Matchboxes | |
} | |
-- BALANCE | |
priceWoodBuy : Money | |
priceWoodBuy = | |
1 | |
priceWoodSell : Money | |
priceWoodSell = | |
1 | |
pricePhosphorusBuy : Money | |
pricePhosphorusBuy = | |
2 | |
pricePhosphorusSell : Money | |
pricePhosphorusSell = | |
2 | |
priceCardboardBuy : Money | |
priceCardboardBuy = | |
10 | |
priceCardboardSell : Money | |
priceCardboardSell = | |
5 | |
priceBoxesSell : Money | |
priceBoxesSell = | |
10 | |
priceMatchesSell : Money | |
priceMatchesSell = | |
4 | |
priceMatchboxesSell : Money | |
priceMatchboxesSell = | |
100 | |
woodPerMatch : Wood | |
woodPerMatch = | |
1 | |
phosphorusPerMatch : Phosphorus | |
phosphorusPerMatch = | |
2 | |
-- INIT | |
init : ( Model, Cmd Msg ) | |
init = | |
( { money = 0 | |
, wood = 0 | |
, phosphorus = 0 | |
, cardboard = 0 | |
, boxes = 1 | |
, matches = 11 | |
, matchboxes = 0 | |
} | |
, Cmd.none | |
) | |
-- MESSAGES | |
type Msg | |
= Tick Time | |
| BuyWood Wood | |
| SellWood Wood | |
| BuyPhosphorus Phosphorus | |
| SellPhosphorus Phosphorus | |
| BuyCardboard Cardboard | |
| SellCardboard Cardboard | |
| SellBoxes Boxes | |
| CraftBoxes Boxes | |
| SellMatches Matches | |
| CraftMatches Matches | |
| SellMatchboxes Matchboxes | |
| CraftMatchboxes Matchboxes | |
-- UPDATE | |
update : Msg -> Model -> ( Model, Cmd Msg ) | |
update msg model = | |
case msg of | |
Tick _ -> | |
( model, Cmd.none ) | |
BuyWood amount -> | |
if model.money >= priceWoodBuy then | |
( { model | money = model.money - priceWoodBuy * amount, wood = model.wood + amount * amount * 100 }, Cmd.none ) | |
else | |
( model, Cmd.none ) | |
SellWood amount -> | |
if model.wood >= amount * 100 then | |
( { model | money = model.money + priceWoodSell * amount, wood = model.wood - amount * 100 }, Cmd.none ) | |
else | |
( model, Cmd.none ) | |
BuyPhosphorus amount -> | |
if model.money >= pricePhosphorusBuy then | |
( { model | money = model.money - pricePhosphorusBuy * amount, phosphorus = model.phosphorus + amount * 100 }, Cmd.none ) | |
else | |
( model, Cmd.none ) | |
SellPhosphorus amount -> | |
if model.phosphorus >= amount * 100 then | |
( { model | money = model.money + pricePhosphorusSell * amount, phosphorus = model.phosphorus - amount * 100 }, Cmd.none ) | |
else | |
( model, Cmd.none ) | |
BuyCardboard amount -> | |
if model.money >= priceCardboardBuy then | |
( { model | money = model.money - priceCardboardBuy * amount, cardboard = model.cardboard + amount }, Cmd.none ) | |
else | |
( model, Cmd.none ) | |
SellCardboard amount -> | |
if model.cardboard >= amount then | |
( { model | money = model.money + priceCardboardSell * amount, cardboard = model.cardboard - amount }, Cmd.none ) | |
else | |
( model, Cmd.none ) | |
SellBoxes amount -> | |
if model.boxes >= amount then | |
( { model | money = model.money + priceBoxesSell * amount, boxes = model.boxes - amount }, Cmd.none ) | |
else | |
( model, Cmd.none ) | |
CraftBoxes amount -> | |
if model.cardboard >= amount then | |
( { model | cardboard = model.cardboard - amount, boxes = model.boxes + amount }, Cmd.none ) | |
else | |
( model, Cmd.none ) | |
SellMatches amount -> | |
if model.matches >= amount then | |
( { model | money = model.money + priceMatchesSell * amount, matches = model.matches - amount }, Cmd.none ) | |
else | |
( model, Cmd.none ) | |
CraftMatches amount -> | |
if model.wood >= woodPerMatch && model.phosphorus >= phosphorusPerMatch then | |
( { model | wood = model.wood - woodPerMatch * amount, phosphorus = model.phosphorus - phosphorusPerMatch * amount, matches = model.matches + amount }, Cmd.none ) | |
else | |
( model, Cmd.none ) | |
SellMatchboxes amount -> | |
if model.matchboxes >= amount then | |
( { model | money = model.money + priceMatchboxesSell * amount, matchboxes = model.matchboxes - amount }, Cmd.none ) | |
else | |
( model, Cmd.none ) | |
CraftMatchboxes amount -> | |
if model.boxes >= amount && model.matches >= 12 * amount then | |
( { model | boxes = model.boxes - amount, matches = model.matches - 12 * amount, matchboxes = model.matchboxes + amount }, Cmd.none ) | |
else | |
( model, Cmd.none ) | |
-- HELPERS | |
formatMoney : Money -> String | |
formatMoney amount = | |
"$" | |
++ toString (amount // 100) | |
++ "." | |
++ (if amount % 100 < 10 then | |
"0" | |
else | |
"" | |
) | |
++ toString (amount % 100) | |
formatWood : Wood -> String | |
formatWood amount = | |
toString (amount // 100) ++ "." ++ toString (amount % 100) | |
formatPhosphorus : Phosphorus -> String | |
formatPhosphorus amount = | |
toString (amount // 100) ++ "." ++ toString (amount % 100) | |
formatCardboard : Cardboard -> String | |
formatCardboard amount = | |
toString amount | |
formatBoxes : Boxes -> String | |
formatBoxes amount = | |
toString amount | |
formatMatches : Matches -> String | |
formatMatches amount = | |
toString amount | |
formatMatchboxes : Matchboxes -> String | |
formatMatchboxes amount = | |
toString amount | |
canCraftBox : Model -> Bool | |
canCraftBox model = | |
model.cardboard >= 1 | |
canCraftMatch : Model -> Bool | |
canCraftMatch model = | |
model.wood >= woodPerMatch && model.phosphorus >= phosphorusPerMatch | |
canCraftMatchbox : Model -> Bool | |
canCraftMatchbox model = | |
model.matches >= 12 && model.boxes >= 1 | |
-- VIEW | |
viewHeader : Html Msg | |
viewHeader = | |
header [] | |
[ h1 [] [ text "Eleven Matches" ] ] | |
viewMain : Model -> Html Msg | |
viewMain model = | |
table [] | |
[ tr [] | |
[ th [] [ text "Money" ] | |
, td [] [ text (formatMoney model.money) ] | |
] | |
, tr [] | |
[ th [] [ text "Wood" ] | |
, td [] [ text (formatWood model.wood) ] | |
, td [] | |
[ button [ onClick (BuyWood 1), disabled (model.money < priceWoodBuy) ] [ text "Buy" ] | |
] | |
, td [] | |
[ button [ onClick (SellWood 1), disabled (model.wood < 1) ] [ text "Sell" ] ] | |
] | |
, tr [] | |
[ th [] [ text "Phosphorus" ] | |
, td [] [ text (formatPhosphorus model.phosphorus) ] | |
, td [] | |
[ button [ onClick (BuyPhosphorus 1), disabled (model.money < pricePhosphorusBuy) ] [ text "Buy" ] | |
] | |
, td [] | |
[ button [ onClick (SellPhosphorus 1), disabled (model.phosphorus < 1) ] [ text "Sell" ] ] | |
] | |
, tr [] | |
[ th [] [ text "Cardboard" ] | |
, td [] [ text (formatCardboard model.cardboard) ] | |
, td [] | |
[ button [ onClick (BuyCardboard 1), disabled (model.money < priceCardboardBuy) ] [ text "Buy" ] | |
] | |
, td [] | |
[ button [ onClick (SellCardboard 1), disabled (model.cardboard < 1) ] [ text "Sell" ] ] | |
] | |
, tr [] | |
[ th [] [ text "Boxes" ] | |
, td [] [ text (formatBoxes model.boxes) ] | |
, td [] [] | |
, td [] | |
[ button [ onClick (SellBoxes 1), disabled (model.boxes < 1) ] [ text "Sell" ] | |
] | |
, td [] | |
[ button [ onClick (CraftBoxes 1), disabled (model |> canCraftBox |> not) ] [ text "Craft" ] ] | |
] | |
, tr [] | |
[ th [] [ text "Matches" ] | |
, td [] [ text (formatMatches model.matches) ] | |
, td [] [] | |
, td [] | |
[ button [ onClick (SellMatches 1), disabled (model.matches < 1) ] [ text "Sell" ] | |
] | |
, td [] | |
[ button [ onClick (CraftMatches 1), disabled (model |> canCraftMatch |> not) ] [ text "Craft" ] ] | |
] | |
, tr [] | |
[ th [] [ text "Matchboxes" ] | |
, td [] [ text (formatMatchboxes model.matchboxes) ] | |
, td [] [] | |
, td [] | |
[ button [ onClick (SellMatchboxes 1), disabled (model.matchboxes < 1) ] [ text "Sell" ] | |
] | |
, td [] | |
[ button [ onClick (CraftMatchboxes 1), disabled (model |> canCraftMatchbox |> not) ] [ text "Craft" ] ] | |
] | |
] | |
view : Model -> Html Msg | |
view model = | |
div [] | |
[ viewHeader | |
, viewMain model | |
] | |
-- SUBSCRIPTIONS | |
subscriptions : Model -> Sub Msg | |
subscriptions model = | |
Time.every second Tick | |
-- MAIN | |
main : Program Never Model Msg | |
main = | |
program | |
{ init = init | |
, view = view | |
, update = update | |
, subscriptions = subscriptions | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment