diff options
author | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2018-09-29 15:07:13 +0200 |
---|---|---|
committer | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2018-09-29 15:07:13 +0200 |
commit | 463aca15fdeeb7fda838dcc7355d2365de5d703d (patch) | |
tree | cbb40583cf96cfb233f7f0d41d5ccdd164d48a8f | |
parent | 80c3b7947eb0bd240d4c1f94808cb64d2fbfbf3d (diff) | |
download | tacticians-extension-463aca15fdeeb7fda838dcc7355d2365de5d703d.zip tacticians-extension-463aca15fdeeb7fda838dcc7355d2365de5d703d.tar.bz2 |
...
-rw-r--r-- | src/background/src/Comm/GetBattles.elm | 5 | ||||
-rw-r--r-- | src/background/src/Comm/GetID.elm | 43 | ||||
-rw-r--r-- | src/background/src/Comm/Send.elm | 22 | ||||
-rw-r--r-- | src/background/src/Comm/SetBattles.elm | 12 | ||||
-rw-r--r-- | src/background/src/ElmModule/Subscriptions.elm | 10 | ||||
-rw-r--r-- | src/background/src/ElmModule/Update.elm | 24 | ||||
-rw-r--r-- | src/background/src/Struct/Event.elm | 3 | ||||
-rw-r--r-- | src/background/src/Struct/Model.elm | 44 | ||||
-rw-r--r-- | src/background/src/Struct/ServerReply.elm | 1 | ||||
-rw-r--r-- | src/background/src/Update/HandleServerReply.elm | 87 | ||||
-rw-r--r-- | src/background/src/Update/RefreshBattles.elm | 15 | ||||
-rw-r--r-- | src/shared/Action/Ports.elm | 4 | ||||
-rw-r--r-- | src/shared/Struct/Flags.elm | 8 | ||||
-rw-r--r-- | www/script/battles.js | 9 | ||||
-rw-r--r-- | www/script/params.js | 80 |
15 files changed, 150 insertions, 217 deletions
diff --git a/src/background/src/Comm/GetBattles.elm b/src/background/src/Comm/GetBattles.elm index 39673d7..fbb8d49 100644 --- a/src/background/src/Comm/GetBattles.elm +++ b/src/background/src/Comm/GetBattles.elm @@ -19,9 +19,10 @@ import Struct.Player -------------------------------------------------------------------------------- -- EXPORTED -------------------------------------------------------------------- -------------------------------------------------------------------------------- -request : Struct.Player.Type -> (Cmd Struct.Event.Type) -request player = +request : Int -> Struct.Player.Type -> (Cmd Struct.Event.Type) +request ix player = (Comm.Send.commit + ix ( (Struct.Player.get_query_url player) ++ "/handler/player/plr_get_battles?pid=" diff --git a/src/background/src/Comm/GetID.elm b/src/background/src/Comm/GetID.elm deleted file mode 100644 index 14d668c..0000000 --- a/src/background/src/Comm/GetID.elm +++ /dev/null @@ -1,43 +0,0 @@ -module Comm.GetID exposing (try) - --- Elm ------------------------------------------------------------------------- -import Json.Encode - --- Extension ------------------------------------------------------------------- -import Comm.Send - -import Constants.IO - -import Struct.Event -import Struct.Model - --------------------------------------------------------------------------------- --- TYPES ------------------------------------------------------------------------ --------------------------------------------------------------------------------- - --------------------------------------------------------------------------------- --- LOCAL ----------------------------------------------------------------------- --------------------------------------------------------------------------------- -try_encoding : String -> Struct.Model.Type -> (Maybe Json.Encode.Value) -try_encoding player_id model = - let - encoded_player_id = (Json.Encode.string player_id) - in - (Just - (Json.Encode.object - [ - ("id", encoded_player_id) - ] - ) - ) - --------------------------------------------------------------------------------- --- EXPORTED -------------------------------------------------------------------- --------------------------------------------------------------------------------- -try : Struct.Model.Type -> String -> (Maybe (Cmd Struct.Event.Type)) -try model = - (Comm.Send.try_sending - model - Constants.IO.get_battles_handler - (try_encoding player_id) - ) diff --git a/src/background/src/Comm/Send.elm b/src/background/src/Comm/Send.elm index 26caf3a..0409e61 100644 --- a/src/background/src/Comm/Send.elm +++ b/src/background/src/Comm/Send.elm @@ -19,11 +19,15 @@ import Struct.ServerReply -------------------------------------------------------------------------------- -- LOCAL ----------------------------------------------------------------------- -------------------------------------------------------------------------------- -internal_decoder : String -> (Json.Decode.Decoder Struct.ServerReply.Type) -internal_decoder reply_type = +internal_decoder : ( + Int -> + String -> + (Json.Decode.Decoder Struct.ServerReply.Type) + ) +internal_decoder ix reply_type = case reply_type of "okay" -> (Comm.Okay.decoder) - "set_battles" -> (Comm.SetBattles.decoder) + "set_battles" -> (Comm.SetBattles.decoder ix) other -> (Json.Decode.fail ( @@ -33,17 +37,17 @@ internal_decoder reply_type = ) ) -decoder : (Json.Decode.Decoder Struct.ServerReply.Type) -decoder = +decoder : Int -> (Json.Decode.Decoder Struct.ServerReply.Type) +decoder ix = (Json.Decode.field "msg" Json.Decode.string) - |> (Json.Decode.andThen (internal_decoder)) + |> (Json.Decode.andThen (internal_decoder ix)) -------------------------------------------------------------------------------- -- EXPORTED -------------------------------------------------------------------- -------------------------------------------------------------------------------- -commit : String -> (Cmd Struct.Event.Type) -commit query = +commit : Int -> String -> (Cmd Struct.Event.Type) +commit ix query = (Http.send Struct.Event.ServerReplied - (Http.get query (Json.Decode.list (decoder))) + (Http.get query (Json.Decode.list (decoder ix))) ) diff --git a/src/background/src/Comm/SetBattles.elm b/src/background/src/Comm/SetBattles.elm index 5fb3243..631ad6e 100644 --- a/src/background/src/Comm/SetBattles.elm +++ b/src/background/src/Comm/SetBattles.elm @@ -38,13 +38,13 @@ internal_decoder = ) ) -to_server_reply : Battles -> Struct.ServerReply.Type -to_server_reply t = - (Struct.ServerReply.SetBattles (t.campaigns, t.invasions, t.events)) +to_server_reply : Int -> Battles -> Struct.ServerReply.Type +to_server_reply ix t = + (Struct.ServerReply.SetBattles (ix, t.campaigns, t.invasions, t.events)) -------------------------------------------------------------------------------- -- EXPORTED -------------------------------------------------------------------- -------------------------------------------------------------------------------- -decoder : (Json.Decode.Decoder Struct.ServerReply.Type) -decoder = - (Json.Decode.map (to_server_reply) (internal_decoder)) +decoder : Int -> (Json.Decode.Decoder Struct.ServerReply.Type) +decoder ix = + (Json.Decode.map (to_server_reply ix) (internal_decoder)) diff --git a/src/background/src/ElmModule/Subscriptions.elm b/src/background/src/ElmModule/Subscriptions.elm index 922b6a3..4163be7 100644 --- a/src/background/src/ElmModule/Subscriptions.elm +++ b/src/background/src/ElmModule/Subscriptions.elm @@ -3,6 +3,8 @@ module ElmModule.Subscriptions exposing (..) -- Elm ------------------------------------------------------------------------- import Time +import Json.Decode + -- Extension ------------------------------------------------------------------- import Action.Ports @@ -21,7 +23,13 @@ subscriptions : Struct.Model.Type -> (Sub Struct.Event.Type) subscriptions model = (Sub.batch [ - (Action.Ports.params_in (\s -> (Struct.Event.ReadParams s))), + (Action.Ports.params_in + (\s -> + case (Json.Decode.decodeString (Struct.Flags.decoder) s) of + (Err _) -> (Struct.Event.ReadParams (Struct.Flags.default)) + (Ok flags) -> (Struct.Event.ReadParams flags) + ) + ), (Time.every ((toFloat (Struct.Flags.get_frequency model.flags)) * Time.minute) (\e -> (Struct.Event.ShouldRefresh)) diff --git a/src/background/src/ElmModule/Update.elm b/src/background/src/ElmModule/Update.elm index aac12f6..a35e710 100644 --- a/src/background/src/ElmModule/Update.elm +++ b/src/background/src/ElmModule/Update.elm @@ -3,6 +3,8 @@ module ElmModule.Update exposing (update) -- Elm ------------------------------------------------------------------------- -- Extension ------------------------------------------------------------------- +import Action.Ports + import Struct.Event import Struct.Model @@ -25,18 +27,18 @@ update event model = let new_model = (Struct.Model.clear_error model) in - case event of - Struct.Event.None -> (model, Cmd.none) + case event of + Struct.Event.None -> (model, Cmd.none) - (Struct.Event.ReadParams (int, str)) -> (model, Cmd.none) + (Struct.Event.ReadParams flags) -> + (Update.RefreshBattles.apply_to + (Struct.Model.set_flags flags model) + ) - Struct.Event.ShouldRefresh -> (Update.RefreshBattles.apply_to model) + Struct.Event.ShouldRefresh -> (model, (Action.Ports.get_params ())) - (Struct.Event.Failed err) -> - ( - (Struct.Model.invalidate err new_model), - Cmd.none - ) + (Struct.Event.Failed err) -> + ((Struct.Model.invalidate err new_model), Cmd.none) - (Struct.Event.ServerReplied result) -> - (Update.HandleServerReply.apply_to model result) + (Struct.Event.ServerReplied result) -> + (Update.HandleServerReply.apply_to model result) diff --git a/src/background/src/Struct/Event.elm b/src/background/src/Struct/Event.elm index 2b8bf0e..aed4d4f 100644 --- a/src/background/src/Struct/Event.elm +++ b/src/background/src/Struct/Event.elm @@ -5,6 +5,7 @@ import Http -- Main Menu ------------------------------------------------------------------- import Struct.Error +import Struct.Flags import Struct.ServerReply -------------------------------------------------------------------------------- @@ -13,7 +14,7 @@ import Struct.ServerReply type Type = None | Failed Struct.Error.Type - | ReadParams (Int, String) + | ReadParams (Struct.Flags.Type) | ShouldRefresh | ServerReplied (Result Http.Error (List Struct.ServerReply.Type)) diff --git a/src/background/src/Struct/Model.elm b/src/background/src/Struct/Model.elm index 6742e96..5e09f9d 100644 --- a/src/background/src/Struct/Model.elm +++ b/src/background/src/Struct/Model.elm @@ -4,15 +4,16 @@ module Struct.Model exposing new, invalidate, reset, - clear_error + clear_error, + set_flags ) -- Elm ------------------------------------------------------------------------- import Array -- Extension ------------------------------------------------------------------- -import Struct.Flags import Struct.Error +import Struct.Flags import Struct.Player -------------------------------------------------------------------------------- @@ -21,10 +22,9 @@ import Struct.Player type alias Type = { flags: Struct.Flags.Type, - error: (Maybe Struct.Error.Type), players: (Array.Array Struct.Player.Type), - query_index: Int, - notify: Bool + remaining_updates: Int, + error: (Maybe Struct.Error.Type) } -------------------------------------------------------------------------------- @@ -34,33 +34,31 @@ type alias Type = -------------------------------------------------------------------------------- -- EXPORTED -------------------------------------------------------------------- -------------------------------------------------------------------------------- +set_flags : Struct.Flags.Type -> Type -> Type +set_flags flags t = + let + players_array = (Array.fromList (Struct.Flags.get_players flags)) + in + {t | + flags = flags, + players = players_array, + remaining_updates = (Array.length players_array) + } + new : Struct.Flags.Type -> Type new flags = { flags = flags, - error = Nothing, - players = - (Array.push - (Struct.Player.default) - (Array.fromList (Struct.Flags.get_players flags)) - ), - query_index = -1, - notify = False + players = (Array.fromList (Struct.Flags.get_players flags)), + remaining_updates = 0, + error = Nothing } reset : Type -> Type -reset model = - {model | - error = Nothing, - notify = False, - query_index = -1 - } +reset model = {model | error = Nothing} invalidate : Struct.Error.Type -> Type -> Type -invalidate err model = - {model | - error = (Just err) - } +invalidate err model = {model | error = (Just err)} clear_error : Type -> Type clear_error model = {model | error = Nothing} diff --git a/src/background/src/Struct/ServerReply.elm b/src/background/src/Struct/ServerReply.elm index 35d3f89..e800b9f 100644 --- a/src/background/src/Struct/ServerReply.elm +++ b/src/background/src/Struct/ServerReply.elm @@ -13,6 +13,7 @@ type Type = Okay | SetBattles ( + Int, (List Struct.BattleSummary.Type), (List Struct.BattleSummary.Type), (List Struct.BattleSummary.Type) diff --git a/src/background/src/Update/HandleServerReply.elm b/src/background/src/Update/HandleServerReply.elm index 81fec2f..116a60c 100644 --- a/src/background/src/Update/HandleServerReply.elm +++ b/src/background/src/Update/HandleServerReply.elm @@ -3,10 +3,12 @@ module Update.HandleServerReply exposing (apply_to) -- Elm ------------------------------------------------------------------------- import Array +import Json.Encode + import Http -- Extension ------------------------------------------------------------------- -import Comm.GetBattles +import Action.Ports import Struct.BattleSummary import Struct.Error @@ -22,8 +24,34 @@ import Struct.ServerReply -------------------------------------------------------------------------------- -- LOCAL ----------------------------------------------------------------------- -------------------------------------------------------------------------------- +maybe_update_storage : ( + Struct.Model.Type -> + (List (Cmd Struct.Event.Type)) -> + (List (Cmd Struct.Event.Type)) + ) +maybe_update_storage model cmds = + if (model.remaining_updates > 0) + then + cmds + else + ( + (Action.Ports.set_results + (Json.Encode.encode + 0 + (Json.Encode.list + (List.map + (Struct.Player.encode) + (Array.toList model.players) + ) + ) + ) + ) + :: cmds + ) + handle_set_battles : ( ( + Int, (List Struct.BattleSummary.Type), (List Struct.BattleSummary.Type), (List Struct.BattleSummary.Type) @@ -34,46 +62,41 @@ handle_set_battles : ( handle_set_battles battles current_state = let (model, cmds) = current_state - (campaigns, invasions, events) = battles + (ix, campaigns, invasions, events) = battles in - case (Array.get model.query_index model.players) of - Nothing -> current_state -- TODO: error + case (Array.get ix model.players) of + Nothing -> + let + updated_model = + {model | remaining_updates = (model.remaining_updates - 1)} + in + ( + updated_model, + cmds + ) + (Just player) -> let - updated_player = - (Struct.Player.set_battles - campaigns - invasions - events - player - ) updated_model = {model | + remaining_updates = (model.remaining_updates - 1), players = - (Array.set - model.query_index - updated_player - model.players - ), - query_index = (model.query_index + 1), - notify = - ( - model.notify - || (Struct.Player.has_active_battles updated_player) + (Array.set + ix + (Struct.Player.set_battles + campaigns + invasions + events + player ) + model.players + ) } in - case (Array.get updated_model.query_index model.players) of - Nothing -> ({updated_model| query_index = -1}, cmds) - - (Just next_player) -> - ( - updated_model, - ( - (Comm.GetBattles.request next_player) - :: cmds - ) - ) + ( + updated_model, + (maybe_update_storage model cmds) + ) apply_command : ( Struct.ServerReply.Type -> diff --git a/src/background/src/Update/RefreshBattles.elm b/src/background/src/Update/RefreshBattles.elm index 6a9d602..3a45ee5 100644 --- a/src/background/src/Update/RefreshBattles.elm +++ b/src/background/src/Update/RefreshBattles.elm @@ -18,7 +18,14 @@ import Struct.Model -------------------------------------------------------------------------------- apply_to : Struct.Model.Type -> (Struct.Model.Type, (Cmd Struct.Event.Type)) apply_to model = - case (Array.get 0 model.players) of - Nothing -> (model, Cmd.none) - (Just player) -> - ({model | query_index = 0}, (Comm.GetBattles.request player)) + ( + model, + (Cmd.batch + (Array.toList + (Array.indexedMap + (Comm.GetBattles.request) + model.players + ) + ) + ) + ) diff --git a/src/shared/Action/Ports.elm b/src/shared/Action/Ports.elm index 70d5183..c14d7f4 100644 --- a/src/shared/Action/Ports.elm +++ b/src/shared/Action/Ports.elm @@ -1,8 +1,8 @@ port module Action.Ports exposing (..) port get_params : () -> (Cmd msg) -port params_in : ((Int, String) -> msg) -> (Sub msg) -port set_params : (Int, String) -> (Cmd msg) +port params_in : (String -> msg) -> (Sub msg) +port set_params : (String) -> (Cmd msg) port get_results : () -> (Cmd msg) port results_in : (String -> msg) -> (Sub msg) diff --git a/src/shared/Struct/Flags.elm b/src/shared/Struct/Flags.elm index 29707a9..0347001 100644 --- a/src/shared/Struct/Flags.elm +++ b/src/shared/Struct/Flags.elm @@ -3,6 +3,7 @@ module Struct.Flags exposing Type, get_frequency, get_players, + default, decoder, encode ) @@ -36,6 +37,13 @@ get_frequency flags = flags.frequency get_players : Type -> (List Struct.Player.Type) get_players flags = flags.players +default : Type +default = + { + frequency = 1, + players = [] + } + decoder : (Json.Decode.Decoder Type) decoder = (Json.Decode.map2 diff --git a/www/script/battles.js b/www/script/battles.js index 5e90959..a5b5864 100644 --- a/www/script/battles.js +++ b/www/script/battles.js @@ -8,7 +8,7 @@ tacticians_online.battles = new Object(); tacticians_online.battles.get = function () { - return localStorage.getItem("battles"); + tacticians_online.app.battles_in.send(localStorage.getItem("battles")); } tacticians_online.battles.set = @@ -17,13 +17,6 @@ function (encoded_battles) localStorage.setItem("battles", encoded_battles); } -tacticians_online.battles.read_battles = -function () -{ - tacticians_online.app.battles_in.send(tacticians_online.battles.get()); -} - - tacticians_online.battles.attach_to = function (app) { diff --git a/www/script/params.js b/www/script/params.js index 14d2674..243ca34 100644 --- a/www/script/params.js +++ b/www/script/params.js @@ -5,85 +5,16 @@ var tacticians_online = tacticians_online || new Object(); tacticians_online.params = new Object(); -tacticians_online.params.private = new Object(); -tacticians_online.params.private.frequency = 1; -tacticians_online.params.private.players = ""; - -tacticians_online.params.reset = -function () -{ - localStorage.removeItem("frequency"); - localStorage.removeItem("players"); -} - -tacticians_online.params.load = -function () -{ - tacticians_online.params.private.frequency = - localStorage.getItem("frequency"); - - tacticians_online.params.private.players = localStorage.getItem("players"); - - if (tacticians_online.params.private.frequency == null) - { - tacticians_online.params.private.frequency = 1; - } - - if (tacticians_online.params.private.players == null) - { - tacticians_online.params.private.players = ""; - } -} - -tacticians_online.params.get_frequency = -function () -{ - return tacticians_online.params.private.frequency; -} - -tacticians_online.params.get_players = -function () -{ - return tacticians_online.params.private.players; -} - -tacticians_online.params.set_frequency = -function (frequency) -{ - tacticians_online.params.private.frequency = frequency; - - localStorage.setItem - ( - "frequency", - tacticians_online.params.private.frequency - ); -} - -tacticians_online.params.set_players = -function (players) -{ - tacticians_online.params.private.players = players; - localStorage.setItem("players", tacticians_online.params.private.players); -} - -tacticians_online.params.set_params = -function (params) -{ - var [frequency, players] = params; - tacticians_online.params.set_frequency(frequency); - tacticians_online.params.set_players(players); -} - -tacticians_online.params.js_get = +tacticians_online.params.get = function () { - return {get_frequency(), get_players()}; + tacticians_online.app.battles_in.send(localStorage.getItem("params")); } -tacticians_online.params.get = -function () +tacticians_online.params.set = +function (encoded_params) { - tacticians_online.app.params_in.send(tacticians_online.params.js_get()); + localStorage.setItem("params", encoded_params); } tacticians_online.params.attach_to = @@ -91,6 +22,5 @@ function (app) { app.ports.get_params.subscribe(tacticians_online.params.get); app.ports.set_params.subscribe(tacticians_online.params.set); - app.ports.reset_params.subscribe(tacticians_online.params.reset); } |