Guest User

Save system for dealwars and classicbbs

a guest
Aug 24th, 2022
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const reset_everything = 0; // enable to reset the save data
  2. let turn = 1;
  3. let cash = 2000;
  4. let bank = 0;
  5. let debt = 5000;
  6. let health = 100;
  7. let space = 30;
  8. let maxSpace = 30;
  9. let spaceTier = 1;
  10. let product = [];
  11. let heldProduct = [0, 0, 0, 0, 0, 0];
  12. let city = location[0];
  13. let room = -1;
  14.  
  15. function onConnect()
  16. {
  17.     //lastKey = '';
  18.  
  19.     //keyBuffer = loadData();
  20.  
  21.     clearScreen();
  22.  
  23.     if (!load_data()) setPrices();
  24. }
  25.  
  26. function save_data()
  27. {
  28.     if (typeof _bbs_save !== 'undefined') // running inside a bbs
  29.     {
  30.         _bbs_save_type('dealwars', 'turn', turn);
  31.         _bbs_save_type('dealwars', 'cash', cash);
  32.         _bbs_save_type('dealwars', 'bank', bank);
  33.         _bbs_save_type('dealwars', 'debt', debt);
  34.         _bbs_save_type('dealwars', 'space', space);
  35.         _bbs_save_type('dealwars', 'maxSpace', maxSpace);
  36.         _bbs_save_type('dealwars', 'spaceTier', spaceTier);
  37.         _bbs_save_type('dealwars', 'product', product);
  38.         _bbs_save_type('dealwars', 'heldProduct', heldProduct);
  39.         _bbs_save_type('dealwars', 'city', city);
  40.         _bbs_save_type('dealwars', 'room', room);
  41.         _bbs_save();
  42.     }
  43.     else // we're not running inside a bbs, fall back on our save system
  44.     {
  45.         let data = stringify(turn) + "|" +
  46.                    stringify(cash) + '|' +
  47.                    stringify(bank) + '|' +
  48.                    stringify(debt) + '|' +
  49.                    stringify(space)+ '|' +
  50.                    stringify(maxSpace) + '|' +
  51.                    stringify(spaceTier) + '|' +
  52.                    stringify(product) + '|' +
  53.                    stringify(heldProduct) + '|' +
  54.                    stringify(city) + '|' +
  55.                    stringify(room);
  56.         saveData(data);
  57.     }
  58. }
  59.  
  60. function split_save_data(s)
  61. {
  62.     let arr = s.split('|');
  63.     turn = JSON.parse(arr[0]);
  64.     cash = JSON.parse(arr[1]);
  65.     bank = JSON.parse(arr[2]);
  66.     debt = JSON.parse(arr[3]);
  67.     space = JSON.parse(arr[4]);
  68.     maxSpace = JSON.parse(arr[5]);
  69.     spaceTier = JSON.parse(arr[6]);
  70.     product = JSON.parse(arr[7]);
  71.     heldProduct = JSON.parse(arr[8]);
  72.     city = JSON.parse(arr[9]);
  73.     room = JSON.parse(arr[10]);
  74.     return arr;
  75. }
  76.  
  77. function load_savedata()
  78. {
  79.     if (typeof _bbs_load !== 'undefined') // running inside a bbs
  80.     {
  81.         if (!_bbs_load()) return 0;
  82.         turn           = _bbs_load_type('dealwars', 1, 'turn');
  83.         cash           = _bbs_load_type('dealwars', 2000, 'cash');
  84.         bank           = _bbs_load_type('dealwars', 0, 'bank');
  85.         debt           = _bbs_load_type('dealwars', 5000, 'debt');
  86.         space          = _bbs_load_type('dealwars', 30, 'space');
  87.         maxSpace       = _bbs_load_type('dealwars', 30, 'maxspace');
  88.         spaceTier      = _bbs_load_type('dealwars', 1, 'spaceTier');
  89.         product        = _bbs_load_type('dealwars', [], 'product');
  90.         heldProduct    = _bbs_load_type('dealwars', [0, 0, 0, 0, 0, 0], 'heldProduct');
  91.         city           = _bbs_load_type('dealwars', location[0], 'city');
  92.         room           = _bbs_load_type('dealwars', -1, 'room');
  93.         if (room == -1) return 0; // We had to default so indicate we didnt have save data.
  94.         return 1;
  95.     }
  96.     else // we're not running inside a bbs, fall back on our load system
  97.     {
  98.         let s = loadData();
  99.         if (s == '') return 0;
  100.         let datum = split_save_data(s);
  101.         turn = JSON.parse(datum[0]);
  102.         cash = JSON.parse(datum[1]);
  103.         bank = JSON.parse(datum[2]);
  104.         debt = JSON.parse(datum[3]);
  105.         space = JSON.parse(datum[4]);
  106.         maxSpace = JSON.parse(datum[5]);
  107.         spaceTier = JSON.parse(datum[6]);
  108.         product = JSON.parse(datum[7]);
  109.         heldProduct = JSON.parse(datum[8]);
  110.         city = JSON.parse(datum[9]);
  111.         room = JSON.parse(datum[10]);
  112.     }
  113.     return 1;
  114. }
  115.  
  116. function load_data()
  117. {
  118.     let hasdata = load_savedata();
  119.     if (reset_everything == 1) { hasdata=0; }
  120.    
  121.     if (!hasdata)
  122.     {
  123.         turn = 1;
  124.         cash = 2000;
  125.         bank = 0;
  126.         debt = 5000;
  127.         health = 100;
  128.         space = 30;
  129.         maxSpace = 30;
  130.         spaceTier = 1;
  131.         product = [];
  132.         heldProduct = [0, 0, 0, 0, 0, 0];
  133.         city = location[0];
  134.         room = -1;
  135.         save_data();
  136.         hasdata = load_savedata();
  137.         return 0;
  138.     }
  139.     return 1;
  140. }
  141.  
  142. function stringify(obj)
  143. {
  144.     //check if the type of object is undefined or a function
  145.     if (typeof(obj) === 'undefined' || typeof(obj) === 'function')
  146.     {
  147.         //if yes return undefined
  148.         return undefined;
  149.     }
  150.  
  151.     //check if the type of object is a string
  152.     if(typeof(obj) === 'string')
  153.     {
  154.         //if its true return the stringified object
  155.         return '"' + obj + '"';
  156.     }
  157.  
  158.     //checks if the input obj is a array/nest array
  159.     if(Array.isArray(obj))
  160.     {
  161.         //if true create a new array variable that is equal to an empty array
  162.         var newArray = [];
  163.         //iterate through each key in the object.
  164.         for(var i = 0; i < obj.length; i++) {
  165.             //if the element at a given index is undefined or a function
  166.             if(obj[i] === undefined || typeof(obj[i]) === 'function')
  167.             {
  168.                 //recursion: invoke the stringify function on null and push the result into the new array
  169.                 newArray.push(stringify(null));
  170.             }
  171.             else
  172.             {
  173.                 //recursion: invoke the stringify function on the value at the given index
  174.                 //and push the result into the new array
  175.                 newArray.push(stringify(obj[i]));  
  176.             }
  177.         }
  178.         //return the concatenation of the  new array with quoted brackets on each side.  
  179.         return '[' + newArray.join(',') + ']';
  180.     }
  181.  
  182.     //checks if the input obj is an object literal and if it is defined
  183.     if(obj && typeof(obj) === 'object')
  184.     {
  185.         //if true, create a new array variable that is equal to an empty array
  186.         var newObjArray = [];
  187.         //iterate through each key in the object.
  188.         for(var key in obj)
  189.         {
  190.             //if the obj[key] is NOT equal to undefined or the typeof obj[key] is NOT function
  191.             if(obj[key] !== undefined || typeof(obj[key]) !== 'function')
  192.             {
  193.                 //recursion: invoke the stringify function on the key and the value and
  194.                 //push formatted property into the new array
  195.                 newObjArray.push(stringify(key) + ":" + stringify(obj[key]));
  196.             }
  197.         }
  198.         //return the concatenation of the new array with quoted curly braces on each side.
  199.         return "{" + newObjArray.join(',') + "}";
  200.     }
  201.     //will take care of any other edge cases such as numbers and null
  202.     return obj + "";
  203. }
  204.  
Tags: lastcallbbs
Advertisement
Add Comment
Please, Sign In to add comment