Skip to content

Instantly share code, notes, and snippets.

@allansideas
Last active July 8, 2017 13:48
Show Gist options
  • Save allansideas/2360e065c13a48b419fce245d0485a12 to your computer and use it in GitHub Desktop.
Save allansideas/2360e065c13a48b419fce245d0485a12 to your computer and use it in GitHub Desktop.
Union type lists, and data structure help from elm slack for diffing purposes (Thanks @gilbert, and jordan.wilcken)
module Main exposing (..)
import Html exposing (Html, text, div, img)
---- MODEL ----
type alias Transition =
{ transitionType : String, toStoryNode : Int, text : Maybe String }
type alias NewsItem =
{ id : Int
, content : List (Html Msg)
}
type alias ChatMessage =
{ id : Int
, chatId : Int
, userId : Int
, content : List (Html Msg)
}
type alias StoryNode =
{ id : Int
, nodeType : NodeType
, nodeContent : StoryNodeContent
, state : Maybe String
, transitions : List Transition
}
type NodeType
= NewsNode
| ChatNode
type StoryNodeContent
= NewsItemContent NewsItem
| ChatMessageContent ChatMessage
type alias Player =
{ visitedStoryNodes : List StoryNode
, currentStoryNode : Maybe StoryNode
}
type alias Model =
{}
init : ( Model, Cmd Msg )
init =
( {}, Cmd.none )
---- UPDATE ----
type Msg
= NoOp
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
( model, Cmd.none )
---- VIEW ----
findById : Int -> List StoryNode
findById id =
rawStoryNodes
|> List.filter
(\node -> node.id == id)
nodeTypeToString : NodeType -> String
nodeTypeToString nodeType =
case nodeType of
ChatNode ->
"ChatNode"
NewsNode ->
"NewsNode"
mapListFn : List String
mapListFn =
List.map
(nodeTypeToString << .nodeType)
rawStoryNodes
view : Model -> Html Msg
view model =
-- div []
-- [ text (unionFn (ARecord { id = 1, nestedRecord = { string = "String" } })) ]
div []
[ text (toString mapListFn)
, text (toString (findById 5))
]
---- PROGRAM ----
main : Program Never Model Msg
main =
Html.program
{ view = view
, init = init
, update = update
, subscriptions = always Sub.none
}
rawStoryNodes : List StoryNode
rawStoryNodes =
[ { id = 1
, nodeType = ChatNode
, state = Nothing
, nodeContent =
ChatMessageContent
{ id = 1
, chatId = 1
, userId = 1
, content = [ text "1: [1]" ]
}
, transitions =
[ { transitionType = "AUTOMATIC"
, toStoryNode = 2
, text = Nothing
}
]
}
, { id = 5
, nodeType = NewsNode
, state = Nothing
, nodeContent =
NewsItemContent
{ id = 4
, content = [ text "2: [1]" ]
}
, transitions =
[ { transitionType = "OptionInList"
, toStoryNode = 6
, text = Just "6"
}
, { transitionType = "OptionInList"
, toStoryNode = 6
, text = Just "6"
}
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment