73 lines
2.2 KiB
Rust
73 lines
2.2 KiB
Rust
//src/model.rs
|
|
use rusqlite::{params, Connection, Result as SqlResult};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
pub struct AiSystem {
|
|
pub personality: Personality,
|
|
pub relationship: Relationship,
|
|
pub environment: Environment,
|
|
pub messaging: Messaging,
|
|
}
|
|
|
|
impl AiSystem {
|
|
pub fn save_to_db(&self, conn: &Connection) -> SqlResult<()> {
|
|
conn.execute(
|
|
"CREATE TABLE IF NOT EXISTS ai_state (id INTEGER PRIMARY KEY, json TEXT)",
|
|
[],
|
|
)?;
|
|
|
|
let json_data = serde_json::to_string(self).map_err(|e| {
|
|
rusqlite::Error::ToSqlConversionFailure(Box::new(e))
|
|
})?;
|
|
|
|
conn.execute(
|
|
"INSERT OR REPLACE INTO ai_state (id, json) VALUES (?1, ?2)",
|
|
params![1, json_data],
|
|
)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn load_from_db(conn: &Connection) -> SqlResult<Self> {
|
|
let mut stmt = conn.prepare("SELECT json FROM ai_state WHERE id = ?1")?;
|
|
let json: String = stmt.query_row(params![1], |row| row.get(0))?;
|
|
|
|
// ここも serde_json のエラーを map_err で変換
|
|
let system: AiSystem = serde_json::from_str(&json).map_err(|e| {
|
|
rusqlite::Error::FromSqlConversionFailure(0, rusqlite::types::Type::Text, Box::new(e))
|
|
})?;
|
|
|
|
Ok(system)
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
pub struct Personality {
|
|
pub kind: String, // e.g., "positive", "negative", "neutral"
|
|
pub strength: f32, // 0.0 - 1.0
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
pub struct Relationship {
|
|
pub trust: f32, // 0.0 - 1.0
|
|
pub intimacy: f32, // 0.0 - 1.0
|
|
pub curiosity: f32, // 0.0 - 1.0
|
|
pub threshold: f32, // if sum > threshold, allow messaging
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
pub struct Environment {
|
|
pub luck_today: f32, // 0.1 - 1.0
|
|
pub luck_history: Vec<f32>, // last 3 values
|
|
pub level: i32, // current mental strength level
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
pub struct Messaging {
|
|
pub enabled: bool,
|
|
pub schedule_time: Option<String>, // e.g., "08:00"
|
|
pub decay_rate: f32, // how quickly emotion fades (0.0 - 1.0)
|
|
pub templates: Vec<String>, // message template variations
|
|
}
|