Permalink
Browse files

Bring in chrono for the replay file timestamp

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 f0d387bc4c86b81f9677881e8a2bd8c8ee2fcf09
Showing with 62 additions and 23 deletions.
  1. +23 −0 Cargo.lock
  2. +4 −1 Cargo.toml
  3. +1 −1 src/game.rs
  4. +5 −2 src/main.rs
  5. +29 −19 src/state.rs

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -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 }
@@ -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"]
@@ -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));
@@ -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;
@@ -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(
@@ -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"),
)
};
@@ -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;

@@ -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 {
@@ -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;

0 comments on commit f0d387b

Please sign in to comment.