feat: Quest publication features for CLI

- Added "quest daily" command
- Added "quest publish" command
This commit is contained in:
Alexey 2025-12-01 16:43:58 +03:00
commit a0bec4003c
3 changed files with 294 additions and 3 deletions

View file

@ -4,6 +4,7 @@ use clap::{Parser,Subcommand,Args,ValueEnum};
use serde::Deserialize;
use squad_quest::{config::Config,quest::{Quest,QuestDifficulty as LibQuestDifficulty}};
use toml::value::Date;
use chrono::{Datelike, NaiveDate, Utc};
#[derive(Deserialize)]
struct DateWrapper {
@ -68,6 +69,10 @@ enum QuestCommands {
Update(QuestUpdateArgs),
/// Delete quest
Delete(QuestDeleteArgs),
/// Make certain quests public
Daily,
/// Publish quest with specified id
Publish(QuestPublishArgs),
}
#[derive(Args)]
@ -78,9 +83,7 @@ struct QuestListArgs {
}
#[derive(Args)]
struct QuestCreateArgs {
/// Difficulty of the quest
#[arg(value_enum)]
struct QuestCreateArgs { /// Difficulty of the quest #[arg(value_enum)]
difficulty: QuestDifficulty,
/// Reward for the quest
reward: u32,
@ -137,6 +140,15 @@ struct QuestDeleteArgs {
id: u16
}
#[derive(Args)]
struct QuestPublishArgs {
/// Id of the quest to publish
id: u16,
/// Make it non-public instead
#[arg(long,short)]
reverse: bool,
}
fn print_quest_short(quest: &Quest) {
println!("Quest #{}: {}", quest.id, quest.name);
}
@ -241,6 +253,50 @@ fn main() {
Err(error) => eprintln!("Error deleting quest #{}: {}", args.id, error),
}
},
QuestCommands::Daily => {
let mut quests = config.load_quests();
let today: NaiveDate = Utc::now().date_naive();
let toml_today = Date {
year: today.year() as u16,
month: today.month() as u8,
day: today.day() as u8
};
let path = config.full_quests_path();
for quest in quests.iter_mut().filter(|q| !q.public && q.available_on.is_some_and(|date| date.le(&toml_today))) {
println!("Quest #{} will be published.", quest.id);
quest.public = true;
if let Err(error) = quest.save(path.clone()) {
eprintln!("Error while saving quest: {error}.");
}
}
},
QuestCommands::Publish(args) => {
let mut quests = config.load_quests();
let quest = quests.iter_mut().find(|q| q.id == args.id);
let path = config.full_quests_path();
match quest {
Some(quest) => {
let not_str = if args.reverse {" not "} else {" "};
if quest.public != args.reverse {
println!("Quest #{} is already{}public", quest.id, not_str);
return;
}
quest.public = !args.reverse;
if let Err(error) = quest.save(path) {
eprintln!("Error while saving quest: {error}.");
};
},
None => {
eprintln!("Error: couldn't find quest with id {}.", args.id);
}
}
}
}
}
}