Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add option to discard unfinished games from PGN output
  • Loading branch information
alwey authored and ilaripih committed Aug 1, 2018
1 parent 2ceaa3d commit 875bc95
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 12 deletions.
7 changes: 5 additions & 2 deletions docs/cutechess-cli.6
@@ -1,4 +1,4 @@
.Dd February 8, 2018
.Dd July 26, 2018
.Dt CUTECHESS-CLI 6
.Os
.Sh NAME
Expand Down Expand Up @@ -278,12 +278,15 @@ is either
(the book is accessed directly on disk).
The default mode is
.Cm ram .
.It Fl pgnout Ar file Bq Cm min
.It Fl pgnout Ar file Bq Cm min Cm Bq fi
Save the games to
.Ar file
in PGN format. Use the
.Cm min
argument to save in a minimal PGN format.
Only finished games will be saved if argument
.Cm fi
is given.
.It Fl epdout Ar file
Save the games to
.Ar file
Expand Down
6 changes: 4 additions & 2 deletions projects/cli/res/doc/help.txt
Expand Up @@ -123,8 +123,10 @@ Options:
-bookmode MODE Set Polyglot book mode to MODE, which can be one of:
'ram': The whole book is loaded into RAM (default)
'disk': The book is accessed directly on disk.
-pgnout FILE [min] Save the games to FILE in PGN format. Use the 'min'
argument to save in a minimal/compact PGN format.
-pgnout FILE [min][fi]
Save the games to FILE in PGN format. Use the 'min'
argument to save in a minimal/compact PGN format. Only
finished games are saved for argument 'fi'.
-epdout FILE Save the end position of the games to FILE in FEN format.
-recover Restart crashed engines instead of stopping the match
-repeat [N] Play each opening twice (or N times). Unless the -noswap
Expand Down
17 changes: 11 additions & 6 deletions projects/cli/src/main.cpp
Expand Up @@ -257,7 +257,7 @@ EngineMatch* parseMatch(const QStringList& args, QObject* parent)
parser.addOption("-debug", QVariant::Bool, 0, 0);
parser.addOption("-openings", QVariant::StringList);
parser.addOption("-bookmode", QVariant::String);
parser.addOption("-pgnout", QVariant::StringList, 1, 2);
parser.addOption("-pgnout", QVariant::StringList, 1, 3);
parser.addOption("-epdout", QVariant::String, 1, 1);
parser.addOption("-repeat", QVariant::Int, 0, 1);
parser.addOption("-noswap", QVariant::Bool, 0, 0);
Expand Down Expand Up @@ -495,12 +495,17 @@ EngineMatch* parseMatch(const QStringList& args, QObject* parent)
{
PgnGame::PgnMode mode = PgnGame::Verbose;
QStringList list = value.toStringList();
if (list.size() == 2)
if (list.size() == 2 || list.size() == 3)
{
if (list.at(1) == "min")
mode = PgnGame::Minimal;
else
ok = false;
for (int i = 1; i < list.size(); i++)
{
if (list.at(i) == "min")
mode = PgnGame::Minimal;
else if (list.at(i) == "fi")
tournament->setPgnWriteUnfinishedGames(false);
else
ok = false;
}
}
if (ok)
tournament->setPgnOutput(list.at(0), mode);
Expand Down
1 change: 1 addition & 0 deletions projects/gui/src/newtournamentdialog.cpp
Expand Up @@ -277,6 +277,7 @@ Tournament* NewTournamentDialog::createTournament(GameManager* gameManager) cons

t->setOpeningRepetitions(ts->openingRepetition()? 2: 1);
t->setRecoveryMode(ts->engineRecovery());
t->setPgnWriteUnfinishedGames(ts->savingOfUnfinishedGames());

const auto engines = m_addedEnginesManager->engines();
for (EngineConfiguration config : engines)
Expand Down
11 changes: 11 additions & 0 deletions projects/gui/src/tournamentsettingswidget.cpp
Expand Up @@ -85,6 +85,11 @@ bool TournamentSettingsWidget::engineRecovery() const
return ui->m_recoverCheck->isChecked();
}

bool TournamentSettingsWidget::savingOfUnfinishedGames() const
{
return ui->m_saveUnfinishedGamesCheck->isChecked();
}

void TournamentSettingsWidget::readSettings()
{
QSettings s;
Expand All @@ -107,6 +112,8 @@ void TournamentSettingsWidget::readSettings()

ui->m_repeatCheck->setChecked(s.value("repeat").toBool());
ui->m_recoverCheck->setChecked(s.value("recover").toBool());
ui->m_saveUnfinishedGamesCheck->setChecked(
s.value("save_unfinished_games", true).toBool());

s.endGroup();
}
Expand Down Expand Up @@ -163,4 +170,8 @@ void TournamentSettingsWidget::enableSettingsUpdates()
{
QSettings().setValue("tournament/recover", checked);
});
connect(ui->m_saveUnfinishedGamesCheck, &QCheckBox::toggled, [=](bool checked)
{
QSettings().setValue("tournament/save_unfinished_games", checked);
});
}
1 change: 1 addition & 0 deletions projects/gui/src/tournamentsettingswidget.h
Expand Up @@ -40,6 +40,7 @@ class TournamentSettingsWidget : public QWidget
int delayBetweenGames() const;
bool openingRepetition() const;
bool engineRecovery() const;
bool savingOfUnfinishedGames() const;

void enableSettingsUpdates();

Expand Down
12 changes: 11 additions & 1 deletion projects/gui/ui/tournamentsettingswidget.ui
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>400</width>
<height>286</height>
<height>314</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -193,6 +193,16 @@
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QCheckBox" name="m_saveUnfinishedGamesCheck">
<property name="text">
<string>Save unfinished games</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down
22 changes: 21 additions & 1 deletion projects/lib/src/tournament.cpp
Expand Up @@ -50,6 +50,7 @@ Tournament::Tournament(GameManager* gameManager, QObject *parent)
m_openingRepetitions(1),
m_recover(false),
m_pgnCleanup(true),
m_pgnWriteUnfinishedGames(true),
m_finished(false),
m_bookOwnership(false),
m_openingSuite(nullptr),
Expand Down Expand Up @@ -252,6 +253,11 @@ void Tournament::setPgnOutput(const QString& fileName, PgnGame::PgnMode mode)
m_pgnOutMode = mode;
}

void Tournament::setPgnWriteUnfinishedGames(bool enabled)
{
m_pgnWriteUnfinishedGames = enabled;
}

void Tournament::setPgnCleanupEnabled(bool enabled)
{
m_pgnCleanup = enabled;
Expand Down Expand Up @@ -452,6 +458,14 @@ void Tournament::startNextGame()
startGame(pair);
}

inline bool faulty(const Chess::Result::Type& type)
{
return type == Chess::Result::NoResult
|| type == Chess::Result::ResultError
|| type == Chess::Result::Disconnection
|| type == Chess::Result::StalledConnection;
}

bool Tournament::writePgn(PgnGame* pgn, int gameNumber)
{
Q_ASSERT(pgn != nullptr);
Expand Down Expand Up @@ -484,6 +498,13 @@ bool Tournament::writePgn(PgnGame* pgn, int gameNumber)
while (m_pgnGames.contains(m_savedGameCount + 1))
{
PgnGame tmp = m_pgnGames.take(++m_savedGameCount);
Chess::Result::Type type = tmp.result().type();
if (!m_pgnWriteUnfinishedGames
&& (tmp.result().isNone() || (m_stopping && faulty(type))))
{
qWarning("Omitted incomplete game %d", m_savedGameCount);
continue;
}
if (!tmp.write(m_pgnOut, m_pgnOutMode)
|| m_pgnFile.error() != QFile::NoError)
{
Expand Down Expand Up @@ -558,7 +579,6 @@ void Tournament::onGameFinished(ChessGame* game)
Q_ASSERT(game != nullptr);

PgnGame* pgn(game->pgn());
Chess::Result result(game->result());

m_finishedGameCount++;

Expand Down
9 changes: 9 additions & 0 deletions projects/lib/src/tournament.h
Expand Up @@ -179,6 +179,14 @@ class LIB_EXPORT Tournament : public QObject
void setPgnOutput(const QString& fileName,
PgnGame::PgnMode mode = PgnGame::Verbose);

/*!
* Sets the PgnGame mode to write unfinished games to \a enabled.
*
* If \a enabled is true (the default) then the generated PGN games
* are saved even if they have no result.
*/
void setPgnWriteUnfinishedGames(bool enabled);

/*!
* Sets PgnGame cleanup mode to \a enabled.
*
Expand Down Expand Up @@ -432,6 +440,7 @@ class LIB_EXPORT Tournament : public QObject
int m_openingRepetitions;
bool m_recover;
bool m_pgnCleanup;
bool m_pgnWriteUnfinishedGames;
bool m_finished;
bool m_bookOwnership;
GameAdjudicator m_adjudicator;
Expand Down

0 comments on commit 875bc95

Please sign in to comment.