Files
cursebreaker-parser-rust/cursebreaker-parser/examples/item_database_demo.rs
2026-01-11 02:46:49 +00:00

104 lines
3.6 KiB
Rust

//! Example demonstrating ItemDatabase usage
//!
//! Run with: cargo run --example item_database_demo
use cursebreaker_parser::ItemDatabase;
use std::env;
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("🎮 Cursebreaker Item Database Demo\n");
// Load items from XML
let cb_assets_path = env::var("CB_ASSETS_PATH").unwrap_or_else(|_| "/home/connor/repos/CBAssets".to_string());
let items_path = format!("{}/Data/XMLs/Items/Items.xml", cb_assets_path);
println!("📚 Loading items from: {}", items_path);
let item_db = ItemDatabase::load_from_xml(items_path)?;
println!("✅ Loaded {} items\n", item_db.len());
// Example 1: Get item by ID
println!("=== Example 1: Get Item by ID ===");
if let Some(item) = item_db.get_by_id(150) {
println!("Item ID 150:");
println!(" Name: {}", item.name);
if let Some(desc) = &item.description {
println!(" Description: {}", desc);
}
if let Some(slot) = &item.slot {
println!(" Slot: {}", slot);
}
if let Some(skill) = &item.skill {
println!(" Skill: {}", skill);
}
println!(" Stats: {} stat entries", item.stats.len());
}
println!();
// Example 2: Get items by category
println!("=== Example 2: Get Items by Category ===");
let bows = item_db.get_by_category("bow");
println!("Found {} bows:", bows.len());
for item in bows.iter().take(5) {
println!(" - {} (ID: {})", item.name, item.id);
}
println!();
// Example 3: Get items by slot
println!("=== Example 3: Get Items by Slot ===");
let consumables = item_db.get_by_slot("consumable");
println!("Found {} consumables (showing first 10):", consumables.len());
for item in consumables.iter().take(10) {
let name = &item.name;
let id = item.id;
if let Some(desc) = &item.description {
println!(" - {} (ID: {}) - {}", name, id, desc.chars().take(50).collect::<String>());
} else {
println!(" - {} (ID: {})", name, id);
}
}
println!();
// Example 4: Get items by skill
println!("=== Example 4: Get Items by Skill ===");
let magic_items = item_db.get_by_skill("magic");
println!("Found {} magic items:", magic_items.len());
for item in magic_items.iter().take(5) {
println!(" - {} (ID: {}, Level: {:?})",
item.name, item.id, item.level);
}
println!();
// Example 5: Statistics
println!("=== Example 5: Database Statistics ===");
let weapons = item_db.get_by_slot("weapon");
let armor = item_db.get_by_slot("armor");
let consumables = item_db.get_by_slot("consumable");
let trinkets = item_db.get_by_slot("trinket");
println!("Item Distribution by Slot:");
println!(" Weapons: {}", weapons.len());
println!(" Armor: {}", armor.len());
println!(" Consumables: {}", consumables.len());
println!(" Trinkets: {}", trinkets.len());
println!();
// Example 6: Prepare for SQL (showing how it would be used)
println!("=== Example 6: SQL Serialization ===");
let sql_data = item_db.prepare_for_sql();
println!("Prepared {} items for SQL insertion", sql_data.len());
println!("Sample SQL inserts (first 3):");
for (id, name, json) in sql_data.iter().take(3) {
let json_preview = if json.len() > 100 {
format!("{}...", &json[..100])
} else {
json.clone()
};
println!(" INSERT INTO items (id, name, data) VALUES ({}, '{}', '{}');",
id, name, json_preview);
}
println!("\n✨ Demo complete!");
Ok(())
}