refactor: this is so fun
ci/woodpecker/push/lint Pipeline was successful Details
ci/woodpecker/push/clippy Pipeline failed Details
ci/woodpecker/push/build Pipeline was successful Details

user-register
Jef Roosens 2023-05-15 15:22:43 +02:00
parent c966529a2b
commit f8ce315d8e
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 41 additions and 38 deletions

View File

@ -44,28 +44,25 @@ pub struct Resource {
impl Resource { impl Resource {
pub fn condensed_hours(&self) -> Vec<(&HourBlock, Duration)> { pub fn condensed_hours(&self) -> Vec<(&HourBlock, Duration)> {
let mut start_hour_opt: Option<&HourBlock> = None; if self.hours.is_empty() {
let mut duration = Duration::seconds(0); return Default::default();
}
let mut start_hour = self.hours.first().unwrap();
let mut duration = Duration::minutes(start_hour.granularity.into());
let mut out: Vec<(&HourBlock, Duration)> = Default::default(); let mut out: Vec<(&HourBlock, Duration)> = Default::default();
for hour in &self.hours { for hour in self.hours.iter().skip(1) {
if let Some(start_hour) = start_hour_opt { if hour.state == start_hour.state {
if hour.state == start_hour.state { duration = duration + Duration::minutes(hour.granularity.into());
duration = duration + Duration::minutes(hour.granularity.into()); } else {
} else { out.push((start_hour, duration));
out.push((start_hour, duration)); start_hour = &hour;
start_hour_opt = Some(hour);
duration = Duration::minutes(hour.granularity.into());
}
} else if hour.state == 1 {
start_hour_opt = Some(hour);
duration = Duration::minutes(hour.granularity.into()); duration = Duration::minutes(hour.granularity.into());
} }
} }
if let Some(start_hour) = start_hour_opt { out.push((start_hour, duration));
out.push((start_hour, duration));
}
out out
} }

View File

@ -1,32 +1,24 @@
use crate::commands::EmbedField;
use crate::{Context, Error}; use crate::{Context, Error};
use affluences_api::Resource;
use chrono::NaiveDate; use chrono::NaiveDate;
use uuid::{uuid, Uuid}; use uuid::{uuid, Uuid};
const STERRE_BIB_ID: Uuid = uuid!("4737e57a-ee05-4f7b-901a-7bb541eeb297"); const STERRE_BIB_ID: Uuid = uuid!("4737e57a-ee05-4f7b-901a-7bb541eeb297");
const TIME_FORMAT: &str = "%H:%M"; const TIME_FORMAT: &str = "%H:%M";
/// List available timeslots for day fn resource_to_embed_field(resource: Resource) -> EmbedField {
#[poise::command(prefix_command, slash_command)] let available_hours = resource.condensed_available_hours();
pub async fn available(ctx: Context<'_>, date: NaiveDate) -> Result<(), Error> {
let client = &ctx.data().client;
let resources = client.available(STERRE_BIB_ID, date, 1).await?;
let mut fields: Vec<(String, String, bool)> = Default::default();
for resource in &resources { if available_hours.is_empty() {
let available_hours = resource.condensed_available_hours(); (
resource.resource_name.clone(),
if available_hours.is_empty() { "Nothing available.".to_string(),
fields.push(( false,
resource.resource_name.clone(), )
"Nothing available.".to_string(), } else {
false, (
));
continue;
}
fields.push((
resource.resource_name.clone(), resource.resource_name.clone(),
available_hours available_hours
.into_iter() .into_iter()
@ -42,13 +34,25 @@ pub async fn available(ctx: Context<'_>, date: NaiveDate) -> Result<(), Error> {
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join("\n"), .join("\n"),
false, false,
)); )
} }
}
/// List available timeslots for day
#[poise::command(prefix_command, slash_command)]
pub async fn available(ctx: Context<'_>, date: NaiveDate) -> Result<(), Error> {
let client = &ctx.data().client;
let resources = client.available(STERRE_BIB_ID, date, 1).await?;
ctx.send(|f| { ctx.send(|f| {
f.embed(|e| { f.embed(|e| {
e.description(format!("Available booking dates for {}.", date)) e.description(format!("Available booking dates for {}.", date))
.fields(fields) .fields(
resources
.into_iter()
.map(resource_to_embed_field)
.collect::<Vec<EmbedField>>(),
)
}) })
}) })
.await?; .await?;

View File

@ -3,6 +3,8 @@ mod minecraft;
use crate::{Context, Data, Error}; use crate::{Context, Data, Error};
type EmbedField = (String, String, bool);
pub fn commands() -> Vec<poise::structs::Command<Data, Error>> { pub fn commands() -> Vec<poise::structs::Command<Data, Error>> {
vec![help(), affluence::available(), minecraft::ping_mc()] vec![help(), affluence::available(), minecraft::ping_mc()]
} }