소스 검색

Working on database loader.

cixo 1 년 전
부모
커밋
fa99bf9279
4개의 변경된 파일98개의 추가작업 그리고 44개의 파일을 삭제
  1. 14 4
      assets/element.js
  2. 9 0
      assets/functions.js
  3. 75 2
      assets/loader.js
  4. 0 38
      assets/parser.js

+ 14 - 4
assets/element.js

@@ -4,8 +4,8 @@ class element {
     #name;
     #description;
     #params;
-    #links;
     #thumbnail;
+    #shop;
 
     constructor(name) {
         if (typeof(name) !== "string") {
@@ -17,10 +17,10 @@ class element {
         this.#description = "";
         this.#mesh = "";
         this.#thumbnail = "";
+        this.#shop = "";
         
         this.#pictures = new Set();
         this.#params = new Map();
-        this.#links = new Map();
     }
 
     get name() {
@@ -59,10 +59,20 @@ class element {
         if (typeof(content) !== "string") {
             throw "Thumbnail URL must be string.";
         }   
+
+        this.#thumbnail = content;
+    }
+
+    set shop(content) {
+        if (typeof(content) !== "string") {
+            throw "Shop link mest be string.";
+        }
+
+        this.#shop = content;
     }
 
-    get links() {
-        return this.#links;
+    get shop() {
+        return this.#shop;
     }
 
     get params() {

+ 9 - 0
assets/functions.js

@@ -0,0 +1,9 @@
+const exists = (item) => {
+    return typeof(item) !== "undefined";
+};
+
+const is_string = (item) => {
+    return typeof(item) === "string";
+}
+
+export { exists, is_string };

+ 75 - 2
assets/loader.js

@@ -1,5 +1,7 @@
 import { project } from "./project.js";
 import { submission } from "./submission.js";
+import { element } from "./element.js";
+import { exists, is_string } from "./functions.js";
 
 class loader {
     #load_from;
@@ -33,13 +35,18 @@ class loader {
         }
         
         const content = this.#content;
+        
+        if (!exists(content.name)) {
+            content.name = "Unnamed";
+        }
+        
         const result = new project(content.name);
 
-        if (typeof(content.description) !== "undefined") {
+        if (exists(content.description)) {
             result.description = content.description;
         }
 
-        if (typeof(content.submissions) === "undefined") {
+        if (!exists(content.submissions)) {
             return result;
         }
 
@@ -54,12 +61,64 @@ class loader {
 
             result.add(item);
         });
+
+        return result;
     }
 
     #prepare_image(url) {
         return this.#database + "/" + url;
     }
 
+    #prepare_element(input) {
+        if (exists(input.name)) {
+            return null;
+        }
+
+        const element = new element(input.name);
+
+        if (exists(input.description)) {
+            element.description = input.description;
+        }
+
+        if (exists(input.mesh)) {
+            element.mesh = this.#prepare_image(input.mesh);
+        }
+
+        if (exists(input.thumbnail)) {
+            element.thumbnail = this.#prepare_image(input.thumbnail);
+        }
+        
+        if (exists(input.pictures)) {
+            const pictures = Array.from(input.pictures);
+        
+            pictures.forEach(picture => {
+                if (!is_string(picture) {
+                    return;
+                }
+
+                element.pictures.add(this.#prepare_image(picture));                
+            });
+        }
+
+        if (exists(input.params)) {
+            const names = Object.keys(input.params);
+            
+            names.forEach(name => {
+                if (!is_string(name) || !is_string(input.params[name])) {
+                    return;
+                }
+
+                element.params.set(name, input.params[name]);
+            });
+        }
+
+        if (exists(input.shop)) {
+            element.shop = input.shop;
+        }
+
+        return element;
+    }
+
     #prepare_one(input) {
         if (typeof(input.name) === "undefined") {
             return null;
@@ -75,6 +134,20 @@ class loader {
             submission.thumbnail = this.#prepare_image(input.thumbnail);
         }
 
+        if (typeof(input.elements) === "undefined") {
+            return submission;
+        }
+
+        const elements = Array.from(input.elements);
+
+        elements.forEach(data => {
+            const element = new this.#prepare_element(data);
+
+            if (element !== null) {
+                submission.add(element);
+            }
+        });
+
         return submission;
     }
 }

+ 0 - 38
assets/parser.js

@@ -1,38 +0,0 @@
-import { element } from "./element.js";
-import { submission } from "./submission.js";
-
-class parser {
-    #source_url;
-    #database;
-
-    constructor(source_url) {
-        if (typeof(source_url) !== "string") {
-            throw "Source URL must be an string.";
-        }   
-
-        this.#source = source;
-        this.#database = undefined;
-    }
-
-    async load() {
-        const result = await fetch(this.#source);
-
-        if (!result.ok) {
-            throw "Can not load database from server.";
-        }
-
-        if (result.status !== 200) {
-            throw "Can not load database from server. Result not 200.";
-        }
-
-        const database = await result.json();
-
-        this.#fetch_result(database);
-    }
-
-    #fetch_result(database) {
-        
-    }
-}
-
-export { parser };