Gingerguy

Cooki Stocker console script

Sep 11th, 2021 (edited)
21,969
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ===========================================================================================
  2. /*         
  3.         Hello there.
  4.     This is the console version of the CookiStocker mod.
  5.     To use it, sinply copy-paste the entire thing in your Cookie Clicker console.
  6.     If you have any questions about the algorithm at hand or are having troubles running it
  7.     - you can just ask Gingerguy#2580 for help on discord on steam guide comments.
  8.         https://steamcommunity.com/sharedfiles/filedetails/?id=2599187047
  9. */
  10.  
  11. //      Version 1.5
  12.  
  13. //          Options
  14.  
  15.         // Announce transactions in game notifications
  16.         const stockerTransactionNotifications = true
  17.  
  18.         // Make regular profit reports
  19.         const stockerActivityReport = true
  20.             // How often to make regular reports in ms (one hour by default)
  21.             const stockerActivityReportFrequency = 1000 * 60 * 60
  22.  
  23.         // Make game notifications fade away on their own
  24.         const stockerFastNotifications = false
  25.  
  26.         // Use console.log for more detailed info on trends
  27.         const stockerConsoleAnnouncements = true
  28.  
  29.         // Logic loop frequency; do not touch it unless you are cheating.
  30.         const stockerLoopFrequency = 1000 * 30
  31.  
  32.         // The cheat itself. Rolls the cycle every time logic loop triggers
  33.         const stockerForceLoopUpdates = false
  34.  
  35.         const stockerGreeting = 'click clack you are now in debt'
  36.  
  37. // ===================================================================================
  38.  
  39.  
  40.  
  41. if (!stockList) {
  42.     var stockList = {
  43.         check: 'dump eet',
  44.         goods: [],
  45.         sessionStart: new Date(),
  46.         sessionProfits: 0,
  47.         sessionPurchases: 0,
  48.         sessionSales: 0,
  49.         stocksRising: 0
  50.     }
  51. }
  52.  
  53. var modeDecoder = ['stable','slowly rising','slowly falling','rapidly rising','rapidly falling','fluctuating'] // meanings of each market trend (good.mode)
  54. var goodIcons = [[2,33],[3,33],[4,33],[15,33],[16,33],[17,33],[5,33],[6,33],[7,33],[8,33],[13,33],[14,33],[19,33],[20,33],[32,33],[33,33],[34,33]];
  55.  
  56. function stockerTimeBeautifier(duration) {
  57.     var milliseconds = Math.floor((duration % 1000) / 100),
  58.       //seconds = Math.floor((duration / 1000) % 60),
  59.       minutes = Math.floor((duration / (1000 * 60)) % 60),
  60.       hours = Math.floor((duration / (1000 * 60 * 60))); //% 24),
  61.       //days = Math.floor((duration / (1000 * 60 * 60 * 24)));
  62.  
  63.     if (hours > 0) {
  64.         if (hours == 1) {
  65.             hours = hours + ' hour'
  66.         }
  67.         else hours = hours + ' hours'
  68.     }
  69.     else hours = -1;
  70.  
  71.     if (minutes >= 1) {
  72.         minutes = (minutes == 1) ? minutes + ' minute' : minutes + ' minutes';
  73.     }
  74.     else minutes = -1;
  75.    
  76.     if ((hours != -1) && (minutes != -1)) { return hours + ' and ' + minutes;   }
  77.     else if ((hours == -1) && (minutes != -1)) {    return minutes; }
  78.     else if ((hours != -1) && (minutes == -1)) {    return hours;   }
  79.     else if ((hours == -1) && (minutes == -1)) {    return 'not that long actually';    }
  80. }
  81.  
  82.  
  83. setTimeout(function waitForGame() {
  84.     if (typeof Game === 'object' && Game.ready) {
  85.    
  86.  
  87. Game.registerMod("gincookistocker",{
  88.     init:function(){
  89.  
  90.         this.startStocking();
  91.     },
  92.     startStocking:function(){
  93.         if (!Game.ObjectsById[5].minigame) {
  94.             console.log('=====$$$== Stock Market minigame has not initialised yet! Will try again in 500 ms.');
  95.             setTimeout(() => {
  96.                 this.startStocking();
  97.             },500);
  98.             return
  99.         }
  100.         else {
  101.             console.log('=====$$$== CookiStocker logic loop initialised at ' + new Date());
  102.             console.log('=====$$$== With main options as follows:')
  103.             console.log('=====$$$== Logic loop frequency: ' + stockerTimeBeautifier(stockerLoopFrequency))
  104.             console.log('=====$$$== Report frequency: ' + stockerTimeBeautifier(stockerActivityReportFrequency))
  105.             console.log('=====$$$== Cheating: ' + stockerForceLoopUpdates)
  106.             Game.Notify(`CookiStocker is ready`,stockerGreeting,[1,33],false);
  107.             console.log(stockList.check);
  108.         }
  109.        
  110.         var market = Game.ObjectsById[5].minigame.goodsById;    // read market
  111.         console.log('Reading the market:');
  112.         stockList.startingProfits = Game.ObjectsById[5].minigame.profit;
  113.         for (let i = 0; i < market.length; i++){
  114.             stockList.goods.push({
  115.                 name: market[i].name,
  116.                 stock: market[i].stock,
  117.                 restingPrice: 10*(i+1) + Game.ObjectsById[i].level - 1,
  118.                 currentPrice: market[i].val,
  119.                 mode: market[i].mode,
  120.                 lastMode: market[i].mode,
  121.                 priceBought: -1,
  122.             });
  123.             console.log('Stock: ' + market[i].name + ' Status: ' + modeDecoder[market[i].mode]);
  124.         }
  125.  
  126.         if (stockerActivityReport) {
  127.             var stockerReportInterval = setInterval(function() {
  128.                 var stockerUptime = new Date() - stockList.sessionStart;
  129.                 if ((stockList.sessionPurchases + stockList.sessionSales) == 0) {
  130.                     Game.Notify(
  131.                         'CookiStocker report',
  132.                         'This session has been running for ' + stockerTimeBeautifier(stockerUptime) +
  133.                         ', but no good investment opportunities were detected! Luck is not on our side, yet.'          
  134.                         ,[26,7],stockerFastNotifications
  135.                     );
  136.                 } else {
  137.                     Game.Notify(
  138.                         'CookiStocker report',
  139.                         'This session has been running for ' + stockerTimeBeautifier(stockerUptime) +
  140.                         ', and has made ' + stockList.sessionProfits.toFixed(0) +
  141.                         '$ in ' + stockList.sessionPurchases + ' purchases and ' + stockList.sessionSales + ' sales.'          
  142.                         ,[26,7],stockerFastNotifications
  143.                     );
  144.                 }
  145.             },stockerActivityReportFrequency);
  146.         }
  147.  
  148.         var stockerLoop = setInterval(function() {
  149.  
  150.             if (stockerForceLoopUpdates) Game.ObjectsById[5].minigame.tick();  
  151.                 // setting stockerForceLoopUpdates to true will make the logic loop force the market to tick every time it triggers,
  152.                 // making this an obvious cheat, and i will personally resent you.
  153.                
  154.                 // but
  155.                 // if you backup your save and set stockerLoopFrequency to like 10 milliseconds it looks very fun and effective.
  156.                 // yes, this is how i made the gif on the steam guide page.
  157.  
  158.  
  159.             stockList.stocksRising = 0;
  160.  
  161.             market = Game.ObjectsById[5].minigame.goodsById;    // update market
  162.             for (let i = 0; i < market.length; i++){
  163.                 //let i = 3;
  164.  
  165.                 // update stockList
  166.                 stockList.goods[i].stock = market[i].stock;
  167.                 stockList.goods[i].currentPrice = market[i].val;
  168.                 stockList.goods[i].mode = market[i].mode;
  169.  
  170.                 let md = stockList.goods[i].mode;
  171.                 let lmd = stockList.goods[i].lastMode;
  172.  
  173.                 if (
  174.                     (md != lmd) && (Game.ObjectsById[i+2].amount > 0)   // new trend detected in a stock that is active
  175.                 )
  176.                 {
  177.                     if (stockerConsoleAnnouncements) {
  178.                         console.log(stockList.goods[i].name + ' has changed the mode from [' + modeDecoder[lmd] + '] to [' + modeDecoder[md] + ']');
  179.                     }
  180.                    
  181.                     if (lmd != 5 && md == 5) {  // ignore unstable stocks
  182.                         if (stockerTransactionNotifications) Game.Notify(stockList.goods[i].name + ' went unstable','Ignoring the stock for a time',[1,33],stockerFastNotifications);
  183.                     }
  184.  
  185.                     if (    // buy conditions
  186.                         (
  187.                             (lmd == 2) && ((md !=4) && (md!=5)) ||  // slow fall stopped
  188.                             (lmd == 4) && ((md !=2) && (md!=5)) ||  // fast fall stopped
  189.                             (lmd == 5) && ((md !=2) && (md!=4))     // chaotic stopped
  190.                         )
  191.                         &&
  192.                         (stockList.goods[i].currentPrice < stockList.goods[i].restingPrice) // only if the price is lower than resting price
  193.                     )
  194.                     {
  195.                         // buying
  196.                         stockList.goods[i].priceBought = stockList.goods[i].currentPrice;
  197.                         Game.ObjectsById[5].minigame.buyGood(i,10000);
  198.                         stockList.sessionPurchases++;
  199.                         if (stockerTransactionNotifications) Game.Notify('Buying ' + stockList.goods[i].name,'The price has stopped ' + modeDecoder[stockList.goods[i].lastMode] + ' at ' + Math.floor(stockList.goods[i].priceBought) + '$ per unit, and is ' + modeDecoder[stockList.goods[i].mode] + ' now.',goodIcons[i],stockerFastNotifications);
  200.                         if (stockerConsoleAnnouncements) console.log('=====$$$== Buying '+ stockList.goods[i].name);
  201.                     }
  202.  
  203.                     if (    // sell conditions
  204.                         (stockList.goods[i].stock > 0)  // only if the stock is present
  205.                         &&
  206.                         (
  207.                             (lmd == 1) && ((md !=3) && (md!=5)) ||  // slow rise stopped
  208.                             (lmd == 3) && ((md !=1) && (md!=5)) ||  // fast rise stopped
  209.                             (lmd == 5) && ((md !=1) && (md!=3))     // chaotic stopped
  210.                         )
  211.                         && (stockList.goods[i].currentPrice > stockList.goods[i].priceBought)   // only if the price is higher than the price it was bought at
  212.                     )
  213.                     {
  214.                         // selling
  215.                         stockList.goods[i].priceBought = -1;
  216.                         Game.ObjectsById[5].minigame.sellGood(i,10000);
  217.                         stockList.sessionSales++;
  218.                         if (stockerTransactionNotifications) Game.Notify('Selling ' + stockList.goods[i].name,'At a profit of ' + Math.floor(stockList.goods[i].currentPrice - stockList.goods[i].priceBought) + '$ per unit (total ' + Math.floor(stockList.goods[i].currentPrice - stockList.goods[i].priceBought)*stockList.goods[i].stock + '$ profit), and is ' + modeDecoder[stockList.goods[i].mode] + ' now.',goodIcons[i],stockerFastNotifications);
  219.                         if (stockerConsoleAnnouncements) ('=====$$$== Selling '+ stockList.goods[i].name +' at a profit of ' + (stockList.goods[i].currentPrice - stockList.goods[i].priceBought).toFixed(2));
  220.  
  221.                     }
  222.                    
  223.                     stockList.sessionProfits = Game.ObjectsById[5].minigame.profit - stockList.startingProfits;
  224.                     stockList.goods[i].lastMode = stockList.goods[i].mode   // update last mode
  225.                 }
  226.             }
  227.         },stockerLoopFrequency);
  228.     },
  229. });
  230.  
  231. }
  232. else setTimeout(waitForGame,100)
  233. })
Add Comment
Please, Sign In to add comment