Skip to content

Commit

Permalink
Merge pull request #29 from SanjayShetty01/payout-feature
Browse files Browse the repository at this point in the history
Payout feature
  • Loading branch information
SanjayShetty01 authored Nov 12, 2024
2 parents 4017ed3 + 022133a commit 54c6b18
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 22 deletions.
124 changes: 124 additions & 0 deletions 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
Expand Up @@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
figlet-rs = "0.1.5"
colored = "2"
tabled = "0.16.0"

[package.metadata.generate-rpm]
name = "betting_odds_converter"
Expand Down
28 changes: 19 additions & 9 deletions src/betting_odds_calculator.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::io;
use colored::* ;
use crate::prob_functions;
use crate::{prob_functions, utils};

pub fn decimal_prob_calc() {
pub fn decimal_prob_calc(wager: f64) {
println!("{}", "Enter the Decimal Odds: ".blue());

let mut user_entered_decimal_value = String::new();
Expand All @@ -13,9 +13,12 @@ pub fn decimal_prob_calc() {

match user_entered_decimal_value.trim().parse::<f32>() {
Ok(value) => {
println!("{}", "Calculating the Odds...".green());
println!("{}", "Calculating the Values...".green());
let prob : f32 = prob_functions::decimal_prob(value);
println!("{}", format!("The Implied Probability is {}%", prob).green());
let payout : f64 = prob_functions::calculate_payout(prob, wager);
let return_on_bet : f64 = prob_functions::calculate_percentage_return(payout, wager);

utils::display_metrics_table(prob as f64, payout, return_on_bet);
}

Err(_) => {
Expand All @@ -24,7 +27,7 @@ pub fn decimal_prob_calc() {
}
}

pub fn money_prob_calc() {
pub fn money_prob_calc(wager: f64) {
println!("{}", "Enter the Moneyline: ".blue());

let mut user_entered_moneyline_value: String = String::new();
Expand All @@ -35,9 +38,12 @@ pub fn money_prob_calc() {

match user_entered_moneyline_value.trim().parse::<f32>() {
Ok(value) => {
println!("{}", "Calculating the Odds...".green());
println!("{}", "Calculating the Values...".green());
let prob : f32 = prob_functions::moneyline_prob(value);
println!("{}", format!("The Implied Probability is {}%", prob).green());
let payout : f64 = prob_functions::calculate_payout(prob, wager);
let return_on_bet : f64 = prob_functions::calculate_percentage_return(payout, wager);

utils::display_metrics_table(prob as f64, payout, return_on_bet);
}

Err(_) => {
Expand All @@ -46,7 +52,7 @@ pub fn money_prob_calc() {
}
}

pub fn fraction_prob_calc() {
pub fn fraction_prob_calc(wager: f64) {
println!("{}", "Enter the Fractional Odds (e.g., 3/4): ".blue());

let mut user_entered_fraction = String::new();
Expand All @@ -62,9 +68,13 @@ pub fn fraction_prob_calc() {
if parts.len() == 2 {
match (parts[0].trim().parse::<f32>(), parts[1].trim().parse::<f32>()) {
(Ok(numerator), Ok(denominator)) if denominator != 0.0 => {
println!("{}", "Calculating the Values...".green());
let value = numerator / denominator;
let prob: f32 = prob_functions::fraction_prob(value);
println!("{}", format!("The Implied Probability is {}%", prob).green());
let payout : f64 = prob_functions::calculate_payout(prob, wager);
let return_on_bet : f64 = prob_functions::calculate_percentage_return(payout, wager);

utils::display_metrics_table(prob as f64, payout, return_on_bet);
}
_ => {
println!("{}", "Invalid fraction entered. Please try again.".red());
Expand Down
22 changes: 18 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ fn main() {
print!("{}", welcome_note.unwrap());
println!("{}", "Calculates Implied Probability".blue());

let mut wager = utils::get_wager();

let mut exit: bool = false;

while !exit {

let mut num = String::new();

utils::display_main_menu();
Expand All @@ -27,9 +29,19 @@ fn main() {
.read_line(&mut num)
.expect("Failed to read the number");

utils::which_calc_decider(&num.trim().parse::<i32>());
let parsed_num = num.trim().parse::<i32>();

match parsed_num {
Ok(value) => {
utils::which_calc_decider(value, wager);
},
Err(_) => {
println!("Invalid number! Please try again.");
}

};

println!("Press 'x' to exit or any other key to continue: ");
println!("Press 'x' to exit, 'c' to change the wager or any other key to continue: ");

let mut final_call = String::new();
io::stdin()
Expand All @@ -38,6 +50,8 @@ fn main() {

if final_call.trim().eq_ignore_ascii_case("x") {
exit = true;
}
} else if final_call.trim().eq_ignore_ascii_case("c") {
wager = utils::get_wager();
}
}
}
10 changes: 10 additions & 0 deletions src/prob_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ pub fn fraction_prob(fractions: f32) -> f32 {
calculate_prob(fractions)
}

pub fn calculate_payout(implied_prob : f32, wager: f64) -> f64 {
let odds : f64 = (1.0 / (implied_prob as f64)) * 100.0 ;

odds * wager
}

pub fn calculate_percentage_return(payout: f64, wager: f64) -> f64 {
let return_percentage = ((payout - wager) / wager) * 100.0;
return return_percentage
}


#[cfg(test)]
Expand Down
68 changes: 59 additions & 9 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use crate::betting_odds_calculator;
use std::num::ParseIntError;
use colored::*;
use std::io;
use tabled::{ settings::{style::Style, themes::Theme, Color},
Tabled, Table};

pub fn which_calc_decider(choice: &Result<i32, ParseIntError>) {
pub fn which_calc_decider(choice: i32, wager: f64) {
match choice {
Ok(value) => match value {
1 => betting_odds_calculator::money_prob_calc(),
2 => betting_odds_calculator::decimal_prob_calc(),
3 => betting_odds_calculator::fraction_prob_calc(),
_ => println!("Please choose a valid option between 1-3."),
},
Err(_) => println!("Invalid number! Please try again."),
1 => betting_odds_calculator::money_prob_calc(wager),
2 => betting_odds_calculator::decimal_prob_calc(wager),
3 => betting_odds_calculator::fraction_prob_calc(wager),
_ => println!("Please choose a valid option between 1-3."),
}
}


pub fn display_main_menu() {
let odds = vec![
"Moneyline to Implied probability",
Expand All @@ -25,4 +25,54 @@ pub fn display_main_menu() {
for (index, odd) in odds.iter().enumerate() {
println!("{}. {}", index + 1, odd);
}
}

pub fn get_wager() -> f64 {
println!("Enter your wager (Default is 100): ");

let mut wager = String::new();
io::stdin().read_line(&mut wager).expect("Failed to read the number");

match wager.trim().parse::<f64>() {
Ok(value) => value,
Err(_) => {
println!("Invalid input, using default wager of 100");
100.0
}
}
}

#[derive(Tabled)]
struct MetricRow {
metric: &'static str,
value: String,
}

pub fn display_metrics_table(implied_prob: f64, payout: f64, return_percentage: f64) {
let metrics = vec![
MetricRow { metric: "Implied Probability (%)", value: format!("{:.2}",implied_prob) },
MetricRow { metric: "Payout", value: format!("{:.2}", payout) },
MetricRow { metric: "Return (%)", value: format!("{:.2}", return_percentage) },
];

let mut style = Theme::from(Style::extended());
style.set_colors_top(Color::FG_RED);
style.set_colors_bottom(Color::FG_CYAN);
style.set_colors_left(Color::FG_BLUE);
style.set_colors_right(Color::FG_GREEN);
style.set_colors_corner_top_left(Color::FG_BLUE);
style.set_colors_corner_top_right(Color::FG_RED);
style.set_colors_corner_bottom_left(Color::FG_CYAN);
style.set_colors_corner_bottom_right(Color::FG_GREEN);
style.set_colors_intersection_bottom(Color::FG_CYAN);
style.set_colors_intersection_top(Color::FG_RED);
style.set_colors_intersection_right(Color::FG_GREEN);
style.set_colors_intersection_left(Color::FG_BLUE);
style.set_colors_intersection(Color::FG_MAGENTA);
style.set_colors_horizontal(Color::FG_MAGENTA);
style.set_colors_vertical(Color::FG_MAGENTA);

let table = Table::new(metrics).with(style).to_string();

println!("{table}")
}

0 comments on commit 54c6b18

Please sign in to comment.