فهرست منبع

Added POC for loading player by name

Oops, there's a bug where we don't check for the Object.kind (So right
now anything else could jump in as the player we're looking for).
Apollo 10 ماه پیش
والد
کامیت
2c715c90c2
2فایلهای تغییر یافته به همراه61 افزوده شده و 16 حذف شده
  1. 23 13
      src/main.rs
  2. 38 3
      src/object.rs

+ 23 - 13
src/main.rs

@@ -2,18 +2,28 @@
 use moonmud::*;
 
 fn main() {
-    println!("Hello, world!");
-    let mut o: Object = Object::default();
-    o.id = next_id();
-    o.kind = Kind::Player;
-    o.name = "Test Player".to_string();
-    o.set_password(&"12345".to_string());
-    o.health = 10;
-    o.max_health = 10;
-    o.build = 1;
-    o.max_build = 6;
-    println!("{:?}", o);
-    if !o.save() {
-        println!("Err while saving?");
+    let o: Option<Object> = Object::load_player_by_name(&"Test Player".to_string());
+    match o {
+        Some(o) => {
+            println!("{:?}", o);
+        }
+        None => {
+            let mut o = Object::default();
+            o.id = next_id();
+            o.kind = Kind::Player;
+            o.name = "Test Player".to_string();
+            o.set_password(&"12345".to_string());
+            o.health = 10;
+            o.max_health = 10;
+            o.build = 1;
+            o.max_build = 3;
+            o.inv.add_one(1);
+            o.inv.add(2, 2);
+            if !o.save() {
+                eprintln!("Failed saving?");
+            } else {
+                println!("{:?}", o);
+            }
+        }
     }
 }

+ 38 - 3
src/object.rs

@@ -1,6 +1,6 @@
 use serde::{Deserialize, Serialize};
 
-use crate::{Id, Inventory, Kind, Valid};
+use crate::{fs_util::{dir_exists, file_exists, join, mkdir, read_dir}, Id, Inventory, Kind, Valid};
 
 #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Default)]
 pub struct Object {
@@ -54,20 +54,55 @@ impl Object {
 
     pub fn load_file(filename: &String) -> Option<Object> {
         let p = crate::fs_util::join(&format!("{}", filename));
+        if !file_exists(&p) {
+            return None;
+        }
         let cont = crate::fs_util::read_file(&p);
         if cont.is_empty() {
             eprintln!("Err reading '{}'", p);
             return None;
         }
+        Object::load(&cont)
+    }
+    pub fn load(cont: &String) -> Option<Object> {
         let obj: Result<Object, serde_json::Error> = serde_json::from_str(cont.as_str());
         match obj {
             Ok(obj) => Some(obj),
-            Err(_) => {
-                eprintln!("Err parsing '{}'", p);
+            Err(e) => {
+                eprintln!("Err parsing, {}", e);
                 None
             }
         }
     }
+    pub fn load_by_id(id: Id) -> Option<Object> {
+        Object::load_file(&format!("data/objects/{}.json", id))
+    }
+    pub fn load_player_by_name(name: &String) -> Option<Object> {
+        let p = join(&"data/objects".to_string());
+        let dir = read_dir(&p, false);
+        if dir.is_empty() {
+            if !dir_exists(&p) {
+                mkdir(&p);
+            }
+            return None;
+        }
+        for entry in dir.iter() {
+            eprintln!("Trying '{}'", entry);
+            let obj = Object::load_file(entry);
+            match obj {
+                Some(obj) => {
+                    eprintln!("OK");
+                    if obj.name == *name {
+                        return Some(obj);
+                    }
+                }
+                None => {
+                    eprintln!("ERR");
+                }
+            }
+        }
+        None
+    }
     pub fn save(&mut self) -> bool {
         if !self.valid() {
             eprintln!("Object not valid, won't save");