-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(command): generate lottery command & add basic functionality
- Loading branch information
Showing
7 changed files
with
133 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
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<void> { | ||
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) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {Args, Command, Flags} from '@oclif/core' | ||
|
||
export default class Lottery extends Command { | ||
static override args = { | ||
file: Args.string({description: 'file to read'}), | ||
} | ||
|
||
static override description = 'describe the command here' | ||
|
||
static override examples = [ | ||
'<%= config.bin %> <%= command.id %>', | ||
] | ||
|
||
static override flags = { | ||
// flag with no value (-f, --force) | ||
force: Flags.boolean({char: 'f'}), | ||
// flag with a value (-n, --name=VALUE) | ||
name: Flags.string({char: 'n', description: 'name to print'}), | ||
} | ||
|
||
public async run(): Promise<void> { | ||
const {args, flags} = await this.parse(Lottery) | ||
|
||
const name = flags.name ?? 'world' | ||
this.log(`hello ${name} from /Users/bittricky/Projects/lottery-cli/src/commands/lottery.ts`) | ||
if (args.file && flags.force) { | ||
this.log(`you input --force and --file: ${args.file}`) | ||
} | ||
} | ||
} |