Here is everything then:
handlers.js:
export const handleKeyPress = (term, state) => {
return (name, matches, data) => {
let s = String.fromCharCode(data.code);
if (s === 'G' || s === 'g') {
state.gold++;
} else {
for (let i = 0; i < state.producers.length; i++) {
if (s === String(state.producers[i].id)) {
state.gold -= state.producers[i].cost;
state.producers[i].cost = state.producers[i].cost * state.producers[i].growthRate;
state.producers[i].count++;
state.productionRate += state.producers[i].baseProduction;
}
}
}
if (!state.isInitCompleted) {
cheapProd = state.producers[0];
for (let i = 1; i < state.producers.length; i++) {
if (cheapProd.cost > state.producers[i].cost) {
cheapProd = state.producers[i];
}
}
if (state.gold >= cheapProd.cost){
state.isInitCompleted = true;
checkInitCompleted(term, state);
}
}
}
}
export const handleStateChange = (term, state) => {
return () => { updateGold(term, state); }
}
functions.js:
term('Welcome to the mining game!');
term.clear();
term.hideCursor();
term.grabInput();
}
export const updateGold = (term, state) => {
term.moveTo(25, 2);
term.eraseLineAfter();
term.bold.yellow(state.gold + ' ');
state.gold += state.productionRate;
}
export const checkInitCompleted = (term, state) => {
term.clear();
term.moveTo(25, 2);
term('You can purchase producers by clicking the number button (1, 2, 3, ...)');
term.moveTo(25, 2);
term.eraseLineAfter();
term(`GOLD: ${state.gold}`);
term.moveTo(25, 2);
term.eraseLineAfter();
term(`PRODUCTION RATE: ${state.productionRate}`);
}
constants.js:
export const term = terminalKit.terminal;
export const config = {
gold: 0,
producers: [{'id': 'id', 'title': 'title', 'cost': 'cost', 'growthRate': 'growthRate', 'baseProduction': 'baseProduction', 'count': 'count'}],
productionRate: 0,
isInitCompleted: false,
isProducerListUpdated: true,
};
gameEngine.js:
import { handleKeyPress, handleStateChange } from './handlers.js';
export const startMiningGame = (term, state) => {
init(term);
term.on('key', handleKeyPress(term, state));
setInterval(handleStateChange(term, state), 1000);
}
solution.js:
import { term, config } from './constants.js';
startMiningGame(term, config);
and i still get these errors:
- checkInitCompleted should be called from handleKeyPress if the user has enough gold to make first purchase
- handleKeyPress arguments should be forwarded into checkInitCompleted
- checkInitCompleted should be called from handleKeyPress only once for the life of the game