Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Basic CLI in place, can roll dice! #167

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 201 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]

members = [
"cli",
"core",
"ui",
"server"
Expand Down
8 changes: 8 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "cli"
version = "0.1.0"
edition = "2021"

[dependencies]
core = { path = "../core" }
clap = { version = "4.5.4", features = ["derive"] }
79 changes: 79 additions & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use clap::{Parser, Subcommand};
use std::num::NonZeroU8;

use core::{Campaign, DiceType, Roll};

/// Dicebag's CLI interface
#[derive(Parser)]
#[command(name = "Dicebag CLI")]
#[command(version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
command: Option<Commands>,
}

#[derive(Subcommand)]
enum Commands {
Campaign {
/// Name of the campaign
#[arg(short, long)]
name: String,

/// Description for the campaign
#[arg(short, long)]
description: String,
},
Player {
#[arg(short, long)]
name: String,
},
Roll {
/// Dice type to roll (d4, d6, d8, d10, d12, d20, d100)
#[arg(short, long)]
dice: String,

/// Number of dice to roll
#[arg(short, long)]
number: Option<NonZeroU8>,
},
}

fn main() {
let cli = Cli::parse();

match &cli.command {
Some(Commands::Campaign { name, description }) => {
let campaign = Campaign::new(0, name.clone(), description.clone());
// TODO: Save the campaign
println!("{}: {}", campaign.name(), campaign.description());
}
Some(Commands::Player { name }) => {
// TODO: Save the player
println!("Hello, {}!", name);
}
Some(Commands::Roll { dice, number }) => match dice.parse::<DiceType>() {
Ok(d) => {
// TODO: Store history somewhere
if number.is_none() {
println!("Rolling a single {:?}...", d);
let result = Roll::roll_one(d);
println!("Result: {}", result);
} else if let Some(n) = number {
let result = Roll::roll(&Roll {
number: *n,
dice: d,
});
let total = result.iter().sum::<i64>();
println!("Rolling a {:?}, {:?}, total = {}", d, result, total);
}
}
Err(d) => {
println!("Invalid dice type: {}", d);
}
},
None => {
// TODO: Start the application
println!("Start the app!");
}
}
}
Loading
Loading