mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 14:17:36 +00:00
Chess: Port to Core::Stream
This commit is contained in:
parent
9a05175a88
commit
a5d5b970ff
3 changed files with 43 additions and 34 deletions
|
@ -8,8 +8,9 @@
|
||||||
#include "PromotionDialog.h"
|
#include "PromotionDialog.h"
|
||||||
#include <AK/DeprecatedString.h>
|
#include <AK/DeprecatedString.h>
|
||||||
#include <AK/Random.h>
|
#include <AK/Random.h>
|
||||||
|
#include <AK/String.h>
|
||||||
#include <LibCore/DateTime.h>
|
#include <LibCore/DateTime.h>
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/Stream.h>
|
||||||
#include <LibGUI/MessageBox.h>
|
#include <LibGUI/MessageBox.h>
|
||||||
#include <LibGUI/Painter.h>
|
#include <LibGUI/Painter.h>
|
||||||
#include <LibGfx/AntiAliasingPainter.h>
|
#include <LibGfx/AntiAliasingPainter.h>
|
||||||
|
@ -527,11 +528,11 @@ DeprecatedString ChessWidget::get_fen() const
|
||||||
return m_playback ? m_board_playback.to_fen() : m_board.to_fen();
|
return m_playback ? m_board_playback.to_fen() : m_board.to_fen();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ChessWidget::import_pgn(Core::File& file)
|
ErrorOr<void> ChessWidget::import_pgn(Core::Stream::File& file)
|
||||||
{
|
{
|
||||||
m_board = Chess::Board();
|
m_board = Chess::Board();
|
||||||
|
|
||||||
ByteBuffer bytes = file.read_all();
|
ByteBuffer bytes = TRY(file.read_until_eof());
|
||||||
StringView content = bytes;
|
StringView content = bytes;
|
||||||
auto lines = content.lines();
|
auto lines = content.lines();
|
||||||
StringView line;
|
StringView line;
|
||||||
|
@ -620,29 +621,31 @@ void ChessWidget::import_pgn(Core::File& file)
|
||||||
m_playback_move_number = m_board_playback.moves().size();
|
m_playback_move_number = m_board_playback.moves().size();
|
||||||
m_playback = true;
|
m_playback = true;
|
||||||
update();
|
update();
|
||||||
|
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
void ChessWidget::export_pgn(Core::File& file) const
|
ErrorOr<void> ChessWidget::export_pgn(Core::Stream::File& file) const
|
||||||
{
|
{
|
||||||
// Tag Pair Section
|
// Tag Pair Section
|
||||||
file.write("[Event \"Casual Game\"]\n"sv);
|
TRY(file.write("[Event \"Casual Game\"]\n"sv.bytes()));
|
||||||
file.write("[Site \"SerenityOS Chess\"]\n"sv);
|
TRY(file.write("[Site \"SerenityOS Chess\"]\n"sv.bytes()));
|
||||||
file.write(DeprecatedString::formatted("[Date \"{}\"]\n", Core::DateTime::now().to_deprecated_string("%Y.%m.%d"sv)));
|
TRY(file.write(DeprecatedString::formatted("[Date \"{}\"]\n", Core::DateTime::now().to_deprecated_string("%Y.%m.%d"sv)).bytes()));
|
||||||
file.write("[Round \"1\"]\n"sv);
|
TRY(file.write("[Round \"1\"]\n"sv.bytes()));
|
||||||
|
|
||||||
DeprecatedString username(getlogin());
|
DeprecatedString username(getlogin());
|
||||||
const DeprecatedString player1 = (!username.is_empty() ? username.view() : "?"sv);
|
auto const player1 = (!username.is_empty() ? username.view() : "?"sv.bytes());
|
||||||
const DeprecatedString player2 = (!m_engine.is_null() ? "SerenityOS ChessEngine"sv : "?"sv);
|
auto const player2 = (!m_engine.is_null() ? "SerenityOS ChessEngine"sv.bytes() : "?"sv.bytes());
|
||||||
file.write(DeprecatedString::formatted("[White \"{}\"]\n", m_side == Chess::Color::White ? player1 : player2));
|
TRY(file.write(DeprecatedString::formatted("[White \"{}\"]\n", m_side == Chess::Color::White ? player1 : player2).bytes()));
|
||||||
file.write(DeprecatedString::formatted("[Black \"{}\"]\n", m_side == Chess::Color::Black ? player1 : player2));
|
TRY(file.write(DeprecatedString::formatted("[Black \"{}\"]\n", m_side == Chess::Color::Black ? player1 : player2).bytes()));
|
||||||
|
|
||||||
file.write(DeprecatedString::formatted("[Result \"{}\"]\n", Chess::Board::result_to_points_deprecated_string(m_board.game_result(), m_board.turn())));
|
TRY(file.write(DeprecatedString::formatted("[Result \"{}\"]\n", Chess::Board::result_to_points_deprecated_string(m_board.game_result(), m_board.turn())).bytes()));
|
||||||
file.write("[WhiteElo \"?\"]\n"sv);
|
TRY(file.write("[WhiteElo \"?\"]\n"sv.bytes()));
|
||||||
file.write("[BlackElo \"?\"]\n"sv);
|
TRY(file.write("[BlackElo \"?\"]\n"sv.bytes()));
|
||||||
file.write("[Variant \"Standard\"]\n"sv);
|
TRY(file.write("[Variant \"Standard\"]\n"sv.bytes()));
|
||||||
file.write("[TimeControl \"-\"]\n"sv);
|
TRY(file.write("[TimeControl \"-\"]\n"sv.bytes()));
|
||||||
file.write("[Annotator \"SerenityOS Chess\"]\n"sv);
|
TRY(file.write("[Annotator \"SerenityOS Chess\"]\n"sv.bytes()));
|
||||||
file.write("\n"sv);
|
TRY(file.write("\n"sv.bytes()));
|
||||||
|
|
||||||
// Movetext Section
|
// Movetext Section
|
||||||
for (size_t i = 0, move_no = 1; i < m_board.moves().size(); i += 2, move_no++) {
|
for (size_t i = 0, move_no = 1; i < m_board.moves().size(); i += 2, move_no++) {
|
||||||
|
@ -650,17 +653,19 @@ void ChessWidget::export_pgn(Core::File& file) const
|
||||||
|
|
||||||
if (i + 1 < m_board.moves().size()) {
|
if (i + 1 < m_board.moves().size()) {
|
||||||
const DeprecatedString black = m_board.moves().at(i + 1).to_algebraic();
|
const DeprecatedString black = m_board.moves().at(i + 1).to_algebraic();
|
||||||
file.write(DeprecatedString::formatted("{}. {} {} ", move_no, white, black));
|
TRY(file.write(DeprecatedString::formatted("{}. {} {} ", move_no, white, black).bytes()));
|
||||||
} else {
|
} else {
|
||||||
file.write(DeprecatedString::formatted("{}. {} ", move_no, white));
|
TRY(file.write(DeprecatedString::formatted("{}. {} ", move_no, white).bytes()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
file.write("{ "sv);
|
TRY(file.write("{ "sv.bytes()));
|
||||||
file.write(Chess::Board::result_to_deprecated_string(m_board.game_result(), m_board.turn()));
|
TRY(file.write(Chess::Board::result_to_deprecated_string(m_board.game_result(), m_board.turn()).bytes()));
|
||||||
file.write(" } "sv);
|
TRY(file.write(" } "sv.bytes()));
|
||||||
file.write(Chess::Board::result_to_points_deprecated_string(m_board.game_result(), m_board.turn()));
|
TRY(file.write(Chess::Board::result_to_points_deprecated_string(m_board.game_result(), m_board.turn()).bytes()));
|
||||||
file.write("\n"sv);
|
TRY(file.write("\n"sv.bytes()));
|
||||||
|
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
void ChessWidget::flip_board()
|
void ChessWidget::flip_board()
|
||||||
|
|
|
@ -48,8 +48,8 @@ public:
|
||||||
void set_show_available_moves(bool e) { m_show_available_moves = e; }
|
void set_show_available_moves(bool e) { m_show_available_moves = e; }
|
||||||
|
|
||||||
DeprecatedString get_fen() const;
|
DeprecatedString get_fen() const;
|
||||||
void import_pgn(Core::File&);
|
ErrorOr<void> import_pgn(Core::Stream::File&);
|
||||||
void export_pgn(Core::File&) const;
|
ErrorOr<void> export_pgn(Core::Stream::File&) const;
|
||||||
|
|
||||||
int resign();
|
int resign();
|
||||||
void flip_board();
|
void flip_board();
|
||||||
|
|
|
@ -67,20 +67,24 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
TRY(game_menu->try_add_separator());
|
TRY(game_menu->try_add_separator());
|
||||||
|
|
||||||
TRY(game_menu->try_add_action(GUI::Action::create("&Import PGN...", { Mod_Ctrl, Key_O }, [&](auto&) {
|
TRY(game_menu->try_add_action(GUI::Action::create("&Import PGN...", { Mod_Ctrl, Key_O }, [&](auto&) {
|
||||||
auto result = FileSystemAccessClient::Client::the().try_open_file_deprecated(window);
|
auto result = FileSystemAccessClient::Client::the().open_file(window);
|
||||||
if (result.is_error())
|
if (result.is_error())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
widget->import_pgn(result.value());
|
if (auto maybe_error = widget->import_pgn(*result.value().release_stream()); maybe_error.is_error())
|
||||||
dbgln("Imported PGN file from {}", result.value()->filename());
|
dbgln("Failed to import PGN: {}", maybe_error.release_error());
|
||||||
|
else
|
||||||
|
dbgln("Imported PGN file from {}", result.value().filename());
|
||||||
})));
|
})));
|
||||||
TRY(game_menu->try_add_action(GUI::Action::create("&Export PGN...", { Mod_Ctrl, Key_S }, [&](auto&) {
|
TRY(game_menu->try_add_action(GUI::Action::create("&Export PGN...", { Mod_Ctrl, Key_S }, [&](auto&) {
|
||||||
auto result = FileSystemAccessClient::Client::the().try_save_file_deprecated(window, "Untitled", "pgn");
|
auto result = FileSystemAccessClient::Client::the().save_file(window, "Untitled", "pgn");
|
||||||
if (result.is_error())
|
if (result.is_error())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
widget->export_pgn(result.value());
|
if (auto maybe_error = widget->export_pgn(*result.value().release_stream()); maybe_error.is_error())
|
||||||
dbgln("Exported PGN file to {}", result.value()->filename());
|
dbgln("Failed to export PGN: {}", maybe_error.release_error());
|
||||||
|
else
|
||||||
|
dbgln("Exported PGN file to {}", result.value().filename());
|
||||||
})));
|
})));
|
||||||
TRY(game_menu->try_add_action(GUI::Action::create("&Copy FEN", { Mod_Ctrl, Key_C }, [&](auto&) {
|
TRY(game_menu->try_add_action(GUI::Action::create("&Copy FEN", { Mod_Ctrl, Key_C }, [&](auto&) {
|
||||||
GUI::Clipboard::the().set_data(widget->get_fen().bytes());
|
GUI::Clipboard::the().set_data(widget->get_fen().bytes());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue