From 962a159ac2ca2b9c0d78be10217b206c051198ea Mon Sep 17 00:00:00 2001 From: knrt10 Date: Mon, 10 Dec 2018 22:19:18 +0530 Subject: [PATCH] refactor code for offline mode --- .gitignore | 3 + LICENSE | 169 +++++++++++++++++++++++++++ gulpfile.js | 26 +++++ package.json | 21 +++- process.yml | 4 + scripts/game.js | 215 ----------------------------------- scripts/paragraph.js | 6 - src/functions/console.ts | 3 + src/functions/index.ts | 3 + src/functions/random.ts | 7 ++ src/functions/randomQuote.ts | 13 +++ src/game/offlineGame.ts | 194 +++++++++++++++++++++++++++++++ src/index.ts | 38 +++++++ src/online/app.ts | 6 + src/online/server.ts | 0 src/questions/index.ts | 2 + src/questions/offline.ts | 22 ++++ src/questions/online.ts | 141 +++++++++++++++++++++++ tsconfig.json | 21 ++++ tslint.json | 30 +++++ 20 files changed, 698 insertions(+), 226 deletions(-) create mode 100644 gulpfile.js create mode 100644 process.yml delete mode 100644 scripts/game.js delete mode 100644 scripts/paragraph.js create mode 100644 src/functions/console.ts create mode 100644 src/functions/index.ts create mode 100644 src/functions/random.ts create mode 100644 src/functions/randomQuote.ts create mode 100644 src/game/offlineGame.ts create mode 100644 src/index.ts create mode 100644 src/online/app.ts create mode 100644 src/online/server.ts create mode 100644 src/questions/index.ts create mode 100644 src/questions/offline.ts create mode 100644 src/questions/online.ts create mode 100644 tsconfig.json create mode 100644 tslint.json diff --git a/.gitignore b/.gitignore index 54145ec..447d9fc 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ node_modules package-lock.json readme.md .env +dist/ +.DS_Store +src/.DS_Store diff --git a/LICENSE b/LICENSE index 2a63103..a3ad068 100644 --- a/LICENSE +++ b/LICENSE @@ -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; + diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..c4a173c --- /dev/null +++ b/gulpfile.js @@ -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")); + diff --git a/package.json b/package.json index 1527077..14c1209 100644 --- a/package.json +++ b/package.json @@ -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": [ @@ -24,8 +26,13 @@ "author": "Kautilya Tripathi ", "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", @@ -33,6 +40,10 @@ "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", diff --git a/process.yml b/process.yml new file mode 100644 index 0000000..40f83f2 --- /dev/null +++ b/process.yml @@ -0,0 +1,4 @@ +apps: + - script : 'app.js' + name : 'TypeRacer-CLI' + node_args: '--inspect=0.0.0.0:5858' diff --git a/scripts/game.js b/scripts/game.js deleted file mode 100644 index 4cb24b0..0000000 --- a/scripts/game.js +++ /dev/null @@ -1,215 +0,0 @@ -/** -* Module dependencies and files from other folders -*/ -const chalk = require('chalk') -const logUpdate = require('log-update') -const para = require('../paragraphs/para') -let randomNumber = require('./paragraph') -const {prompt} = require('inquirer') -let quote -quote = para[randomNumber].para -if (quote.length < 100) { - quote = para[randomNumber].para + ' ' + para[randomNumber - 1].para -} - -// Creating an interface for reading line from console - -const stdin = process.stdin -const stdout = process.stdout -stdin.setRawMode(true) -stdin.resume() -require('readline').emitKeypressEvents(stdin) -let stringTyped = '' -let gameEnd = true -let timeStarted = 0 -let time = 0 -let wordsPermin = 0 - -// setting questions for retry - -const question1 = [{ - type: 'list', - name: 'whatdo', - message: 'What do you want to do?', - choices: [ - 'Retry', - 'Exit' - ] -}] - -// To clear the terminal -stdout.on('resize', () => { - stdout.write('\u001B[2J\u001B[0;0f') - updateColor() -}) - -/** -* @function keypress -* @param {Object} chunk -* @param {String} key -*/ - -function keypress (chunk, key) { - if (key.ctrl && key.name === 'c') { - 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 -*/ - -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) - let 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 -* @param {String} quote -* @param {String} stringTyped -*/ - -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 -*/ - -function Time () { - time = (Date.now() - timeStarted) / 1000 -} - -/** -* @function updateWpm -*/ - -function updateWpm () { - if (stringTyped.length > 0) { - wordsPermin = stringTyped.split(' ').length / (time / 60) - } -} - -/** -* @function randomNumRetry -*/ - -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 -*/ - -function gameEnded () { - stdin.removeListener('keypress', keypress) - prompt(question1).then(answers => { - switch (answers.whatdo) { - case 'Retry': - randomNumRetry() - game() - break - case 'Exit': - process.exit() - default: - process.exit() - } - }) -} - -/** -* @function game -*/ - -function game () { - stdout.write('\u001B[2J\u001B[0;0f') - gameEnd = false - stringTyped = '' - timeStarted = Date.now() + 5000 - wordsPermin = 0 - let 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 diff --git a/scripts/paragraph.js b/scripts/paragraph.js deleted file mode 100644 index 86a5902..0000000 --- a/scripts/paragraph.js +++ /dev/null @@ -1,6 +0,0 @@ -const para = require('../paragraphs/para') - -// Generating a random paragraph -const random = Math.floor((Math.random() * para.length)) - -module.exports = random diff --git a/src/functions/console.ts b/src/functions/console.ts new file mode 100644 index 0000000..14f9db7 --- /dev/null +++ b/src/functions/console.ts @@ -0,0 +1,3 @@ +export function output(input: any) { + return console.log(input); +} diff --git a/src/functions/index.ts b/src/functions/index.ts new file mode 100644 index 0000000..b01f745 --- /dev/null +++ b/src/functions/index.ts @@ -0,0 +1,3 @@ +export * from "./console"; +export * from "./random"; +export * from "./randomQuote"; diff --git a/src/functions/random.ts b/src/functions/random.ts new file mode 100644 index 0000000..fbd80c0 --- /dev/null +++ b/src/functions/random.ts @@ -0,0 +1,7 @@ +const para: any = require("../../paragraphs/para"); + +/** + * @constant randomNumber + * @exports random number of paragraph + */ +export const randomNumber = Math.floor((Math.random() * para.length)); diff --git a/src/functions/randomQuote.ts b/src/functions/randomQuote.ts new file mode 100644 index 0000000..10b1d86 --- /dev/null +++ b/src/functions/randomQuote.ts @@ -0,0 +1,13 @@ +const para: any = require("../../paragraphs/para"); + +/** + * @function randomNumQuote + */ +export function randomNumQuote() { + const randomNumber: number = Math.floor((Math.random() * para.length)); + let quote: string = para[randomNumber].para; + if (quote.length < 100) { + quote = para[randomNumber].para + " " + para[randomNumber - 1].para; + } + return quote; +} diff --git a/src/game/offlineGame.ts b/src/game/offlineGame.ts new file mode 100644 index 0000000..613fce3 --- /dev/null +++ b/src/game/offlineGame.ts @@ -0,0 +1,194 @@ +/** + * Module dependencies and files from other folders + */ +const chalk = require("chalk"); +import { prompt } from "inquirer"; +import logUpdate = require("log-update"); +import { output } from "../functions"; +import { randomNumQuote } from "../functions/randomQuote"; +import { offline } from "../questions"; + +/** + * @exports Game + * @class + */ + +export class Game { + private static stdin: any; + private static stdout: any; + private static time: number; + private static wordsPermin: number; + private static gameEnd: boolean; + private static timeStarted: number; + private static quote: string; + private static stringTyped: string; + + /** + * @method keypress private + * @static + * @param chunk + * @param key + */ + private static keypress(chunk, key) { + if (key.ctrl && key.name === "c") { + Game.stdout.write("\n"); + process.exit(); + } + if (key && key.name === "backspace") { + if (Game.stringTyped.length === 0) { return; } + Game.stringTyped = Game.stringTyped.slice(0, -1); + } else if (Game.stringTyped.length < Game.quote.length) { + Game.stringTyped += chunk; + } + Game.updateColor(); + } + + /** + * @method updateColor + * @static + */ + private static updateColor() { + // Clearing the terminal for double time error + output("\u001B[2J\u001B[0;0f"); + + let updatedString = Game.color(Game.quote, Game.stringTyped); + updatedString += Game.quote.slice(Game.stringTyped.length, Game.quote.length); + const timeColour = "cyan"; + let wordsPerminColor = "cyan"; + if (Game.wordsPermin < 30) { + wordsPerminColor = "red"; + } + logUpdate( + `${updatedString} + wordsPermin: ${chalk[wordsPerminColor](Math.round(Game.wordsPermin * 10) / 10)} + time: ${chalk[timeColour](Math.round(Game.time * 10) / 10)}s`); + } + + /** + * @method color private + * @static + * @param quote + * @param stringTyped + */ + private static 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) { + this.gameEnd = true; + } + } else { + wrongInput = true; + colouredString += chalk.bgRed(quoteLetters[i]); + } + } + return colouredString; + } + /** + * @method Time private + * @static + */ + private static Time() { + Game.time = (Date.now() - Game.timeStarted) / 1000; + } + + /** + * This counts words per minute typed by user + * @method updateWpm private + */ + private static updateWpm() { + if (Game.stringTyped.length > 0) { + Game.wordsPermin = Game.stringTyped.split(" ").length / (Game.time / 60); + } + } + + /** + * @method gameEnded private + * @static + */ + private static gameEnded() { + Game.stdin.removeListener("keypress", Game.keypress); + prompt(offline.question1).then((answers) => { + switch (answers.whatdo) { + case "Retry": + const game: Game = new Game(randomNumQuote()); + game.game(); + break; + case "Exit": + process.exit(); + default: + process.exit(); + } + }); + } + + /** + * @constructor + */ + constructor(randQuote: string) { + Game.gameEnd = true; + Game.timeStarted = 0; + Game.time = 0; + Game.wordsPermin = 0; + Game.quote = randQuote; + // Creating an interface for reading line from console + Game.stdin = process.stdin; + Game.stdout = process.stdout; + Game.stdin.setRawMode(true); + Game.stdin.resume(); + require("readline").emitKeypressEvents(Game.stdin); + } + + /** + * @method game public + */ + public game() { + output("\u001B[2J\u001B[0;0f"); + Game.gameEnd = false; + Game.stringTyped = ""; + Game.timeStarted = Date.now() + 1000; + Game.wordsPermin = 0; + const startColor = "yellowBright"; + // Game Start + const startinterval = setInterval(() => { + const timeInterval = (Math.round((Date.now() - Game.timeStarted) / 1000 * 10) / 10); + logUpdate(`\nGame Starting in ${chalk[startColor](timeInterval)} sec`); + if (timeInterval === 0) { + clearInterval(startinterval); + // displaying the quote after clearing the terminal + output("\u001B[2J\u001B[0;0f"); + Game.stdout.write(Game.quote); + // Moving cursor to start + Game.stdout.cursorTo(0); + Game.stdin.on("keypress", Game.keypress); + Game.stdin.setRawMode(true); + Game.stdin.resume(); + } + }, 1000); + + // After game starts + + const interval = setInterval(() => { + if (Game.gameEnd) { + Game.gameEnded(); + clearInterval(interval); + } else { + Game.Time(); + Game.updateWpm(); + } + }, 1000); + } + } diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..3497a8c --- /dev/null +++ b/src/index.ts @@ -0,0 +1,38 @@ +#!/usr/bin/env node + +"use strict"; +/* + Import modules +*/ + +import program = require("commander"); +import crypto = require("crypto"); +import figlet = require("figlet"); +import { prompt } from "inquirer"; +import {output} from "./functions"; +import { randomNumQuote } from "./functions/randomQuote"; +import { Game } from "./game/offlineGame"; +import {offline} from "./questions"; +const chalk = require("chalk"); + +// Clearing the terminal +output("\u001B[2J\u001B[0;0f"); + +output( + chalk.red( + figlet.textSync("typeracer-cli", { horizontalLayout: "full" }), + ), +); + +program + .command("practice") + .alias("p") + .description("Starts typeracer in practice mode") + .action(() => { + const game: Game = new Game(randomNumQuote()); + game.game(); + }); + +program.parse(process.argv); + +if (program.args.length === 0) { program.help(); } diff --git a/src/online/app.ts b/src/online/app.ts new file mode 100644 index 0000000..444e5d1 --- /dev/null +++ b/src/online/app.ts @@ -0,0 +1,6 @@ +#!/usr/bin/env node + +function hello(compiler: string) { + console.log(`Hello from ${compiler}`); +} +hello("TypeScript"); diff --git a/src/online/server.ts b/src/online/server.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/questions/index.ts b/src/questions/index.ts new file mode 100644 index 0000000..1ebad0d --- /dev/null +++ b/src/questions/index.ts @@ -0,0 +1,2 @@ +export * from "./offline"; +export * from "./online"; diff --git a/src/questions/offline.ts b/src/questions/offline.ts new file mode 100644 index 0000000..70bd3de --- /dev/null +++ b/src/questions/offline.ts @@ -0,0 +1,22 @@ +/** + * @constant question1 + * Setting Questions for offline mode to retry + */ + +const question1 = [{ + type: "list", + name: "whatdo", + message: "What do you want to do?", + choices: [ + "Retry", + "Exit", + ], +}]; + +/** + * @exports offline + * @constant + */ +export const offline = { + question1, +}; diff --git a/src/questions/online.ts b/src/questions/online.ts new file mode 100644 index 0000000..78dc177 --- /dev/null +++ b/src/questions/online.ts @@ -0,0 +1,141 @@ +/** + * @constant question1 + * @constant question2 + * @constant question3 + * Setting Questions for online mode + */ + +const question1 = [{ + type: "confirm", + name: "join", + message: "Are you starting server for race ?", + default: false, +}]; + +const question2 = [ + { + type: "input", + name: "username", + message: "Enter Username", + validate(value) { + if (!value) { + return "Please enter Username"; + } + return true; + }, + }, + { + type: "input", + name: "roomNumber", + message: "Enter room address that you got above", + validate(value) { + if (!value) { + return "Please enter room address"; + } + return true; + }, + }, + { + type: "input", + name: "number", + message: "Enter number of players (max 5)", + validate(value) { + if (value > 5) { + return "Cannot exceed 5"; + } else if (value <= 1) { + return "Cannot have competition with yourself in online mode"; + } else if (!value) { + return "Please enter a value"; + } else if (isNaN(value) === true) { + return "Please enter number only"; + } + return true; + }, + }, + { + type: "input", + name: "randomNumber", + message: "Enter any number between 1 & 2405 (It's a password), share it with your friends", + validate(value) { + if (value > 2405) { + return "Cannot exceed 2405"; + } else if (value < 1) { + return "Cannot be less than 1"; + } else if (!value) { + return "Please enter a value"; + } else if (isNaN(value) === true) { + return "Please enter number only"; + } + return true; + }, + }, +]; + +const question3 = [ + { + type: "input", + name: "username", + message: "Enter Username", + validate(value) { + if (!value) { + return "Please enter Username"; + } + return true; + }, + }, + { + type: "input", + name: "roomNumber", + message: "Enter room address to join", + validate(value) { + if (!value) { + return "Please enter room address"; + } + return true; + }, + }, + { + type: "input", + name: "number", + message: "(Confirmation) Enter number of players", + validate(value) { + if (value > 5) { + return "Cannot exceed 5"; + } else if (value <= 1) { + return "Cannot have competition with yourself in online mode"; + } else if (!value) { + return "Please enter a value"; + } else if (isNaN(value) === true) { + return "Please enter number only"; + } + return true; + }, + }, + { + type: "input", + name: "randomNumber", + message: "(Confirmation) Enter number when server was created", + validate(value) { + if (value > 2405) { + return "Cannot exceed 2405"; + } else if (value < 1) { + return "Cannot be less than 1"; + } else if (!value) { + return "Please enter a value"; + } else if (isNaN(value) === true) { + return "Please enter number only"; + } + return true; + }, + }, +]; + +/** + * @exports online + * @constant + */ +export const online = { + question1, + question2, + question3, +}; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..b064f32 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "outDir": "./dist", + "moduleResolution": "node", + "baseUrl": "./src", + "sourceMap": true, + "pretty": true, + "strictNullChecks": true, + "module": "commonjs", + "target": "es6", + "allowJs": true + }, + "include": [ + "src/**/*.ts" + ], + "exclude": [ + "node_modules" + ] +} diff --git a/tslint.json b/tslint.json new file mode 100644 index 0000000..ba204fa --- /dev/null +++ b/tslint.json @@ -0,0 +1,30 @@ +{ + "defaultSeverity": "error", + "extends": [ + "tslint:recommended" + ], + "jsRules": {}, + "rules": { + "object-literal-sort-keys": [ + false + ], + "no-console": false, + "only-arrow-functions": [ + false + ], + "max-classes-per-file": [ + true, + 5 + ], + "no-shadowed-variable": false, + "interface-name": [ + false + ], + "max-line-length": [ + true, + 200 + ], + "no-var-requires": false + }, + "rulesDirectory": [] +}