nguyent063

script.js

Feb 21st, 2021 (edited)
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Declare initial stock values
  2. const basePrices = [50, 100, 250, 150, 500, 375]
  3. var stocks = [{name: "MonkeySoft", price: 50},
  4.               {name: "ApeBook", price: 100},                  {name: "Alibaboon", price: 250},                
  5.               {name: "GorillaStop", price: 150},
  6.               {name: "CapuchinAndCapuchin", price: 500},
  7.               {name: "ApeBNB", price: 375}]
  8.  
  9.  
  10. //testing place
  11.  
  12.  
  13.  
  14. //testing place
  15.  
  16. //Timer variables
  17. var countdownTime = 30, cash = 5000, netWorth = 5000,totalStockVal = 0;
  18. var time = countdownTime;
  19. var boxInput = null;
  20. var date = 1;
  21.  
  22. var chartData = [
  23.           ['Date:', 'MonkeySoft', 'ApeBook', 'Alibaboon', 'GorillaStop', 'Capuchin & Capuchin', 'ApeBNB'],
  24.           ["Day 1", 50, 100, 250, 150, 500, 375]
  25.           ];
  26.  
  27. var news = "", positiveCompany = "", negativeCompany = "";
  28. var stocksOwned = [{name: "MonkeySoft", shareVal: 0, amount: 0},
  29.                   {name: "ApeBook", totalVal: 0, shareVal: 0, amount: 0},
  30.                   {name: "Alibaboon", totalVal: 0, shareVal: 0, amount: 0},
  31.                   {name: "GorillaStop", totalVal: 0, shareVal: 0, amount: 0},
  32.                   {name: "CapuchinAndCapuchin", totalVal: 0, shareVal: 0, amount: 0},
  33.                   {name: "ApeBNB", totalVal: 0, shareVal: 0, amount: 0}];
  34. var marketRandom = 0, marketTrending = 0;
  35. /*
  36.   -2: the great depression
  37.   -1: trending downward
  38.   0: default (avg)
  39.   1: trending upward
  40.   2: $GME TO THE MOON BABY
  41. */
  42.  
  43. var dates = [];
  44. var stocksOnChart = [];
  45.  
  46. function success(buySell, numShares, name, price)
  47. {
  48.   if (buySell)
  49.   {
  50.     console.log("Bought: "+name);
  51.     document.getElementById("stockShower").innerHTML = "You bought "+numShares+" banana(s) from "+name+" for "+(numShares*price)+"MM!"
  52.   }
  53.   else
  54.   {
  55.     console.log("Sold: "+name);
  56.     document.getElementById("stockShower").innerHTML = "You sold "+numShares+" bananas to "+name+" for $"+(numShares*price)+"MM!"
  57.   }
  58. }
  59.  
  60. function buyStock(val)  //displays bought stock
  61. {
  62.   //idx = stocks.find(String(val));
  63.   //console.log(idx);
  64.   boxInput = parseInt(document.getElementById("monkeysoftNum").value);
  65.  
  66.   //console.log(stocks[0].random);
  67.   if (val === "MonkeySoft")
  68.   {
  69.     boxInput = parseInt(document.getElementById("monkeysoftNum").value);
  70.     if (boxInput > 0 && (stocks[0].price*boxInput < cash) && Number.isInteger(boxInput))
  71.     {
  72.       //stocksOwned[0].totalVal += boxInput*stocks[0].price
  73.       stocksOwned[0].amount += boxInput
  74.       cash -= stocks[0].price*boxInput
  75.       success(true, boxInput, val, stocks[0].price);
  76.       console.log("Cash remaining: "+cash)
  77.     } //end nested if statement
  78.   } //end MonkeySoft if statement
  79.   else if(val === "ApeBook")
  80.   {
  81.     boxInput = parseInt(document.getElementById("apebookNum").value);
  82.     if (boxInput > 0 && (stocks[1].price*boxInput < cash) && Number.isInteger(boxInput))
  83.     {
  84.       stocksOwned[1].amount += boxInput
  85.       cash -= stocks[1].price*boxInput
  86.       success(true, boxInput, val, stocks[1].price);
  87.         console.log("Cash remaining: "+cash)
  88.     } //end nested if statement
  89.   } //end ApeBook if statement
  90.   else if(val === "Alibaboon")
  91.   {
  92.     boxInput = parseInt(document.getElementById("alibaboonNum").value);
  93.     if (boxInput > 0 && (stocks[2].price*boxInput < cash) && Number.isInteger(boxInput))
  94.     {
  95.       stocksOwned[2].amount += boxInput
  96.       cash -= stocks[2].price*boxInput
  97.       success(true, boxInput, val, stocks[2].price);
  98.         console.log("Cash remaining: "+cash)
  99.     } //end nested if statement
  100.   } //end Alibaboon if statement
  101.   else if(val === "GorillaStop")
  102.   {
  103.     boxInput = parseInt(document.getElementById("gorillastopNum").value);
  104.     if (boxInput > 0 && (stocks[3].price*boxInput < cash) && Number.isInteger(boxInput))
  105.     {
  106.       stocksOwned[3].amount += boxInput
  107.       cash -= stocks[3].price*boxInput
  108.       success(true, boxInput, val, stocks[3].price);
  109.         console.log("Cash remaining: "+cash)
  110.     } //end nested if statement
  111.   } //end GorillaStop if statement
  112.   else if(val === "CapuchinAndCapuchin")
  113.   {
  114.     boxInput = parseInt(document.getElementById("capuchinandcapuchinNum").value);
  115.     if (boxInput > 0 && (stocks[4].price*boxInput < cash) && Number.isInteger(boxInput))
  116.     {
  117.       stocksOwned[4].amount += boxInput
  118.       cash -= stocks[4].price*boxInput
  119.       success(true, boxInput, val, stocks[4].price);
  120.         console.log("Cash remaining: "+cash)
  121.     } //end nested if statement
  122.   } //end CapuchinAndCapuchin if statement
  123.   else if(val === "ApeBNB")
  124.   {
  125.     boxInput = parseInt(document.getElementById("apebnbNum").value);
  126.     console.log("ape bnb!")
  127.     if (boxInput > 0 && (stocks[5].price*boxInput < cash) && Number.isInteger(boxInput))
  128.     {
  129.       stocksOwned[5].amount += boxInput
  130.       cash -= stocks[5].price*boxInput
  131.       success(true, boxInput, val, stocks[5].price);
  132.         console.log("Cash remaining: "+cash)
  133.     } //end nested if statement
  134.   } //end ApeBNB if statement
  135. } //end buyStock() function
  136.  
  137.  
  138.  
  139.  
  140. function sellStock(val) //displays sold stock
  141. {
  142.   if (val === "MonkeySoft")
  143.   {
  144.     boxInput = parseInt(document.getElementById("monkeysoftNum").value);
  145.     if (boxInput > 0 && boxInput <= stocksOwned[0].amount && Number.isInteger(boxInput))
  146.     {
  147.       stocksOwned[0].amount -= boxInput
  148.       stocksOwned[0].total -= boxInput*stocks[0].price
  149.       cash += stocks[0].price*boxInput
  150.       success(false, boxInput, val, stocks[0].price);
  151.       console.log("Cash remaining: "+cash)
  152.     } //end nested if statement
  153.   } //end MonkeySoft if statement
  154.   else if(val === "ApeBook")
  155.   {
  156.     boxInput = parseInt(document.getElementById("apebookNum").value);
  157.     if (boxInput > 0 && boxInput <= stocksOwned[1].amount && Number.isInteger(boxInput))
  158.     {
  159.       stocksOwned[1].amount -= boxInput
  160.       stocksOwned[1].total -= boxInput*stocks[1].price
  161.       cash += stocks[1].price*boxInput
  162.       success(false, boxInput, val, stocks[1].price);
  163.       console.log("Cash remaining: "+cash)
  164.     } //end nested if statement
  165.   } //end ApeBook if statement
  166.   else if(val === "Alibaboon")
  167.   {
  168.     boxInput = parseInt(document.getElementById("alibaboonNum").value);
  169.     if (boxInput > 0 && boxInput <= stocksOwned[2].amount && Number.isInteger(boxInput))
  170.     {
  171.       stocksOwned[2].amount -= boxInput
  172.       stocksOwned[2].total -= boxInput*stocks[2].price
  173.       cash += stocks[2].price*boxInput
  174.       success(false, boxInput, val, stocks[2].price);
  175.       console.log("Cash remaining: "+cash)
  176.     } //end nested if statement
  177.   } //end Alibaboon if statement
  178.   else if(val === "GorillaStop")
  179.   {
  180.     boxInput = parseInt(document.getElementById("gorillastopNum").value);
  181.     if (boxInput > 0 && boxInput <= stocksOwned[3].amount && Number.isInteger(boxInput))
  182.     {
  183.       stocksOwned[3].amount -= boxInput
  184.       stocksOwned[3].total -= boxInput*stocks[3].price
  185.       cash += stocks[3].price*boxInput
  186.       success(false, boxInput, val, stocks[3].price);
  187.       console.log("Cash remaining: "+cash)
  188.     } //end nested if statement
  189.   } //end GorillaStop if statement
  190.   else if(val === "CapuchinAndCapuchin")
  191.   {
  192.     boxInput = parseInt(document.getElementById("capuchinandcapuchinNum").value);
  193.     if (boxInput > 0 && boxInput <= stocksOwned[4].amount && Number.isInteger(boxInput))
  194.     {
  195.       stocksOwned[4].amount -= boxInput
  196.       stocksOwned[4].total -= boxInput*stocks[4].price
  197.       cash += stocks[4].price*boxInput
  198.       success(false, boxInput, val, stocks[4].price);
  199.       console.log("Cash remaining: "+cash)
  200.     } //end nested if statement
  201.   } //end CapuchinAndCapuchin if statement
  202.   else if(val === "ApeBNB")
  203.   {
  204.     boxInput = parseInt(document.getElementById("apebnbNum").value);
  205.     console.log("ape bnb!")
  206.     if (boxInput > 0 && boxInput <= stocksOwned[5].amount && Number.isInteger(boxInput))
  207.     {
  208.       stocksOwned[5].amount -= boxInput
  209.       stocksOwned[5].total -= boxInput*stocks[5].price
  210.       cash += stocks[5].price*boxInput
  211.       success(false, boxInput, val, stocks[5].price);
  212.       console.log("Cash remaining: "+cash)
  213.     } //end nested if statement
  214.   } //end ApeBNB if statement
  215. } //end sellStock() function
  216.  
  217. /*
  218. document.getElementById("monkeyMarket").onclick = function(){buyStock()}
  219.  
  220. document.getElementById("monkeyMarket").onclick = function(){sellStock()}*/
  221.  
  222. function randomNewsEvent(trend)
  223. {
  224.   marketRandom = Math.floor(Math.random() * 7);
  225.   if (trend) // for trend - true: upward, false: downward
  226.   {
  227.     negativeCompany = "none"
  228.     if (marketRandom == 0)
  229.     {
  230.       news = "A new banana species has been discovered by monkey biologists. Stocks rise!"
  231.       positiveCompany = "all"
  232.     }
  233.     else if (marketRandom == 1)
  234.     {
  235.       news = "MonkeySoft’s CEO unveils a new monkeyputer. Stocks for MonkeySoft go up!"
  236.       positiveCompany = "MonkeySoft"
  237.     }
  238.     else if (marketRandom == 2)
  239.     {
  240.       news = "ApeBook acquires a monkey social media company for 20 million MM. Stocks for ApeBook go up!"
  241.       positiveCompany = "ApeBook"
  242.     }
  243.     else if (marketRandom == 3)
  244.     {
  245.       news = "Alibaboon expands their shipping centers to 10 new jungles. Stocks for Alibaboon increase!"
  246.       positiveCompany = "Alibaboon"
  247.     }
  248.     else if (marketRandom == 4)
  249.     {
  250.       news = "Capuchin & Capuchin’s new baby monkey powder is recommended by 9/10 moms."
  251.       positiveCompany = "Capuchin & Capuchin"
  252.     }
  253.     else if (marketRandom == 5)
  254.     {
  255.       news = "GorillaStop runs a new ad campaign featuring Monkeanu Reeves! Stocks for GorillaStop"
  256.       positiveCompany = "GorillaStop"
  257.     }
  258.     else if (marketRandom == 6)
  259.     {
  260.       news = "ApeBNB runs a smear campaign against monkey hotels. Stocks for ApeBNB increase!"
  261.       positiveCompany = "ApeBNB"
  262.     }
  263.   } //end if statement
  264.   else
  265.   {
  266.     positiveCompany = "none"
  267.     if (marketRandom == 0)
  268.     {
  269.       news = "Oh no! Monkeys are devastated by global deforestation! All stocks go down…"
  270.       negativeCompany = "all"
  271.     }
  272.     else if (marketRandom == 1)
  273.     {
  274.       news = "MonkeySoft tries to sell a monkeyphone, but it flops. Stocks for MonkeySoft go down…"
  275.       negativeCompany = "MonkeySoft"
  276.     }
  277.     else if (marketRandom == 2)
  278.     {
  279.       news = "ApeBook is revealed to have been selling monkey user information! Stocks for ApeBook go down…"
  280.       negativeCompany = "ApeBook"
  281.     }
  282.     else if (marketRandom == 3)
  283.     {
  284.       news = "Alibaboon’s monkey workers go on strike! Stocks for Alibaboon go down…"
  285.       negativeCompany = "Alibaboon"
  286.     }
  287.     else if (marketRandom == 4)
  288.     {
  289.       news = "Capuchin & Capuchin’s mouthwash brand gives monkeys terrible breath! Capuchin & Capuchin stocks decrease…"
  290.       negativeCompany = "Capuchin & Capuchin"
  291.     }
  292.     else if (marketRandom == 5)
  293.     {
  294.       news = "GorillaStop refuses to sell the latest Pokemonkey game for unknown reasons. Stocks for GorillaStop decrease."
  295.       negativeCompany = "GorillaStop"
  296.     }
  297.     else if (marketRandom == 6)
  298.     {
  299.       news = "An ApeBook post goes viral about their horrible ApeBNB experience. ApeBNB stocks decrease…"
  300.       negativeCompany = "ApeBNB"
  301.     }
  302.   } //end else statement
  303. } //end randomNewsEvent()
  304.  
  305. function randomMarketTrend()
  306. {
  307.   marketRandom = Math.floor(Math.random() * 51);
  308.   if (marketRandom == 0)
  309.   {
  310.     news = "earth literally dies"
  311.     marketTrending = -2
  312.   }
  313.   else if (marketRandom == 50)
  314.   {
  315.     news = "King Monkey publicly supports all banana stocks! "
  316.     marketTrending = 2
  317.   }
  318.   else if (marketRandom > 0 && marketRandom < 6)
  319.   {
  320.     randomNewsEvent(false);
  321.     marketTrending = -1
  322.   }
  323.   else if (marketRandom > 44 && marketRandom < 50)
  324.   {
  325.     randomNewsEvent(true);
  326.     marketTrending = 1
  327.   }
  328.   else
  329.   {
  330.     marketTrending = 0
  331.     news = ""
  332.   }
  333. } //end randomMarketTrend
  334.  
  335.  
  336. //Random number functions
  337. function getRandomIntNeutral(max) {
  338.   return Math.floor(Math.random() * Math.floor(max)) * (Math.round(Math.random()) ? 1 : -1);
  339. }
  340.  
  341. function getRandomIntPositive(max) {
  342.   return Math.floor(Math.random() * Math.floor(max))
  343. }
  344.  
  345. function getRandomIntNegative(max) {
  346.   return Math.floor(Math.random() * Math.floor(-max))
  347. }
  348.  
  349. function changeStockValue(stockName, isPositive) {
  350.   if (isPositive) {
  351.     stocks[stockName].price += getRandomIntPositive(stocks[stockName].price * 0.1) + stocks[stockName].price * 0.1;
  352.   } else {
  353.     stocks[stockName].price += getRandomIntNegative(stocks[stockName].price * 0.1) + stocks[stockName].price * 0.1;
  354.   }
  355. }
  356.  
  357. function calculatePercentageChange()
  358. {
  359.   /*
  360.   -2: the great depression
  361.   -1: trending downward
  362.   0: default (avg)
  363.   1: trending upward
  364.   2: $GME TO THE MOON BABY
  365.   */
  366.   if (marketTrending == 0)
  367.   {
  368.     stocks.forEach(stock => stock["price"] += getRandomIntNeutral(stock["price"] * 0.1));
  369.   }
  370.   else if (marketTrending == 2)
  371.   {
  372.     stocks.forEach(stock => stock["price"] += getRandomIntPositive(stock["price"] * 0.25) + stock["price"] * 0.25);
  373.   }
  374.   else if (marketTrending == -2)
  375.   {
  376.     stocks.forEach(stock => stock["price"] += getRandomIntNegative(stock["price"] * 0.25) - stock["price"] * 0.25);
  377.   }
  378.   else if (positiveCompany === "none")
  379.   {
  380.     if (negativeCompany === "all")
  381.     {
  382.       stocks.forEach(stock => stock["price"] += getRandomIntNegative(stock["price"] * 0.10) - stock["price"] * 0.10);
  383.     }
  384.     else if (negativeCompany === "MonkeySoft")
  385.     {
  386.       changeStockValue("MonkeySoft", false);
  387.     }
  388.     else if (negativeCompany === "ApeBook")
  389.     {
  390.       changeStockValue("ApeBook", false);
  391.     }
  392.     else if (negativeCompany === "Alibaboon")
  393.     {
  394.       changeStockValue("Alibaboon", false);
  395.     }
  396.     else if (negativeCompany === "Capuchin & Capuchin")
  397.     {
  398.       changeStockValue("CapuchinAndCapuchin", false);
  399.     }
  400.     else if (negativeCompany === "GorillaStop")
  401.     {
  402.       changeStockValue("GorillaStop", false);
  403.     }
  404.     else if (negativeCompany === "ApeBNB")
  405.     {
  406.       changeStockValue("ApeBNB", false);
  407.     }
  408.   }
  409.   else
  410.   {
  411.     if (positiveCompany === "all")
  412.     {
  413.       stocks.forEach(stock => stock["price"] += getRandomIntPositive(stock["price"] * 0.10) - stock["price"] * 0.10);
  414.     }
  415.     else if (positiveCompany === "MonkeySoft")
  416.     {
  417.       changeStockValue("MonkeySoft", true);
  418.     }
  419.     else if (positiveCompany === "ApeBook")
  420.     {
  421.       changeStockValue("ApeBook", true);
  422.     }
  423.     else if (positiveCompany === "Alibaboon")
  424.     {
  425.       changeStockValue("Alibaboon", true);
  426.     }
  427.     else if (positiveCompany === "Capuchin & Capuchin")
  428.     {
  429.       changeStockValue("CapuchinAndCapuchin", true);
  430.     }
  431.     else if (positiveCompany === "GorillaStop")
  432.     {
  433.       changeStockValue("GorillaStop", true);
  434.     }
  435.     else if (positiveCompany === "ApeBNB")
  436.     {
  437.       changeStockValue("ApeBNB", true);
  438.     }
  439.   }
  440. }
  441.  
  442. //Chart variables
  443.  
  444. var chartLabels = ['Time', "MonkeySoft", "ApeBook", "Alibaboon", "GorillaStop", "Capuchin & Capuchin", "ApeBNB"];
  445.  
  446. var stockPrices = ["Day "+date, stocks[0].price.toFixed(2), stocks[1].price.toFixed(2), stocks[2].price.toFixed(2), stocks[3].price.toFixed(2), stocks[4].price.toFixed(2), stocks[5].price.toFixed(2)];
  447.  
  448. var stockDates = []
  449. var stockDisplay = []
  450.  
  451. var clock = setInterval(function() {
  452.  
  453.     if (time <= 0) {
  454.       time = countdownTime
  455.  
  456.       randomMarketTrend();
  457.  
  458.       calculatePercentageChange(); //function that handles all the stock price changes
  459.  
  460.       //Time runs out, price changes
  461.  
  462.       totalStockVal = 0;
  463.       for (var i = 0; i < 6; i++)
  464.       {
  465.         let amountOfStokzz = parseInt(stocksOwned[i].amount);
  466.         //console.log(amountOfStokzz);
  467.         if (amountOfStokzz > 0)
  468.         {
  469.           totalStockVal += (stocks[i].price * amountOfStokzz);
  470.           console.log(totalStockVal);
  471.         }
  472.       }
  473.  
  474.       //netWorth = totalStockVal + cash;
  475.  
  476.       date += 1;
  477.  
  478.       var printDate = "Day " + date;
  479.  
  480.       var daysPrice = [printDate, stocks[0].price, stocks[1].price, stocks[2].price, stocks[3].price, stocks[4].price, stocks[5].price];
  481.  
  482.       if (news.length > 0)
  483.       {
  484.         document.getElementById("news").innerHTML = "Breaking News! "+ "<span>" + news + "</span>";
  485.       } //end if statement
  486.       else
  487.       {
  488.         document.getElementById("news").innerHTML = "News: " +  "<span>" + "Nothing much is happening..." + "</span>";
  489.       } //end else statement
  490.  
  491.       // if companies go bankrupt, then:
  492.       let numBankrupt = 0, bankruptStr = "";
  493.       for (var i = 0; i < 6; i++)
  494.       {
  495.         if (stocks[i].price <= 0)
  496.         {
  497.           bankruptStr += "and "+stocks[i].name+" ";
  498.           numBankrupt++;
  499.           stocksOwned[i].amount = 0;
  500.  
  501.           stocks[i].price = basePrices[i];
  502.         }
  503.       }
  504.       bankruptStr.substring(4);
  505.       if (numBankrupt > 0)
  506.       {
  507.         document.getElementById("news").innerHTML = bankruptStr+"went bankrupt!";
  508.       }
  509.  
  510.       // stocks.forEach(stock => console.log(stock)
  511.      
  512.       // );
  513.  
  514.       // stocksOwned.forEach(stock => console.log(stock)
  515.      
  516.       // );
  517.  
  518.     //Update chart
  519.  
  520.     // stockPrices = ["Day " + date, stocks[0].price, stocks[1].price,                            stocks[2].price, stocks[3].price, stocks[4]                          .price, stocks[5].price];
  521.  
  522.     // stockDates.push(stockPrices);
  523.  
  524.     if (date > 1)
  525.     {
  526.       chartData.push(daysPrice);
  527.     }
  528.  
  529.     if (chartData.length > 11)
  530.     {
  531.       chartData.splice(1, 1);
  532.     }
  533.     //take the chartData after it's been spliced to chart.js
  534.  
  535.     console.log(chartData);
  536.    
  537.     if (stockDates.length > 10) {
  538.       stockDates.shift();
  539.     }
  540.  
  541.     } else {
  542.       time -= 1;
  543.     } //End of if statement
  544.  
  545.     var minutes = Math.floor((time/60) % 60);
  546.     var seconds = Math.floor(time % 60);
  547.  
  548.     document.getElementById("date").innerHTML = "Day: " + "<span>" + date + "</span>";
  549.  
  550.     document.getElementById("timer").innerHTML = "Prices update in: " + "<span>" + minutes + "m " + seconds + "s" + "</span>";
  551.  
  552. }, 1000);
  553.  
  554. // var clock = setInterval(function() {
  555.  
  556. //   google.charts.load('current', {'packages':['corechart']});
  557. //   google.charts.setOnLoadCallback(drawChart);
  558. //   function drawChart() {
  559. //     var data = google.visualization.arrayToDataTable(chartData);
  560.    
  561. //     var options = {
  562. //       title: 'Stock Price',
  563. //       legend: { position: 'right' }
  564. //     };
  565.  
  566. //     var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
  567.  
  568. //     chart.draw(data, options);
  569. //   }
  570.  
  571. // }, 1000 * 5);
Add Comment
Please, Sign In to add comment