Skip to content

Instantly share code, notes, and snippets.

@samanthadoran
Last active March 15, 2016 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samanthadoran/2f12b0dec74fd6946b59 to your computer and use it in GitHub Desktop.
Save samanthadoran/2f12b0dec74fd6946b59 to your computer and use it in GitHub Desktop.
use std::collections::{HashMap, HashSet};
use super::item::Item;
pub struct Character {
pub attributes: Item,
pub inventory: HashMap<String, (Item, i32)>,
pub feats: HashMap<String, HashMap<String, i32>>,
pub scores: HashMap<String, i32>,
pub ability_score_names: HashSet<String>,
pub equipment: HashMap<String, Item>,
}
impl Character {
pub fn new() -> Character {
Character {
attributes: Item::new(),
inventory: HashMap::new(),
feats: HashMap::new(),
scores: HashMap::new(),
ability_score_names: HashSet::new(),
equipment: HashMap::new(),
}
}
//Give the character count number of item.
pub fn modify_inventory(&mut self, item: &Item, mut count: i32) {
let name = item.qualitative_descriptors.get(&"name".to_string()).unwrap();
{
let mut i = self.inventory.entry(name.to_string()).or_insert((item.clone(), 0));
i.1 += count;
count = i.1;
}
if count <= 0 {
self.inventory.remove(&name.to_string());
}
}
pub fn equip(&mut self, item: &Item) {
match item.qualitative_descriptors.get("slot") {
Some(slot) => {
match self.equipment.insert(slot.to_string(), item.clone()) {
Some(i) => self.modify_inventory(&i, 1),
None => {},
}
},
None => {},
};
}
}
use std::collections::HashMap;
#[derive(Clone)]
pub struct Item {
pub qualitative_descriptors: HashMap<String, String>,
pub quantitative_descriptors: HashMap<String, f32>,
}
impl Item {
pub fn new() -> Item {
Item {
qualitative_descriptors: HashMap::new(),
quantitative_descriptors: HashMap::new(),
}
}
//Return a string describing the item
pub fn detail(&self) -> String {
let mut working: String = "".to_string();
&working.push_str("Qualities:\n");
for (name, data) in &self.qualitative_descriptors {
working = working + &format!("\t{}: {}\n", name, data);
}
&working.push_str("Quantities:\n");
for (name, data) in &self.quantitative_descriptors {
working = working + &format!("\t{}: {}\n", name, data);
}
working
}
}
mod character;
mod item;
use item::Item;
use character::Character;
fn main() {
//Some random values for a character
let mut f = Character::new();
f.attributes.quantitative_descriptors.insert("age".to_string(), 22.);
f.attributes.qualitative_descriptors.insert("name".to_string(), "Samantha".to_string());
//Some random values for an item
let mut i = Item::new();
i.qualitative_descriptors.insert("name".to_string(), "Potion".to_string());
i.quantitative_descriptors.insert("Potency".to_string(), 50f32);
let mut equip = Item::new();
equip.qualitative_descriptors.insert("name".to_string(), "helmet".to_string());
equip.qualitative_descriptors.insert("slot".to_string(), "head".to_string());
equip.quantitative_descriptors.insert("AC".to_string(), 2f32);
//Give 10 of the item to the character
f.give_item(&i, 10);
f.equip(&equip);
println!("Welcome to charactersheet");
//List qualities and quantities of character
println!("{}", Item::detail(&f.attributes));
//List inventory of character
println!("Inventory:");
for (name, data) in &f.inventory {
println!("\t{}: #{}", name, data.1);
//This is really hacky and I should feel bad...
let formatted = Item::detail(&data.0).replace("\n", "\n\t").replace("\t", "\t\t");
println!("\n\t\t{}", formatted);
}
println!("Equipment:");
for (slot, data) in &f.equipment {
println!("\t{}:", slot);
//This is really hacky and I should feel bad...
let formatted = Item::detail(&data).replace("\n", "\n\t").replace("\t", "\t\t");
println!("\n\t\t{}", formatted);
}
}
[samanthadoran@samdoran-linux charactersheet]$ cargo run
Running `target/debug/charactersheet`
Welcome to charactersheet
Qualities:
name: Samantha
Quantities:
age: 22
Inventory:
Potion: #10
Qualities:
name: Potion
Quantities:
Potency: 50
Equipment:
head:
Qualities:
slot: head
name: helmet
Quantities:
AC: 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment