File indexing completed on 2024-04-28 04:03:11

0001 /*
0002  *  This file is part of Knights, a chess board for KDE SC 4.
0003  *  SPDX-FileCopyrightText: 2009, 2010, 2011 Miha Čančula <miha@noughmad.eu>
0004  *
0005  *  SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006  */
0007 
0008 #include "externalcontrol.h"
0009 #include "gamemanager.h"
0010 #include "knightsadaptor.h"
0011 #include "knightsdebug.h"
0012 
0013 #include <QRandomGenerator>
0014 
0015 #define FORWARD_FUNCTION(name, action) void ExternalControl::name() { Manager::self()->sendOffer(Action##action, NoColor, QRandomGenerator::global()->bounded(RAND_MAX)); }
0016 
0017 using namespace Knights;
0018 
0019 ExternalControl::ExternalControl(QObject* parent) : QObject(parent) {
0020     connect(Manager::self(), &Manager::pieceMoved, this, &ExternalControl::slotMoveMade);
0021     new KnightsAdaptor(this);
0022     qCDebug(LOG_KNIGHTS) << QDBusConnection::sessionBus().registerObject(QStringLiteral("/Knights"), this);
0023     qCDebug(LOG_KNIGHTS) << QDBusConnection::sessionBus().registerService(QStringLiteral("org.kde.Knights"));
0024 }
0025 
0026 ExternalControl::~ExternalControl() = default;
0027 
0028 FORWARD_FUNCTION(adjourn,Adjourn)
0029 FORWARD_FUNCTION(abort,Abort)
0030 FORWARD_FUNCTION(pauseGame,Pause)
0031 FORWARD_FUNCTION(resumeGame,Resume)
0032 FORWARD_FUNCTION(undo,Undo)
0033 FORWARD_FUNCTION(offerDraw,Draw)
0034 
0035 void ExternalControl::movePiece(const QString& move) {
0036     Manager::self()->moveByExternalControl(Move(move));
0037 }
0038 
0039 void ExternalControl::slotMoveMade(const Knights::Move& move) {
0040     Q_EMIT moveMade(move.string());
0041 }
0042 
0043 #include "moc_externalcontrol.cpp"