-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
200 lines (166 loc) · 5.6 KB
/
script.js
File metadata and controls
200 lines (166 loc) · 5.6 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* eslint-disable no-alert */
/**************
* SLICE 1
**************/
function updateCoffeeView(coffeeQty) {
document.getElementById('coffee_counter').innerText = coffeeQty
}
function clickCoffee(data) {
data.coffee ++;
updateCoffeeView(data.coffee);
renderProducers(data);
}
/**************
* SLICE 2
**************/
function unlockProducers(producers, coffeeCount) {
producers.forEach(producer => {
if (!producer.unlocked) {
producer.unlocked = coffeeCount>= producer.price/2? true:false
}
})
}
function getUnlockedProducers(data) {
return data.producers.filter(producer => producer.unlocked)
}
function makeDisplayNameFromId(id) {
return id.split("_")
.map(word => word[0].toUpperCase() + word.slice(1))
.join(" ")
}
// You shouldn't need to edit this function-- its tests should pass once you've written makeDisplayNameFromId
function makeProducerDiv(producer) {
const containerDiv = document.createElement('div');
containerDiv.className = 'producer';
const displayName = makeDisplayNameFromId(producer.id);
const currentCost = producer.price;
const html = `
<div class="producer-column">
<span>❤ <div class="producer-title">${displayName}</div></span>
<button type="button" id="buy_${producer.id}">Buy</button>
</div>
<div class="producer-column">
<div>Quantity: ${producer.qty}</div>
<div>Coffee/second: ${producer.cps}</div>
<div id="cost">Cost: ${currentCost} coffee</div>
</div>
`;
containerDiv.innerHTML = html;
return containerDiv;
}
function deleteAllChildNodes(parent) {
Array.from(parent.childNodes)
.forEach(child => parent.removeChild(child))
}
function renderProducers(data) {
const producerContainer = document.querySelector('#producer_container');
unlockProducers(data.producers, data.coffee);
deleteAllChildNodes(producerContainer);
getUnlockedProducers(data).map(producer => makeProducerDiv(producer))
.forEach(containerDiv => {
producerContainer.append(containerDiv)
})
};
/**************
* SLICE 3
**************/
function getProducerById(data, producerId) {
return data.producers.find(producer => producer.id === producerId)
}
function canAffordProducer(data, producerId) {
return data.coffee >= getProducerById(data,producerId).price? true:false
}
function updateCPSView(cps) {
document.getElementById('cps').innerText = cps
}
function updatePrice(oldPrice) {
return Math.floor(oldPrice*1.25)
}
function attemptToBuyProducer(data, producerId) {
let producer = getProducerById(data,producerId);
if (canAffordProducer(data,producerId)) {
producer.qty++;
data.coffee -= producer.price;
producer.price = updatePrice(producer.price);
data.totalCPS += producer.cps;
return true
} else {
return false
}
}
function buyButtonClick(event, data) {
const id = String(event.target.id).slice(4);
const producer = getProducerById(data,id);
if (event.target.tagName === 'BUTTON') {
if (canAffordProducer(data,id)) {
document.getElementById('producer_container').append(producer);
attemptToBuyProducer(data,id);
updateCoffeeView(data.coffee);
updateCPSView(data.totalCPS);
}
else {
window.alert("Not enough coffee!")
}
}
// your code here
}
function tick(data) {
data.coffee += data.totalCPS;
updateCoffeeView(data.coffee);
renderProducers(data);
}
/*************************
* Start your engines!
*************************/
// You don't need to edit any of the code below
// But it is worth reading so you know what it does!
// So far we've just defined some functions; we haven't actually
// called any of them. Now it's time to get things moving.
// We'll begin with a check to see if we're in a web browser; if we're just running this code in node for purposes of testing, we don't want to 'start the engines'.
// How does this check work? Node gives us access to a global variable /// called `process`, but this variable is undefined in the browser. So,
// we can see if we're in node by checking to see if `process` exists.
if (typeof process === 'undefined') {
// Get starting data from the window object
// (This comes from data.js)
const data = window.data;
// Add an event listener to the giant coffee emoji
const bigCoffee = document.getElementById('big_coffee');
bigCoffee.addEventListener('click', () => clickCoffee(data));
// Add an event listener to the container that holds all of the producers
// Pass in the browser event and our data object to the event listener
const producerContainer = document.getElementById('producer_container');
producerContainer.addEventListener('click', event => {
buyButtonClick(event, data);
});
// Call the tick function passing in the data object once per second
setInterval(() => tick(data), 1000);
}
// Meanwhile, if we aren't in a browser and are instead in node
// we'll need to exports the code written here so we can import and
// Don't worry if it's not clear exactly what's going on here;
// We just need this to run the tests in Mocha.
else if (process) {
module.exports = {
updateCoffeeView,
clickCoffee,
unlockProducers,
getUnlockedProducers,
makeDisplayNameFromId,
makeProducerDiv,
deleteAllChildNodes,
renderProducers,
updateCPSView,
getProducerById,
canAffordProducer,
updatePrice,
attemptToBuyProducer,
buyButtonClick,
tick
};
}
document.addEventListener('mouseover',(event) => {
if (event.target.innerText === 'BUY') {
event.target.parentElement.parentElement.children[1].children[2].style.filter = "contrast(300%)";
event.target.previousElementSibling.children[0].style.fontWeight = "bolder"
}
})