-
Notifications
You must be signed in to change notification settings - Fork 0
/
stockticker.js
128 lines (95 loc) · 3.45 KB
/
stockticker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
var stockMarket = {
stocks : [],
newsFeed : [],
cash : 0,
messageClearCounter:0,
init: function () {
this.stocks = [
{name: "Gold", price: 100, shares:0},
{name: "Silver", price: 100, shares:0},
{name: "Bonds", price: 100, shares:0},
{name: "Oil", price: 100, shares:0},
{name: "Industrials", price: 100, shares:0},
{name: "Grain", price: 100, shares:0}
];
this.newsFeed = [];
this.cash = 1000;
this.messageClearCounter = 0
},
broadcast:function (message) {
this.newsFeed.unshift(message);
},
update: function() {
var roll1 = Math.floor(Math.random() * 3 + 1); // action
var roll2 = Math.floor(Math.random() * 3 + 1) * 5; // amount
var roll3 = Math.floor(Math.random() * this.stocks.length); // stock choice
var affectedStock = this.stocks[roll3];
switch(roll1) {
case 1:
if (affectedStock.price >= 100 && affectedStock.shares > 0) {
var dividend = affectedStock.shares * roll2;
this.broadcast(affectedStock.name + " pays dividend of " + dividend);
this.cash += dividend;
}
break;
case 2:
affectedStock.price += roll2;
this.broadcast(affectedStock.name + " goes up " + roll2);
break;
case 3:
affectedStock.price -= roll2;
this.broadcast(affectedStock.name + " goes down " + roll2);
break;
}
if (affectedStock.price >= 200) {
affectedStock.shares *= 2;
affectedStock.price = 100;
this.broadcast(affectedStock.name + " has split, shares have doubled!");
} else if (affectedStock.price <= 0) {
affectedStock.shares = 0;
affectedStock.price = 100;
this.broadcast(affectedStock.name + " has crashed, shares are gone :(");
}
if (this.newsFeed.length > 25) {
this.newsFeed.pop();
}
},
getStockPrice(stockName) {
for(var i=0;i < this.stocks.length; i++) {
if (this.stocks[i].name == stockName) {
return this.stocks[i].price;
}
}
return -1;
},
getStockByName(stockName) {
for(var i=0;i < this.stocks.length; i++) {
if (this.stocks[i].name == stockName) {
return this.stocks[i];
}
}
return null;
},
buyShare(stockName) {
//get price
var thisStock = this.getStockByName(stockName);
var price = thisStock.price;
if (this.cash >= price) {
thisStock.shares += 1;
this.cash -= price;
this.broadcast("Bought a share of " + stockName + " for " + price);
} else {
console.log("You can't afford that stock");
}
},
sellShare(stockName) {
var thisStock = this.getStockByName(stockName);
if (thisStock.shares > 0) {
this.cash += thisStock.price;
thisStock.shares -= 1;
this.broadcast("Sold a share of " + stockName + " for " + thisStock.price);
}
}
};
stockMarket.init();
stockMarket.interval = setInterval(function() { stockMarket.update()}, 2000);