Skip to content

Commit

Permalink
consultar aceita mais de uma aposta atraves do terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
cleissonalves committed Dec 24, 2024
1 parent 26559c7 commit 90e3632
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 195 deletions.
16 changes: 10 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ codegen-units = 1 # Allow for maximum size reduction optimizations.
# panic = "abort" # remove unwinds of stack on panics to produce helpful backtraces. Use with caution, possible impact on behaviour.

[dependencies]
anyhow = "1.0.93"
anyhow = "1.0.95"
atty = "0.2.14"
clap = { version = "4.5.21", features = ["derive"] }
colored = "2.1.0"
clap = { version = "4.5.23", features = ["derive"] }
colored = "2.2.0"
# indicatif = "0.17.9"
# derive_more = { version = "1.0.0", features = ["display"] }
minreq = { version = "2.12.0", features = ["https"] }
serde = { version = "1.0.215", features = ["derive"] }
serde_json = "1.0.133"
minreq = { version = "2.13.0", features = ["https"] }
serde = { version = "1.0.216", features = ["derive"] }
serde_json = "1.0.134"
strum = { version = "0.26.3", features = ["derive"] }

[lints.clippy]
bool_comparison = "allow"
zero_prefixed_literal = "allow"
25 changes: 14 additions & 11 deletions src/cli_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ pub enum Commands {
#[clap(about = "Consultar ultimo sorteio e comparar com aposta(s).")]
Consultar {
jogo: data::Jogo,
#[clap(help = "Opcional: Numeros de uma aposta. Ex. 04-08-15-16-23-42")]
numeros: Option<String>,
#[clap(help = "Opcional: Apostas. Ex. 04-08-15-16-23-42 03-07-14-15-22-41")]
numeros: Option<Vec<String>>,
},
#[clap(about = "Mostrar historico de sorteios.")]
Historico {
Expand Down Expand Up @@ -71,31 +71,34 @@ pub fn run() -> Result<()> {
Ok(())
}

fn consultar(jogo: &data::Jogo, numeros: &Option<String>) -> Result<()> {
fn consultar(jogo: &data::Jogo, numeros: &Option<Vec<String>>) -> Result<()> {
match numeros {
Some(nums) => {
let numeros = parsers::vec_u8_from_str(&nums)?;
jogo.validar(&numeros)?;
commands::consultar_numeros(&jogo, &vec![numeros.as_slice()])?;
let apostas = parsers::vstrings_to_vu8s(nums)?;
for aposta in &apostas {
jogo.validar(aposta)?;
}
let apostas_refs: Vec<&[u8]> = apostas.iter().map(|v| v.as_slice()).collect();
commands::consultar_numeros(jogo, &apostas_refs)?;
}
None => {
if atty::isnt(atty::Stream::Stdin) {
// the stdin does not come directly from the terminal, but a file redirection (<)
let numbers_from_file = parsers::vec_u8_from_buffer()?;
let numbers_refs: Vec<&[u8]> =
numbers_from_file.iter().map(|v| v.as_slice()).collect();
commands::consultar_numeros(&jogo, &numbers_refs)?;
commands::consultar_numeros(jogo, &numbers_refs)?;
} else {
commands::consultar(&jogo)?;
commands::consultar(jogo)?;
}
}
}
Ok(())
}

fn analisar(jogo: &data::Jogo, numeros: &String, quantidade: usize) -> Result<()> {
let numeros = parsers::vec_u8_from_str(&numeros)?;
fn analisar(jogo: &data::Jogo, numeros: &str, quantidade: usize) -> Result<()> {
let numeros = parsers::vec_u8_from_str(numeros)?;
jogo.validar(&numeros)?;
commands::analisar(&jogo, numeros.as_slice(), quantidade)?;
commands::analisar(jogo, numeros.as_slice(), quantidade)?;
Ok(())
}
4 changes: 2 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use anyhow::{bail, Context, Ok, Result};
use std::fs::File;

// https://github.com/guto-alves/loterias-api
pub const API_PATH: &'static str = "https://loteriascaixa-api.herokuapp.com/api/";
pub const API_PATH: &str = "https://loteriascaixa-api.herokuapp.com/api/";

pub fn fetch_latest(jogo: &Jogo) -> Result<Sorteio> {
let request_url = format!("{}{}/latest", API_PATH, jogo);
Expand Down Expand Up @@ -53,7 +53,7 @@ pub fn download_json(jogo: &Jogo) -> Result<()> {
let content = response.as_str().context("Parsing Response to json str")?;

let json: serde_json::Value =
serde_json::from_str(&content).context("Deserializing json str to serde_json::Value")?;
serde_json::from_str(content).context("Deserializing json str to serde_json::Value")?;

let mut file = File::create(format!("{jogo}.json")).context("Opening File")?;

Expand Down
42 changes: 21 additions & 21 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use anyhow::Result;
use colored::{ColoredString, Colorize};

pub fn consultar_numeros(jogo: &Jogo, apostas: &Vec<&[u8]>) -> Result<()> {
let sorteio = client::fetch_latest(&jogo)?;
let sorteio = client::fetch_latest(jogo)?;

let show_aposta_id = apostas.len() > 1;

Expand Down Expand Up @@ -44,7 +44,7 @@ pub fn consultar_numeros(jogo: &Jogo, apostas: &Vec<&[u8]>) -> Result<()> {
}

pub fn consultar(jogo: &Jogo) -> Result<()> {
let sorteio = client::fetch_latest(&jogo)?;
let sorteio = client::fetch_latest(jogo)?;

print_sorteio_info(&sorteio);

Expand Down Expand Up @@ -99,7 +99,7 @@ fn analisar_sorteio(aposta: &[u8], sorteio: &Sorteio) -> (u8, Vec<ColoredString>

for n in &sorteio.numeros {
let n_string = format!("{:02}", n).to_string();
if aposta.contains(&n) {
if aposta.contains(n) {
matches += 1;
output.push(n_string.bright_green());
} else {
Expand All @@ -125,10 +125,10 @@ fn analisar_sorteio(aposta: &[u8], sorteio: &Sorteio) -> (u8, Vec<ColoredString>
(matches, output)
}

fn analisar_sorteios(jogo: &Jogo, aposta: &[u8], sorteios: &Vec<Sorteio>) {
fn analisar_sorteios(jogo: &Jogo, aposta: &[u8], sorteios: &[Sorteio]) {
let matches: Vec<(u8, Vec<ColoredString>)> = sorteios
.iter()
.map(|s| analisar_sorteio(&aposta, &s))
.map(|s| analisar_sorteio(aposta, s))
.collect();

let prize_matches = jogo.get_prize_matches();
Expand All @@ -146,7 +146,7 @@ fn analisar_sorteios(jogo: &Jogo, aposta: &[u8], sorteios: &Vec<Sorteio>) {
.map(|s| s.1.clone())
.collect();

if sorteios_com_n_acertos.len() == 0 {
if sorteios_com_n_acertos.is_empty() {
continue;
}

Expand All @@ -160,28 +160,28 @@ fn analisar_sorteios(jogo: &Jogo, aposta: &[u8], sorteios: &Vec<Sorteio>) {
for m in s {
print!("{} ", m);
}
print!("\n");
println!();
}
print!("\n");
println!();
}

print!("Total de premiacoes: {}\n", matches.len());
println!("Total de premiacoes: {}", matches.len());
}

pub fn historico(jogo: &Jogo, quantidade: usize) -> Result<()> {
let mut sorteios: Vec<Sorteio>;

if file::is_update_needed(&jogo).unwrap_or(true) {
let content = client::fetch_all(&jogo)?;
if file::is_update_needed(jogo).unwrap_or(true) {
let content = client::fetch_all(jogo)?;
file::save_json(&content)?;
}

if let Ok(value) = file::load_json(&jogo) {
if let Ok(value) = file::load_json(jogo) {
sorteios = value;
} else {
let content = client::fetch_all(&jogo)?;
let content = client::fetch_all(jogo)?;
file::save_json(&content)?;
sorteios = file::load_json(&jogo)?;
sorteios = file::load_json(jogo)?;
}

if quantidade > 0 {
Expand Down Expand Up @@ -246,7 +246,7 @@ pub fn get_numeros_e_ocorrencias(sorteios: &Vec<Sorteio>) -> Vec<(u8, u32)> {
}

pub fn imprimir_numeros_mais_sorteados(sorteios: &Vec<Sorteio>, quantidade: usize) {
let ocorrencias = get_numeros_e_ocorrencias(&sorteios);
let ocorrencias = get_numeros_e_ocorrencias(sorteios);
let ocorrencias = ocorrencias.iter().take(quantidade);

let mut output = Vec::new();
Expand All @@ -267,17 +267,17 @@ pub fn imprimir_numeros_mais_sorteados(sorteios: &Vec<Sorteio>, quantidade: usiz
pub fn analisar(jogo: &Jogo, numeros: &[u8], quantidade: usize) -> Result<()> {
let mut sorteios: Vec<Sorteio>;

if file::is_update_needed(&jogo).unwrap_or(true) {
let content = client::fetch_all(&jogo)?;
if file::is_update_needed(jogo).unwrap_or(true) {
let content = client::fetch_all(jogo)?;
file::save_json(&content)?;
}

if let Ok(value) = file::load_json(&jogo) {
if let Ok(value) = file::load_json(jogo) {
sorteios = value;
} else {
let content = client::fetch_all(&jogo)?;
let content = client::fetch_all(jogo)?;
file::save_json(&content)?;
sorteios = file::load_json(&jogo)?;
sorteios = file::load_json(jogo)?;
}

if quantidade > 0 {
Expand All @@ -286,7 +286,7 @@ pub fn analisar(jogo: &Jogo, numeros: &[u8], quantidade: usize) -> Result<()> {

sorteios.reverse();

analisar_sorteios(&jogo, &numeros, &sorteios);
analisar_sorteios(jogo, numeros, &sorteios);

println!("\nPara mais resultados, adicione a opcao '--quantidade <valor>' ou '-q <valor>'.");

Expand Down
Loading

0 comments on commit 90e3632

Please sign in to comment.