From 7d82a7bcfc94cd939e02e9d100399d2aac592215 Mon Sep 17 00:00:00 2001 From: insects Date: Tue, 4 Feb 2025 21:50:28 +0100 Subject: [PATCH] properly handle uptime --- src/data.rs | 17 +++++++++++++++++ src/forecast.rs | 2 +- src/main.rs | 19 +++++++++++++++---- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/data.rs b/src/data.rs index 91fcafd..433a15e 100644 --- a/src/data.rs +++ b/src/data.rs @@ -122,6 +122,23 @@ impl<'a> CombinedFish<'a> { .unwrap(); data.forecasts.get(&spot.forecast_id) } + + pub fn is_in_correct_weather(&self, forecast: &Forecast) -> bool { + if self.entry.weather_set.is_empty() { + return true; + } + let cur_weather = forecast.weather_now(); + self.entry + .weather_set + .iter() + .any(|ws| ws == &cur_weather.weather_id) + } + + pub fn is_up(&self, data: &Data) -> bool { + let forecast = self.get_forecast(data); + self.is_in_time_range() + && forecast.is_none_or(|forecast| self.is_in_correct_weather(forecast)) + } } impl Data { diff --git a/src/forecast.rs b/src/forecast.rs index 1f762b6..7aeddd0 100644 --- a/src/forecast.rs +++ b/src/forecast.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use chrono::{DateTime, NaiveTime, Timelike, Utc}; +use chrono::{DateTime, Timelike, Utc}; /// A forecast is used to divine specific weather patterns for a zone. #[derive(Debug)] diff --git a/src/main.rs b/src/main.rs index 43ff07a..8bd422e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use std::sync::Arc; use axum::{extract::State, http::StatusCode, response::IntoResponse, routing::get, Router}; -use data::Data; +use data::{CombinedFish, Data}; use maud::{html, Markup}; pub mod clock; @@ -36,12 +36,23 @@ where #[axum::debug_handler] async fn main_handler(state: State>) -> Result { let meta = state.data.fish_with_meta(); - let values = meta.values(); + let mut values: Vec<(&CombinedFish, bool)> = meta + .values() + .filter_map(|fish| { + if fish.entry.big_fish { + let is_up = fish.is_up(&state.data); + Some((fish, is_up)) + } else { + None + } + }) + .collect(); + values.sort_by(|(afish, aup), (bfish, bup)| bup.cmp(aup)); Ok(html! { h1 { "Hello! Current ET " (clock::get_current_eorzea_date().format("%H:%M")) } - @for fish in values { + @for (fish, is_up) in values { li { - @if fish.is_in_time_range() { + @if is_up { "Up! " } (fish.meta.name_en)