Improve pgn save performance

This commit is contained in:
Loic Guegan 2022-02-25 11:42:46 +01:00
parent 64dec753e7
commit c6f648cfb4
8 changed files with 48 additions and 24 deletions

View file

@ -144,3 +144,9 @@ std::string Game::GetPGN() {
}
void Game::SetResult(std::string result) { this->result = result; }
void Game::BuildAndVerify() {
if (moves != NULL) {
moves->BuildAndVerify(GetFen());
}
}

View file

@ -37,4 +37,5 @@ public:
std::vector<std::string> ListTags();
std::string GetPGN();
void SetResult(std::string result);
void BuildAndVerify();
};

View file

@ -10,6 +10,7 @@ GameTab::GameTab(wxFrame *parent, Game *game)
splitter->SetMinimumPaneSize(100);
// Panels
game->BuildAndVerify();
board_panel = new BoardPanel((wxFrame *)splitter, game);
editor_panel = new EditorPanel((wxFrame *)splitter, game);
splitter->SplitVertically(board_panel, editor_panel);

View file

@ -115,27 +115,16 @@ void HalfMove::SetAsMainline() {
HalfMove *HalfMove::GetMainline() { return (mainline); }
HalfMove::HalfMove(pgnp::HalfMove *m, std::string initial_fen) : capture(' ') {
chessarbiter::ChessArbiter arbiter;
arbiter.Setup(initial_fen);
bool work = arbiter.Play(arbiter.ParseSAN(m->move));
if (!work) {
wxLogDebug("Bug! %s", m->move);
}
char capture = arbiter.GetCapture();
if (capture != ' ') {
this->capture = capture;
}
this->fen = arbiter.GetFEN();
HalfMove::HalfMove(pgnp::HalfMove *m) : capture(' ') {
this->move = m->move;
this->IsBlack = m->isBlack;
this->SetComment(m->comment);
this->Number = m->count;
if (m->MainLine != NULL) {
this->SetMainline(new HalfMove(m->MainLine, arbiter.GetFEN()));
this->SetMainline(new HalfMove(m->MainLine));
}
for (pgnp::HalfMove *v : m->variations) {
this->AddVariation(new HalfMove(v, initial_fen));
this->AddVariation(new HalfMove(v));
}
}
@ -216,3 +205,25 @@ std::string HalfMove::GetPGN(bool needDots) {
return (part);
}
void HalfMove::BuildAndVerify(HalfMove *m, std::string fen) {
arbiter.Setup(fen);
bool work = arbiter.Play(arbiter.ParseSAN(m->move));
if (!work) {
wxLogDebug("Bug! %s", m->move);
}
char capture = arbiter.GetCapture();
if (capture != ' ') {
m->capture = capture;
}
m->fen = arbiter.GetFEN();
if (m->mainline != NULL) {
BuildAndVerify(m->mainline, arbiter.GetFEN());
}
for (HalfMove *v : m->variations) {
BuildAndVerify(v,fen);
}
}
void HalfMove::BuildAndVerify(std::string initial_fen) {
BuildAndVerify(this, initial_fen);
}

View file

@ -16,15 +16,17 @@
class HalfMove : public cgeditor::CGEHalfMove {
HalfMove *parent = NULL;
HalfMove *mainline = NULL;
chessarbiter::ChessArbiter arbiter;
std::vector<HalfMove *> variations;
std::string fen;
char capture;
std::string GetPGN(bool needDots);
void BuildAndVerify(HalfMove *m, std::string fen);
public:
HalfMove(std::string move);
HalfMove(std::string move, std::string fen);
HalfMove(pgnp::HalfMove *m, std::string initial_fen);
HalfMove(pgnp::HalfMove *m);
~HalfMove();
/// @brief Add variation to current move
@ -53,5 +55,5 @@ public:
void SetFen(std::string fen);
void SetCapture(char c);
std::string GetPGN();
void BuildAndVerify(std::string initial_fen);
};