From dcf39aedd1cedd1d29d6765225841bf906bc3677 Mon Sep 17 00:00:00 2001 From: nsensfel Date: Thu, 8 Nov 2018 17:16:05 +0100 Subject: (Locally) Saves changes to roster. --- src/roster-editor/src/Comm/UpdateRoster.elm | 49 +++++++++++++++++++++++++++++ src/roster-editor/src/Constants/IO.elm.m4 | 3 ++ src/roster-editor/src/ElmModule/Update.elm | 15 ++++++++- src/roster-editor/src/Struct/Character.elm | 14 +++++++-- src/roster-editor/src/Struct/Event.elm | 1 + src/roster-editor/src/Struct/Model.elm | 16 ++++++++++ src/roster-editor/src/Update/SendRoster.elm | 35 +++++++++++++++++++++ src/roster-editor/src/View/MainMenu.elm | 4 +-- 8 files changed, 132 insertions(+), 5 deletions(-) create mode 100644 src/roster-editor/src/Comm/UpdateRoster.elm create mode 100644 src/roster-editor/src/Update/SendRoster.elm diff --git a/src/roster-editor/src/Comm/UpdateRoster.elm b/src/roster-editor/src/Comm/UpdateRoster.elm new file mode 100644 index 0000000..fa58cb0 --- /dev/null +++ b/src/roster-editor/src/Comm/UpdateRoster.elm @@ -0,0 +1,49 @@ +module Comm.UpdateRoster exposing (try) + +-- Elm ------------------------------------------------------------------------- +import Array + +import List + +import Json.Encode + +-- Roster Editor --------------------------------------------------------------- +import Comm.Send + +import Constants.IO + +import Struct.Character +import Struct.Event +import Struct.Model + +-------------------------------------------------------------------------------- +-- TYPES ------------------------------------------------------------------------ +-------------------------------------------------------------------------------- + +-------------------------------------------------------------------------------- +-- LOCAL ----------------------------------------------------------------------- +-------------------------------------------------------------------------------- +try_encoding : Struct.Model.Type -> (Maybe Json.Encode.Value) +try_encoding model = + (Just + (Json.Encode.list + (List.map + (Struct.Character.encode) + (List.filter + (Struct.Character.get_was_edited) + (Array.toList model.characters) + ) + ) + ) + ) + +-------------------------------------------------------------------------------- +-- EXPORTED -------------------------------------------------------------------- +-------------------------------------------------------------------------------- +try : Struct.Model.Type -> (Maybe (Cmd Struct.Event.Type)) +try model = + (Comm.Send.try_sending + model + Constants.IO.roster_update_handler + try_encoding + ) diff --git a/src/roster-editor/src/Constants/IO.elm.m4 b/src/roster-editor/src/Constants/IO.elm.m4 index 39a6951..17683ac 100644 --- a/src/roster-editor/src/Constants/IO.elm.m4 +++ b/src/roster-editor/src/Constants/IO.elm.m4 @@ -9,6 +9,9 @@ roster_handler_url = (base_url ++ "/handler/roster") roster_loading_handler : String roster_loading_handler = (roster_handler_url ++ "/rst_load") +roster_update_handler : String +roster_update_handler = (roster_handler_url ++ "/rst_update") + armors_data_url : String armors_data_url = (base_url ++ "/asset/data/armors.json") diff --git a/src/roster-editor/src/ElmModule/Update.elm b/src/roster-editor/src/ElmModule/Update.elm index 02e417a..20a4c81 100644 --- a/src/roster-editor/src/ElmModule/Update.elm +++ b/src/roster-editor/src/ElmModule/Update.elm @@ -11,6 +11,7 @@ import Update.GoToMainMenu import Update.HandleServerReply import Update.SelectCharacter import Update.SelectTab +import Update.SendRoster import Update.SetArmor import Update.SetGlyph import Update.SetGlyphBoard @@ -48,7 +49,19 @@ update event model = (Update.SelectCharacter.apply_to new_model char_id) (Struct.Event.TabSelected tab) -> - (Update.SelectTab.apply_to new_model tab) + (Update.SelectTab.apply_to + ( + case tab of + Struct.UI.CharacterSelectionTab -> + (Struct.Model.save_character new_model) + + _ -> new_model + ) + tab + ) + + Struct.Event.SaveRequest -> + (Update.SendRoster.apply_to (Struct.Model.save_character new_model)) (Struct.Event.ClickedOnWeapon is_main) -> (Update.SelectTab.apply_to diff --git a/src/roster-editor/src/Struct/Character.elm b/src/roster-editor/src/Struct/Character.elm index 19663f1..f762370 100644 --- a/src/roster-editor/src/Struct/Character.elm +++ b/src/roster-editor/src/Struct/Character.elm @@ -17,6 +17,8 @@ module Struct.Character exposing set_glyph_board, get_glyphs, set_glyph, + set_was_edited, + get_was_edited, decoder, encode ) @@ -66,7 +68,8 @@ type alias Type = armor : Struct.Armor.Type, glyph_board : Struct.GlyphBoard.Type, glyphs : (Array.Array Struct.Glyph.Type), - current_omnimods : Struct.Omnimods.Type + current_omnimods : Struct.Omnimods.Type, + was_edited : Bool } -------------------------------------------------------------------------------- @@ -91,7 +94,8 @@ finish_decoding add_char = armor = armor, glyph_board = glyph_board, glyphs = glyphs, - current_omnimods = add_char.current_omnimods + current_omnimods = add_char.current_omnimods, + was_edited = False } in (almost_char, add_char.prt, add_char.awp, add_char.swp, add_char.ar) @@ -188,6 +192,12 @@ set_glyph : Int -> Struct.Glyph.Type -> Type -> Type set_glyph index glyph char = (refresh_omnimods {char | glyphs = (Array.set index glyph char.glyphs)}) +get_was_edited : Type -> Bool +get_was_edited char = char.was_edited + +set_was_edited : Bool -> Type -> Type +set_was_edited val char = {char | was_edited = False} + decoder : (Json.Decode.Decoder (Type, String, Int, Int, Int)) decoder = (Json.Decode.map diff --git a/src/roster-editor/src/Struct/Event.elm b/src/roster-editor/src/Struct/Event.elm index 4bb1083..570f84b 100644 --- a/src/roster-editor/src/Struct/Event.elm +++ b/src/roster-editor/src/Struct/Event.elm @@ -26,6 +26,7 @@ type Type = | ServerReplied (Result Http.Error (List Struct.ServerReply.Type)) | TabSelected Struct.UI.Tab | ClickedOnWeapon Bool + | SaveRequest | SelectedArmor Struct.Armor.Ref | SelectedGlyph (Struct.Glyph.Ref, Int) diff --git a/src/roster-editor/src/Struct/Model.elm b/src/roster-editor/src/Struct/Model.elm index 65bce6e..ef5b21c 100644 --- a/src/roster-editor/src/Struct/Model.elm +++ b/src/roster-editor/src/Struct/Model.elm @@ -5,6 +5,7 @@ module Struct.Model exposing add_character, update_character, update_character_fun, + save_character, add_weapon, add_armor, add_portrait, @@ -161,6 +162,21 @@ update_character ix new_val model = characters = (Array.set ix new_val model.characters) } +save_character : Type -> Type +save_character model = + case model.edited_char of + Nothing -> model + + (Just char) -> + {model | + characters = + (Array.set + (Struct.Character.get_index char) + (Struct.Character.set_was_edited True char) + model.characters + ) + } + update_character_fun : ( Int -> ((Maybe Struct.Character.Type) -> (Maybe Struct.Character.Type)) -> diff --git a/src/roster-editor/src/Update/SendRoster.elm b/src/roster-editor/src/Update/SendRoster.elm new file mode 100644 index 0000000..b6f0510 --- /dev/null +++ b/src/roster-editor/src/Update/SendRoster.elm @@ -0,0 +1,35 @@ +module Update.SendRoster exposing (apply_to) + +-- Elm ------------------------------------------------------------------------- +import Array + +-- Roster Editor --------------------------------------------------------------- +import Comm.UpdateRoster + +import Struct.Character +import Struct.Event +import Struct.Model + +-------------------------------------------------------------------------------- +-- LOCAL ----------------------------------------------------------------------- +-------------------------------------------------------------------------------- + +-------------------------------------------------------------------------------- +-- EXPORTED -------------------------------------------------------------------- +-------------------------------------------------------------------------------- +apply_to : Struct.Model.Type -> (Struct.Model.Type, (Cmd Struct.Event.Type)) +apply_to model = + ( + {model | + characters = + (Array.map + (Struct.Character.set_was_edited False) + model.characters + ) + }, + (case (Comm.UpdateRoster.try model) of + (Just cmd) -> cmd + Nothing -> Cmd.none + ) + ) + diff --git a/src/roster-editor/src/View/MainMenu.elm b/src/roster-editor/src/View/MainMenu.elm index 133ba83..3663076 100644 --- a/src/roster-editor/src/View/MainMenu.elm +++ b/src/roster-editor/src/View/MainMenu.elm @@ -33,14 +33,14 @@ get_characters_button_html = get_reset_button_html : (Html.Html Struct.Event.Type) get_reset_button_html = (Html.button - [ ] + [ ] [ (Html.text "Reset") ] ) get_save_button_html : (Html.Html Struct.Event.Type) get_save_button_html = (Html.button - [ ] + [ (Html.Events.onClick Struct.Event.SaveRequest) ] [ (Html.text "Save") ] ) -- cgit v1.2.3-70-g09d2