From 370779e4b1e58d5b02449bd17831b19825db2e4e Mon Sep 17 00:00:00 2001 From: Mitul Patel Date: Fri, 31 May 2024 17:14:31 -0400 Subject: [PATCH] feat(command): update file name & add other major lotteries --- src/commands/generate.ts | 137 ++++++++++++++++++++++ src/commands/lottery.ts | 76 ------------ test/commands/{lottery.ts => generate.ts} | 0 3 files changed, 137 insertions(+), 76 deletions(-) create mode 100644 src/commands/generate.ts delete mode 100644 src/commands/lottery.ts rename test/commands/{lottery.ts => generate.ts} (100%) diff --git a/src/commands/generate.ts b/src/commands/generate.ts new file mode 100644 index 0000000..8c9dfc9 --- /dev/null +++ b/src/commands/generate.ts @@ -0,0 +1,137 @@ +import {Command, Flags} from '@oclif/core' + +const generateUniqueNumbers = (count: number, min: number, max: number) => { + const numbers = new Set() + while (numbers.size < count) { + const num = Math.floor(Math.random() * (max - min + 1) + min) + numbers.add(num) + } + + return [...numbers] +} + +const generateLotteryNumbers = (lotteryType: string) => { + switch (lotteryType) { + case 'powerball': { + const mainNumbers = generateUniqueNumbers(5, 1, 69) + const powerballNumber = generateUniqueNumbers(1, 1, 26) + return [...mainNumbers, ...powerballNumber] + } + + case 'megamillions': { + const mainNumbers = generateUniqueNumbers(5, 1, 70) + const megaBallNumber = generateUniqueNumbers(1, 1, 25) + return [...mainNumbers, ...megaBallNumber] + } + + case 'euromillions': { + const mainNumbers = generateUniqueNumbers(5, 1, 50) + const luckyStars = generateUniqueNumbers(2, 1, 12) + return [...mainNumbers, ...luckyStars] + } + + case 'uklotto': { + const mainNumbers = generateUniqueNumbers(6, 1, 59) + return mainNumbers + } + + case 'elgordo': { + const mainNumbers = generateUniqueNumbers(5, 0, 54) + return mainNumbers + } + + case 'superenalotto': { + const mainNumbers = generateUniqueNumbers(6, 1, 90) + return mainNumbers + } + + case 'auspowerball': { + const mainNumbers = generateUniqueNumbers(7, 1, 35) + const powerballNumber = generateUniqueNumbers(1, 1, 20) + return [...mainNumbers, ...powerballNumber] + } + + case 'ozlotto': { + const mainNumbers = generateUniqueNumbers(7, 1, 45) + return mainNumbers + } + + case 'canada649': { + const mainNumbers = generateUniqueNumbers(6, 1, 49) + return mainNumbers + } + + case 'canadamax': { + const mainNumbers = generateUniqueNumbers(7, 1, 50) + return mainNumbers + } + + case 'franceloto': { + const mainNumbers = generateUniqueNumbers(5, 1, 49) + const luckyNumber = generateUniqueNumbers(1, 1, 10) + return [...mainNumbers, ...luckyNumber] + } + + case 'germanlotto': { + const mainNumbers = generateUniqueNumbers(6, 1, 49) + const superzahl = generateUniqueNumbers(1, 0, 9) + return [...mainNumbers, ...superzahl] + } + + default: { + throw new Error('Unsupported lottery') + } + } +} + +export default class Lottery extends Command { + static override description = 'Generate lottery numbers for major global lotteries' + + static override examples = [ + '<%= config.bin %> <%= command.id %> --lotto powerball', + '<%= config.bin %> <%= command.id %> --lotto megamillions', + '<%= config.bin %> <%= command.id %> --lotto euromillions', + '<%= config.bin %> <%= command.id %> --lotto uklotto', + '<%= config.bin %> <%= command.id %> --lotto elgordo', + '<%= config.bin %> <%= command.id %> --lotto superenalotto', + '<%= config.bin %> <%= command.id %> --lotto auspowerball', + '<%= config.bin %> <%= command.id %> --lotto ozlotto', + '<%= config.bin %> <%= command.id %> --lotto canada649', + '<%= config.bin %> <%= command.id %> --lotto canadamax', + '<%= config.bin %> <%= command.id %> --lotto franceloto', + '<%= config.bin %> <%= command.id %> --lotto germanlotto', + ] + + static override flags = { + lotto: Flags.string({char: 'l', description: 'type of lottery', required: true}), + } + + public async run(): Promise { + const {flags} = await this.parse(Lottery) + + const lotteryType = flags.lotto + try { + const numbers = generateLotteryNumbers(lotteryType) + const numString = numbers.join(' ') + + const art = ` + ______ + /\\ o o o\\ + /o \\ o o o\\_______ +< >------> o /| + \\ o/ o /_____/o| + \\/______/ |oo| + | o |o/ + |_______|/ + +Here are your ${lotteryType} numbers: + +${numString} + ` + + this.log(art) + } catch (error: any) { + this.error(error.message) + } + } +} diff --git a/src/commands/lottery.ts b/src/commands/lottery.ts deleted file mode 100644 index 488d00d..0000000 --- a/src/commands/lottery.ts +++ /dev/null @@ -1,76 +0,0 @@ -import {Args, Command, Flags} from '@oclif/core' - -const generateUniqueNumbers = (count: number, min: number, max: number) => { - const numbers = new Set() - while (numbers.size < count) { - const num = Math.floor(Math.random() * (max - min + 1) + min) - numbers.add(num) - } - - return [...numbers] -} - -const generateLotteryNumbers = (lotteryType: string) => { - switch (lotteryType) { - case 'powerball': { - const mainNumbers = generateUniqueNumbers(5, 1, 69) - const powerballNumbers = generateUniqueNumbers(1, 1, 26) - return [...mainNumbers, ...powerballNumbers] - } - - case 'megamillions': { - const mainNumbers = generateUniqueNumbers(5, 1, 70) - const megaBallNumbers = generateUniqueNumbers(1, 1, 26) - return [...mainNumbers, megaBallNumbers] - } - - /* TODO: add all global lotteries here */ - default: { - throw new Error('Unsupported lottery type') - } - } -} - -export default class Lottery extends Command { - static override args = { - file: Args.string({description: 'file to read'}), - } - - static override description = 'Generate lottery numbers for major global lotteries' - - static override examples = [ - '<%= config.bin %> <%= command.id %> --type powerball', - '<%= config.bin %> <%= command.id %> --type megamillions', - ] - - static override flags = { - type: Flags.string({char: 't', description: 'type of lottery', required: true}), - } - - public async run(): Promise { - const {flags} = await this.parse(Lottery) - - const lotteryType = flags.type - try { - const numbers = generateLotteryNumbers(lotteryType) - const numString = numbers.join(', ') - - const art = ` - ______ - /\\ o o o\\ - /o \\ o o o\\_______ -< >------> o /| - \\ o/ o /_____/o| - \\/______/ |oo| - | o |o/ - |_______|/ - -Here are your ${lotteryType} numbers: ${numString} - ` - - this.log(art) - } catch (error: any) { - this.error(error.message) - } - } -} diff --git a/test/commands/lottery.ts b/test/commands/generate.ts similarity index 100% rename from test/commands/lottery.ts rename to test/commands/generate.ts