Communicate name and rank from the UI to the core

This commit is contained in:
Savanni D'Gerinel 2023-06-15 22:20:59 -04:00
parent ebb7062041
commit 9266387833
4 changed files with 97 additions and 39 deletions

View File

@ -1,5 +1,5 @@
use crate::{ use crate::{
types::{AppState, Rank}, types::{AppState, GameState, Player, Rank},
ui::{new_game, playing_field, NewGameView, PlayingFieldView}, ui::{new_game, playing_field, NewGameView, PlayingFieldView},
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -41,8 +41,17 @@ pub enum PlayerInfoRequest {
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[typeshare] #[typeshare]
pub struct HotseatPlayerRequest { pub struct HotseatPlayerRequest {
pub name: Option<String>, pub name: String,
pub rank: Option<Rank>, pub rank: Option<String>,
}
impl From<HotseatPlayerRequest> for Player {
fn from(p: HotseatPlayerRequest) -> Self {
Self {
name: p.name,
rank: p.rank.and_then(|r| Rank::try_from(r.as_ref()).ok()),
}
}
} }
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
@ -81,10 +90,25 @@ impl CoreApp {
For the initial version, I want only to show the game creation screen. Then I will backtrack record application state so that the only decisions can be made. For the initial version, I want only to show the game creation screen. Then I will backtrack record application state so that the only decisions can be made.
} }
*/ */
CoreRequest::CreateGame(_) => { CoreRequest::CreateGame(create_request) => {
let app_state = self.state.write().unwrap(); let mut app_state = self.state.write().unwrap();
let game = app_state.game.as_ref().unwrap(); let white_player = {
CoreResponse::PlayingFieldView(playing_field(game)) match create_request.white_player {
PlayerInfoRequest::Hotseat(request) => Player::from(request),
}
};
let black_player = {
match create_request.black_player {
PlayerInfoRequest::Hotseat(request) => Player::from(request),
}
};
app_state.game = Some(GameState {
white_player,
black_player,
..GameState::new()
});
let game_state = app_state.game.as_ref().unwrap();
CoreResponse::PlayingFieldView(playing_field(game_state))
} }
CoreRequest::LaunchScreen => CoreResponse::NewGameView(new_game()), CoreRequest::LaunchScreen => CoreResponse::NewGameView(new_game()),
CoreRequest::NewGame => CoreResponse::NewGameView(new_game()), CoreRequest::NewGame => CoreResponse::NewGameView(new_game()),

View File

@ -93,8 +93,8 @@ impl From<Rank> for String {
#[derive(Debug)] #[derive(Debug)]
pub struct Player { pub struct Player {
name: String, pub name: String,
rank: Rank, pub rank: Option<Rank>,
} }
#[derive(Debug)] #[derive(Debug)]
@ -113,19 +113,19 @@ pub struct GameState {
} }
impl GameState { impl GameState {
fn new() -> GameState { pub fn new() -> GameState {
GameState { GameState {
board: Board::new(), board: Board::new(),
past_positions: vec![], past_positions: vec![],
conversation: vec![], conversation: vec![],
current_player: Color::Black, current_player: Color::Black,
white_player: Player { white_player: Player {
name: "Savanni".to_owned(), name: "".to_owned(),
rank: Rank::Kyu(10), rank: None,
}, },
black_player: Player { black_player: Player {
name: "Opal".to_owned(), name: "".to_owned(),
rank: Rank::Kyu(10), rank: None,
}, },
white_clock: Duration::from_secs(600), white_clock: Duration::from_secs(600),
black_clock: Duration::from_secs(600), black_clock: Duration::from_secs(600),

View File

@ -21,16 +21,24 @@ pub fn playing_field(game: &GameState) -> PlayingFieldView {
let player_card_black = types::PlayerCardElement { let player_card_black = types::PlayerCardElement {
color: Color::Black, color: Color::Black,
name: "Savanni".to_owned(), name: game.black_player.name.clone(),
rank: "10k".to_owned(), rank: game
clock: "24:53".to_owned(), .black_player
.rank
.map(String::from)
.unwrap_or("".to_owned()),
clock: "".to_owned(),
}; };
let player_card_white = types::PlayerCardElement { let player_card_white = types::PlayerCardElement {
color: Color::White, color: Color::White,
name: "Opal".to_owned(), name: game.white_player.name.clone(),
rank: "10k".to_owned(), rank: game
clock: "25:00".to_owned(), .black_player
.rank
.map(String::from)
.unwrap_or("".to_owned()),
clock: "".to_owned(),
}; };
let chat = types::ChatElement { let chat = types::ChatElement {

View File

@ -1,14 +1,14 @@
use crate::CoreApi; use crate::CoreApi;
use glib::{subclass, Object, ParamSpec, Properties, Value}; use glib::Object;
use gtk::{glib, prelude::*, subclass::prelude::*}; use gtk::{glib, prelude::*, subclass::prelude::*};
use kifu_core::{ use kifu_core::{
ui::{NewGameView, PlayerElement}, ui::{NewGameView, PlayerElement},
CoreRequest, CreateGameRequest, HotseatPlayerRequest, PlayerInfoRequest, CoreRequest, CreateGameRequest, HotseatPlayerRequest, PlayerInfoRequest,
}; };
use std::{cell::Cell, cell::RefCell, rc::Rc}; use std::{cell::RefCell, rc::Rc};
struct PlayerDataEntryPrivate { struct PlayerDataEntryPrivate {
placeholder: gtk::Text, name: gtk::Text,
rank: gtk::DropDown, rank: gtk::DropDown,
} }
@ -16,7 +16,7 @@ impl Default for PlayerDataEntryPrivate {
fn default() -> Self { fn default() -> Self {
let rank = gtk::DropDown::builder().build(); let rank = gtk::DropDown::builder().build();
Self { Self {
placeholder: gtk::Text::builder().build(), name: gtk::Text::builder().build(),
rank, rank,
} }
} }
@ -47,7 +47,7 @@ impl PlayerDataEntry {
match element { match element {
PlayerElement::Hotseat(player) => { PlayerElement::Hotseat(player) => {
if let Some(placeholder) = player.placeholder { if let Some(placeholder) = player.placeholder {
s.imp().placeholder.set_placeholder_text(Some(&placeholder)); s.imp().name.set_placeholder_text(Some(&placeholder));
} }
player.ranks.iter().for_each(|rank| rank_model.append(&gtk::StringObject::new(rank))); player.ranks.iter().for_each(|rank| rank_model.append(&gtk::StringObject::new(rank)));
} }
@ -55,11 +55,31 @@ impl PlayerDataEntry {
// PlayerElement::Bot(_) => s.imp().placeholder.set_text("bot player"), // PlayerElement::Bot(_) => s.imp().placeholder.set_text("bot player"),
} }
s.append(&s.imp().placeholder); s.append(&s.imp().name);
s.append(&s.imp().rank); s.append(&s.imp().rank);
s s
} }
pub fn text(&self) -> String {
let name = self.imp().name.buffer().text().to_string();
if name.is_empty() {
self.imp()
.name
.placeholder_text()
.map(|s| s.to_string())
.unwrap_or("".to_owned())
} else {
name
}
}
pub fn rank(&self) -> Option<String> {
self.imp().rank.selected_item().and_then(|obj| {
let str_obj = obj.downcast::<gtk::StringObject>().ok()?;
Some(str_obj.string().clone().to_string())
})
}
} }
pub struct NewGamePrivate { pub struct NewGamePrivate {
@ -97,28 +117,34 @@ impl NewGame {
let black_player = PlayerDataEntry::new(view.black_player); let black_player = PlayerDataEntry::new(view.black_player);
s.attach(&black_player, 1, 1, 1, 1); s.attach(&black_player, 1, 1, 1, 1);
*s.imp().black_player.borrow_mut() = Some(black_player); *s.imp().black_player.borrow_mut() = Some(black_player.clone());
let white_player = PlayerDataEntry::new(view.white_player); let white_player = PlayerDataEntry::new(view.white_player);
s.attach(&white_player, 2, 1, 1, 1); s.attach(&white_player, 2, 1, 1, 1);
*s.imp().white_player.borrow_mut() = Some(white_player); *s.imp().white_player.borrow_mut() = Some(white_player.clone());
let new_game_button = gtk::Button::builder().label(&view.start_game.label).build(); let new_game_button = gtk::Button::builder().label(&view.start_game.label).build();
s.attach(&new_game_button, 2, 2, 1, 1); s.attach(&new_game_button, 2, 2, 1, 1);
new_game_button.connect_clicked(move |_| { new_game_button.connect_clicked({
move |_| {
let black_player = black_player.clone();
let white_player = white_player.clone();
api.dispatch(CoreRequest::CreateGame(CreateGameRequest { api.dispatch(CoreRequest::CreateGame(CreateGameRequest {
black_player: PlayerInfoRequest::Hotseat(HotseatPlayerRequest { black_player: player_info(black_player.clone()),
name: None, white_player: player_info(white_player.clone()),
rank: None,
}),
white_player: PlayerInfoRequest::Hotseat(HotseatPlayerRequest {
name: None,
rank: None,
}),
})); }));
}
}); });
s s
} }
} }
fn player_info(player: PlayerDataEntry) -> PlayerInfoRequest {
PlayerInfoRequest::Hotseat(HotseatPlayerRequest {
name: player.text(),
rank: player.rank(),
})
}