upload diceroller
This commit is contained in:
commit
de2ce4ed31
5 changed files with 1221 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
target/
|
1145
Cargo.lock
generated
Normal file
1145
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
9
Cargo.toml
Normal file
9
Cargo.toml
Normal file
|
@ -0,0 +1,9 @@
|
|||
[package]
|
||||
name = "roll"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
clap = "4.5.10"
|
||||
rand = "0.8.5"
|
||||
rodio = "0.19.0"
|
BIN
diceroll.mp3
Normal file
BIN
diceroll.mp3
Normal file
Binary file not shown.
66
src/main.rs
Normal file
66
src/main.rs
Normal file
|
@ -0,0 +1,66 @@
|
|||
use clap::{Arg, Command};
|
||||
use rand::Rng;
|
||||
use rodio::{Decoder, OutputStream, Sink};
|
||||
use std::io::Cursor;
|
||||
use std::process;
|
||||
|
||||
fn main() {
|
||||
// Define the CLI using clap
|
||||
let matches = Command::new("Roll")
|
||||
.version("1.0")
|
||||
.author("James Musselman <support+diceroller@musselman.dev>")
|
||||
.about("Rolls dice based on the format nDs")
|
||||
.arg(
|
||||
Arg::new("dice_notation")
|
||||
.value_name("DICE")
|
||||
.help("Sets the dice notation (nDs)")
|
||||
.required(true),
|
||||
)
|
||||
.get_matches();
|
||||
|
||||
// Check for the dice_notation argument
|
||||
let dice_notation = matches
|
||||
.get_one::<String>("dice_notation")
|
||||
.unwrap()
|
||||
.to_uppercase();
|
||||
|
||||
// Parse the dice notation
|
||||
let parts: Vec<&str> = dice_notation.split('D').collect();
|
||||
if parts.len() != 2 {
|
||||
eprintln!("Invalid dice notation. Use 'nDs' format.");
|
||||
process::exit(1);
|
||||
}
|
||||
|
||||
let num_dice: u32 = parts[0].parse().unwrap_or(1);
|
||||
let num_sides: u32 = parts[1].parse().unwrap_or(6);
|
||||
|
||||
// Play the diceroll sound
|
||||
play_dice_roll_sound();
|
||||
|
||||
// Roll the dice
|
||||
let mut rng = rand::thread_rng();
|
||||
let mut results = vec![];
|
||||
|
||||
for _ in 0..num_dice {
|
||||
let roll: u32 = rng.gen_range(1..=num_sides);
|
||||
results.push(roll);
|
||||
}
|
||||
|
||||
// Print the results
|
||||
println!("Rolling {}D{}: {:?}", num_dice, num_sides, results);
|
||||
}
|
||||
|
||||
// Function to play the dice roll sound
|
||||
fn play_dice_roll_sound() {
|
||||
// Embed the MP3 file in the binary
|
||||
let dice_roll_mp3 = include_bytes!("diceroll.mp3");
|
||||
|
||||
let (_stream, stream_handle) = OutputStream::try_default().unwrap();
|
||||
let sink = Sink::try_new(&stream_handle).unwrap();
|
||||
|
||||
let cursor = Cursor::new(dice_roll_mp3);
|
||||
let source = Decoder::new(cursor).unwrap();
|
||||
|
||||
sink.append(source);
|
||||
sink.sleep_until_end();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue