Skip to content

Commit

Permalink
refactor code for offline mode
Browse files Browse the repository at this point in the history
  • Loading branch information
knrt10 committed Dec 10, 2018
1 parent 2cb1474 commit 962a159
Show file tree
Hide file tree
Showing 20 changed files with 698 additions and 226 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ node_modules
package-lock.json
readme.md
.env
dist/
.DS_Store
src/.DS_Store
169 changes: 169 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,172 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


/**
* Module dependencies and files from other folders
*/
const chalk = require("chalk");
const para = require("../../paragraphs/para");
import { prompt } from "inquirer";
import logUpdate = require("log-update");
import { output } from "../functions";
import { randomNum } from "../functions/random";
import { offline } from "../questions";

const stdin: any = process.stdin;
const stdout: any = process.stdout;
let randomNumber: number = randomNum;
let quote: string;
quote = para[randomNumber].para;
if (quote.length < 100) {
quote = para[randomNumber].para + " " + para[randomNumber - 1].para;
}

let stringTyped: string = "";
let gameEnd: boolean = true;
let timeStarted: number = 0;
let time: number = 0;
let wordsPermin: number = 0;

/**
* @function keypress
* @param {Object} chunk
* @param {String} key
*/

function keypress(chunk, key) {
if (key.ctrl && key.name === "c") {
this.stdout.write("\n");
process.exit();
}
if (key && key.name === "backspace") {
if (stringTyped.length === 0) { return; }
stringTyped = stringTyped.slice(0, -1);
} else if (stringTyped.length < quote.length) {
stringTyped += chunk;
}
updateColor();
}

function updateColor() {
// Clearing the terminal for double time error

stdout.write("\u001B[2J\u001B[0;0f");

let updatedString = color(quote, stringTyped);
updatedString += quote.slice(stringTyped.length, quote.length);
const timeColour = "cyan";
let wordsPerminColor = "cyan";
if (wordsPermin < 30) {
wordsPerminColor = "red";
}
logUpdate(
`${updatedString}
wordsPermin: ${chalk[wordsPerminColor](Math.round(wordsPermin * 10) / 10)}
time: ${chalk[timeColour](Math.round(time * 10) / 10)}s`);
}

function color(quote, stringTyped) {
let colouredString = "";
let wrongInput = false;

const quoteLetters = quote.split("");
const typedLetters = stringTyped.split("");
for (let i = 0; i < typedLetters.length; i++) {
// if a single mistake,
// the rest of the coloured string will appear red
if (wrongInput) {
colouredString += chalk.bgRed(quoteLetters[i]);
continue;
}

if (typedLetters[i] === quoteLetters[i]) {
wrongInput = false;
colouredString += chalk.green(quoteLetters[i]);
if (quote === stringTyped) {
gameEnd = true;
}
} else {
wrongInput = true;
colouredString += chalk.bgRed(quoteLetters[i]);
}
}
return colouredString;
}

function Time() {
time = (Date.now() - timeStarted) / 1000;
}

function updateWpm() {
if (stringTyped.length > 0) {
wordsPermin = stringTyped.split(" ").length / (time / 60);
}
}

function randomNumRetry() {
randomNumber = Math.floor((Math.random() * para.length));
quote = para[randomNumber].para;
if (quote.length < 100) {
quote = para[randomNumber].para + " " + para[randomNumber - 1].para;
}
return quote;
}

function gameEnded() {
stdin.removeListener("keypress", keypress);
prompt(offline.question1).then((answers) => {
switch (answers.whatdo) {
case "Retry":
randomNumRetry();
game();
break;
case "Exit":
process.exit();
default:
process.exit();
}
});
}

function game() {
stdout.write("\u001B[2J\u001B[0;0f");
gameEnd = false;
stringTyped = "";
timeStarted = Date.now() + 5000;
wordsPermin = 0;
const startColor = "yellowBright";
// Game Start
const startinterval = setInterval(() => {
const timeInterval = (Math.round((Date.now() - timeStarted) / 1000 * 10) / 10);
logUpdate(`\nGame Starting in ${chalk[startColor](timeInterval)} sec`);
if (timeInterval === 0) {
clearInterval(startinterval);
// displaying the quote
stdout.write("\u001B[2J\u001B[0;0f");
stdout.write(quote);
// Moving cursor to start
stdout.cursorTo(0);

stdin.on("keypress", keypress);
stdin.setRawMode(true);
stdin.resume();
}
}, 1000);

// After game starts

const interval = setInterval(() => {
if (gameEnd) {
gameEnded();
clearInterval(interval);
} else {
Time();
updateWpm();
}
}, 1000);
}

module.exports = game;

26 changes: 26 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const gulp = require("gulp");
const ts = require("gulp-typescript");
const souremaps = require("gulp-sourcemaps");
const tsProject = ts.createProject("tsconfig.json", {
typescript : require("typescript")
})

gulp.task("build", function() {
gulp.src("process.yml")
.pipe(gulp.dest("dist"));

return tsProject.src()
.pipe(souremaps.init())
.pipe(tsProject())
.js
.pipe(souremaps.write())
.pipe(gulp.dest("dist"));
});

gulp.task("watchTask", function () {
gulp.watch("src/**/*.ts", ["build"]);
});

gulp.task("default", gulp.series("build"));
gulp.task("watch", gulp.series("build", "watchTask"));

21 changes: 16 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{
"name": "typeracer-cli",
"version": "1.0.1",
"version": "2.0.0",
"description": "Typing practice from your terminal and features like online competition.",
"main": "index.js",
"main": "dist/index.js",
"bin": {
"typerace": "index.js"
"typerace": "dist/index.js"
},
"scripts": {
"build": "gulp && node dist/index.js",
"test": "jest && standard --fix",
"dev": "nodemon online/server.js",
"dev": "nodemon --no-deprecation --watch 'src/**/*.ts' --ignore 'src/**/*.spec.ts' --exec 'ts-node' src/online/app.ts",
"lint": "tslint -c tslint.json 'src/**/*.ts' --fix",
"start": "node online/server.js"
},
"keywords": [
Expand All @@ -24,15 +26,24 @@
"author": "Kautilya Tripathi <tripathi.kautilya@gmail.com>",
"license": "MIT",
"devDependencies": {
"@types/node": "^10.12.12",
"jest": "^22.4.3",
"standard": "^11.0.1"
"tslint": "^5.11.0",
"nodemon": "^1.18.7",
"standard": "^11.0.1",
"ts-node": "^7.0.1",
"typescript": "^3.2.2"
},
"dependencies": {
"chalk": "^2.4.1",
"cli-table3": "^0.5.0",
"commander": "^2.15.1",
"dotenv": "^5.0.1",
"express": "^4.16.3",
"figlet": "^1.2.1",
"gulp": "^4.0.0",
"gulp-sourcemaps": "^2.6.4",
"gulp-typescript": "^5.0.0",
"inquirer": "^5.2.0",
"log-update": "^2.3.0",
"mongoose": "^5.1.0",
Expand Down
4 changes: 4 additions & 0 deletions process.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apps:
- script : 'app.js'
name : 'TypeRacer-CLI'
node_args: '--inspect=0.0.0.0:5858'
Loading

0 comments on commit 962a159

Please sign in to comment.