| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- var PREFS_FILE = "SpaceGameMultiPlayer.json";
- // Defaults
- var MASTER_SERVER_IP = "52.37.100.204";
- var MASTER_SERVER_PORT = 41234;
- var SERVER_PORT = 27000;
- var filesystem = Atomic.getFileSystem();
- // Get out documents folder
- var documentsDir = filesystem.getUserDocumentsDir();
- var prefFilePath = documentsDir + PREFS_FILE;
- function LocalStorage() {
- }
- function getJSONPrefData() {
- if (filesystem.fileExists(prefFilePath)) {
- var file = new Atomic.File(prefFilePath, Atomic.FILE_READ);
- // Read the data string and parse the JSON back to an object
- var fileData = file.readString();
- var json = JSON.parse(fileData);
- return json;
- }
- return {};
- }
- LocalStorage.prototype.setServerName = function(serverName) {
- var mydata = getJSONPrefData();
- mydata.server_name = serverName;
- var file = new Atomic.File(prefFilePath, Atomic.FILE_WRITE);
- // Convert the data object to a string and write it
- file.writeString(JSON.stringify(mydata));
- // close the file
- file.close();
- };
- LocalStorage.prototype.getServerName = function() {
- var json = getJSONPrefData();
- if (json.server_name) {
- return json.server_name;
- }
- return "Server";
- };
- LocalStorage.prototype.getServerPort = function() {
- var json = getJSONPrefData();
- if (json.server_port) {
- return json.server_port;
- }
- return SERVER_PORT;
- };
- LocalStorage.prototype.getMasterServerIP = function() {
- var json = getJSONPrefData();
- if (json.master_server_ip) {
- return json.master_server_ip;
- }
- return MASTER_SERVER_IP;
- };
- LocalStorage.prototype.getMasterServerPort = function() {
- var json = getJSONPrefData();
- if (json.master_server_port) {
- return json.master_server_port;
- }
- return MASTER_SERVER_PORT;
- };
- LocalStorage.prototype.setPlayerName = function(playerName) {
- var mydata = getJSONPrefData();
- mydata.player_name = playerName;
- var file = new Atomic.File(prefFilePath, Atomic.FILE_WRITE);
- // Convert the data object to a string and write it
- file.writeString(JSON.stringify(mydata));
- // close the file
- file.close();
- };
- LocalStorage.prototype.getPlayerName = function() {
- var json = getJSONPrefData();
- if (json.player_name) {
- return json.player_name;
- }
- return "Player";
- };
- Atomic.localStorage = exports.localStorage = new LocalStorage();
|