Skip to content

Commit

Permalink
feat: back to cjs cause npx is annoying
Browse files Browse the repository at this point in the history
  • Loading branch information
golota60 committed May 22, 2022
1 parent 71d42a0 commit acd91b1
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 229 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ build
lib
.yalc
yarn-error.log
.termi-clicker/
termi-clicker/

.DS_STORE
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "termi-clicker",
"author": "Szymon Wiszczuk<golota60@gmail.com>",
"version": "0.0.5",
"exports": "./lib/index.mjs",
"type": "module",
"description": "A programming-themed clicker game for your terminal!",
"main": "./lib/index.js",
"license": "MIT",
"homepage": "https://github.com/golota60/termi-clicker#readme",
"repository": {
Expand All @@ -20,7 +20,7 @@
"*": "yarn test"
},
"bin": {
"termi-clicker": "./lib/index.mjs"
"termi-clicker": "./lib/index.js"
},
"engines": {
"node": ">=14.16"
Expand All @@ -37,7 +37,7 @@
},
"scripts": {
"build": "rimraf lib && tsc",
"start": "yarn build && node lib/index.mjs",
"start": "yarn build && node lib/index.js",
"clean": "rimraf lib",
"lint": "yarn eslint --ext '.js,.ts' ./src --fix",
"jest": "jest tests",
Expand All @@ -47,9 +47,9 @@
"release": "standard-version"
},
"dependencies": {
"chalk": "^5.0.1",
"chalk": "^4.0.0",
"inquirer": "^8.2.4",
"log-update": "^5.0.1",
"log-update": "^4.0.0",
"telejson": "^6.0.8"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/gameplay.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import chalk, { ChalkInstance } from 'chalk';
import chalk from 'chalk';
import { initialInfra, upgradesInOrder } from './initialValues.js';
import { createDirAndSaveGame, initGame, loadGame } from './save.js';

Expand All @@ -24,7 +24,7 @@ export interface Infrastructure {
gs: GameState,
// This mirrors genericGetColor
getGenericColor: (base: Infrastructure) => any
) => ChalkInstance;
) => any;
alias: string; // How the value should be displayed
buyKey: number;
multiplier: number;
Expand Down
165 changes: 0 additions & 165 deletions src/index.mts

This file was deleted.

166 changes: 166 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import process from 'process';
import readline from 'readline';
import logUpdate from 'log-update';
import chalk from 'chalk';
import {
equalizeStringArray,
getTabledInfra,
handleGracefulExit,
Message,
renderMessage,
wrapInBorder,
} from './helpers/io.js';
import { initialInfra } from './helpers/initialValues.js';
import {
gameState,
updateGameState,
setupGame,
calcMoneyPerSec,
renderActiveState,
bulkModeState,
swapBulkMode,
getNextUpgrade,
buyUpgrade,
} from './helpers/gameplay.js';
import { createDirAndSaveGame } from './helpers/save.js';

const mainKeyName = 'space';
(async () => {
// this has to be done before effin with stdin
await setupGame();

let stdin = process.stdin;
readline.emitKeypressEvents(stdin);
stdin.setRawMode(true);
stdin.resume();
stdin.setEncoding('utf-8');

let currKey: string;
// let debug: Record<string, any>;

let message: Message | undefined;
stdin.on('keypress', function (key, a) {
const { sequence, name } = a;
if (name !== 't')
updateGameState({ ...gameState, nOfActions: gameState.nOfActions + 1 });

// after each action reset the message
message = undefined;
currKey = name;

const ctrlC = '\x03';
// const ctrlX = '\x18';
// const ctrlZ = '\x1A';

if (name === 'v') {
buyUpgrade();
}

if (name === 't') {
swapBulkMode();
}
if (name === mainKeyName) {
updateGameState({
...gameState,
money: gameState.money + 1 * gameState.clickPower,
});
}
// if (name === shopButton) {
// updateGameState({ ...gameState, mode: 'shop' });
// }

// if (gameState.mode === 'shop') {
// if (name === 'x') updateGameState({ ...gameState, mode: 'main' });

Object.entries(gameState.infrastructure).forEach(([key, val]) => {
if (name === String(val.buyKey)) {
const cost = val.getCost(gameState);
if (gameState.money < cost) {
message = {
type: 'danger',
content: "You don't have enough money to buy that!",
};
} else {
updateGameState({
...gameState,
infrastructure: {
...gameState.infrastructure,
[key]: {
...gameState.infrastructure[key],
level:
gameState.infrastructure[key].level + 1 * gameState.bulkMode,
},
},
money: gameState.money - cost,
});
}
}
});

if (sequence === ctrlC) {
handleGracefulExit();
}
});

const frames = ['-', '\\', '|', '/'];
let index = 0;

//save game each 60sec
setInterval(async () => {
await createDirAndSaveGame();
}, 60000);

setInterval(() => {
const moneyPerSec = calcMoneyPerSec();
updateGameState({ ...gameState, money: gameState.money + moneyPerSec });
}, 1000);

// output loop
setInterval(() => {
const frame = frames[(index = ++index % frames.length)];
index > frames.length && (index = 0); //so that index doesn't get out of hand

const nextUpgrade = getNextUpgrade();
const firstRow = `The game is on! Click '${mainKeyName}' to earn money
Your infrastructure:
${getTabledInfra(gameState.infrastructure, [
['getCost', 'Price'],
['buyKey', 'Upgrade key'],
['getMoneyPerSec', '$ per sec'],
['getPercentage', '% of all income'],
['level', 'Level'],
])}
Next upgrade: ${
`${chalk.yellow(nextUpgrade.name)} - ${
nextUpgrade.price > gameState.money
? chalk.red(`$${nextUpgrade.price}`)
: chalk.green(`$${nextUpgrade.price}`)
} - click 'v' to buy` || 'No upgrades left :('
}
`;

const statusRow = `
Money: ${gameState.money}
Money per second: ${calcMoneyPerSec()}
Last key: ${currKey}
Click power: ${gameState.clickPower}
Number of action in this run: ${gameState.nOfActions}
Current buying amount(click 't' to switch): ${renderActiveState(
gameState.bulkMode,
bulkModeState
)}
`;

logUpdate(
wrapInBorder(`
${equalizeStringArray(firstRow, {
endLineOffset: 1,
}).join('\n')}
${equalizeStringArray(statusRow, {
endLineOffset: 1,
}).join('\n')}`),
message ? renderMessage(message) : ''
);
}, 60);
})();

0 comments on commit acd91b1

Please sign in to comment.