Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Declare initial stock values
- const basePrices = [50, 100, 250, 150, 500, 375]
- var stocks = [{name: "MonkeySoft", price: 50},
- {name: "ApeBook", price: 100}, {name: "Alibaboon", price: 250},
- {name: "GorillaStop", price: 150},
- {name: "CapuchinAndCapuchin", price: 500},
- {name: "ApeBNB", price: 375}]
- //testing place
- //testing place
- //Timer variables
- var countdownTime = 30, cash = 5000, netWorth = 5000,totalStockVal = 0;
- var time = countdownTime;
- var boxInput = null;
- var date = 1;
- var chartData = [
- ['Date:', 'MonkeySoft', 'ApeBook', 'Alibaboon', 'GorillaStop', 'Capuchin & Capuchin', 'ApeBNB'],
- ["Day 1", 50, 100, 250, 150, 500, 375]
- ];
- var news = "", positiveCompany = "", negativeCompany = "";
- var stocksOwned = [{name: "MonkeySoft", shareVal: 0, amount: 0},
- {name: "ApeBook", totalVal: 0, shareVal: 0, amount: 0},
- {name: "Alibaboon", totalVal: 0, shareVal: 0, amount: 0},
- {name: "GorillaStop", totalVal: 0, shareVal: 0, amount: 0},
- {name: "CapuchinAndCapuchin", totalVal: 0, shareVal: 0, amount: 0},
- {name: "ApeBNB", totalVal: 0, shareVal: 0, amount: 0}];
- var marketRandom = 0, marketTrending = 0;
- /*
- -2: the great depression
- -1: trending downward
- 0: default (avg)
- 1: trending upward
- 2: $GME TO THE MOON BABY
- */
- var dates = [];
- var stocksOnChart = [];
- function success(buySell, numShares, name, price)
- {
- if (buySell)
- {
- console.log("Bought: "+name);
- document.getElementById("stockShower").innerHTML = "You bought "+numShares+" banana(s) from "+name+" for "+(numShares*price)+"MM!"
- }
- else
- {
- console.log("Sold: "+name);
- document.getElementById("stockShower").innerHTML = "You sold "+numShares+" bananas to "+name+" for $"+(numShares*price)+"MM!"
- }
- }
- function buyStock(val) //displays bought stock
- {
- //idx = stocks.find(String(val));
- //console.log(idx);
- boxInput = parseInt(document.getElementById("monkeysoftNum").value);
- //console.log(stocks[0].random);
- if (val === "MonkeySoft")
- {
- boxInput = parseInt(document.getElementById("monkeysoftNum").value);
- if (boxInput > 0 && (stocks[0].price*boxInput < cash) && Number.isInteger(boxInput))
- {
- //stocksOwned[0].totalVal += boxInput*stocks[0].price
- stocksOwned[0].amount += boxInput
- cash -= stocks[0].price*boxInput
- success(true, boxInput, val, stocks[0].price);
- console.log("Cash remaining: "+cash)
- } //end nested if statement
- } //end MonkeySoft if statement
- else if(val === "ApeBook")
- {
- boxInput = parseInt(document.getElementById("apebookNum").value);
- if (boxInput > 0 && (stocks[1].price*boxInput < cash) && Number.isInteger(boxInput))
- {
- stocksOwned[1].amount += boxInput
- cash -= stocks[1].price*boxInput
- success(true, boxInput, val, stocks[1].price);
- console.log("Cash remaining: "+cash)
- } //end nested if statement
- } //end ApeBook if statement
- else if(val === "Alibaboon")
- {
- boxInput = parseInt(document.getElementById("alibaboonNum").value);
- if (boxInput > 0 && (stocks[2].price*boxInput < cash) && Number.isInteger(boxInput))
- {
- stocksOwned[2].amount += boxInput
- cash -= stocks[2].price*boxInput
- success(true, boxInput, val, stocks[2].price);
- console.log("Cash remaining: "+cash)
- } //end nested if statement
- } //end Alibaboon if statement
- else if(val === "GorillaStop")
- {
- boxInput = parseInt(document.getElementById("gorillastopNum").value);
- if (boxInput > 0 && (stocks[3].price*boxInput < cash) && Number.isInteger(boxInput))
- {
- stocksOwned[3].amount += boxInput
- cash -= stocks[3].price*boxInput
- success(true, boxInput, val, stocks[3].price);
- console.log("Cash remaining: "+cash)
- } //end nested if statement
- } //end GorillaStop if statement
- else if(val === "CapuchinAndCapuchin")
- {
- boxInput = parseInt(document.getElementById("capuchinandcapuchinNum").value);
- if (boxInput > 0 && (stocks[4].price*boxInput < cash) && Number.isInteger(boxInput))
- {
- stocksOwned[4].amount += boxInput
- cash -= stocks[4].price*boxInput
- success(true, boxInput, val, stocks[4].price);
- console.log("Cash remaining: "+cash)
- } //end nested if statement
- } //end CapuchinAndCapuchin if statement
- else if(val === "ApeBNB")
- {
- boxInput = parseInt(document.getElementById("apebnbNum").value);
- console.log("ape bnb!")
- if (boxInput > 0 && (stocks[5].price*boxInput < cash) && Number.isInteger(boxInput))
- {
- stocksOwned[5].amount += boxInput
- cash -= stocks[5].price*boxInput
- success(true, boxInput, val, stocks[5].price);
- console.log("Cash remaining: "+cash)
- } //end nested if statement
- } //end ApeBNB if statement
- } //end buyStock() function
- function sellStock(val) //displays sold stock
- {
- if (val === "MonkeySoft")
- {
- boxInput = parseInt(document.getElementById("monkeysoftNum").value);
- if (boxInput > 0 && boxInput <= stocksOwned[0].amount && Number.isInteger(boxInput))
- {
- stocksOwned[0].amount -= boxInput
- stocksOwned[0].total -= boxInput*stocks[0].price
- cash += stocks[0].price*boxInput
- success(false, boxInput, val, stocks[0].price);
- console.log("Cash remaining: "+cash)
- } //end nested if statement
- } //end MonkeySoft if statement
- else if(val === "ApeBook")
- {
- boxInput = parseInt(document.getElementById("apebookNum").value);
- if (boxInput > 0 && boxInput <= stocksOwned[1].amount && Number.isInteger(boxInput))
- {
- stocksOwned[1].amount -= boxInput
- stocksOwned[1].total -= boxInput*stocks[1].price
- cash += stocks[1].price*boxInput
- success(false, boxInput, val, stocks[1].price);
- console.log("Cash remaining: "+cash)
- } //end nested if statement
- } //end ApeBook if statement
- else if(val === "Alibaboon")
- {
- boxInput = parseInt(document.getElementById("alibaboonNum").value);
- if (boxInput > 0 && boxInput <= stocksOwned[2].amount && Number.isInteger(boxInput))
- {
- stocksOwned[2].amount -= boxInput
- stocksOwned[2].total -= boxInput*stocks[2].price
- cash += stocks[2].price*boxInput
- success(false, boxInput, val, stocks[2].price);
- console.log("Cash remaining: "+cash)
- } //end nested if statement
- } //end Alibaboon if statement
- else if(val === "GorillaStop")
- {
- boxInput = parseInt(document.getElementById("gorillastopNum").value);
- if (boxInput > 0 && boxInput <= stocksOwned[3].amount && Number.isInteger(boxInput))
- {
- stocksOwned[3].amount -= boxInput
- stocksOwned[3].total -= boxInput*stocks[3].price
- cash += stocks[3].price*boxInput
- success(false, boxInput, val, stocks[3].price);
- console.log("Cash remaining: "+cash)
- } //end nested if statement
- } //end GorillaStop if statement
- else if(val === "CapuchinAndCapuchin")
- {
- boxInput = parseInt(document.getElementById("capuchinandcapuchinNum").value);
- if (boxInput > 0 && boxInput <= stocksOwned[4].amount && Number.isInteger(boxInput))
- {
- stocksOwned[4].amount -= boxInput
- stocksOwned[4].total -= boxInput*stocks[4].price
- cash += stocks[4].price*boxInput
- success(false, boxInput, val, stocks[4].price);
- console.log("Cash remaining: "+cash)
- } //end nested if statement
- } //end CapuchinAndCapuchin if statement
- else if(val === "ApeBNB")
- {
- boxInput = parseInt(document.getElementById("apebnbNum").value);
- console.log("ape bnb!")
- if (boxInput > 0 && boxInput <= stocksOwned[5].amount && Number.isInteger(boxInput))
- {
- stocksOwned[5].amount -= boxInput
- stocksOwned[5].total -= boxInput*stocks[5].price
- cash += stocks[5].price*boxInput
- success(false, boxInput, val, stocks[5].price);
- console.log("Cash remaining: "+cash)
- } //end nested if statement
- } //end ApeBNB if statement
- } //end sellStock() function
- /*
- document.getElementById("monkeyMarket").onclick = function(){buyStock()}
- document.getElementById("monkeyMarket").onclick = function(){sellStock()}*/
- function randomNewsEvent(trend)
- {
- marketRandom = Math.floor(Math.random() * 7);
- if (trend) // for trend - true: upward, false: downward
- {
- negativeCompany = "none"
- if (marketRandom == 0)
- {
- news = "A new banana species has been discovered by monkey biologists. Stocks rise!"
- positiveCompany = "all"
- }
- else if (marketRandom == 1)
- {
- news = "MonkeySoft’s CEO unveils a new monkeyputer. Stocks for MonkeySoft go up!"
- positiveCompany = "MonkeySoft"
- }
- else if (marketRandom == 2)
- {
- news = "ApeBook acquires a monkey social media company for 20 million MM. Stocks for ApeBook go up!"
- positiveCompany = "ApeBook"
- }
- else if (marketRandom == 3)
- {
- news = "Alibaboon expands their shipping centers to 10 new jungles. Stocks for Alibaboon increase!"
- positiveCompany = "Alibaboon"
- }
- else if (marketRandom == 4)
- {
- news = "Capuchin & Capuchin’s new baby monkey powder is recommended by 9/10 moms."
- positiveCompany = "Capuchin & Capuchin"
- }
- else if (marketRandom == 5)
- {
- news = "GorillaStop runs a new ad campaign featuring Monkeanu Reeves! Stocks for GorillaStop"
- positiveCompany = "GorillaStop"
- }
- else if (marketRandom == 6)
- {
- news = "ApeBNB runs a smear campaign against monkey hotels. Stocks for ApeBNB increase!"
- positiveCompany = "ApeBNB"
- }
- } //end if statement
- else
- {
- positiveCompany = "none"
- if (marketRandom == 0)
- {
- news = "Oh no! Monkeys are devastated by global deforestation! All stocks go down…"
- negativeCompany = "all"
- }
- else if (marketRandom == 1)
- {
- news = "MonkeySoft tries to sell a monkeyphone, but it flops. Stocks for MonkeySoft go down…"
- negativeCompany = "MonkeySoft"
- }
- else if (marketRandom == 2)
- {
- news = "ApeBook is revealed to have been selling monkey user information! Stocks for ApeBook go down…"
- negativeCompany = "ApeBook"
- }
- else if (marketRandom == 3)
- {
- news = "Alibaboon’s monkey workers go on strike! Stocks for Alibaboon go down…"
- negativeCompany = "Alibaboon"
- }
- else if (marketRandom == 4)
- {
- news = "Capuchin & Capuchin’s mouthwash brand gives monkeys terrible breath! Capuchin & Capuchin stocks decrease…"
- negativeCompany = "Capuchin & Capuchin"
- }
- else if (marketRandom == 5)
- {
- news = "GorillaStop refuses to sell the latest Pokemonkey game for unknown reasons. Stocks for GorillaStop decrease."
- negativeCompany = "GorillaStop"
- }
- else if (marketRandom == 6)
- {
- news = "An ApeBook post goes viral about their horrible ApeBNB experience. ApeBNB stocks decrease…"
- negativeCompany = "ApeBNB"
- }
- } //end else statement
- } //end randomNewsEvent()
- function randomMarketTrend()
- {
- marketRandom = Math.floor(Math.random() * 51);
- if (marketRandom == 0)
- {
- news = "earth literally dies"
- marketTrending = -2
- }
- else if (marketRandom == 50)
- {
- news = "King Monkey publicly supports all banana stocks! "
- marketTrending = 2
- }
- else if (marketRandom > 0 && marketRandom < 6)
- {
- randomNewsEvent(false);
- marketTrending = -1
- }
- else if (marketRandom > 44 && marketRandom < 50)
- {
- randomNewsEvent(true);
- marketTrending = 1
- }
- else
- {
- marketTrending = 0
- news = ""
- }
- } //end randomMarketTrend
- //Random number functions
- function getRandomIntNeutral(max) {
- return Math.floor(Math.random() * Math.floor(max)) * (Math.round(Math.random()) ? 1 : -1);
- }
- function getRandomIntPositive(max) {
- return Math.floor(Math.random() * Math.floor(max))
- }
- function getRandomIntNegative(max) {
- return Math.floor(Math.random() * Math.floor(-max))
- }
- function changeStockValue(stockName, isPositive) {
- if (isPositive) {
- stocks[stockName].price += getRandomIntPositive(stocks[stockName].price * 0.1) + stocks[stockName].price * 0.1;
- } else {
- stocks[stockName].price += getRandomIntNegative(stocks[stockName].price * 0.1) + stocks[stockName].price * 0.1;
- }
- }
- function calculatePercentageChange()
- {
- /*
- -2: the great depression
- -1: trending downward
- 0: default (avg)
- 1: trending upward
- 2: $GME TO THE MOON BABY
- */
- if (marketTrending == 0)
- {
- stocks.forEach(stock => stock["price"] += getRandomIntNeutral(stock["price"] * 0.1));
- }
- else if (marketTrending == 2)
- {
- stocks.forEach(stock => stock["price"] += getRandomIntPositive(stock["price"] * 0.25) + stock["price"] * 0.25);
- }
- else if (marketTrending == -2)
- {
- stocks.forEach(stock => stock["price"] += getRandomIntNegative(stock["price"] * 0.25) - stock["price"] * 0.25);
- }
- else if (positiveCompany === "none")
- {
- if (negativeCompany === "all")
- {
- stocks.forEach(stock => stock["price"] += getRandomIntNegative(stock["price"] * 0.10) - stock["price"] * 0.10);
- }
- else if (negativeCompany === "MonkeySoft")
- {
- changeStockValue("MonkeySoft", false);
- }
- else if (negativeCompany === "ApeBook")
- {
- changeStockValue("ApeBook", false);
- }
- else if (negativeCompany === "Alibaboon")
- {
- changeStockValue("Alibaboon", false);
- }
- else if (negativeCompany === "Capuchin & Capuchin")
- {
- changeStockValue("CapuchinAndCapuchin", false);
- }
- else if (negativeCompany === "GorillaStop")
- {
- changeStockValue("GorillaStop", false);
- }
- else if (negativeCompany === "ApeBNB")
- {
- changeStockValue("ApeBNB", false);
- }
- }
- else
- {
- if (positiveCompany === "all")
- {
- stocks.forEach(stock => stock["price"] += getRandomIntPositive(stock["price"] * 0.10) - stock["price"] * 0.10);
- }
- else if (positiveCompany === "MonkeySoft")
- {
- changeStockValue("MonkeySoft", true);
- }
- else if (positiveCompany === "ApeBook")
- {
- changeStockValue("ApeBook", true);
- }
- else if (positiveCompany === "Alibaboon")
- {
- changeStockValue("Alibaboon", true);
- }
- else if (positiveCompany === "Capuchin & Capuchin")
- {
- changeStockValue("CapuchinAndCapuchin", true);
- }
- else if (positiveCompany === "GorillaStop")
- {
- changeStockValue("GorillaStop", true);
- }
- else if (positiveCompany === "ApeBNB")
- {
- changeStockValue("ApeBNB", true);
- }
- }
- }
- //Chart variables
- var chartLabels = ['Time', "MonkeySoft", "ApeBook", "Alibaboon", "GorillaStop", "Capuchin & Capuchin", "ApeBNB"];
- 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)];
- var stockDates = []
- var stockDisplay = []
- var clock = setInterval(function() {
- if (time <= 0) {
- time = countdownTime
- randomMarketTrend();
- calculatePercentageChange(); //function that handles all the stock price changes
- //Time runs out, price changes
- totalStockVal = 0;
- for (var i = 0; i < 6; i++)
- {
- let amountOfStokzz = parseInt(stocksOwned[i].amount);
- //console.log(amountOfStokzz);
- if (amountOfStokzz > 0)
- {
- totalStockVal += (stocks[i].price * amountOfStokzz);
- console.log(totalStockVal);
- }
- }
- //netWorth = totalStockVal + cash;
- date += 1;
- var printDate = "Day " + date;
- var daysPrice = [printDate, stocks[0].price, stocks[1].price, stocks[2].price, stocks[3].price, stocks[4].price, stocks[5].price];
- if (news.length > 0)
- {
- document.getElementById("news").innerHTML = "Breaking News! "+ "<span>" + news + "</span>";
- } //end if statement
- else
- {
- document.getElementById("news").innerHTML = "News: " + "<span>" + "Nothing much is happening..." + "</span>";
- } //end else statement
- // if companies go bankrupt, then:
- let numBankrupt = 0, bankruptStr = "";
- for (var i = 0; i < 6; i++)
- {
- if (stocks[i].price <= 0)
- {
- bankruptStr += "and "+stocks[i].name+" ";
- numBankrupt++;
- stocksOwned[i].amount = 0;
- stocks[i].price = basePrices[i];
- }
- }
- bankruptStr.substring(4);
- if (numBankrupt > 0)
- {
- document.getElementById("news").innerHTML = bankruptStr+"went bankrupt!";
- }
- // stocks.forEach(stock => console.log(stock)
- // );
- // stocksOwned.forEach(stock => console.log(stock)
- // );
- //Update chart
- // stockPrices = ["Day " + date, stocks[0].price, stocks[1].price, stocks[2].price, stocks[3].price, stocks[4] .price, stocks[5].price];
- // stockDates.push(stockPrices);
- if (date > 1)
- {
- chartData.push(daysPrice);
- }
- if (chartData.length > 11)
- {
- chartData.splice(1, 1);
- }
- //take the chartData after it's been spliced to chart.js
- console.log(chartData);
- if (stockDates.length > 10) {
- stockDates.shift();
- }
- } else {
- time -= 1;
- } //End of if statement
- var minutes = Math.floor((time/60) % 60);
- var seconds = Math.floor(time % 60);
- document.getElementById("date").innerHTML = "Day: " + "<span>" + date + "</span>";
- document.getElementById("timer").innerHTML = "Prices update in: " + "<span>" + minutes + "m " + seconds + "s" + "</span>";
- }, 1000);
- // var clock = setInterval(function() {
- // google.charts.load('current', {'packages':['corechart']});
- // google.charts.setOnLoadCallback(drawChart);
- // function drawChart() {
- // var data = google.visualization.arrayToDataTable(chartData);
- // var options = {
- // title: 'Stock Price',
- // legend: { position: 'right' }
- // };
- // var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
- // chart.draw(data, options);
- // }
- // }, 1000 * 5);
Add Comment
Please, Sign In to add comment