File indexing completed on 2024-05-12 04:04:15

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 #ifndef KNIGHTS_PROTOCOL_H
0009 #define KNIGHTS_PROTOCOL_H
0010 
0011 #include <core/move.h>
0012 #include <core/piece.h>
0013 
0014 #include <QObject>
0015 #include <QTime>
0016 #include <QPointer>
0017 
0018 namespace Knights {
0019 
0020 class Offer;
0021 
0022 
0023 struct TimeControl;
0024 
0025 
0026 class ChatWidget;
0027 
0028 class ProtocolPrivate;
0029 
0030 class Protocol : public QObject {
0031     Q_OBJECT
0032     Q_ENUMS ( Feature )
0033     Q_ENUMS ( ErrorCode )
0034     Q_FLAGS ( Features )
0035     Q_PROPERTY ( Color color READ color WRITE setColor )
0036     Q_PROPERTY ( QString playerName READ playerName WRITE setPlayerName )
0037 
0038 public:
0039     enum Feature {
0040         NoFeatures = 0x00, /**< The protocol supports none of the optional features */
0041         TimeLimit = 0x01, /**< The protocol supports setting a time limit for players */
0042         SetTimeLimit = 0x02, /**< The protocol can enable/disable and set the time limits */
0043         UpdateTime = 0x04, /**< The protocol notifies the programs of changes to times */
0044         Pause = 0x08, /**< The protocol supports pausing the clock */
0045         History = 0x10,
0046         Undo = 0x20, /**< It is possible to undo a move */
0047         GameOver = 0x40, /**< The protocol emits gameOver() when the game is over */
0048         Draw = 0x80,
0049         Adjourn = 0x100,
0050         Resign = 0x200,
0051         Abort = 0x400,
0052         SetDifficulty = 0x800, /**< It is possible to set the difficulty level before starting the game */
0053         AdjustDifficulty = 0x1000 /**< It is possible to change the difficulty level during the game */
0054     };
0055     Q_DECLARE_FLAGS ( Features, Feature )
0056 
0057     enum ErrorCode {
0058         NoError = 0,
0059         UserCancelled,
0060         NetworkError,
0061         InstallationError,
0062         UnknownError
0063     };
0064 
0065     enum ToolWidgetType {
0066         ConsoleToolWidget,
0067         ChatToolWidget,
0068         OtherToolWidget
0069     };
0070 
0071     struct ToolWidgetData {
0072         QWidget* widget;
0073         QString title;
0074         QString name;
0075         ToolWidgetType type;
0076         Color owner;
0077     };
0078 
0079     static QString stringFromErrorCode ( ErrorCode code );
0080     static Protocol* white();
0081     static void setWhiteProtocol ( Protocol* p );
0082     static Protocol* black();
0083     static void setBlackProtocol ( Protocol* p );
0084     static Protocol* byColor ( Color color );
0085 
0086     explicit Protocol(QObject* parent = nullptr);
0087     ~Protocol() override;
0088 
0089     // Needed functions
0090     virtual bool isLocal();
0091     virtual bool isComputer();
0092 
0093     Color color() const;
0094     QString playerName() const;
0095     QVariant attribute ( const QString& attribute ) const;
0096     QVariant attribute ( const char* attribute ) const;
0097 
0098     void setColor ( Color color );
0099     void setPlayerName ( const QString& name );
0100     void setAttribute ( const QString& attribute, QVariant value );
0101     void setAttribute ( const char* attribute, QVariant value );
0102     void setAttributes ( QVariantMap attributes );
0103 
0104 protected:
0105 
0106     ChatWidget* createChatWidget();
0107     ChatWidget* createConsoleWidget();
0108     void initComplete();
0109 
0110 public Q_SLOTS:
0111     virtual void move ( const Move& m ) = 0;
0112     virtual void init() = 0;
0113     virtual void startGame() = 0;
0114     virtual void makeOffer ( const Offer& offer ) = 0;
0115     virtual void acceptOffer ( const Offer& offer ) = 0;
0116     virtual void declineOffer ( const Offer& offer ) = 0;
0117 
0118     // Optional features
0119     virtual void setWinner ( Color winner );
0120 
0121 public:
0122     virtual Features supportedFeatures();
0123     virtual int timeRemaining();
0124     virtual QList<ToolWidgetData> toolWidgets();
0125     virtual void setTimeControl ( const TimeControl& c );
0126     virtual bool isReady();
0127     virtual void setDifficulty ( int depth, int memory );
0128 
0129 Q_SIGNALS:
0130     void pieceMoved ( const Move& m );
0131     void illegalMove();
0132     void gameOver ( Color winner );
0133 
0134 
0135     void initSuccesful();
0136     void error ( const Protocol::ErrorCode& errorCode, const QString& errorString = QString() );
0137 
0138     void timeChanged ( const QTime& time );
0139     void undoPossible ( bool possible );
0140     void redoPossible ( bool possible );
0141 
0142 private:
0143     static QPointer<Protocol> m_white;
0144     static QPointer<Protocol> m_black;
0145     ProtocolPrivate* d_ptr;
0146     Q_DECLARE_PRIVATE ( Protocol )
0147 };
0148 
0149 Q_DECLARE_OPERATORS_FOR_FLAGS ( Protocol::Features )
0150 }
0151 
0152 #endif // KNIGHTS_PROTOCOL_H