Skip to content

Commit 64e8de9

Browse files
committed
Add Fudge support
1 parent de924af commit 64e8de9

1 file changed

Lines changed: 66 additions & 1 deletion

File tree

src/main.rs

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ fn main() {
3939
client.with_framework(StandardFramework::new()
4040
.configure(|c| c.prefix("$"))
4141
.cmd("roll", roll_command)
42-
.cmd("r", roll_command));
42+
.cmd("r", roll_command)
43+
.cmd("fudge", roll_fudge_command)
44+
.cmd("f", roll_fudge_command));
4345

4446
if let Err(why) = client.start() {
4547
println!("An error occurred while running the client: {:?}", why);
@@ -51,6 +53,11 @@ command!(roll_command(_context, message) {
5153
message.reply(&response[..]).expect("failed to send message");
5254
});
5355

56+
command!(roll_fudge_command(_context, message) {
57+
let response = roll_fudge(&message.content);
58+
message.reply(&response[..]).expect("failed to send message");
59+
});
60+
5461
fn roll(message: &String) -> String {
5562
//Stop if the message isn't an ASCII string
5663
//This is because I haven't found an easy way to slice a Unicode string
@@ -195,6 +202,64 @@ fn parse_args(unparsed_args: String) -> Vec<i32> {
195202
parsed_args
196203
}
197204

205+
fn roll_fudge(message: &String) -> String {
206+
if !message.is_ascii() {
207+
return String::from("Failed to roll: Message is not ASCII");
208+
}
209+
210+
let number_of_dice = match message.len() {
211+
//If the command is just "%r" with no arguments (Discord removes trailing spaces), roll 4 dice,
212+
//since that's the most common number of dice to roll in Fate.
213+
2 => 4,
214+
_ => {
215+
//Get the rest of the message, post-%r, as a slice.
216+
//If that slice is a valid u32, use that as the number of dice.
217+
//Else, use 4.
218+
message.as_str()[3..].parse::<i32>().unwrap_or(4)
219+
}
220+
};
221+
222+
//Fill the vector with dice rolls.
223+
let mut roll_results = Vec::new();
224+
225+
for _i in 0 .. number_of_dice {
226+
let mut rng = rand::thread_rng();
227+
228+
roll_results.push(rng.gen_range::<i32>(1, 4) - 2);
229+
}
230+
231+
//total is the number that will be printed, showing all the dice added together.
232+
let mut total: i32 = 0;
233+
234+
//dice_rolls is the visual display, showing the results of each individual die.
235+
let mut dice_rolls = String::new();
236+
237+
dice_rolls.push('`');
238+
239+
for i in roll_results.iter() {
240+
match i {
241+
&-1 => {
242+
total -= 1;
243+
dice_rolls.push('-');
244+
},
245+
&0 => {
246+
dice_rolls.push(' ')
247+
},
248+
&1 => {
249+
total += 1;
250+
dice_rolls.push('+');
251+
},
252+
_ => {
253+
panic!("invalid roll result generated!");
254+
}
255+
}
256+
}
257+
258+
dice_rolls.push('`');
259+
260+
format!("{}. Total: {}", dice_rolls.as_str(), total)
261+
}
262+
198263
fn generate_number(max: i32) -> i32 {
199264
//Get the thread's RNG
200265
let mut rng = rand::thread_rng();

0 commit comments

Comments
 (0)