Skip to content

Commit

Permalink
Finally Reproducible Randomness
Browse files Browse the repository at this point in the history
  • Loading branch information
Selyatin committed Nov 19, 2021
1 parent 6e98e55 commit 2106b5f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 39 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ opt-level = 3
anyhow = "1.0.47"
crossterm = "0.22.1"
fastrand = "1.5.0"
lazy_static = "1.4.0"
56 changes: 17 additions & 39 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[macro_use] extern crate lazy_static;

mod screens;
mod socket;
mod types;
Expand All @@ -20,6 +22,13 @@ use std::{
};
use types::{Action, Player, Screen, State, Word};

lazy_static! {
static ref DICTIONARY: Vec<Word> = include_str!("../dictionary.txt")
.split("\n")
.map(|s| Word::new(s, 0, 0))
.collect();
}

fn reset_state(state: &mut State) {
state.socket = None;
state.err = None;
Expand Down Expand Up @@ -100,19 +109,11 @@ fn main_loop(stdout: &mut Stdout, state: &mut State) -> anyhow::Result<()> {
state.session_token = Some(session_token);

state.socket.as_ref().unwrap().init_reader()?;

state.dictionary = DICTIONARY.clone();

let rng = fastrand::Rng::with_seed(state.session_token.unwrap().into());

queue!(stdout, Clear(ClearType::All))?;

for _ in 0..100 {
queue!(stdout, Print(rng.u16(1..100)), Print(' '))?;
}

stdout.flush()?;

thread::sleep(Duration::from_secs(10));

rng.shuffle(&mut state.dictionary);

state
Expand Down Expand Up @@ -149,7 +150,7 @@ fn main_loop(stdout: &mut Stdout, state: &mut State) -> anyhow::Result<()> {
modifiers: KeyModifiers::NONE,
}) => {
reset_state(state);

let rng = fastrand::Rng::new();

rng.shuffle(&mut state.dictionary);
Expand All @@ -173,18 +174,10 @@ fn main_loop(stdout: &mut Stdout, state: &mut State) -> anyhow::Result<()> {

state.socket.as_ref().unwrap().init_reader()?;

state.dictionary = DICTIONARY.clone();

let rng = fastrand::Rng::with_seed(state.session_token.unwrap().into());

queue!(stdout, Clear(ClearType::All))?;

for _ in 0..100 {
queue!(stdout, Print(rng.u16(1..100)), Print(' '))?;
}

stdout.flush()?;

thread::sleep(Duration::from_secs(10));

rng.shuffle(&mut state.dictionary);

state
Expand Down Expand Up @@ -219,6 +212,8 @@ fn main_loop(stdout: &mut Stdout, state: &mut State) -> anyhow::Result<()> {
Ok(())
}



fn main() -> anyhow::Result<()> {
let sock_addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_owned());

Expand All @@ -227,23 +222,10 @@ fn main() -> anyhow::Result<()> {
// Get initial terminal size
let (columns, rows) = terminal::size()?;

let rng = fastrand::Rng::new();

// Include the dictionary file as a string
let dictionary_string = include_str!("../dictionary.txt");

// Parse dictionary
let dictionary: Vec<Word> = dictionary_string
.split("\n")
.map(|s| Word::new(s, 0, rng.u16(0..rows - 1)))
.collect();

drop(dictionary_string);

let mut state = State {
columns,
rows,
dictionary,
dictionary: DICTIONARY.clone(),
sock_addr,
screen: Screen::Main,
players: vec![],
Expand All @@ -267,10 +249,6 @@ fn main() -> anyhow::Result<()> {

stdout.flush()?;

rng.shuffle(&mut state.dictionary);

drop(rng);

loop {
if let Err(err) = main_loop(&mut stdout, &mut state) {
state.err = Some(err);
Expand Down
1 change: 1 addition & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub enum Screen {
Join,
}

#[derive(Clone)]
pub struct Word {
pub value: String,
pub x: u16,
Expand Down

0 comments on commit 2106b5f

Please sign in to comment.