Record actions in file
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
5a057a49e6
commit
ebabf90ba6
3 changed files with 36 additions and 2 deletions
|
@ -7,6 +7,7 @@ authors = ["Maxime “pep” Buquet <pep@bouah.net>"]
|
||||||
description = "Keep track of your Hanabi plays and analyse them"
|
description = "Keep track of your Hanabi plays and analyse them"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
chrono = "0.4.20"
|
||||||
clap = { version = "3.2.16", features = ["derive"] }
|
clap = { version = "3.2.16", features = ["derive"] }
|
||||||
nom = "7.1.1"
|
nom = "7.1.1"
|
||||||
rustyline = "10.0.0"
|
rustyline = "10.0.0"
|
||||||
|
|
|
@ -17,9 +17,11 @@ use nom;
|
||||||
use rustyline::error::ReadlineError;
|
use rustyline::error::ReadlineError;
|
||||||
use std::error::Error as StdError;
|
use std::error::Error as StdError;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use std::io::Error as IOError;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
|
IOError(IOError),
|
||||||
ParseColorError(String),
|
ParseColorError(String),
|
||||||
ParseDigitError(String),
|
ParseDigitError(String),
|
||||||
NomError(nom::Err<nom::error::Error<String>>),
|
NomError(nom::Err<nom::error::Error<String>>),
|
||||||
|
@ -31,6 +33,7 @@ impl StdError for Error {}
|
||||||
impl fmt::Display for Error {
|
impl fmt::Display for Error {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
|
Error::IOError(err) => write!(f, "IO error: {}", err),
|
||||||
Error::ParseDigitError(err) => write!(f, "Parse digit error: {}", err),
|
Error::ParseDigitError(err) => write!(f, "Parse digit error: {}", err),
|
||||||
Error::ParseColorError(err) => write!(f, "Parse color error: {}", err),
|
Error::ParseColorError(err) => write!(f, "Parse color error: {}", err),
|
||||||
Error::NomError(err) => write!(f, "Nom error: {}", err),
|
Error::NomError(err) => write!(f, "Nom error: {}", err),
|
||||||
|
@ -56,6 +59,12 @@ impl<'a> From<nom::Err<nom::error::Error<&'a str>>> for Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<IOError> for Error {
|
||||||
|
fn from(err: IOError) -> Error {
|
||||||
|
Error::IOError(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<ReadlineError> for Error {
|
impl From<ReadlineError> for Error {
|
||||||
fn from(err: ReadlineError) -> Error {
|
fn from(err: ReadlineError) -> Error {
|
||||||
Error::ReadlineError(err)
|
Error::ReadlineError(err)
|
||||||
|
|
28
src/main.rs
28
src/main.rs
|
@ -23,9 +23,22 @@ use crate::error::Error;
|
||||||
use crate::parser::parse_line;
|
use crate::parser::parse_line;
|
||||||
use crate::types::{Action, GameAction};
|
use crate::types::{Action, GameAction};
|
||||||
|
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
use chrono::{DateTime, Utc};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use rustyline::{error::ReadlineError, Editor};
|
use rustyline::{error::ReadlineError, Editor};
|
||||||
|
|
||||||
|
fn record_actions(filename: &str, actions: Vec<GameAction>) -> Result<(), Error> {
|
||||||
|
let mut file = File::create(filename)?;
|
||||||
|
actions
|
||||||
|
.iter()
|
||||||
|
.map(|action| format!("{}\n", action))
|
||||||
|
.try_for_each(|action| file.write(action.as_bytes()).map(|_| ()))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), Error> {
|
fn main() -> Result<(), Error> {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
println!(
|
println!(
|
||||||
|
@ -33,8 +46,16 @@ fn main() -> Result<(), Error> {
|
||||||
args.players, args.variant
|
args.players, args.variant
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut actions: Vec<GameAction> = vec![];
|
let dt: DateTime<Utc> = Utc::now();
|
||||||
|
let filename = format!(
|
||||||
|
"hanabi_{}p_{}.txt",
|
||||||
|
args.players,
|
||||||
|
dt.format("%Y-%m-%d_%H:%M:%S")
|
||||||
|
);
|
||||||
|
println!("Recording at: {}", filename);
|
||||||
|
|
||||||
let mut rl = Editor::<()>::new()?;
|
let mut rl = Editor::<()>::new()?;
|
||||||
|
let mut actions: Vec<GameAction> = vec![];
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let readline = rl.readline(">> ");
|
let readline = rl.readline(">> ");
|
||||||
|
@ -44,7 +65,10 @@ fn main() -> Result<(), Error> {
|
||||||
if let Ok(action) = parse_line(line.as_str()) {
|
if let Ok(action) = parse_line(line.as_str()) {
|
||||||
println!("Action: {:?}", action);
|
println!("Action: {:?}", action);
|
||||||
match action {
|
match action {
|
||||||
Action::Game(action) => actions.push(action),
|
Action::Game(action) => {
|
||||||
|
actions.push(action);
|
||||||
|
record_actions(filename.clone().as_str(), actions.clone())?
|
||||||
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue