Updated motivations to use new randoms
+ Handle task failures more gracefully
This commit is contained in:
10 files changed
+76
-70
No files matched your search
@@ -1,19 +1,21 @@
|
||||
use crate::album_manager::{AlbumManager, AlbumQuery, Image};
|
||||
use crate::error::Error;
|
||||
use crate::models::random::RandomConfig;
|
||||
use j_db::database::Database;
|
||||
use j_db::error::JDbError;
|
||||
use j_db::model::JdbModel;
|
||||
use rand::prelude::{IteratorRandom, SliceRandom};
|
||||
use rand::prelude::SliceRandom;
|
||||
use rand::thread_rng;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt::Debug;
|
||||
|
||||
pub const GOAL_RANDOM_GROUP: &str = "Goal";
|
||||
pub const ACTION_RANDOM_GROUP: &str = "Action";
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct MotivationConfig {
|
||||
id: Option<u64>,
|
||||
pub album: Vec<u64>,
|
||||
pub action: Vec<String>,
|
||||
pub goal: Vec<String>,
|
||||
}
|
||||
|
||||
impl JdbModel for MotivationConfig {
|
||||
@@ -42,8 +44,6 @@ impl MotivationConfig {
|
||||
.unwrap_or(Self {
|
||||
id: None,
|
||||
album: vec![],
|
||||
action: vec![],
|
||||
goal: vec![],
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -56,24 +56,6 @@ impl MotivationConfig {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn add_action(db: &Database, action: &str) -> Result<(), Error> {
|
||||
let mut motivation = Self::get_motivation_config(db)?;
|
||||
motivation.action.push(action.to_string());
|
||||
|
||||
db.insert(motivation)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn add_goal(db: &Database, goal: &str) -> Result<(), Error> {
|
||||
let mut motivation = Self::get_motivation_config(db)?;
|
||||
motivation.goal.push(goal.to_string());
|
||||
|
||||
db.insert(motivation)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn generate_motivation(
|
||||
db: &Database,
|
||||
picox: &AlbumManager,
|
||||
@@ -112,9 +94,11 @@ impl MotivationConfig {
|
||||
.choose(&mut thread_rng())
|
||||
.ok_or(Error::NoAlbumFound)?;
|
||||
|
||||
let action = motivation.action.iter().choose(&mut thread_rng()).unwrap();
|
||||
let actions = RandomConfig::get_random(db, ACTION_RANDOM_GROUP)?.unwrap_or_default();
|
||||
let goals = RandomConfig::get_random(db, GOAL_RANDOM_GROUP)?.unwrap_or_default();
|
||||
|
||||
let goal = motivation.goal.iter().choose(&mut thread_rng()).unwrap();
|
||||
let action = actions.get_response(db)?.unwrap_or_default();
|
||||
let goal = goals.get_response(db)?.unwrap_or_default();
|
||||
|
||||
let image = picox.get_image_by_id(*image).await.unwrap();
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ impl JdbModel for Random {
|
||||
!self.response.eq_ignore_ascii_case(&other.response)
|
||||
}
|
||||
}
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
|
||||
pub struct RandomConfig {
|
||||
id: Option<u64>,
|
||||
pub name: String,
|
||||
|
||||
+13
-2
@@ -173,7 +173,12 @@ impl Task {
|
||||
}
|
||||
TaskType::HandleReload => {
|
||||
println!("Reloading config...");
|
||||
global_data.reload().await.unwrap();
|
||||
let res = global_data.reload().await;
|
||||
|
||||
match res {
|
||||
Ok(_) => println!("Finished reloading config!"),
|
||||
Err(err) => println!("Error reloading config: {:?}", err),
|
||||
}
|
||||
|
||||
Task::add_task(
|
||||
&global_data.db,
|
||||
@@ -183,7 +188,13 @@ impl Task {
|
||||
}
|
||||
TaskType::RestockShop => {
|
||||
println!("Restocking Shop...");
|
||||
restock_shop(ctx, global_data).await.unwrap();
|
||||
let res = restock_shop(ctx, global_data).await;
|
||||
|
||||
match res {
|
||||
Ok(_) => println!("Finished restocking shop!"),
|
||||
Err(err) => println!("Error restocking shop: {:?}", err),
|
||||
}
|
||||
|
||||
Task::add_task(
|
||||
&global_data.db,
|
||||
TaskType::RestockShop,
|
||||
|
||||
Reference in new issue
Block a user