Can read config files now

- Now reads config from `$XDG_CONFIG_HOME/.config/snake/snake.yaml`
- Added error enum from config and try to remove all usages of unwrap
- Bumped version to 0.2.3
This commit is contained in:
Uttarayan Mondal
2021-03-15 22:27:35 +05:30
parent 2a4c4889af
commit 3ddcb84265
6 changed files with 118 additions and 40 deletions
+9 -9
View File
@@ -8,10 +8,9 @@ use std::ops::Sub;
use std::thread::sleep;
use std::time::Duration;
#[derive(Serialize, Deserialize, Clone, Copy)]
#[serde(tag = "type", content = "value")]
#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
pub enum Difficulty {
Linear(f32),
Linear(u8),
Flat,
}
@@ -162,9 +161,10 @@ impl Board {
}
pub fn check_collision(&mut self, snake: &Snake) -> bool {
let (snake_line, snake_col): (u32, u32) = snake.posyx();
if (snake_line >= self.maxlines - 1) || (snake_col >= self.maxcols - 1)
// || (snake_line <= 0)
// || (snake_col <= 0)
if (snake_line >= self.maxlines - 1)
|| (snake_col >= self.maxcols - 1)
|| (snake_line == 0)
|| (snake_col == 0)
{
self.gamestate = GameState::Failed(FailState::Wall);
return true;
@@ -222,7 +222,7 @@ pub struct Snake {
direction: Direction,
difficulty: Difficulty,
grow: bool,
speed: u32,
speed: f32,
last_tail: Option<Cell>,
}
impl Snake {
@@ -275,13 +275,13 @@ impl Snake {
// let time: std::time::Duration =
// std::time::Duration::from_millis((1000 / self.speed) as u64);
// sleep(time);
sleep(Duration::from_millis((1000 / self.speed) as u64));
sleep(Duration::from_millis(1000 / self.speed as u64));
self.smove(self.direction);
}
pub fn scale_difficulty(&mut self) {
match self.difficulty {
Difficulty::Flat => (),
Difficulty::Linear(scale) => self.speed = (self.speed as f32 * scale) as u32,
Difficulty::Linear(scale) => self.speed *= scale as f32 / 256_f32 + 1_f32,
}
}
pub fn grow(&mut self) {
+1 -1
View File
@@ -28,7 +28,7 @@ pub fn start(config: &Config) {
loop {
frontend::draw_snake(&snake, game_win); // always draw snake before board because the snake will clear the game win
frontend::draw_board(&board, game_win);
frontend::_log(&snake, &board);
// frontend::_log(&snake, &board);
if board.check_collision(&snake) {
// Add stuff here to show the score and
// how You lose screen
+2 -2
View File
@@ -1,10 +1,11 @@
extern crate ncurses;
#[macro_use]
extern crate derive_error;
mod game;
mod highscore;
mod menu;
mod settings;
// use game::{Cell, Snake};
// use ncurses::*;
use ncurses::{
curs_set, endwin, getmaxyx, initscr, keypad, noecho, raw, refresh, setlocale, stdscr,
LcCategory, CURSOR_VISIBILITY,
@@ -36,5 +37,4 @@ fn main() {
}
refresh();
endwin();
config.write();
}
+16 -13
View File
@@ -1,7 +1,5 @@
extern crate dirs;
extern crate serde;
// extern crate serde_derive;
extern crate toml;
use serde::{Deserialize, Serialize};
// use serde_derive::{Deserialize, Serialize};
use crate::game::Difficulty;
@@ -10,21 +8,26 @@ use std::io::{Read, Write};
use std::path::PathBuf;
use std::thread::sleep;
#[derive(Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
pub difficulty: Difficulty,
pub speed: u32,
pub speed: f32,
}
#[derive(Debug, Error)]
pub enum ConfigError {
FileSystemError(std::io::Error),
ParsingError(serde_yaml::Error),
}
impl Config {
pub fn new() -> Result<Self, std::io::Error> {
pub fn new() -> Result<Self, ConfigError> {
let config_path = Config::config_path().unwrap();
if config_path.exists() {
let mut config_file = File::open(config_path).unwrap();
let mut config_file_str: String = String::new();
config_file.read_to_string(&mut config_file_str).unwrap();
// let config: Config = toml::from_str(&config_file_str).expect("Error in config file");
let config: Config = match toml::from_str(&config_file_str) {
let config: Config = match serde_yaml::from_str(&config_file_str) {
Ok(config) => config,
Err(e) => {
eprintln!(
@@ -33,7 +36,7 @@ impl Config {
e,
);
eprintln!("Falling back to using the default values");
sleep(std::time::Duration::from_millis(3000));
sleep(std::time::Duration::from_millis(1000));
Config::default()
}
};
@@ -46,11 +49,11 @@ impl Config {
fn default() -> Self {
Self {
difficulty: Difficulty::Flat, //Default set to ten blocks per second
speed: 10,
speed: 10_f32,
}
}
pub fn write(&self) {
let config_file_str: String = toml::to_string(self).unwrap();
pub fn _write(&self) {
let config_file_str: String = serde_yaml::to_string(self).unwrap();
let config_path = Config::config_path().unwrap();
let mut config_file: File =
File::create(config_path).expect("Couldn't open config file to write");
@@ -62,13 +65,13 @@ impl Config {
let config_path: PathBuf = match dirs::config_dir() {
Some(mut path) => {
path.push("snake");
path.push("snake.toml");
path.push("snake.yaml");
path
}
None => {
return Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"snake.toml not found",
"snake.yaml not found",
));
}
};