Add the game result to the list of visible games #65

Merged
savanni merged 1 commits from kifu/game-result into main 2023-08-20 17:26:43 +00:00
2 changed files with 26 additions and 8 deletions
Showing only changes of commit 3c063af525 - Show all commits

View File

@ -1,14 +1,15 @@
use serde::{Deserialize, Serialize};
use sgf::go::Game;
use sgf::go::{Game, GameResult, Win};
use typeshare::typeshare;
#[derive(Clone, Debug, Deserialize, Serialize)]
#[typeshare]
pub struct GamePreviewElement {
pub date: Vec<String>,
pub date: String,
pub name: String,
pub black_player: String,
pub white_player: String,
pub result: String,
}
impl GamePreviewElement {
@ -37,16 +38,32 @@ impl GamePreviewElement {
None => format!("{} vs. {}", black_player, white_player),
};
let format_win = |win: &Win| match win {
Win::Resignation => "Resignation".to_owned(),
Win::Time => "Timeout".to_owned(),
Win::Forfeit => "Forfeit".to_owned(),
Win::Score(score) => format!("{:.1}", score),
};
let result = match game.info.result {
Some(GameResult::Annulled) => "Annulled".to_owned(),
Some(GameResult::Draw) => "Draw".to_owned(),
Some(GameResult::Black(ref win)) => format!("Black by {}", format_win(win)),
Some(GameResult::White(ref win)) => format!("White by {}", format_win(win)),
None => "".to_owned(),
};
GamePreviewElement {
date: game
.info
.date
.iter()
.first()
.map(|dt| dt.to_string())
.collect::<Vec<String>>(),
.unwrap_or("".to_owned()),
name,
black_player,
white_player,
result,
}
}
}

View File

@ -4,10 +4,11 @@ use kifu_core::ui::GamePreviewElement;
#[derive(Default)]
pub struct GamePreviewPrivate {
date: gtk::Label,
title: gtk::Label,
black_player: gtk::Label,
white_player: gtk::Label,
date: gtk::Label,
result: gtk::Label,
}
#[glib::object_subclass]
@ -36,6 +37,7 @@ impl GamePreview {
s.append(&s.imp().title);
s.append(&s.imp().black_player);
s.append(&s.imp().white_player);
s.append(&s.imp().result);
s
}
@ -44,8 +46,7 @@ impl GamePreview {
self.imp().black_player.set_text(&element.black_player);
self.imp().white_player.set_text(&element.white_player);
self.imp().title.set_text(&element.name);
if let Some(date) = element.date.first() {
self.imp().date.set_text(&date);
}
self.imp().date.set_text(&element.date);
self.imp().result.set_text(&element.result);
}
}