Skip to content

Commit

Permalink
feat(command): generate lottery command & add basic functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
bittricky committed May 31, 2024
1 parent db08ceb commit bd4050c
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 82 deletions.
47 changes: 27 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
lottery-cli
=================
# lottery-cli

Command line tool to generate global lottery numbers


[![oclif](https://img.shields.io/badge/cli-oclif-brightgreen.svg)](https://oclif.io)
[![Version](https://img.shields.io/npm/v/lottery-cli.svg)](https://npmjs.org/package/lottery-cli)
[![Downloads/week](https://img.shields.io/npm/dw/lottery-cli.svg)](https://npmjs.org/package/lottery-cli)

<!-- [![Version](https://img.shields.io/npm/v/lottery-cli.svg)](https://npmjs.org/package/lottery-cli)
[![Downloads/week](https://img.shields.io/npm/dw/lottery-cli.svg)](https://npmjs.org/package/lottery-cli) -->

<!-- toc -->
* [Usage](#usage)
* [Commands](#commands)

- [Usage](#usage)
- [Commands](#commands)
<!-- tocstop -->

# Usage

<!-- usage -->

```sh-session
$ npm install -g lottery-cli
$ lottery COMMAND
Expand All @@ -26,22 +28,26 @@ USAGE
$ lottery COMMAND
...
```

<!-- usagestop -->

# Commands

<!-- commands -->
* [`lottery hello PERSON`](#lottery-hello-person)
* [`lottery hello world`](#lottery-hello-world)
* [`lottery help [COMMAND]`](#lottery-help-command)
* [`lottery plugins`](#lottery-plugins)
* [`lottery plugins add PLUGIN`](#lottery-plugins-add-plugin)
* [`lottery plugins:inspect PLUGIN...`](#lottery-pluginsinspect-plugin)
* [`lottery plugins install PLUGIN`](#lottery-plugins-install-plugin)
* [`lottery plugins link PATH`](#lottery-plugins-link-path)
* [`lottery plugins remove [PLUGIN]`](#lottery-plugins-remove-plugin)
* [`lottery plugins reset`](#lottery-plugins-reset)
* [`lottery plugins uninstall [PLUGIN]`](#lottery-plugins-uninstall-plugin)
* [`lottery plugins unlink [PLUGIN]`](#lottery-plugins-unlink-plugin)
* [`lottery plugins update`](#lottery-plugins-update)

- [`lottery hello PERSON`](#lottery-hello-person)
- [`lottery hello world`](#lottery-hello-world)
- [`lottery help [COMMAND]`](#lottery-help-command)
- [`lottery plugins`](#lottery-plugins)
- [`lottery plugins add PLUGIN`](#lottery-plugins-add-plugin)
- [`lottery plugins:inspect PLUGIN...`](#lottery-pluginsinspect-plugin)
- [`lottery plugins install PLUGIN`](#lottery-plugins-install-plugin)
- [`lottery plugins link PATH`](#lottery-plugins-link-path)
- [`lottery plugins remove [PLUGIN]`](#lottery-plugins-remove-plugin)
- [`lottery plugins reset`](#lottery-plugins-reset)
- [`lottery plugins uninstall [PLUGIN]`](#lottery-plugins-uninstall-plugin)
- [`lottery plugins unlink [PLUGIN]`](#lottery-plugins-unlink-plugin)
- [`lottery plugins update`](#lottery-plugins-update)

## `lottery hello PERSON`

Expand Down Expand Up @@ -393,4 +399,5 @@ DESCRIPTION
```

_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v5.1.3/src/commands/plugins/update.ts)_

<!-- commandsstop -->
25 changes: 0 additions & 25 deletions src/commands/hello/index.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/commands/hello/world.ts

This file was deleted.

76 changes: 76 additions & 0 deletions src/commands/lottery.ts
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)
}
}
}
9 changes: 0 additions & 9 deletions test/commands/hello/index.test.ts

This file was deleted.

9 changes: 0 additions & 9 deletions test/commands/hello/world.test.ts

This file was deleted.

30 changes: 30 additions & 0 deletions test/commands/lottery.ts
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}`)
}
}
}

0 comments on commit bd4050c

Please sign in to comment.