Не могу понять в чем ошибка. Исхожу из того, что в config.baseProduction указана производительность в секунду.
functions.js
export const init = (term) => {
term.clear();
term ('Welcome to the mining game!');
term.hideCursor();
term.grabInput();
}
export const updateGold = (term, state) => {
term.moveTo(25, 2);
term.eraseLineAfter();
state.gold += state.productionRate*state.tickSpeed/1000;
term.bold.yellow(formatNumber(state.gold) + ' ');
}
export const checkInitCompleted = (term, state) => {
term.moveTo(1, 1);
term.eraseLineAfter();
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:');
state.isInitCompleted = true;
state.isProducerListUpdated = false;
}
export const updateProducerList = (term, state) => {
let i = 0;
for (let j = 0; j< state.producers.length; j++) {
if (state.producers[j].count > 0) {
term.moveTo(1, 5 + i);
term(state.producers[i].title + ': ' + state.producers[i].count + ' | Production per second: ' + state.producers[i].baseProduction.toFixed(1) + ' | Cost: ' + state.producers[i].cost.toFixed(1));
i++;
}
}
if ((i === 0) || ((i > 0) && (i < state.producers.length))) {
term.moveTo(1, 5 + i);
term(state.producers[i].title + ': ' + state.producers[i].count + ' | Production per second: ' + state.producers[i].baseProduction.toFixed(1) + ' | Cost: ' + state.producers[i].cost.toFixed(1));
}
term.moveTo(25, 3);
term.green(formatNumber(state.productionRate));
state.isProducerListUpdated = true;
}
export const formatNumber = (n) => {
if ( n < 1000 ) {
return n.toFixed(1);
}
if ( (n >= 1000) && (n < 1000000) ) {
return (n/1000).toFixed(2) + 'K';
}
if ( (n >= 1000000) && (n < 1000000000)) {
return (n/1000000).toFixed(2) + 'M';
}
if ( (n >= 1000000000) && (n < 1000000000000)) {
return (n/1000000000).toFixed(2) + 'B';
}
if (n >= 1000000000000) {
return (n/1000000000000).toFixed(2) + 'T';
}
}
handlers.js
import { checkInitCompleted, updateGold, updateProducerList } from "./functions.js";
export const handleKeyPress = (term, state) =>{
return (name, matches, data) => {
if (String.fromCharCode(data.code)==='g'||String.fromCharCode(data.code)==='G') {
state.gold++;
}
let minimalProducerCost = state.producers[0].cost;
for (const i in state.producers) {
if (minimalProducerCost>state.producers[i].cost) {
minimalProducerCost = state.producers[i].cost;
}
}
if ((state.gold>=minimalProducerCost)&&(state.isInitCompleted===false)) {
checkInitCompleted(term, state);
}
for (const i in state.producers) {
if ((String.fromCharCode(data.code)===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;
state.isProducerListUpdated = false;
break;
}
}
if (state.isProducerListUpdated === false) {
updateProducerList(term, state);
}
}
}
export const handleStateChange = (term, state) => {
return () => {
updateGold(term, state);
}
}