-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.js
30 lines (27 loc) · 1.04 KB
/
handlers.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
import { formatNumber, updateGold, updateProducerList, checkInitCompleted } from './functions.js';
export const handleKeyPress = (term, state) => (name, matches, data) => {
const key = String.fromCharCode(data.code);
if (key === 'g' || key === 'G') {
state.gold++;
} else if (Number(key) >= 0 && Number(key) <= 9) {
const producer = state.producers.find(item => key.includes(item.id));
if (producer && state.gold >= producer.cost) {
state.gold -= producer.cost;
producer.cost *= producer.growthRate;
producer.count++;
state.isProducerListUpdated = false;
state.productionRate += producer.baseProduction * state.tickSpeed / 1000;
term.moveTo(25, 3);
term.green(formatNumber(state.productionRate * 1000 / state.tickSpeed) + ' ');
}
}
if (!state.isInitCompleted && state.gold >= 10) {
checkInitCompleted(term, state);
}
if (!state.isProducerListUpdated) {
updateProducerList(term, state);
}
}
export const handleStateChange = (term, state) => () => {
updateGold(term, state);
};