Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to programatically load a json format map ? #150

Open
clodion opened this issue Dec 28, 2021 · 2 comments
Open

How to programatically load a json format map ? #150

clodion opened this issue Dec 28, 2021 · 2 comments

Comments

@clodion
Copy link

clodion commented Dec 28, 2021

Hi Ondras,

I would like to load a map saved in the default json format, as in #149

It is possible to do it with the user interface but I can't get how to do it automatically when launching the map for example. I think it is already programmed because you can update the map when using it in realtime.

the json format would be as string format, uploaded from the server or a database for example.

@clodion
Copy link
Author

clodion commented Jan 1, 2022

Hi, the answer is :

in // .js/ui/backend/file.js, change the FileUI class with adding the function :

    async load_from_txt(txt) { // txt is the json txt file of the map to load

        let format = repo6.get("native"); // only native format here
        let json = format.from(txt);
        this.loadDone(json);

    }

Add a script to load the json file stored on the server as a .json file (if not stored in a database) :

 // load file 

  var fileSrc = 'exercice_files/map_2025.json' // your path to the json file containing the map on the server

  function doGET(path, callback) {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() { 
        if (xhr.readyState == 4) {
            // it works ?
            if (xhr.status == 200) {
                // ***Yes, use `xhr.responseText` here***
                callback(xhr.responseText);
            } else {
                // ***No, tell the callback the call failed***
                callback(null);
            }
        }
    };
    xhr.open("GET", path);
    xhr.send();
}

function handleFileData(fileData) {
    if (!fileData) {
        // Show error
        return;
    }
    // Use the file data
    setTimeout(function() { // to avoid delaying problems

      var file_to_load = new FileUI(); // native format

      file_to_load.load_from_txt(fileData);

    }, 200)
   
}

// Do the request

doGET(fileSrc, handleFileData);

Wherever you want to programmatically load a map saved in the json native format :

      var file_to_load = new FileUI(); // native format
      file_to_load.load_from_txt(fileData); // fileData is the jsondata of the map

@ondras
Copy link
Owner

ondras commented Jan 2, 2022

First of all, it is generally a bad idea to rely on anything included in the generated *.js files (such as repo6). These names are automatically created by tsc and can change anytime. A better option is to adjust the source *.ts and then re-compile (via make).

Second, your suggestion closely mirrors what the WebDAV backend already does. So if you wish to fetch a remote map stored in the native/json format, you can simply do:

import { repo } from "./ui/backend/backend.js";

repo.get("webdav").load("url of your saved map");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants