Skip to content

Commit

Permalink
Bring in chrono for the replay file timestamp
Browse files Browse the repository at this point in the history
This makes the whole thing compile (and work) again. And replays are
now behind a feature as well.
  • Loading branch information
tomassedovic committed Dec 13, 2017
1 parent 60f6f76 commit f0d387b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 23 deletions.
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Expand Up @@ -18,6 +18,7 @@ serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"

chrono = { version = "0.4.0", optional = true }
tcod = { git = "https://github.com/tomassedovic/tcod-rs", optional = true }
rustbox = { version = "0.9.0", optional = true }
piston_window = { version = "0.63.0", optional = true }
Expand All @@ -32,10 +33,12 @@ image = "0.12.2"

[features]

default = ["opengl", "cli"]
default = ["opengl", "cli", "replay"]

cli = ["clap"]

replay = ["chrono"]

piston = ["piston_window", "image"]

opengl = ["glium", "image"]
Expand Down
2 changes: 1 addition & 1 deletion src/game.rs
Expand Up @@ -70,7 +70,7 @@ pub fn update(
state.panel_width,
state.display_size,
state.exit_after,
&state::generate_replay_path(),
state::generate_replay_path(),
state.player.invincible,
);
return Some((settings, state));
Expand Down
7 changes: 5 additions & 2 deletions src/main.rs
Expand Up @@ -11,6 +11,9 @@ extern crate serde_json;
#[cfg(feature = "cli")]
extern crate clap;

#[cfg(feature = "replay")]
extern crate chrono;

#[macro_use]
#[cfg(feature = "opengl")]
extern crate glium;
Expand Down Expand Up @@ -287,7 +290,7 @@ fn process_cli_and_run_game(
);
}
let replay_file = match matches.value_of("replay-file") {
Some(file) => Path::new(file).into(),
Some(file) => Some(file.into()),
None => state::generate_replay_path(),
};
State::new_game(
Expand All @@ -296,7 +299,7 @@ fn process_cli_and_run_game(
panel_width,
display_size,
matches.is_present("exit-after"),
&replay_file,
replay_file,
matches.is_present("invincible"),
)
};
Expand Down
48 changes: 29 additions & 19 deletions src/state.rs
Expand Up @@ -13,7 +13,7 @@ use std::collections::VecDeque;
use std::fs::{self, File};
use std::io::{self, BufRead, BufReader, Write};
use std::path::{Path, PathBuf};
use std::time::{self, Duration};
use std::time::Duration;
use timer::Timer;
use world::World;

Expand Down Expand Up @@ -50,24 +50,27 @@ pub enum Command {
}


pub fn generate_replay_path() -> PathBuf {
let cur_time = time::Instant::now();
#[cfg(feature = "replay")]
pub fn generate_replay_path() -> Option<PathBuf> {
use chrono::prelude::*;
let local_time = Local::now();

// Timestamp in format: 2016-11-20T20-04-39.123. We can't use the
// colons in the timestamp -- Windows don't allow them in a path.
let timestamp = format!(
"{}.{:03}",
time::strftime("%FT%H-%M-%S", &cur_time).unwrap(),
(cur_time.tm_nsec / 1000000)
);
let timestamp = local_time.format("%FT%H-%M-%S%.3f");
let replay_dir = &Path::new("replays");
assert!(replay_dir.is_relative());
if !replay_dir.exists() {
fs::create_dir_all(replay_dir).unwrap();
}
let replay_path = &replay_dir.join(format!("replay-{}", timestamp));
replay_path.into()
Some(replay_path.into())
}

#[cfg(not(feature = "replay"))]
pub fn generate_replay_path() -> Option<PathBuf> {
None
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct Verification {
Expand Down Expand Up @@ -195,24 +198,31 @@ impl State {
panel_width: i32,
display_size: Point,
exit_after: bool,
replay_path: &Path,
replay_path: Option<PathBuf>,
invincible: bool,
) -> State {
let commands = VecDeque::new();
let verifications = VecDeque::new();
let seed = rand::random::<u32>();
let mut writer = match File::create(replay_path) {
Ok(f) => f,
Err(msg) => {
panic!(
"Failed to create the replay file at '{:?}'.
let mut writer: Box<Write> = if let Some(replay_path) = replay_path {
match File::create(&replay_path) {
Ok(f) => {
println!("Recording the gameplay to '{}'", replay_path.display());
Box::new(f)
}
Err(msg) => {
panic!(
"Failed to create the replay file at '{:?}'.
Reason: '{}'.",
replay_path.display(),
msg
)
replay_path.display(),
msg
)
}
}
} else {
Box::new(io::sink())
};
println!("Recording the gameplay to '{}'", replay_path.display());

log_seed(&mut writer, seed);
let cheating = false;
let replay = false;
Expand Down

0 comments on commit f0d387b

Please sign in to comment.