Skip to content

Commit

Permalink
Remove uneeded code for random selection
Browse files Browse the repository at this point in the history
  • Loading branch information
rgiot committed Jul 9, 2020
1 parent 741ee64 commit 0586fb6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
22 changes: 13 additions & 9 deletions src/db.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use rand::seq::IteratorRandom;
use rand::rngs::ThreadRng;
use itertools::Itertools;
use rand::Rng;
use rand::random;

pub use std::convert::TryFrom;

include!(concat!(env!("OUT_DIR"), "/", "db.rs"));
Expand Down Expand Up @@ -217,12 +218,15 @@ impl Music {
}

pub fn is_theme_valid(&self) -> bool {
self.selected_theme() < self.header().unwrap().nb_themes()
match self.header() {
Ok(h) => self.selected_theme() < h.nb_themes(),
Err(_) => true
}
}

pub fn select_random_theme(&mut self, rng: &mut ThreadRng) {
pub fn select_random_theme(&mut self) {
self.select_theme(
rng.gen::<u8>() % self.header().unwrap().nb_themes()
random::<u8>() % self.header().unwrap().nb_themes()
)
}

Expand All @@ -249,7 +253,7 @@ impl CpcMix {
/// Return None if:
/// - music is not found
/// - music is found but theme does not exists
pub fn music(&self, key: &str, rng: &mut ThreadRng) -> Option<Music> {
pub fn music(&self, key: &str) -> Option<Music> {

let (key, theme) = if key.contains(":") {
let words = key.split(':').collect::<Vec<&str>>();
Expand All @@ -267,7 +271,7 @@ impl CpcMix {
}))
.and_then(|mut m| {
let theme = if theme == "?" {
m.select_random_theme(rng);
m.select_random_theme();
}
else {
let theme = u8::from_str_radix(theme, 10).unwrap();
Expand All @@ -282,9 +286,9 @@ impl CpcMix {
})
}

pub fn random(&self, rng: &mut ThreadRng) -> Music {
let mut music = self.music(self.keys().choose(rng).unwrap(), rng).unwrap();
music.select_random_theme(rng);
pub fn random(&self) -> Music {
let mut music = self.music(self.keys().choose(&mut rand::thread_rng()).unwrap()).unwrap();
music.select_random_theme();
music
}
}
7 changes: 3 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::thread;
use itertools::Itertools;
use cpcmix;


/// Dummy player
/// M4 must have a directory /tmp
/// cpcmix <cpcip> <command>
Expand Down Expand Up @@ -37,7 +36,7 @@ with <command> having such values:
"list" => {

let repr = mix.keys()
.map(|k| (k, mix.music(k, &mut rand::thread_rng()).unwrap()))
.map(|k| (k, mix.music(k).unwrap()))
.map(|(k, m) | (
k,
m.title().unwrap_or("".to_owned()),
Expand Down Expand Up @@ -68,7 +67,7 @@ with <command> having such values:
);
return;
}
any => mix.music(any, &mut rng)
any => mix.music(any)
};

let music = match music {
Expand All @@ -78,7 +77,7 @@ with <command> having such values:


if let Some(music) = mix.keys()
.map(|k| mix.music(k, &mut rand::thread_rng()).unwrap())
.map(|k| mix.music(k).unwrap())
.filter(|m| {
m.author().unwrap_or("??".to_owned()).to_lowercase() == args[2]
})
Expand Down

0 comments on commit 0586fb6

Please sign in to comment.