summaryrefslogtreecommitdiff
blob: 983043db6ebcb829b072730a3f32a688092f8510 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
module Update.HandleServerReply exposing (apply_to)

-- Elm -------------------------------------------------------------------------
import Array

import Json.Encode

import Http

import List

-- Extension -------------------------------------------------------------------
import Action.Ports

import Struct.BattleSummary
import Struct.Error
import Struct.Event
import Struct.Model
import Struct.Player
import Struct.ServerReply

--------------------------------------------------------------------------------
-- TYPES -----------------------------------------------------------------------
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
-- 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
      let
         players_list = (Array.toList model.players)
      in
         (
            (Action.Ports.set_results
               (Json.Encode.encode
                  0
                  (Json.Encode.list
                     (List.map (Struct.Player.encode) players_list)
                  )
               )
            )
            ::
            (
               (
                  if (List.any (Struct.Player.has_active_battles) players_list)
                  then (Action.Ports.activate_notification ())
                  else (Action.Ports.disable_notification ())
               )
               :: cmds
            )
         )

handle_set_battles : (
      (
         Int,
         (List Struct.BattleSummary.Type),
         (List Struct.BattleSummary.Type),
         (List Struct.BattleSummary.Type)
      ) ->
      (Struct.Model.Type, (List (Cmd Struct.Event.Type))) ->
      (Struct.Model.Type, (List (Cmd Struct.Event.Type)))
   )
handle_set_battles battles current_state =
   let
      (model, cmds) = current_state
      (ix, campaigns, invasions, events) = battles
   in
      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_model =
                  {model |
                     remaining_updates = (model.remaining_updates - 1),
                     players =
                     (Array.set
                        ix
                        (Struct.Player.set_battles
                           campaigns
                           invasions
                           events
                           player
                        )
                        model.players
                     )
                  }
            in
               (
                  updated_model,
                  (maybe_update_storage model cmds)
               )

apply_command : (
      Struct.ServerReply.Type ->
      (Struct.Model.Type, (List (Cmd Struct.Event.Type))) ->
      (Struct.Model.Type, (List (Cmd Struct.Event.Type)))
   )
apply_command command current_state =
   case command of
      Struct.ServerReply.Okay -> current_state
      (Struct.ServerReply.SetBattles battles) ->
         (handle_set_battles battles current_state)

--------------------------------------------------------------------------------
-- EXPORTED --------------------------------------------------------------------
--------------------------------------------------------------------------------
apply_to : (
      Struct.Model.Type ->
      (Result Http.Error (List Struct.ServerReply.Type)) ->
      (Struct.Model.Type, (Cmd Struct.Event.Type))
   )
apply_to model query_result =
   case query_result of
      (Result.Err error) ->
         (
            (Struct.Model.invalidate
               (Struct.Error.new Struct.Error.Networking (toString error))
               model
            ),
            Cmd.none
         )

      (Result.Ok commands) ->
         let
            (new_model, elm_commands) =
               (List.foldl (apply_command) (model, [Cmd.none]) commands)
         in
            (
               new_model,
               (
                  case elm_commands of
                     [] -> Cmd.none
                     [cmd] -> cmd
                     _ -> (Cmd.batch elm_commands)
               )
            )