Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const reset_everything = 0; // enable to reset the save data
- let turn = 1;
- let cash = 2000;
- let bank = 0;
- let debt = 5000;
- let health = 100;
- let space = 30;
- let maxSpace = 30;
- let spaceTier = 1;
- let product = [];
- let heldProduct = [0, 0, 0, 0, 0, 0];
- let city = location[0];
- let room = -1;
- function onConnect()
- {
- //lastKey = '';
- //keyBuffer = loadData();
- clearScreen();
- if (!load_data()) setPrices();
- }
- function save_data()
- {
- if (typeof _bbs_save !== 'undefined') // running inside a bbs
- {
- _bbs_save_type('dealwars', 'turn', turn);
- _bbs_save_type('dealwars', 'cash', cash);
- _bbs_save_type('dealwars', 'bank', bank);
- _bbs_save_type('dealwars', 'debt', debt);
- _bbs_save_type('dealwars', 'space', space);
- _bbs_save_type('dealwars', 'maxSpace', maxSpace);
- _bbs_save_type('dealwars', 'spaceTier', spaceTier);
- _bbs_save_type('dealwars', 'product', product);
- _bbs_save_type('dealwars', 'heldProduct', heldProduct);
- _bbs_save_type('dealwars', 'city', city);
- _bbs_save_type('dealwars', 'room', room);
- _bbs_save();
- }
- else // we're not running inside a bbs, fall back on our save system
- {
- let data = stringify(turn) + "|" +
- stringify(cash) + '|' +
- stringify(bank) + '|' +
- stringify(debt) + '|' +
- stringify(space)+ '|' +
- stringify(maxSpace) + '|' +
- stringify(spaceTier) + '|' +
- stringify(product) + '|' +
- stringify(heldProduct) + '|' +
- stringify(city) + '|' +
- stringify(room);
- saveData(data);
- }
- }
- function split_save_data(s)
- {
- let arr = s.split('|');
- turn = JSON.parse(arr[0]);
- cash = JSON.parse(arr[1]);
- bank = JSON.parse(arr[2]);
- debt = JSON.parse(arr[3]);
- space = JSON.parse(arr[4]);
- maxSpace = JSON.parse(arr[5]);
- spaceTier = JSON.parse(arr[6]);
- product = JSON.parse(arr[7]);
- heldProduct = JSON.parse(arr[8]);
- city = JSON.parse(arr[9]);
- room = JSON.parse(arr[10]);
- return arr;
- }
- function load_savedata()
- {
- if (typeof _bbs_load !== 'undefined') // running inside a bbs
- {
- if (!_bbs_load()) return 0;
- turn = _bbs_load_type('dealwars', 1, 'turn');
- cash = _bbs_load_type('dealwars', 2000, 'cash');
- bank = _bbs_load_type('dealwars', 0, 'bank');
- debt = _bbs_load_type('dealwars', 5000, 'debt');
- space = _bbs_load_type('dealwars', 30, 'space');
- maxSpace = _bbs_load_type('dealwars', 30, 'maxspace');
- spaceTier = _bbs_load_type('dealwars', 1, 'spaceTier');
- product = _bbs_load_type('dealwars', [], 'product');
- heldProduct = _bbs_load_type('dealwars', [0, 0, 0, 0, 0, 0], 'heldProduct');
- city = _bbs_load_type('dealwars', location[0], 'city');
- room = _bbs_load_type('dealwars', -1, 'room');
- if (room == -1) return 0; // We had to default so indicate we didnt have save data.
- return 1;
- }
- else // we're not running inside a bbs, fall back on our load system
- {
- let s = loadData();
- if (s == '') return 0;
- let datum = split_save_data(s);
- turn = JSON.parse(datum[0]);
- cash = JSON.parse(datum[1]);
- bank = JSON.parse(datum[2]);
- debt = JSON.parse(datum[3]);
- space = JSON.parse(datum[4]);
- maxSpace = JSON.parse(datum[5]);
- spaceTier = JSON.parse(datum[6]);
- product = JSON.parse(datum[7]);
- heldProduct = JSON.parse(datum[8]);
- city = JSON.parse(datum[9]);
- room = JSON.parse(datum[10]);
- }
- return 1;
- }
- function load_data()
- {
- let hasdata = load_savedata();
- if (reset_everything == 1) { hasdata=0; }
- if (!hasdata)
- {
- turn = 1;
- cash = 2000;
- bank = 0;
- debt = 5000;
- health = 100;
- space = 30;
- maxSpace = 30;
- spaceTier = 1;
- product = [];
- heldProduct = [0, 0, 0, 0, 0, 0];
- city = location[0];
- room = -1;
- save_data();
- hasdata = load_savedata();
- return 0;
- }
- return 1;
- }
- function stringify(obj)
- {
- //check if the type of object is undefined or a function
- if (typeof(obj) === 'undefined' || typeof(obj) === 'function')
- {
- //if yes return undefined
- return undefined;
- }
- //check if the type of object is a string
- if(typeof(obj) === 'string')
- {
- //if its true return the stringified object
- return '"' + obj + '"';
- }
- //checks if the input obj is a array/nest array
- if(Array.isArray(obj))
- {
- //if true create a new array variable that is equal to an empty array
- var newArray = [];
- //iterate through each key in the object.
- for(var i = 0; i < obj.length; i++) {
- //if the element at a given index is undefined or a function
- if(obj[i] === undefined || typeof(obj[i]) === 'function')
- {
- //recursion: invoke the stringify function on null and push the result into the new array
- newArray.push(stringify(null));
- }
- else
- {
- //recursion: invoke the stringify function on the value at the given index
- //and push the result into the new array
- newArray.push(stringify(obj[i]));
- }
- }
- //return the concatenation of the new array with quoted brackets on each side.
- return '[' + newArray.join(',') + ']';
- }
- //checks if the input obj is an object literal and if it is defined
- if(obj && typeof(obj) === 'object')
- {
- //if true, create a new array variable that is equal to an empty array
- var newObjArray = [];
- //iterate through each key in the object.
- for(var key in obj)
- {
- //if the obj[key] is NOT equal to undefined or the typeof obj[key] is NOT function
- if(obj[key] !== undefined || typeof(obj[key]) !== 'function')
- {
- //recursion: invoke the stringify function on the key and the value and
- //push formatted property into the new array
- newObjArray.push(stringify(key) + ":" + stringify(obj[key]));
- }
- }
- //return the concatenation of the new array with quoted curly braces on each side.
- return "{" + newObjArray.join(',') + "}";
- }
- //will take care of any other edge cases such as numbers and null
- return obj + "";
- }
Advertisement
Add Comment
Please, Sign In to add comment