Enable PGN save (slow for now so do not use it).

This commit is contained in:
Loic Guegan 2022-02-25 11:13:35 +01:00
parent 6f866f55ee
commit 64dec753e7
11 changed files with 109 additions and 30 deletions

View file

@ -21,7 +21,7 @@ BasePanelBF::BasePanelBF( wxWindow* parent, wxWindowID id, const wxPoint& pos, c
current_base->Wrap( -1 ); current_base->Wrap( -1 );
top_sizer->Add( current_base, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5 ); top_sizer->Add( current_base, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
save_button = new wxButton( this, wxID_ANY, wxT("Save"), wxDefaultPosition, wxDefaultSize, 0 ); save_button = new wxButton( this, ID_SAVE_BUTTON, wxT("Save"), wxDefaultPosition, wxDefaultSize, 0 );
top_sizer->Add( save_button, 0, wxALL, 5 ); top_sizer->Add( save_button, 0, wxALL, 5 );
export_button = new wxButton( this, wxID_ANY, wxT("Export"), wxDefaultPosition, wxDefaultSize, 0 ); export_button = new wxButton( this, wxID_ANY, wxT("Export"), wxDefaultPosition, wxDefaultSize, 0 );
@ -51,7 +51,7 @@ BasePanelBF::BasePanelBF( wxWindow* parent, wxWindowID id, const wxPoint& pos, c
bottom_sizer->Add( 0, 0, 1, wxEXPAND, 5 ); bottom_sizer->Add( 0, 0, 1, wxEXPAND, 5 );
delete_button = new wxButton( this, wxID_ANY, wxT("Delete selection"), wxDefaultPosition, wxDefaultSize, 0 ); delete_button = new wxButton( this, ID_DELETE_BUTTON, wxT("Delete selection"), wxDefaultPosition, wxDefaultSize, 0 );
bottom_sizer->Add( delete_button, 0, wxALL, 5 ); bottom_sizer->Add( delete_button, 0, wxALL, 5 );

View file

@ -27,6 +27,8 @@
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
#define ID_SAVE_BUTTON 1000
#define ID_DELETE_BUTTON 1001
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
/// Class BasePanelBF /// Class BasePanelBF

View file

@ -12,13 +12,36 @@ BaseTab::BaseTab(wxFrame *parent, std::string base_file)
game_list->InsertColumn(5, L"Result", wxLIST_FORMAT_LEFT, 200); game_list->InsertColumn(5, L"Result", wxLIST_FORMAT_LEFT, 200);
game_list->InsertColumn(6, L"ECO", wxLIST_FORMAT_LEFT, 200); game_list->InsertColumn(6, L"ECO", wxLIST_FORMAT_LEFT, 200);
this->Bind(wxEVT_BUTTON, &BaseTab::OnBim, this, wxID_ANY); this->Bind(wxEVT_BUTTON, &BaseTab::OnDelete, this, ID_DELETE_BUTTON);
this->Bind(wxEVT_BUTTON, &BaseTab::OnSave, this, ID_SAVE_BUTTON);
this->Bind(wxEVT_LIST_ITEM_ACTIVATED, &BaseTab::OnOpenGame, this, wxID_ANY); this->Bind(wxEVT_LIST_ITEM_ACTIVATED, &BaseTab::OnOpenGame, this, wxID_ANY);
current_base->SetLabel(base_file); current_base->SetLabel(base_file);
LoadFile(base_file); LoadFile(base_file);
} }
void BaseTab::OnBim(wxCommandEvent &event) {} void BaseTab::OnDelete(wxCommandEvent &event) {
long selected =
game_list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
if (selected >= 0) {
wxListItem listItem;
listItem.m_itemId = selected; // sets row
listItem.m_col = 0; // sets column
game_list->GetItem(listItem); // gets item
deleted.push_back(std::stoi(listItem.GetText().ToStdString()));
for (std::uint32_t &i : deleted) {
wxLogDebug("%d", i);
}
game_list->SetItemBackgroundColour(selected, *wxRED);
}
}
void BaseTab::OnSave(wxCommandEvent &event) {
std::vector<GameBase *> new_games_bases;
std::vector<Game *> new_games;
base->Save(deleted, new_games_bases, new_games);
game_list->ClearAll();
deleted.clear();
}
void BaseTab::OnOpenGame(wxListEvent &event) { void BaseTab::OnOpenGame(wxListEvent &event) {
wxLogDebug("Open!"); wxLogDebug("Open!");
@ -39,7 +62,7 @@ void BaseTab::LoadFile(std::string path) {
wxString ext = file.GetExt().Lower(); wxString ext = file.GetExt().Lower();
if (ext == "pgn") { if (ext == "pgn") {
base = new PGNGameBase(path); base = new PGNGameBase(path);
SetLabel(file.GetName()+ "(PGN)"); SetLabel(file.GetName() + "(PGN)");
} }
if (base != NULL) { if (base != NULL) {

View file

@ -10,11 +10,13 @@ wxDECLARE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent);
class BaseTab : public BasePanelBF, public TabInfos { class BaseTab : public BasePanelBF, public TabInfos {
GameBase *base; GameBase *base;
std::vector<std::uint32_t> deleted;
public: public:
BaseTab(wxFrame *parent, std::string base_file); BaseTab(wxFrame *parent, std::string base_file);
void ApplyPreferences(); void ApplyPreferences();
void LoadFile(std::string path); void LoadFile(std::string path);
void OnBim(wxCommandEvent &event); void OnDelete(wxCommandEvent &event);
void OnSave(wxCommandEvent &event);
void OnOpenGame(wxListEvent &event); void OnOpenGame(wxListEvent &event);
}; };

View file

@ -1,10 +1,16 @@
#pragma once #pragma once
#include "game_tab/Game.hpp" #include "game_tab/Game.hpp"
#include <algorithm>
#include <vector>
class GameBase { class GameBase {
public: public:
virtual Game *GetGame(std::uint32_t id) = 0; virtual Game *GetGame(std::uint32_t id) = 0;
virtual void Save(std::vector<std::uint32_t> to_ignore,
std::vector<GameBase *> new_games_bases,
std::vector<Game *> new_games) = 0;
virtual Game *GetCurrentGame() = 0;
virtual bool NextGame() = 0; virtual bool NextGame() = 0;
virtual std::string GetTag(std::string tag) = 0; virtual std::string GetTag(std::string tag) = 0;
virtual void Reset() = 0; virtual void Reset() = 0;

View file

@ -1,4 +1,5 @@
#include "PGNGameBase.hpp" #include "PGNGameBase.hpp"
#include <wx/stdpaths.h>
PGNGameBase::PGNGameBase(std::string pgn_file) : pgn(new pgnp::PGN()) { PGNGameBase::PGNGameBase(std::string pgn_file) : pgn(new pgnp::PGN()) {
file = pgn_file; file = pgn_file;
@ -23,6 +24,22 @@ std::string PGNGameBase::GetTag(std::string tag) {
return (""); return ("");
} }
Game *PGNGameBase::GetCurrentGame() {
pgnp::HalfMove *pgnp_moves = new pgnp::HalfMove();
pgn->GetMoves(pgnp_moves);
std::string fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
if (pgn->HasTag("FEN")) {
fen = pgn->GetTagValue("FEN");
}
HalfMove *m = new HalfMove(pgnp_moves, fen);
Game *g = new Game(m, fen);
for (std::string &s : pgn->GetTagList()) {
g->SetTag(s, pgn->GetTagValue(s));
}
g->SetResult(pgn->GetResult());
return (g);
}
void PGNGameBase::Reset() { void PGNGameBase::Reset() {
delete pgn; delete pgn;
pgn = new pgnp::PGN(); pgn = new pgnp::PGN();
@ -34,21 +51,41 @@ Game *PGNGameBase::GetGame(std::uint32_t id) {
std::uint32_t curid = 0; std::uint32_t curid = 0;
while (NextGame()) { while (NextGame()) {
if (id == curid) { if (id == curid) {
pgnp::HalfMove *pgnp_moves = new pgnp::HalfMove(); return (GetCurrentGame());
pgn->GetMoves(pgnp_moves);
std::string fen =
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
if (pgn->HasTag("FEN")) {
fen = pgn->GetTagValue("FEN");
}
HalfMove *m = new HalfMove(pgnp_moves, fen);
Game *g = new Game(m, fen);
for (std::string &s : pgn->GetTagList()) {
g->SetTag(s, pgn->GetTagValue(s));
}
return (g);
} }
curid++; curid++;
} }
return (NULL); return (NULL);
} }
void PGNGameBase::Save(std::vector<std::uint32_t> to_ignore,
std::vector<GameBase *> new_games_bases,
std::vector<Game *> new_games) {
wxStandardPaths stdPaths = wxStandardPaths::Get();
wxString tmp = stdPaths.GetTempDir() + "/save_pgn_tmp.pgn";
wxFile new_pgn(tmp, wxFile::write);
Reset();
std::uint32_t id = 0;
while (NextGame()) {
if (std::find(to_ignore.begin(), to_ignore.end(), id) == to_ignore.end()) {
Game *g = GetCurrentGame();
new_pgn.Write(g->GetPGN());
new_pgn.Write("\n\n");
delete g;
}
id++;
}
// Now add new games
// new_games->Reset();
// while (new_games->NextGame()) {
// Game *g = new_games->GetCurrentGame();
// new_pgn.Write(g->GetPGN());
// delete g;
// }
new_pgn.Close();
wxCopyFile(tmp, file);
wxRemoveFile(tmp);
}

View file

@ -10,6 +10,10 @@ public:
PGNGameBase(std::string pgn_file); PGNGameBase(std::string pgn_file);
Game *GetGame(std::uint32_t id); Game *GetGame(std::uint32_t id);
bool NextGame(); bool NextGame();
Game *GetCurrentGame();
std::string GetTag(std::string tag); std::string GetTag(std::string tag);
void Save(std::vector<std::uint32_t> to_ignore,
std::vector<GameBase *> new_games_bases,
std::vector<Game *> new_games);
void Reset(); void Reset();
}; };

View file

@ -139,5 +139,8 @@ std::string Game::GetPGN() {
} }
pgn += moves->GetPGN(); pgn += moves->GetPGN();
} }
pgn += " " + result;
return (pgn); return (pgn);
} }
void Game::SetResult(std::string result) { this->result = result; }

View file

@ -8,6 +8,7 @@
class Game { class Game {
std::string board; std::string board;
std::string initial_fen; std::string initial_fen;
std::string result;
std::unordered_map<std::string, std::string> tags; std::unordered_map<std::string, std::string> tags;
HalfMove *moves; HalfMove *moves;
HalfMove *current; HalfMove *current;
@ -35,4 +36,5 @@ public:
void SetCurrent(HalfMove *m); void SetCurrent(HalfMove *m);
std::vector<std::string> ListTags(); std::vector<std::string> ListTags();
std::string GetPGN(); std::string GetPGN();
void SetResult(std::string result);
}; };

View file

@ -115,16 +115,16 @@ void HalfMove::SetAsMainline() {
HalfMove *HalfMove::GetMainline() { return (mainline); } HalfMove *HalfMove::GetMainline() { return (mainline); }
HalfMove::HalfMove(pgnp::HalfMove *m, std::string initial_fen): capture(' ') { HalfMove::HalfMove(pgnp::HalfMove *m, std::string initial_fen) : capture(' ') {
chessarbiter::ChessArbiter arbiter; chessarbiter::ChessArbiter arbiter;
arbiter.Setup(initial_fen); arbiter.Setup(initial_fen);
bool work=arbiter.Play(arbiter.ParseSAN(m->move)); bool work = arbiter.Play(arbiter.ParseSAN(m->move));
if(!work){ if (!work) {
wxLogDebug("Bug! %s",m->move); wxLogDebug("Bug! %s", m->move);
} }
char capture=arbiter.GetCapture(); char capture = arbiter.GetCapture();
if(capture != ' '){ if (capture != ' ') {
this->capture=capture; this->capture = capture;
} }
this->fen = arbiter.GetFEN(); this->fen = arbiter.GetFEN();
this->move = m->move; this->move = m->move;

View file

@ -61,7 +61,7 @@
<property name="border">5</property> <property name="border">5</property>
<property name="flag">wxEXPAND</property> <property name="flag">wxEXPAND</property>
<property name="proportion">0</property> <property name="proportion">0</property>
<object class="wxBoxSizer" expanded="0"> <object class="wxBoxSizer" expanded="1">
<property name="minimum_size"></property> <property name="minimum_size"></property>
<property name="name">top_sizer</property> <property name="name">top_sizer</property>
<property name="orient">wxHORIZONTAL</property> <property name="orient">wxHORIZONTAL</property>
@ -164,7 +164,7 @@
<property name="font"></property> <property name="font"></property>
<property name="gripper">0</property> <property name="gripper">0</property>
<property name="hidden">0</property> <property name="hidden">0</property>
<property name="id">wxID_ANY</property> <property name="id">ID_SAVE_BTN</property>
<property name="label">Save</property> <property name="label">Save</property>
<property name="margins"></property> <property name="margins"></property>
<property name="markup">0</property> <property name="markup">0</property>
@ -587,7 +587,7 @@
<property name="font"></property> <property name="font"></property>
<property name="gripper">0</property> <property name="gripper">0</property>
<property name="hidden">0</property> <property name="hidden">0</property>
<property name="id">wxID_ANY</property> <property name="id">ID_DELETE_BUTTON</property>
<property name="label">Delete selection</property> <property name="label">Delete selection</property>
<property name="margins"></property> <property name="margins"></property>
<property name="markup">0</property> <property name="markup">0</property>