| summaryrefslogtreecommitdiff | 
diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/ElmModule/Update.elm | 6 | ||||
| -rw-r--r-- | src/Struct/Flags.elm | 3 | ||||
| -rw-r--r-- | src/Struct/UI.elm | 6 | ||||
| -rw-r--r-- | src/Tonkadur/Execute.elm | 25 | ||||
| -rw-r--r-- | src/Tonkadur/PlayerInput.elm | 85 | ||||
| -rw-r--r-- | src/Tonkadur/Types.elm | 84 | ||||
| -rw-r--r-- | src/Update/Story.elm | 112 | 
7 files changed, 201 insertions, 120 deletions
| diff --git a/src/ElmModule/Update.elm b/src/ElmModule/Update.elm index d7617bd..9e45fde 100644 --- a/src/ElmModule/Update.elm +++ b/src/ElmModule/Update.elm @@ -1,7 +1,6 @@  module ElmModule.Update exposing (update)  -- Elm ------------------------------------------------------------------------- -import Html  -- Local Module ----------------------------------------------------------------  import Struct.Event @@ -37,9 +36,8 @@ update event model =                 (                    {model |                       ui = -                        -- TODO: display the actual error. -                        (Struct.UI.display_error -                           (Html.text "Failed to load story") +                        (Struct.UI.display_string_error +                           "Failed to load story"                             model.ui                          )                    }, diff --git a/src/Struct/Flags.elm b/src/Struct/Flags.elm index 43fc7c2..3614469 100644 --- a/src/Struct/Flags.elm +++ b/src/Struct/Flags.elm @@ -13,7 +13,8 @@ import List  --------------------------------------------------------------------------------  type alias Type =     { -      url_parameters : (List (List String)) +      url_parameters : (List (List String)), +      random_number : Int     }  -------------------------------------------------------------------------------- diff --git a/src/Struct/UI.elm b/src/Struct/UI.elm index b3bdaae..108ab04 100644 --- a/src/Struct/UI.elm +++ b/src/Struct/UI.elm @@ -52,6 +52,12 @@ display_error : (Html.Html Struct.Event.Type) -> Type -> Type  display_error html ui =     {ui | displayed_errors = (List.append ui.displayed_errors [html])} +display_string_error : String -> Type -> Type +display_string_error string ui = +   {ui | +      displayed_errors = (List.append ui.displayed_errors [(Html.text string)]) +   } +  display_choice : Int -> (Html.Html Struct.Event.Type) -> Type -> Type  display_choice ix html ui =     {ui | displayed_choices = (List.append ui.displayed_choices [(ix, html)])} diff --git a/src/Tonkadur/Execute.elm b/src/Tonkadur/Execute.elm index d302cc5..4a94701 100644 --- a/src/Tonkadur/Execute.elm +++ b/src/Tonkadur/Execute.elm @@ -105,18 +105,11 @@ initialize type_name address state =        new_state =           {state |              memory = -               (Tonkadur.Types.apply_at_address +               (Tonkadur.Types.set_at_address                    address_as_list -                  (\last_addr dict -> -                     (Dict.insert -                        last_addr -                        (Tonkadur.Types.get_default state type_name) -                        dict -                     ) -                  ) +                  (Tonkadur.Types.get_default state type_name)                    state.memory                 ) -            -- TODO: detect allocated memory for special handling.           }     in        case address_as_list of @@ -268,11 +261,11 @@ set_random address min max state =     in     {state |        memory = -         (Tonkadur.Types.apply_at_address +         (Tonkadur.Types.set_at_address              (Tonkadur.Types.value_to_address                 (Tonkadur.Compute.compute state address)              ) -            (\last_addr dict -> (Dict.insert last_addr (Tonkadur.Types.IntValue value) dict)) +            (Tonkadur.Types.IntValue value)              state.memory           ), @@ -288,17 +281,11 @@ set : (  set address value state =     {state |        memory = -         (Tonkadur.Types.apply_at_address +         (Tonkadur.Types.set_at_address              (Tonkadur.Types.value_to_address                 (Tonkadur.Compute.compute state address)              ) -            (\last_addr dict -> -               (Dict.insert -                  last_addr -                  (Tonkadur.Compute.compute state value) -                  dict -               ) -            ) +            (Tonkadur.Compute.compute state value)              state.memory           )     } diff --git a/src/Tonkadur/PlayerInput.elm b/src/Tonkadur/PlayerInput.elm deleted file mode 100644 index facf9e5..0000000 --- a/src/Tonkadur/PlayerInput.elm +++ /dev/null @@ -1,85 +0,0 @@ -module Tonkadur.PlayerInput exposing (..) - --- Elm ------------------------------------------------------------------------- -import Dict -import List - --- Tonkadur -------------------------------------------------------------------- -import Tonkadur.Types - --------------------------------------------------------------------------------- --- TYPES ----------------------------------------------------------------------- --------------------------------------------------------------------------------- - --------------------------------------------------------------------------------- --- LOCAL ----------------------------------------------------------------------- --------------------------------------------------------------------------------- - --------------------------------------------------------------------------------- --- EXPORTED -------------------------------------------------------------------- --------------------------------------------------------------------------------- -select_choice : Int -> Tonkadur.Types.State -> Tonkadur.Types.State -select_choice index state = {state | last_choice_index = index} - -input_string : String -> Tonkadur.Types.State -> Tonkadur.Types.State -input_string string state = -   {state | -      memory = -         (Tonkadur.Types.apply_at_address -            (Tonkadur.Types.value_to_address state.memorized_target) -            (\last_address dict -> -               (Dict.insert last_address (StringValue string) dict) -            ) -            state.memory -         ) -   } - -input_int : Int -> Tonkadur.Types.State -> Tonkadur.Types.State -input_int int state = -   {state | -      memory = -         (Tonkadur.Types.apply_at_address -            (Tonkadur.Types.value_to_address state.memorized_target) -            (\last_address dict -> -               (Dict.insert last_address (IntValue int) dict) -            ) -            state.memory -         ) -   } - -input_command : ( -      (List String) -> -      Tonkadur.Types.State -> -      Tonkadur.Types.State -   ) -input_command commands state = -   {state | -      memory = -         (Tonkadur.Types.apply_at_address -            (Tonkadur.Types.value_to_address state.memorized_target) -            (\last_address dict -> -               (Dict.insert -                  last_address -                  (ListValue -                     (Dict.fromList -                        (List.indexedMap -                           (\index value -> -                              ( -                                 ( -                                    case (String.fromInt index) of -                                       (Just i) -> i -                                       Nothing -> "invalid_index" -                                 ), -                                 value -                              ) -                           ) -                           commands -                        ) -                     ) -                  ) -                  dict -               ) -            ) -            state.memory -         ) -   } diff --git a/src/Tonkadur/Types.elm b/src/Tonkadur/Types.elm index bf8ae56..77cd707 100644 --- a/src/Tonkadur/Types.elm +++ b/src/Tonkadur/Types.elm @@ -318,9 +318,89 @@ apply_at_address address fun memory =              memory           ) +set_at_address : ( +      (List String) -> +      Value -> +      (Dict.Dict String Value) -> +      (Dict.Dict String Value) +   ) +set_at_address address value memory = +   (apply_at_address +      address +      (\last_address dict -> (Dict.insert last_address value dict)) +      memory +   ) +  allow_continuing : State -> State  allow_continuing state = {state | last_instruction_effect = MustContinue}  compare_pointers : (List String) -> (List String) -> Int -compare_pointers p0 p1 = 0 -   -- TODO: implement +compare_pointers p0 p1 = +   case (p0, p1) of +      ((h0 :: t0), (h1 :: t1)) -> +         if (h0 == h1) +         then (compare_pointers t0 t1) +         else if (h0 < h1) +         then -1 +         else 1 + +      ([], []) -> 0 +      (_, []) -> 1 +      ([], _) -> -1 + +elm_list_to_wyrd_list : (x -> Value) -> (List x) -> Value +elm_list_to_wyrd_list elm_value_to_wyrd_value list = +   let +      (final_next_index, final_as_dict) = +         (List.foldl +            (\value (next_index, as_dict) -> +               ( +                  (next_index + 1), +                  (Dict.insert +                     (String.fromInt next_index) +                     (elm_value_to_wyrd_value value) +                     as_dict +                  ) +               ) +            ) +            (0, (Dict.empty)) +            list +         ) +   in +      (ListValue final_as_dict) + +set_last_choice_index : Int -> State -> State +set_last_choice_index ix state = {state | last_choice_index = ix} + +set_target_from_string : String -> State -> State +set_target_from_string str state = +   {state | +      memory = +         (set_at_address +            (value_to_address state.memorized_target) +            (StringValue str) +            state.memory +         ) +   } + +set_target_from_integer : Int -> State -> State +set_target_from_integer int state = +   {state | +      memory = +         (set_at_address +            (value_to_address state.memorized_target) +            (IntValue int) +            state.memory +         ) +   } + +set_target_from_command : (List String) -> State -> State +set_target_from_command list state = +   {state | +      memory = +         (set_at_address +            (value_to_address state.memorized_target) +            (elm_list_to_wyrd_list (\command -> (StringValue command)) list) +            state.memory +         ) +   } diff --git a/src/Update/Story.elm b/src/Update/Story.elm index 68ed403..d2fc75f 100644 --- a/src/Update/Story.elm +++ b/src/Update/Story.elm @@ -80,7 +80,8 @@ step model =           }        Tonkadur.Types.MustPromptChoice -> -         let (last_ix, new_ui) = +         let +            (last_ix, new_ui) =                 (List.foldl                    (\option (ix, ui) ->                       case option of @@ -140,17 +141,110 @@ start : Struct.Model.Type -> Struct.Model.Type  start model = (step model)  select_choice : Int -> Struct.Model.Type -> Struct.Model.Type -select_choice ix model = model -   -- TODO: implement +select_choice ix model = +   (step +      {model | +         tonkadur = +            (Tonkadur.Types.set_last_choice_index +               ix +               model.tonkadur +            ) +      } +   )  input_string : String -> Struct.Model.Type -> Struct.Model.Type -input_string string model = model -   -- TODO: implement +input_string string model = +   let string_length = (String.length string) in +   if ((string_length < model.ui.min) || (string_length > model.ui.max)) +   then +      {model | +         ui = +            (Struct.UI.display_string_error +               ( +                  "Input string should be between " +                  ++ (String.fromInt model.ui.min) +                  ++ " and " +                  ++ (String.fromInt model.ui.max) +                  ++ " characters." +               ) +               model.ui +            ) +      } +   else +      (step +         {model | +            tonkadur = +               (Tonkadur.Types.set_target_from_string +                  string +                  model.tonkadur +               ) +         } +      )  input_integer : String -> Struct.Model.Type -> Struct.Model.Type -input_integer string model = model -   -- TODO: implement +input_integer string model = +   case (String.toInt string) of +      Nothing -> +         {model | +            ui = +               (Struct.UI.display_string_error +                  "Input expects an integer." +                  model.ui +               ) +         } + +      (Just int) -> +         if ((int < model.ui.min) || (int > model.ui.max)) +         then +            {model | +               ui = +                  (Struct.UI.display_string_error +                     ( +                        "Input integer should be between " +                        ++ (String.fromInt model.ui.min) +                        ++ " and " +                        ++ (String.fromInt model.ui.max) +                        ++ "." +                     ) +                     model.ui +                  ) +            } +         else +            (step +               {model | +                  tonkadur = +                     (Tonkadur.Types.set_target_from_integer +                        int +                        model.tonkadur +                     ) +               } +            )  input_command : String -> Struct.Model.Type -> Struct.Model.Type -input_command string model = model -   -- TODO: implement +input_command string model = +   let string_length = (String.length string) in +   if ((string_length < model.ui.min) || (string_length > model.ui.max)) +   then +      {model | +         ui = +            (Struct.UI.display_string_error +               ( +                  "Input string should be between " +                  ++ (String.fromInt model.ui.min) +                  ++ " and " +                  ++ (String.fromInt model.ui.max) +                  ++ " characters." +               ) +               model.ui +            ) +      } +   else +      (step +         {model | +            tonkadur = +               (Tonkadur.Types.set_target_from_command +                  (String.words string) +                  model.tonkadur +               ) +         } +      ) | 


