solution.js
import { startMiningGame } from './gameEngine.js';
import { term, config } from './constants.js';
startMiningGame(term, config);
=========
gameEngine.js
import { init } from './functions.js';
import { handleKeyPress, handleStateChange } from './handlers.js';
import { term } from './constants.js'
export const startMiningGame = (term, config) => {
init(term);
setInterval(handleStateChange(term, config), 1000);
const handler = handleKeyPress(term, config);
term.on('key', handler);
}
=========
handlers.js
import { updateGold, checkInitCompleted } from './functions.js';
export const handleKeyPress = (term, state) => {
return (name, matches, data) => {
let key = String.fromCharCode(data.code);
if (key === 'g' || key === 'G') {
state.gold++;
} else {
for (let i = 0; i < state.producers.length; i++) {
if (key === String(state.producers[i].id) && state.gold >= state.producers[i].cost) {
state.gold -= state.producers[i].cost;
state.producers[i].cost *= state.producers[i].growthRate;
state.producers[i].count++;
state.productionRate += state.producers[i].baseProduction;
}
}
// for (const producer of state.producers) {
// if (key === String(producer.id) && state.gold >= producer.cost) {
// state.gold -= producer.cost;
// producer.cost *= producer.growthRate;
// producer.count++;
// state.productionRate += producer.baseProduction;
// }
// }
}
let minCost = 0;
for (let i = 0; i < state.producers.length; i++) {
if (minCost === 0 || minCost > state.producers[i].cost) {
minCost = state.producers[i].cost;
}
}
// for (const producer of state.producers) {
// if (minCost === 0 || minCost > producer.cost) {
// minCost = producer.cost;
// }
// }
if (state.gold >= minCost && !state.isInitCompleted) {
checkInitCompleted(term, state);
}
// updateGold(term, state);
}
}
export const handleStateChange = (term, state) => {
return () => updateGold(term, state);
}
=========
functions.js
export const init = (term) => {
term.clear();
term.hideCursor();
term('Welcome to the mining game!');
term.grabInput();
}
export const updateGold = (term, state) => {
state.gold += state.productionRate;
term.moveTo(25, 2);
term.eraseLineAfter();
term.bold.yellow(`${state.gold} `);
term.moveTo(25, 3, `${state.productionRate}`);
}
export const checkInitCompleted = (term, state) => {
state.isInitCompleted = true;
term.moveTo(1, 1);
term.eraseLine();
term('You can purchase producers by clicking the number button (1, 2, 3, ...)');
term.moveTo(1, 2);
term('GOLD:');
term.moveTo(1, 3);
term('PRODUCTION RATE:');
}
=========
constants.js
import terminalKit from 'terminal-kit';
export const term = terminalKit.terminal;
export const config = {
gold: 0,
productionRate: 0,
isInitCompleted: false,
isProducerListUpdated: true,
producers: [
{ id: 1, title: 'Miner', cost: 10, growthRate: 1.13, baseProduction: 0.1, count: 0 } ,
{ id: 2, title: 'Adventurer', cost: 100, growthRate: 1.17, baseProduction: 1, count: 0 },
{ id: 3, title: 'Professional', cost: 1200, growthRate: 1.14, baseProduction: 9, count: 0 }
]
}