Skip to content

Commit

Permalink
minor tweaks, blue board, more letters
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed Mar 9, 2022
1 parent a151c68 commit 3af7c80
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 30 deletions.
Binary file modified letters.xcf
Binary file not shown.
5 changes: 5 additions & 0 deletions networking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
io::{Read, Write},
path::Path,
sync::Arc,
time::Duration,
};

use anyhow::anyhow;
Expand Down Expand Up @@ -94,6 +95,10 @@ impl Networking {
.await?;
save_json(base_path.as_ref().join("node-identity.json"), node.node_identity_ref())?;

node.connectivity()
.wait_for_connectivity(Duration::from_secs(30))
.await?;

let worker = Self {
dht,
in_msg,
Expand Down
Binary file modified ui/assets/letters.bmp
Binary file not shown.
15 changes: 12 additions & 3 deletions ui/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ impl Color {
impl Color {
pub const fn white() -> Self {
Self {
r: 0xff,
g: 0xff,
b: 0xff,
r: 255,
g: 253,
b: 253,
a: 0xff,
}
}
Expand Down Expand Up @@ -75,6 +75,15 @@ impl Color {
}
}

pub const fn dark_blue() -> Self {
Self {
r: 0,
g: 81,
b: 101,
a: 0xff,
}
}

pub const fn grey(v: u8) -> Self {
Self {
r: v,
Expand Down
16 changes: 14 additions & 2 deletions ui/src/components/listbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl ListBox {
r.set_border(2, Color::light_grey());
r
},
is_active: true,
is_active: false,
selected: 0,
}
}
Expand Down Expand Up @@ -61,8 +61,20 @@ impl ListBox {
pub fn update(&mut self, window: &Window) {
if let Some((x, y)) = window.get_mouse_pos(MouseMode::Discard) {
if window.get_mouse_down(MouseButton::Left) {
let active = self.is_in_boundary(x.round() as u32, y.round() as u32);
let x = x.round() as u32;
let y = y.round() as u32;
let active = self.is_in_boundary(x, y);
self.set_active(active);
if active {
if let Some(rel_y) = y.checked_sub(self.rect.y()) {
if rel_y <= self.rect.h() {
let idx = (rel_y / 25) as usize;
if idx < self.values.len() {
self.selected = idx;
}
}
}
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions ui/src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ impl GameCollection {
.games
.drain(..)
.enumerate()
// Keep last 10, after that only keep active games
.filter(|(i, g)| *i < 10 || !g.has_completed())
// Keep last 3, after that only keep active games
.filter(|(i, g)| *i < 3 || !g.has_completed())
.map(|(_, g)| g)
.collect();
}
Expand All @@ -71,7 +71,7 @@ impl GameCollection {
}

pub fn sort(&mut self) {
self.games.sort_by(|a, b| a.last_activity.cmp(&b.last_activity));
self.games.sort_by(|a, b| b.last_activity.cmp(&a.last_activity));
}
}

Expand Down
25 changes: 22 additions & 3 deletions ui/src/game_screen.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::path::PathBuf;
use std::{
fmt::{Display, Formatter},
path::PathBuf,
};

use minifb::{MouseButton, MouseMode, Window};
use pleco::{BitMove, Player};
Expand Down Expand Up @@ -130,13 +133,18 @@ impl GameScreen {
.set_text(format!("MOVE: {}", self.seq))
.set_bg_color(Color::black());

let mut label3 = Label::new(Frame::new(self.board.height() + 10, 130, 100, 20));
let mut label3 = Label::new(Frame::new(self.board.height() + 10, 140, 100, 20));
label3
.set_text(format!("Turn: {}", self.board.turn()))
.set_bg_color(Color::black());

let mut label4 = Label::new(Frame::new(self.board.height() + 10, 170, 100, 20));
label4
.set_text(format!("Status: {}", self.state().game_status()))
.set_bg_color(Color::black());

Drawables {
items: vec![label1, label2, label3],
items: vec![label1, label2, label3, label4],
}
}
}
Expand Down Expand Up @@ -273,3 +281,14 @@ impl Default for GameStatus {
Self::InProgress
}
}

impl Display for GameStatus {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
GameStatus::InProgress => write!(f, "In progress"),
GameStatus::StaleMate => write!(f, "Stale mate"),
GameStatus::CheckMate(player) => write!(f, "Checkmate! {} won", player),
GameStatus::Resign(player) => write!(f, "{} resigned", player),
}
}
}
3 changes: 3 additions & 0 deletions ui/src/letters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ fn init_letters_sprite() -> SpriteSheet<char, Bitmap> {
}
sprite_sheet.add_area('?', letters.offset_xy(165, 40));
sprite_sheet.add_area('!', letters.offset_xy(180, 40));
sprite_sheet.add_area(':', letters.offset_xy(195, 40));
sprite_sheet.add_area('-', letters.offset_xy(210, 40));
sprite_sheet.add_area('#', letters.offset_xy(225, 40));

sprite_sheet
}
29 changes: 13 additions & 16 deletions ui/src/screen_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
color::Color,
drawable::{Drawable, FrameBuffer},
game::{Game, GameCollection, GameResult},
game_screen::{GameConfig, GameScreen, GameStatus},
game_screen::{GameConfig, GameScreen},
start_screen::StartScreen,
};

Expand Down Expand Up @@ -135,25 +135,22 @@ impl ScreenManager {
},
})
.unwrap();
if let Some(game_mut) = self.games.get_mut(game.game_id()) {
game_mut.board_fen = game.to_board_fen();
game_mut.last_activity = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();
}
}
match game.state().game_status() {
// TODO: display winner
GameStatus::StaleMate | GameStatus::CheckMate(_) | GameStatus::Resign(_) => {
buf.clear(Color::black());
self.active_screen =
Screen::Start(StartScreen::new(self.clipboard.clone(), self.public_key.clone()));
},
_ => {
if game.was_back_clicked() {
buf.clear(Color::black());
self.active_screen =
Screen::Start(StartScreen::new(self.clipboard.clone(), self.public_key.clone()));
}
},

if game.was_back_clicked() {
buf.clear(Color::black());
self.active_screen =
Screen::Start(StartScreen::new(self.clipboard.clone(), self.public_key.clone()));
}
},
}

let _ = self.save_games();

match self.channel.try_recv() {
Ok(op) => {
dbg!(&op);
Expand All @@ -176,7 +173,7 @@ impl ScreenManager {
id: op.game_id,
opponent: op.from,
board_fen: board::INITIAL_BOARD.to_string(),
seq: 1,
seq: 0,
player: match *player {
0 => Player::White,
1 => Player::Black,
Expand Down
5 changes: 4 additions & 1 deletion ui/src/start_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ impl StartScreen {
let mut show_game = Button::new(Rect::new(10, 580, 100, 30, Color::white()));
show_game.set_text("Open Game");

let games_selector = ListBox::new(Frame::new(10, 350, 900, 200));
let mut games_selector = ListBox::new(Frame::new(10, 350, 900, 200));
// Always capture keys
games_selector.set_active(true);

Self {
public_key_input,
start_button,
Expand Down
4 changes: 2 additions & 2 deletions ui/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ impl ChessUi {
let config = GameConfig {
window_width: self.window_width as u32,
window_height: self.window_height as u32,
light_color: Color::cream(),
dark_color: Color::dark_green(),
light_color: Color::white(),
dark_color: Color::dark_blue(),
save_path: self.base_path.join("p2pc-games.json"),
};

Expand Down

0 comments on commit 3af7c80

Please sign in to comment.