aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLoic Guegan <manzerbredes@mailbox.org>2022-02-25 11:13:35 +0100
committerLoic Guegan <manzerbredes@mailbox.org>2022-02-25 11:13:35 +0100
commit64dec753e7a6594a8b68f7978773e80353d8e869 (patch)
treece8b862fde15c51b8b981f1d628f0d43cc2bfdc7 /src
parent6f866f55ee39c5f92c600ab33e787f628b00c1a4 (diff)
Enable PGN save (slow for now so do not use it).
Diffstat (limited to 'src')
-rw-r--r--src/base_tab/BasePanelBF.cpp4
-rw-r--r--src/base_tab/BasePanelBF.h2
-rw-r--r--src/base_tab/BaseTab.cpp29
-rw-r--r--src/base_tab/BaseTab.hpp4
-rw-r--r--src/base_tab/gamebase/GameBase.hpp6
-rw-r--r--src/base_tab/gamebase/PGNGameBase.cpp63
-rw-r--r--src/base_tab/gamebase/PGNGameBase.hpp4
-rw-r--r--src/game_tab/Game.cpp5
-rw-r--r--src/game_tab/Game.hpp2
-rw-r--r--src/game_tab/HalfMove.cpp14
10 files changed, 106 insertions, 27 deletions
diff --git a/src/base_tab/BasePanelBF.cpp b/src/base_tab/BasePanelBF.cpp
index e179ec0..4c25cf9 100644
--- a/src/base_tab/BasePanelBF.cpp
+++ b/src/base_tab/BasePanelBF.cpp
@@ -21,7 +21,7 @@ BasePanelBF::BasePanelBF( wxWindow* parent, wxWindowID id, const wxPoint& pos, c
current_base->Wrap( -1 );
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 );
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 );
- 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 );
diff --git a/src/base_tab/BasePanelBF.h b/src/base_tab/BasePanelBF.h
index 1bbbea1..167ce99 100644
--- a/src/base_tab/BasePanelBF.h
+++ b/src/base_tab/BasePanelBF.h
@@ -27,6 +27,8 @@
///////////////////////////////////////////////////////////////////////////
+#define ID_SAVE_BUTTON 1000
+#define ID_DELETE_BUTTON 1001
///////////////////////////////////////////////////////////////////////////////
/// Class BasePanelBF
diff --git a/src/base_tab/BaseTab.cpp b/src/base_tab/BaseTab.cpp
index 82a0db0..13530c7 100644
--- a/src/base_tab/BaseTab.cpp
+++ b/src/base_tab/BaseTab.cpp
@@ -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(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);
current_base->SetLabel(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) {
wxLogDebug("Open!");
@@ -39,7 +62,7 @@ void BaseTab::LoadFile(std::string path) {
wxString ext = file.GetExt().Lower();
if (ext == "pgn") {
base = new PGNGameBase(path);
- SetLabel(file.GetName()+ "(PGN)");
+ SetLabel(file.GetName() + "(PGN)");
}
if (base != NULL) {
diff --git a/src/base_tab/BaseTab.hpp b/src/base_tab/BaseTab.hpp
index 3283102..d0e359c 100644
--- a/src/base_tab/BaseTab.hpp
+++ b/src/base_tab/BaseTab.hpp
@@ -10,11 +10,13 @@ wxDECLARE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent);
class BaseTab : public BasePanelBF, public TabInfos {
GameBase *base;
+ std::vector<std::uint32_t> deleted;
public:
BaseTab(wxFrame *parent, std::string base_file);
void ApplyPreferences();
void LoadFile(std::string path);
- void OnBim(wxCommandEvent &event);
+ void OnDelete(wxCommandEvent &event);
+ void OnSave(wxCommandEvent &event);
void OnOpenGame(wxListEvent &event);
}; \ No newline at end of file
diff --git a/src/base_tab/gamebase/GameBase.hpp b/src/base_tab/gamebase/GameBase.hpp
index 828248c..db58bcc 100644
--- a/src/base_tab/gamebase/GameBase.hpp
+++ b/src/base_tab/gamebase/GameBase.hpp
@@ -1,10 +1,16 @@
#pragma once
#include "game_tab/Game.hpp"
+#include <algorithm>
+#include <vector>
class GameBase {
public:
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 std::string GetTag(std::string tag) = 0;
virtual void Reset() = 0;
diff --git a/src/base_tab/gamebase/PGNGameBase.cpp b/src/base_tab/gamebase/PGNGameBase.cpp
index 8f9974d..69dcc4f 100644
--- a/src/base_tab/gamebase/PGNGameBase.cpp
+++ b/src/base_tab/gamebase/PGNGameBase.cpp
@@ -1,4 +1,5 @@
#include "PGNGameBase.hpp"
+#include <wx/stdpaths.h>
PGNGameBase::PGNGameBase(std::string pgn_file) : pgn(new pgnp::PGN()) {
file = pgn_file;
@@ -23,6 +24,22 @@ std::string PGNGameBase::GetTag(std::string tag) {
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() {
delete pgn;
pgn = new pgnp::PGN();
@@ -34,21 +51,41 @@ Game *PGNGameBase::GetGame(std::uint32_t id) {
std::uint32_t curid = 0;
while (NextGame()) {
if (id == curid) {
- 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));
- }
- return (g);
+ return (GetCurrentGame());
}
curid++;
}
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);
+}
diff --git a/src/base_tab/gamebase/PGNGameBase.hpp b/src/base_tab/gamebase/PGNGameBase.hpp
index 1ad58a1..7a1a63d 100644
--- a/src/base_tab/gamebase/PGNGameBase.hpp
+++ b/src/base_tab/gamebase/PGNGameBase.hpp
@@ -10,6 +10,10 @@ public:
PGNGameBase(std::string pgn_file);
Game *GetGame(std::uint32_t id);
bool NextGame();
+ Game *GetCurrentGame();
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();
}; \ No newline at end of file
diff --git a/src/game_tab/Game.cpp b/src/game_tab/Game.cpp
index b9181a9..2b2955a 100644
--- a/src/game_tab/Game.cpp
+++ b/src/game_tab/Game.cpp
@@ -139,5 +139,8 @@ std::string Game::GetPGN() {
}
pgn += moves->GetPGN();
}
+ pgn += " " + result;
return (pgn);
-} \ No newline at end of file
+}
+
+void Game::SetResult(std::string result) { this->result = result; }
diff --git a/src/game_tab/Game.hpp b/src/game_tab/Game.hpp
index 7cad269..348bff9 100644
--- a/src/game_tab/Game.hpp
+++ b/src/game_tab/Game.hpp
@@ -8,6 +8,7 @@
class Game {
std::string board;
std::string initial_fen;
+ std::string result;
std::unordered_map<std::string, std::string> tags;
HalfMove *moves;
HalfMove *current;
@@ -35,4 +36,5 @@ public:
void SetCurrent(HalfMove *m);
std::vector<std::string> ListTags();
std::string GetPGN();
+ void SetResult(std::string result);
}; \ No newline at end of file
diff --git a/src/game_tab/HalfMove.cpp b/src/game_tab/HalfMove.cpp
index 94d9a8c..2b84382 100644
--- a/src/game_tab/HalfMove.cpp
+++ b/src/game_tab/HalfMove.cpp
@@ -115,16 +115,16 @@ void HalfMove::SetAsMainline() {
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;
arbiter.Setup(initial_fen);
- bool work=arbiter.Play(arbiter.ParseSAN(m->move));
- if(!work){
- wxLogDebug("Bug! %s",m->move);
+ bool work = arbiter.Play(arbiter.ParseSAN(m->move));
+ if (!work) {
+ wxLogDebug("Bug! %s", m->move);
}
- char capture=arbiter.GetCapture();
- if(capture != ' '){
- this->capture=capture;
+ char capture = arbiter.GetCapture();
+ if (capture != ' ') {
+ this->capture = capture;
}
this->fen = arbiter.GetFEN();
this->move = m->move;