Advertisement
jargon

Roe2Js :: "files.js"

Jun 23rd, 2024
595
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* files.js */
  2.  
  3. function loadDocument(filePath) {
  4.  
  5.     filePath = filePath.replace(` `,`%20`);
  6.  
  7.     while(right(filePath,1) === `:`){
  8.         filePath = left( filePath, len( filePath ) - 1 );
  9.     }
  10.  
  11.     var xhr = new XMLHttpRequest();
  12.     xhr.open("GET", filePath, false);  // 'false' makes the request synchronous
  13.     xhr.setRequestHeader("Accept", "text/plain");
  14.     xhr.send(null);
  15.    
  16.     if (xhr.status === 200) {
  17.         return xhr.responseText;
  18.     } else {
  19.         throw new Error('Error loading document: ' + xhr.status);
  20.     }
  21. }
  22.  
  23. function loadJSON(filePath) {
  24.  
  25.     filePath = filePath.replace(` `,`%20`);
  26.    
  27.     while(right(filePath,1) === `:`){
  28.         filePath = left( filePath, len( filePath ) - 1 );
  29.     }
  30.  
  31.     var xhr = new XMLHttpRequest();
  32.     xhr.open("GET", filePath, false);  // 'false' makes the request synchronous
  33.     xhr.setRequestHeader("Accept", "application/json");
  34.     xhr.send(null);
  35.    
  36.     if (xhr.status === 200) {
  37.         return JSON.parse(xhr.responseText);
  38.     } else {
  39.         throw new Error('Error loading JSON file: ' + xhr.status);
  40.     }
  41. }
  42.  
  43. async function loadFullMap() {         
  44.     // Load the full map
  45.     const full_map = await fetch(`${host}/test dictionary/scripts/json/maps/${map}.json`).then(response => response.json());
  46.  
  47.     // Initialize full_stats array
  48.     const full_stats = [];
  49.  
  50.     for (let x = 0; x < full_map.length; x++) {
  51.         full_stats[x] = []; // Initialize the second dimension
  52.         for (let y = 0; y < full_map[x].length; y++) {
  53.             full_stats[x][y] = []; // Initialize the third dimension
  54.  
  55.             for (let z = 0; z < layers.gui; z++) {
  56.                 const sectionValue = sections(full_map[x][y][z], 4)[0];
  57.                 const url = `${host}/entity dictionary/scripts/json/${sectionValue}.json`;
  58.  
  59.                 if(preloaded.indexOf(sectionValue) !== -1){
  60.                     full_stats[x][y][z] = preloaded[sections(full_map[x][y][z],4)[0]];
  61.                     continue;
  62.                 }
  63.                
  64.                 full_stats[x][y][z] = preloaded[sections(full_map[x][y][z],4)[0]];
  65.  
  66.                 // Check if the file exists before fetching it
  67.                 if (await fileExists(url)) {
  68.                     full_stats[x][y][z] = await fetch(url).then(response => response.json());
  69.                 } else {
  70.                     // console.warn(`File not found: ${url}`);
  71.                     full_stats[x][y][z] = null; // or handle the missing file case as needed
  72.                     continue;
  73.                 }
  74.                
  75.                 // Capture the first key
  76.                 const keys = layers.Object.keys(full_stats[x][y][z]);
  77.                
  78.                 if (keys.length === 0) {
  79.                     full_stats[x][y][z] = null;
  80.                     continue;
  81.                 }
  82.                
  83.                 const key = keys[0];
  84.                 if ( key === sections( full_map[x][y][z], 4 )[0] ){
  85.                     preloaded[sections(full_map[x][y][z],4)[0]] = full_stats[x][y][z][sections(full_map[x][y][z],4)[0]];
  86.                 } else {
  87.                     preloaded[sections(full_map[x][y][z],4)[0]] = null;
  88.                 }
  89.             }
  90.         }
  91.     }
  92.     return full_stats;
  93. }
  94.  
  95. // Function to check if a file exists using a HEAD request
  96. async function fileExists(url) {
  97.  
  98.     url = url.replace(` `,`%20`);
  99.  
  100.     while(right(url,1) === `:`){
  101.         url = left( url, len( url ) - 1 );
  102.     }
  103.  
  104.     try {
  105.         const response = await fetch(url, { method: 'HEAD' });
  106.         return response.ok;
  107.     } catch (error) {
  108.         // console.error(`Error checking file existence: ${error}`);
  109.         return false;
  110.     }
  111. }
  112.  
  113. // Load cardinal directions from JSON with enhanced debugging
  114. function loadJSON(url) {
  115.  
  116.     url = url.replace(` `,`%20`);
  117.    
  118.     while(right(url,1) === `:`){
  119.         url = left( url, len( url ) - 1 );
  120.     }
  121.    
  122.     try {
  123.         const data = loadJSON(url);
  124.         if (!data) {
  125.             console.error(`Failed to load JSON from ${url}`);
  126.         }
  127.         return data;
  128.     } catch (error) {
  129.         console.error(`Error loading JSON from ${url}:`, error);
  130.         return null;
  131.     }
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement