summaryrefslogtreecommitdiff
blob: 7f02d7920c94e08f609030dbaa8920c8bbbba372 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
module ElmModule.Update exposing (update)

-- Elm -------------------------------------------------------------------------
import Http

-- Local Module ----------------------------------------------------------------
import Struct.Event
import Struct.Model
import Struct.UI

import Update.Story

--------------------------------------------------------------------------------
-- LOCAL -----------------------------------------------------------------------
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
-- EXPORTED --------------------------------------------------------------------
--------------------------------------------------------------------------------
update : (
      Struct.Event.Type ->
      Struct.Model.Type ->
      (Struct.Model.Type, (Cmd Struct.Event.Type))
   )
update event model =
   case event of
      (Struct.Event.ChoiceSelected ix) ->
         ((Update.Story.select_choice ix model), Cmd.none)

      Struct.Event.None -> (model, Cmd.none)
      (Struct.Event.UserInputInProgress string) ->
         (
            {model | ui = (Struct.UI.set_field_content string model.ui)},
            Cmd.none
         )

      Struct.Event.UserInputValidated ->
         ((Update.Story.handle_prompt_input model), Cmd.none)

      (Struct.Event.LoadStory http_result) ->
         case http_result of
            (Ok story) ->
               (
                  (Update.Story.start
                     {model |
                        tonkadur =
                           {story |
                              random_seed = model.tonkadur.random_seed
                           }
                     }
                  ),
                     Cmd.none
               )

            (Err error) ->
               (
                  {model |
                     ui =
                        (Struct.UI.display_string_error
                           (
                              "Failed to load story:\n"
                              ++
                              (
                                 case error of
                                    (Http.BadUrl details) ->
                                       ("Bad URL: " ++ details)

                                    Http.Timeout -> "Timeout."
                                    Http.NetworkError -> "Network Error."
                                    (Http.BadStatus code) ->
                                       (
                                          "Error code "
                                          ++ (String.fromInt code)
                                          ++ "."
                                       )

                                    (Http.BadBody details) ->
                                       (
                                          "Invalid content: "
                                          ++ details
                                       )
                              )
                           )
                           model.ui
                        )
                  },
                  Cmd.none
               )