Add strike timeout setting

This commit is contained in:
2022-10-17 07:42:17 +02:00
parent 8c1a3f2c5f
commit ba1ed84b33
6 changed files with 84 additions and 12 deletions

View File

@ -5,6 +5,9 @@ use std::time::{Duration, Instant};
use crossterm::event;
use crossterm::event::{Event, KeyCode};
use sea_battle_backend::consts::{
MAX_BOATS_NUMBER, MAX_MAP_HEIGHT, MAX_MAP_WIDTH, MAX_STRIKE_TIMEOUT,
};
use tui::backend::Backend;
use tui::layout::{Constraint, Direction, Layout, Margin};
use tui::style::{Color, Style};
@ -26,6 +29,7 @@ enum EditingField {
MapWidth = 0,
MapHeight,
BoatsList,
StrikeTimeout,
BoatsCanTouch,
PlayerContinueOnHit,
BotType,
@ -114,23 +118,45 @@ impl GameRulesConfigurationScreen {
{
self.rules.remove_last_boat();
}
if self.curr_field == EditingField::StrikeTimeout {
match self.rules.strike_timeout.unwrap_or(0) / 10 {
0 => self.rules.strike_timeout = None,
v => self.rules.strike_timeout = Some(v),
}
}
}
KeyCode::Char(c) if ('0'..='9').contains(&c) => {
let val = c.to_string().parse::<usize>().unwrap_or_default();
if self.curr_field == EditingField::MapWidth {
self.rules.map_width *= 10;
self.rules.map_width += val;
if self.rules.map_width <= MAX_MAP_WIDTH {
self.rules.map_width *= 10;
self.rules.map_width += val;
}
}
if self.curr_field == EditingField::MapHeight {
self.rules.map_height *= 10;
self.rules.map_height += val;
if self.rules.map_height <= MAX_MAP_HEIGHT {
self.rules.map_height *= 10;
self.rules.map_height += val;
}
}
if self.curr_field == EditingField::BoatsList {
self.rules.add_boat(val);
if self.rules.boats_list().len() < MAX_BOATS_NUMBER {
self.rules.add_boat(val);
}
}
if self.curr_field == EditingField::StrikeTimeout {
let mut timeout = self.rules.strike_timeout.unwrap_or(0);
if timeout <= MAX_STRIKE_TIMEOUT {
timeout *= 10;
timeout += val;
self.rules.strike_timeout = Some(timeout);
}
}
}
@ -153,7 +179,7 @@ impl GameRulesConfigurationScreen {
}
fn ui<B: Backend>(&mut self, f: &mut Frame<B>) {
let area = centered_rect_size(50, 20, &f.size());
let area = centered_rect_size(50, 23, &f.size());
let block = Block::default().title("Game rules").borders(Borders::ALL);
f.render_widget(block, area);
@ -161,11 +187,12 @@ impl GameRulesConfigurationScreen {
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(3),
Constraint::Length(3),
Constraint::Length(3),
Constraint::Length(1),
Constraint::Length(1),
Constraint::Length(3), // Map width
Constraint::Length(3), // Map height
Constraint::Length(3), // Boats list
Constraint::Length(3), // Strike timeout
Constraint::Length(1), // Boats can touch
Constraint::Length(1), // Player continue on hit
Constraint::Length(3), // Bot type
Constraint::Length(1), // Margin
Constraint::Length(1), // Buttons
@ -203,6 +230,13 @@ impl GameRulesConfigurationScreen {
);
f.render_widget(editor, chunks[EditingField::BoatsList as usize]);
let editor = TextEditorWidget::new(
"Strike timeout (0 to disable)",
&self.rules.strike_timeout.unwrap_or(0).to_string(),
self.curr_field == EditingField::StrikeTimeout,
);
f.render_widget(editor, chunks[EditingField::StrikeTimeout as usize]);
let editor = CheckboxWidget::new(
"Boats can touch",
self.rules.boats_can_touch,