Skip to content

Commit

Permalink
Merge pull request #8 from jako4295/add-game-features
Browse files Browse the repository at this point in the history
Added a settings page
  • Loading branch information
jako4295 authored Jul 11, 2024
2 parents 640d3ed + f49bda6 commit 3f2fa3a
Show file tree
Hide file tree
Showing 7 changed files with 537 additions and 35 deletions.
171 changes: 171 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ color-eyre = "0.6.3"
crossterm = "0.27.0"
ratatui = "0.26.3"
chrono = "0.4.38"
confy = "0.6.1"
serde_derive = "1.0.203"
serde = "1.0.203"
52 changes: 37 additions & 15 deletions src/game/game_page.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::char_lib::load_chars;
use crate::settings::settings_struct;
use chrono::{DateTime, Duration, Local};
use dict::{Dict, DictIface};

Expand All @@ -16,6 +17,20 @@ pub fn get_dict() -> Dict<bool> {
dict
}

//pub fn load_settings() -> settings_struct::Settings {
// // Load settings from .config/crabtype

// settings_struct::Settings::default()
//}

fn _history() -> Vec<char> {
todo!();
}

fn _future() -> Vec<char> {
todo!();
}

#[derive(Debug)]
pub struct GameLogic {
pub time: Duration,
Expand All @@ -25,13 +40,16 @@ pub struct GameLogic {
pub score: u32,
pub play: bool,
pub current_char: Option<char>,
pub history: Vec<char>,
pub settings: settings_struct::Settings,
}

impl Default for GameLogic {
fn default() -> GameLogic {
let dict: Dict<bool> = get_dict();
let start_t = Local::now();
let load_char = load_chars::load_files_to_vec(dict);
let loaded_settings = settings_struct::Settings::read_config();

GameLogic {
time: Local::now().signed_duration_since(start_t),
Expand All @@ -41,6 +59,8 @@ impl Default for GameLogic {
score: 0,
play: true,
current_char: None,
history: Vec::new(),
settings: Result::expect(loaded_settings, "Did not find settings"),
}
}
}
Expand All @@ -56,7 +76,7 @@ impl GameLogic {
self.play = true;
}
pub fn compare_pressed_char(&mut self, character: char) {
if self.time >= Duration::seconds(30) {
if self.time >= Duration::seconds(self.settings.total_time_sec.into()) {
self.play = false
}
if self.random_char == character {
Expand All @@ -67,29 +87,30 @@ impl GameLogic {

pub fn render(&self, area: Rect, buf: &mut Buffer) {
let title = Title::from(" CrabType ".bold());
// let instructions = Title::from(Line::from(vec![
// " Decrement ".into(),
// "<Left>".blue().bold(),
// " Increment ".into(),
// "<Right>".blue().bold(),
// " Quit ".into(),
// "<Q> ".blue().bold(),
// ]));
let instructions = Title::from(Line::from(vec![
" Exit ".into(),
" <Esc> ".yellow().bold(),
" Restart ".into(),
" <Space> ".yellow().bold(),
]));
let block = Block::default()
.title(title.alignment(Alignment::Center))
// .title(
// instructions
// .alignment(Alignment::Center)
// .position(Position::Bottom),
// )
.title(
instructions
.alignment(Alignment::Center)
.position(Position::Bottom),
)
.borders(Borders::ALL)
.style(Style::default().fg(Color::Yellow).bg(Color::Black))
.border_set(border::THICK);

let text = vec![
text::Line::from(vec![
Span::from("Timer: "),
Span::from(self.time.num_seconds().to_string()),
Span::from("sec / 30 sec"),
Span::from("sec / "),
Span::from(self.settings.total_time_sec.to_string()),
Span::from(" sec"),
]),
text::Line::from(" "),
text::Line::from(vec![
Expand All @@ -106,6 +127,7 @@ impl GameLogic {
Paragraph::new(text)
.centered()
.block(block)
.fg(Color::White)
.render(area, buf);
} else {
Paragraph::new(text2)
Expand Down
1 change: 1 addition & 0 deletions src/settings/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod settings_page;
pub mod settings_struct;
Loading

0 comments on commit 3f2fa3a

Please sign in to comment.