@@ -11,14 +11,11 @@ defmodule GalaxiesWeb.ResourcesLive do
11
11
_ = Planets . process_planet_events ( socket . assigns . current_planet . id )
12
12
build_queue = Planets . get_building_queue ( socket . assigns . current_planet . id )
13
13
planet_buildings = Accounts . get_planet_resource_buildings ( socket . assigns . current_planet )
14
- building_timers = timers_from_build_queue ( build_queue )
15
- schedule_next_timer_update ( )
16
14
17
15
{ :ok ,
18
16
socket
19
17
|> assign ( :build_queue , build_queue )
20
- |> assign ( :planet_buildings , planet_buildings )
21
- |> assign ( :building_timers , building_timers ) }
18
+ |> assign ( :planet_buildings , planet_buildings ) }
22
19
end
23
20
24
21
defp timers_from_build_queue ( [ ] ) , do: % { }
@@ -80,7 +77,13 @@ defmodule GalaxiesWeb.ResourcesLive do
80
77
{ hd ( @ build_queue ) . level }
81
78
</ td >
82
79
< td class = "whitespace-nowrap px-3 py-4 text-sm text-gray-500 " >
83
- { format_timer ( @ building_timers [ hd ( @ build_queue ) . building_id ] ) }
80
+ < span
81
+ id = "build-queue-countdown "
82
+ phx-hook = "Countdown "
83
+ data-target = { hd ( @ build_queue ) . completed_at }
84
+ >
85
+ { hd ( @ build_queue ) . completed_at }
86
+ </ span >
84
87
</ td >
85
88
< td class = "relative whitespace-nowrap py-4 pl-3 pr-4 text-right text-sm font-medium sm:pr-6 lg:pr-8 " >
86
89
< a href = "# " class = "text-indigo-600 hover:text-indigo-900 " >
@@ -159,43 +162,54 @@ defmodule GalaxiesWeb.ResourcesLive do
159
162
"""
160
163
end
161
164
162
- def handle_info ( :update_timers , socket ) do
163
- num_timers = Enum . count ( socket . assigns . building_timers )
164
-
165
- building_timers =
166
- socket . assigns . building_timers
167
- |> Enum . reduce ( % { } , fn
168
- { _building_id , secs } , acc when secs <= 1 ->
169
- acc
170
-
171
- { building_id , seconds } , acc ->
172
- Map . put ( acc , building_id , seconds - 1 )
173
- end )
174
-
175
- socket =
176
- if Enum . count ( building_timers ) != num_timers do
177
- # building timer reached zero, meaning some building was upgraded
178
- # re-fetch planet_buildings building queue and update building timers
179
- current_planet = socket . assigns . current_planet
180
- _ = Planets . process_planet_events ( current_planet . id )
181
- build_queue = Planets . get_building_queue ( current_planet . id )
182
- planet_buildings = Accounts . get_planet_resource_buildings ( current_planet )
183
-
184
- building_timers = timers_from_build_queue ( build_queue )
185
-
186
- socket
187
- |> assign ( :build_queue , build_queue )
188
- |> assign ( :building_timers , building_timers )
189
- |> assign ( :planet_buildings , planet_buildings )
190
- else
191
- socket
192
- |> assign ( :building_timers , building_timers )
193
- end
194
-
195
- # no need for periodic ticks if the current building timers Map is empty
196
- unless Enum . empty? ( socket . assigns . building_timers ) do
197
- schedule_next_timer_update ( )
198
- end
165
+ def handle_info ( event , socket ) when event in [ :update_timers , "update_timers" ] do
166
+ dbg ( :ping )
167
+
168
+ # building timer reached zero, meaning some building was upgraded
169
+ # re-fetch planet_buildings building queue and update building timers
170
+ current_planet = socket . assigns . current_planet
171
+ _ = Planets . process_planet_events ( current_planet . id )
172
+ build_queue = Planets . get_building_queue ( current_planet . id )
173
+
174
+ socket
175
+ |> assign ( :build_queue , build_queue )
176
+
177
+ # num_timers = Enum.count(socket.assigns.building_timers)
178
+
179
+ # building_timers =
180
+ # socket.assigns.building_timers
181
+ # |> Enum.reduce(%{}, fn
182
+ # {_building_id, secs}, acc when secs <= 1 ->
183
+ # acc
184
+
185
+ # {building_id, seconds}, acc ->
186
+ # Map.put(acc, building_id, seconds - 1)
187
+ # end)
188
+
189
+ # socket =
190
+ # if Enum.count(building_timers) != num_timers do
191
+ # # building timer reached zero, meaning some building was upgraded
192
+ # # re-fetch planet_buildings building queue and update building timers
193
+ # current_planet = socket.assigns.current_planet
194
+ # _ = Planets.process_planet_events(current_planet.id)
195
+ # build_queue = Planets.get_building_queue(current_planet.id)
196
+ # planet_buildings = Accounts.get_planet_resource_buildings(current_planet)
197
+
198
+ # building_timers = timers_from_build_queue(build_queue)
199
+
200
+ # socket
201
+ # |> assign(:build_queue, build_queue)
202
+ # |> assign(:building_timers, building_timers)
203
+ # |> assign(:planet_buildings, planet_buildings)
204
+ # else
205
+ # socket
206
+ # |> assign(:building_timers, building_timers)
207
+ # end
208
+
209
+ # # no need for periodic ticks if the current building timers Map is empty
210
+ # unless Enum.empty?(socket.assigns.building_timers) do
211
+ # schedule_next_timer_update()
212
+ # end
199
213
200
214
{ :noreply , socket }
201
215
end
@@ -222,16 +236,16 @@ defmodule GalaxiesWeb.ResourcesLive do
222
236
223
237
building_timers = timers_from_build_queue ( build_queue )
224
238
225
- if Enum . empty? ( socket . assigns . building_timers ) do
226
- # building timers was previously empty so we need to restart the periodic timer update
227
- schedule_next_timer_update ( )
228
- end
239
+ # if Enum.empty?(socket.assigns.building_timers) do
240
+ # # building timers was previously empty so we need to restart the periodic timer update
241
+ # # schedule_next_timer_update()
242
+ # end
229
243
230
244
{ :noreply ,
231
245
socket
232
246
|> assign ( :build_queue , build_queue )
233
247
|> assign ( :current_planet , current_planet )
234
- |> assign ( :building_timers , building_timers )
248
+ # |> assign(:building_timers, building_timers)
235
249
|> assign ( :planet_buildings , planet_buildings ) }
236
250
237
251
{ :error , error } ->
@@ -240,7 +254,7 @@ defmodule GalaxiesWeb.ResourcesLive do
240
254
end
241
255
242
256
defp schedule_next_timer_update ( ) do
243
- :erlang . send_after ( @ timer_update_interval , self ( ) , :update_timers )
257
+ # :erlang.send_after(@timer_update_interval, self(), :update_timers)
244
258
end
245
259
246
260
# defp list_replace(list, to_replace, acc \\ [])
0 commit comments