haxball / haxball-issues

115 stars 43 forks source link

Saving data #392

Closed Basirius007 closed 6 years ago

Basirius007 commented 6 years ago

Hello, I have a question if anybody figured out, how to save data, so when i set up new room there are already player names and their goals, assists and so on that the had in previous room?

olehmisar commented 6 years ago

You can use localStorage. But localStorage have limitation of ~5mb(depends on browser). If you want to make big storage to store a lot of data, you should look at IndexedDB or make backend server. But I think the simplest way to store a big amount of data is localforage library.

VukBakic commented 6 years ago

@olegmisar Don't you need to have installed localforge library on server? And by server I mean where haxball.com/headless is hosted. The problem is cross domain policy. I tried setting cors server locally but couldn't connect to haxball using xmlhttprequest....

olehmisar commented 6 years ago

How have u tried to connect to haxball? Code, please.

VukBakic commented 6 years ago
function createCORSRequest(method, url) {
  var xhr = new XMLHttpRequest();
  if ("withCredentials" in xhr) {

    // Check if the XMLHttpRequest object has a "withCredentials" property.
    // "withCredentials" only exists on XMLHTTPRequest2 objects.
    xhr.open(method, url, true);

  } else if (typeof XDomainRequest != "undefined") {

    // Otherwise, check if XDomainRequest.
    // XDomainRequest only exists in IE, and is IE's way of making CORS requests.
    xhr = new XDomainRequest();
    xhr.open(method, url);

  } else {

    // Otherwise, CORS is not supported by the browser.
    xhr = null;

  }
  return xhr;
}

var request = createCORSRequest( "GET", "http://localhost:8123/scoreboard.JSON" );
if ( request ){
  // Define a callback function
  request.onload = function(){
      scoreBoard=JSON.parse(request.responseText);
  };
  // Send request
  request.send();
}

I think it was this one. Error was that I can't acces http server on my pc when it's https protocol.

olehmisar commented 6 years ago

What browser do u use?

VukBakic commented 6 years ago

chrome

olehmisar commented 6 years ago

You have to allow mixed content. Here are solutions for different browsers.

VukBakic commented 6 years ago

@olegmisar That actually worked :D Problem is now that anyone can access my files at 8123 port xD Still I would need to update my whole scoreboard.json everytime some event happens (player scores goal, team wins etc.). I am not so familiar with php to make backend script on my server to fix that. What do you personally use for storing info olegmisar?

olehmisar commented 6 years ago

Maybe I will use IndexedDB. Or localforage library, which is a wrapper for IndexedDB. Localforage is simple to use and doesn't require backend, it saves all the data in browser. But you should read about limitations. I believe that IndexedDB doesn't have limits but I am not sure.

VukBakic commented 6 years ago

@olegmisar Is there way to update indexeddb store without fetching whole player object , updating it and then using put request to update it?

ghost commented 5 years ago

@Wolf72 Do you have discord? I want to ask you something.

VukBakic commented 5 years ago

@meteus Wolf72x#6765

ghost commented 5 years ago

@Wolf72 Hey, added you. My name is 187#3248