Calculate winner based on daily messages

This commit is contained in:
2024-10-30 15:28:50 +01:00
parent a3af9b7817
commit 78c84d5430
4 changed files with 55 additions and 22 deletions

View File

@@ -5,24 +5,29 @@ defmodule MihainatorWeb.ResultComponent do
@impl true
def update(assigns, socket) do
socket = assign(socket, assigns)
months = get_one_day_for_each_month(assigns.calendar_dates)
{:ok, socket}
end
socket = assign(socket, assigns: assigns, months: months)
@impl true
def mount(socket) do
{:ok, socket}
end
def time_button(assigns) do
{day, outgoing} = assigns.day
{day, info} = assigns.day
day = get_date(day)
{out_messages, in_messages} =
Enum.split_with(info, fn %{direction: direction} -> direction == "out" end)
length_out = length(out_messages)
length_in = length(in_messages)
state =
case outgoing do
true -> "bg-green-300"
false -> "bg-red-300"
_ -> ""
cond do
length_in < length_out -> "bg-green-300"
length_in > length_out -> "bg-red-300"
true -> ""
end
assigns = assign(assigns, state: state, day: day.day)
@@ -35,13 +40,9 @@ defmodule MihainatorWeb.ResultComponent do
end
def month(assigns) do
days_of_month = assigns.days_of_month
day = get_date(assigns.day)
day =
Enum.at(days_of_month, 0)
|> elem(0)
formatted_month = Calendar.strftime(day, "%B")
formatted_month = day |> Calendar.strftime("%B")
assigns = assign(assigns, formatted_month: formatted_month, year: day.year)
@@ -54,4 +55,26 @@ defmodule MihainatorWeb.ResultComponent do
</div>
"""
end
def get_days_of_month(socket, first_of_month) do
first_of_month = get_date(first_of_month)
start_of_range = NaiveDateTime.to_date(first_of_month) |> Date.shift(day: -1)
end_of_range = Date.end_of_month(first_of_month) |> Date.shift(day: 1)
Map.filter(socket.assigns.calendar_dates, fn {date, _} ->
date = get_date(date)
Date.after?(date, start_of_range) and Date.before?(date, end_of_range)
end)
end
defp get_one_day_for_each_month(calendar_dates) do
Map.keys(calendar_dates)
|> Enum.sort()
|> Enum.filter(fn x -> String.ends_with?(x, "-01") end)
end
defp get_date(date) do
Timex.parse!(date, "{YYYY}-{M}-{D}")
end
end