From 7a516f37b48f23dee8ceb443db6977c20f2e4a52 Mon Sep 17 00:00:00 2001 From: levin Date: Sun, 20 Oct 2024 21:19:57 +0000 Subject: [PATCH] values in both directions --- .../live/pages/live_home_page.ex | 135 ++++++------------ 1 file changed, 44 insertions(+), 91 deletions(-) diff --git a/lib/algground_web/live/pages/live_home_page.ex b/lib/algground_web/live/pages/live_home_page.ex index 3f11e1c..c7cd43d 100644 --- a/lib/algground_web/live/pages/live_home_page.ex +++ b/lib/algground_web/live/pages/live_home_page.ex @@ -26,75 +26,24 @@ defmodule AlggroundWeb.LiveHomePage do region: region.region, groundwater: trunc(:rand.uniform() * 100), rainfall: trunc(:rand.uniform() * 100), - reservoir: trunc(:rand.uniform() * 10_000_000), + reservoir: trunc(:rand.uniform() * 1_000), image: region.image } end) - generated_groundwaterlevels = - 1..40 - |> Enum.to_list() - |> Enum.map(fn _ -> trunc(:rand.uniform() * 100) end) - - generated_rainfalllevels = - 1..40 - |> Enum.to_list() - |> Enum.map(fn _ -> trunc(:rand.uniform() * 100) end) - - generated_reservoirlevels = - 1..40 - |> Enum.to_list() - |> Enum.map(fn _ -> trunc(:rand.uniform() * 10_000_000) end) - {:ok, socket - |> assign(:groundwater_levels, generated_groundwaterlevels) - |> assign(:rainfall_levels, generated_rainfalllevels) - |> assign(:reservoir_levels, generated_reservoirlevels) + |> assign(:groundwater_levels, Enum.map(regions, & &1.groundwater)) + |> assign(:rainfall_levels, Enum.map(regions, & &1.rainfall)) + |> assign(:reservoir_levels, Enum.map(regions, & &1.reservoir)) |> assign(:date_start, Datex.Date.today()) |> assign(:date_end, Datex.Date.add(Datex.Date.today(), 92)) |> assign(:regions, regions) - |> assign(:groundwater, trunc(:rand.uniform() * 100)) - |> assign(:rainfall, trunc(:rand.uniform() * 100)) - |> assign(:reservoirs, trunc(:rand.uniform() * 10_000_000)) |> assign(:display_groundwater, true) |> assign(:display_rainfall, false) |> assign(:display_reservoir, false)} end - def handle_info("new_values", socket) do - groundwater = trunc(:rand.uniform() * 100) - rainfall = trunc(:rand.uniform() * 100) - reservoir = trunc(:rand.uniform() * 10_000_000) - - regions = - Enum.map(@region_names, fn region -> - %{ - region: region.region, - groundwater: groundwater, - rainfall: rainfall, - reservoir: reservoir, - image: region.image - } - end) - - new_groundwaters = maybe_add_value(socket.assigns.groundwater_levels ++ [groundwater]) - new_rainfalls = maybe_add_value(socket.assigns.rainfall_levels ++ [rainfall]) - new_reservoirs = maybe_add_value(socket.assigns.reservoir_levels ++ [reservoir]) - - {:noreply, - socket - |> assign(:groundwater_levels, new_groundwaters) - |> assign(:rainfall_levels, new_rainfalls) - |> assign(:reservoir_levels, new_reservoirs) - |> assign(:regions, regions) - |> assign(:date_start, Datex.Date.add(socket.assigns.date_start, 92)) - |> assign(:date_end, Datex.Date.add(socket.assigns.date_end, 92)) - |> assign(:groundwater, trunc(:rand.uniform() * 100)) - |> assign(:rainfall, trunc(:rand.uniform() * 100)) - |> assign(:reservoirs, trunc(:rand.uniform() * 10_000_000))} - end - def handle_event("display_groundwater", _params, socket) do {:noreply, socket @@ -119,7 +68,7 @@ defmodule AlggroundWeb.LiveHomePage do def handle_event("backward", _params, socket) do groundwater = trunc(:rand.uniform() * 100) rainfall = trunc(:rand.uniform() * 100) - reservoir = trunc(:rand.uniform() * 10_000_000) + reservoir = trunc(:rand.uniform() * 1_000) regions = Enum.map(@region_names, fn region -> @@ -132,9 +81,9 @@ defmodule AlggroundWeb.LiveHomePage do } end) - new_groundwaters = maybe_add_value(socket.assigns.groundwater_levels ++ [groundwater]) - new_rainfalls = maybe_add_value(socket.assigns.rainfall_levels ++ [rainfall]) - new_reservoirs = maybe_add_value(socket.assigns.reservoir_levels ++ [reservoir]) + new_groundwaters = maybe_remove_value([groundwater] ++ socket.assigns.groundwater_levels) + new_rainfalls = maybe_remove_value([rainfall] ++ socket.assigns.rainfall_levels) + new_reservoirs = maybe_remove_value([reservoir] ++ socket.assigns.reservoir_levels) {:noreply, socket @@ -143,16 +92,13 @@ defmodule AlggroundWeb.LiveHomePage do |> assign(:reservoir_levels, new_reservoirs) |> assign(:regions, regions) |> assign(:date_start, Datex.Date.add(socket.assigns.date_start, 92)) - |> assign(:date_end, Datex.Date.add(socket.assigns.date_end, 92)) - |> assign(:groundwater, trunc(:rand.uniform() * 100)) - |> assign(:rainfall, trunc(:rand.uniform() * 100)) - |> assign(:reservoirs, trunc(:rand.uniform() * 10_000_000))} + |> assign(:date_end, Datex.Date.add(socket.assigns.date_end, 92))} end def handle_event("forward", _params, socket) do groundwater = trunc(:rand.uniform() * 100) rainfall = trunc(:rand.uniform() * 100) - reservoir = trunc(:rand.uniform() * 10_000_000) + reservoir = trunc(:rand.uniform() * 1_000) regions = Enum.map(@region_names, fn region -> @@ -176,10 +122,7 @@ defmodule AlggroundWeb.LiveHomePage do |> assign(:reservoir_levels, new_reservoirs) |> assign(:regions, regions) |> assign(:date_start, Datex.Date.add(socket.assigns.date_start, 92)) - |> assign(:date_end, Datex.Date.add(socket.assigns.date_end, 92)) - |> assign(:groundwater, trunc(:rand.uniform() * 100)) - |> assign(:rainfall, trunc(:rand.uniform() * 100)) - |> assign(:reservoirs, trunc(:rand.uniform() * 10_000_000))} + |> assign(:date_end, Datex.Date.add(socket.assigns.date_end, 92))} end def render(assigns) do @@ -203,10 +146,10 @@ defmodule AlggroundWeb.LiveHomePage do

- in <%= Enum.random(@regions).region %> + in <%= get_region(@regions, "Albufeira").region %>

- <%= display_groundwater(assigns) %> + <%= display_groundwater(get_region(@regions, "Albufeira")) %>

Ground Water Level @@ -219,16 +162,16 @@ defmodule AlggroundWeb.LiveHomePage do

Rainfall

-

- <%= display_rainfall(assigns) %> +

+ <%= display_rainfall(get_region(@regions, "Albufeira")) %>

Reservoirs

-

- <%= display_reservoirs(assigns) %> +

+ <%= display_reservoirs(get_region(@regions, "Albufeira")) %>

@@ -324,21 +267,21 @@ defmodule AlggroundWeb.LiveHomePage do assigns.groundwater >= 150 -> ~H"""

- <%= @groundwater %>m + <%= @groundwater %> m

""" assigns.groundwater < 150 and assigns.groundwater >= 50 -> ~H""" -

- <%= @groundwater %>m +

+ <%= @groundwater %> m

""" assigns.groundwater < 50 -> ~H""" -

- <%= @groundwater %>m +

+ <%= @groundwater %> m

""" end @@ -349,21 +292,21 @@ defmodule AlggroundWeb.LiveHomePage do assigns.rainfall >= 80 -> ~H"""

- <%= @rainfall %>ml + <%= @rainfall %> ml

""" assigns.rainfall < 80 and assigns.rainfall >= 30 -> ~H"""

- <%= @rainfall %>ml + <%= @rainfall %> ml

""" assigns.rainfall < 30 -> ~H"""

- <%= @rainfall %>ml + <%= @rainfall %> ml

""" end @@ -371,32 +314,42 @@ defmodule AlggroundWeb.LiveHomePage do defp display_reservoirs(assigns) do cond do - assigns.reservoirs >= 6_000_000 -> + assigns.reservoir >= 6_000_000 -> ~H"""

- <%= @reservoirs %>10⁶m³ + <%= @reservoir %> 10⁶km³

""" - assigns.reservoirs < 6_000_000 and assigns.reservoirs >= 1_200_000 -> + assigns.reservoir < 6_000_000 and assigns.reservoir >= 1_200_000 -> ~H"""

- <%= @reservoirs %> 10⁶m³ + <%= @reservoir %> 10⁶km³

""" - assigns.reservoirs < 1_200_000 -> + assigns.reservoir < 1_200_000 -> ~H"""

- <%= @reservoirs %>10⁶m³ + <%= @reservoir %> 10⁶km³

""" end end - defp maybe_add_value(measurements) when length(measurements) > 40 do - Enum.drop(measurements, 1) - end + defp maybe_add_value(measurements) when length(measurements) > 15, + do: Enum.drop(measurements, 1) defp maybe_add_value(measurements), do: measurements + + defp maybe_remove_value(measurements) when length(measurements) > 15 do + measurements + |> Enum.reverse() + |> Enum.drop(1) + |> Enum.reverse() + end + + defp maybe_remove_value(measurements), do: measurements + + defp get_region(regions, region), do: List.first(Enum.filter(regions, &(&1.region == region))) end