2022-02-24 11:50:16 +01:00
|
|
|
#include "PGNGameBase.hpp"
|
|
|
|
|
2022-02-24 15:22:56 +01:00
|
|
|
PGNGameBase::PGNGameBase(std::string pgn_file) : pgn(new pgnp::PGN()) {
|
|
|
|
file = pgn_file;
|
2022-02-24 12:09:21 +01:00
|
|
|
pgn->FromFile(pgn_file);
|
|
|
|
}
|
|
|
|
|
2022-02-24 15:22:56 +01:00
|
|
|
bool PGNGameBase::NextGame() {
|
|
|
|
bool game_found = false;
|
2022-02-24 12:09:21 +01:00
|
|
|
try {
|
|
|
|
pgn->ParseNextGame();
|
2022-02-24 15:22:56 +01:00
|
|
|
game_found = true;
|
2022-02-24 12:09:21 +01:00
|
|
|
} catch (...) {
|
2022-02-24 15:22:56 +01:00
|
|
|
game_found = false;
|
2022-02-24 12:09:21 +01:00
|
|
|
}
|
2022-02-24 15:22:56 +01:00
|
|
|
return (game_found);
|
2022-02-24 12:09:21 +01:00
|
|
|
}
|
|
|
|
|
2022-02-24 15:22:56 +01:00
|
|
|
std::string PGNGameBase::GetTag(std::string tag) {
|
|
|
|
if (pgn->HasTag(tag)) {
|
|
|
|
return (pgn->GetTagValue(tag));
|
2022-02-24 12:09:21 +01:00
|
|
|
}
|
2022-02-24 15:22:56 +01:00
|
|
|
return ("");
|
|
|
|
}
|
2022-02-24 12:09:21 +01:00
|
|
|
|
2022-02-24 15:22:56 +01:00
|
|
|
void PGNGameBase::Reset() {
|
|
|
|
delete pgn;
|
|
|
|
pgn = new pgnp::PGN();
|
|
|
|
pgn->FromFile(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
Game *PGNGameBase::GetGame(std::uint32_t id) {
|
|
|
|
Reset();
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
curid++;
|
|
|
|
}
|
|
|
|
return (NULL);
|
2022-02-24 12:09:21 +01:00
|
|
|
}
|