diff --git a/plugins/RetroChess/README.txt b/plugins/RetroChess/README.txt new file mode 100644 index 000000000..ee1f2ed60 --- /dev/null +++ b/plugins/RetroChess/README.txt @@ -0,0 +1,18 @@ +RS .6 Chess Plugin +================== + +Both players must currently select each other and press "play game" - ensure your friend has their chess window open before making a move! + + +this is a combination of an existing chess game, and the exampleplugin (rename script makes it easy to use as base for new plugin) +https://github.com/RetroShare/ExampleRSPlugin + + +--disclaimer-- +may not support playing more than one game of chess +may crash RS +may kill your cat, dog, or other loved one. + + +--extra info-- +based on: https://github.com/chozabu/CHESS-in-Qt diff --git a/plugins/RetroChess/RetroChess.pro b/plugins/RetroChess/RetroChess.pro new file mode 100644 index 000000000..bc314edc3 --- /dev/null +++ b/plugins/RetroChess/RetroChess.pro @@ -0,0 +1,70 @@ +!include("../Common/retroshare_plugin.pri"): error("Could not include file ../Common/retroshare_plugin.pri") + +greaterThan(QT_MAJOR_VERSION, 4) { + # Qt 5 + QT += widgets +} + +exists($$[QMAKE_MKSPECS]/features/mobility.prf) { + CONFIG += mobility +} else { + QT += multimedia +} +CONFIG += qt uic qrc resources +MOBILITY = multimedia + +DEPENDPATH += ../../retroshare-gui/src/temp/ui ../../libretroshare/src +INCLUDEPATH += ../../retroshare-gui/src/temp/ui ../../libretroshare/src +INCLUDEPATH += ../../retroshare-gui/src/retroshare-gui + +INCLUDEPATH += ../../rapidjson-1.1.0 + +#################################### Windows ##################################### + +linux-* { + #INCLUDEPATH += /usr/include + #LIBS += $$system(pkg-config --libs opencv) +} + +win32 { + LIBS_DIR = $$PWD/../../../libs + #LIBS += -L"$$LIBS_DIR/lib/opencv" + + #OPENCV_VERSION = 249 + #LIBS += -lopencv_core$$OPENCV_VERSION -lopencv_highgui$$OPENCV_VERSION -lopencv_imgproc$$OPENCV_VERSION -llibjpeg -llibtiff -llibpng -llibjasper -lIlmImf -lole32 -loleaut32 -luuid -lavicap32 -lavifil32 -lvfw32 -lz +} + +QMAKE_CXXFLAGS *= -Wall + +SOURCES = RetroChessPlugin.cpp \ + services/p3RetroChess.cc \ + services/rsRetroChessItems.cc \ + gui/NEMainpage.cpp \ + gui/RetroChessNotify.cpp \ + gui/chess.cpp \ + gui/tile.cpp \ + gui/validation.cpp \ + gui/RetroChessChatWidgetHolder.cpp + +HEADERS = RetroChessPlugin.h \ + services/p3RetroChess.h \ + services/rsRetroChessItems.h \ + interface/rsRetroChess.h \ + gui/NEMainpage.h \ + gui/RetroChessNotify.h \ + gui/tile.h \ + gui/validation.h \ + gui/chess.h \ + gui/RetroChessChatWidgetHolder.h + +#FORMS = gui/AudioInputConfig.ui + +TARGET = RetroChess + +RESOURCES = gui/RetroChess_images.qrc + + +#LIBS += -lspeex -lspeexdsp + +FORMS += \ + gui/NEMainpage.ui diff --git a/plugins/RetroChess/RetroChessPlugin.cpp b/plugins/RetroChess/RetroChessPlugin.cpp new file mode 100644 index 000000000..844059d77 --- /dev/null +++ b/plugins/RetroChess/RetroChessPlugin.cpp @@ -0,0 +1,200 @@ +/**************************************************************** + * RetroShare is distributed under the following license: + * + * Copyright (C) 2015 + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ****************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include "gui/chat/ChatWidget.h" + +#include "RetroChessPlugin.h" +#include "interface/rsRetroChess.h" +#include "gui/NEMainpage.h" +#include "gui/RetroChessNotify.h" +#include "gui/RetroChessChatWidgetHolder.h" + + +#define IMAGE_RetroChess ":/images/chess.png" + +static void *inited = new RetroChessPlugin() ; + +extern "C" { + + // This is *the* functions required by RS plugin system to give RS access to the plugin. + // Be careful to: + // - always respect the C linkage convention + // - always return an object of type RsPlugin* + // + void *RETROSHARE_PLUGIN_provide() + { + static RetroChessPlugin *p = new RetroChessPlugin() ; + + return (void*)p ; + } + + // This symbol contains the svn revision number grabbed from the executable. + // It will be tested by RS to load the plugin automatically, since it is safe to load plugins + // with same revision numbers, assuming that the revision numbers are up-to-date. + // + uint32_t RETROSHARE_PLUGIN_revision = abs(atoi(RS_EXTRA_VERSION)) ; + + // This symbol contains the svn revision number grabbed from the executable. + // It will be tested by RS to load the plugin automatically, since it is safe to load plugins + // with same revision numbers, assuming that the revision numbers are up-to-date. + // + uint32_t RETROSHARE_PLUGIN_api = RS_PLUGIN_API_VERSION ; +} + +void RetroChessPlugin::getPluginVersion(int& major, int& minor, int& build, int& svn_rev) const +{ + major = RS_MAJOR_VERSION ; + minor = RS_MINOR_VERSION ; + build = RS_MINI_VERSION ; + svn_rev = abs(atoi(RS_EXTRA_VERSION)) ; +} + +RetroChessPlugin::RetroChessPlugin() +{ + qRegisterMetaType("RsPeerId"); + mainpage = NULL ; + mRetroChess = NULL ; + mPlugInHandler = NULL; + mPeers = NULL; + config_page = NULL ; + mIcon = NULL ; + + mRetroChessNotify = new RetroChessNotify; +} + +void RetroChessPlugin::setInterfaces(RsPlugInInterfaces &interfaces) +{ + mPeers = interfaces.mPeers; +} + +/*ConfigPage *RetroChessPlugin::qt_config_page() const +{ + // The config pages are deleted when config is closed, so it's important not to static the + // created object. + // + return new AudioInputConfig() ; +}*/ + +QDialog *RetroChessPlugin::qt_about_page() const +{ + static QMessageBox *about_dialog = NULL ; + + if(about_dialog == NULL) + { + about_dialog = new QMessageBox() ; + + QString text ; + text += QObject::tr("

RetroShare RetroChess plugin


* Contributors: Cyril Soler, Josselin Jacquard
") ; + text += QObject::tr("
The RetroChess plugin adds RetroChess to the private chat window of RetroShare. to use it, proceed as follows:
    ") ; + text += QObject::tr("
  • setup microphone levels using the configuration panel
  • ") ; + text += QObject::tr("
  • check your microphone by looking at the VU-metters
  • ") ; + text += QObject::tr("
  • in the private chat, enable sound input/output by clicking on the two RetroChess icons
") ; + text += QObject::tr("Your friend needs to run the plugin to talk/listen to you, or course.") ; + text += QObject::tr("

This is an experimental feature. Don't hesitate to send comments and suggestion to the RS dev team.") ; + + about_dialog->setText(text) ; + about_dialog->setStandardButtons(QMessageBox::Ok) ; + } + + return about_dialog ; +} + +ChatWidgetHolder *RetroChessPlugin::qt_get_chat_widget_holder(ChatWidget *chatWidget) const +{ + switch (chatWidget->chatType()) { + case ChatWidget::CHATTYPE_PRIVATE: + return new RetroChessChatWidgetHolder(chatWidget, mRetroChessNotify); + case ChatWidget::CHATTYPE_UNKNOWN: + case ChatWidget::CHATTYPE_LOBBY: + case ChatWidget::CHATTYPE_DISTANT: + break; + } + + return NULL; +} + +p3Service *RetroChessPlugin::p3_service() const +{ + if(mRetroChess == NULL) + rsRetroChess = mRetroChess = new p3RetroChess(mPlugInHandler,mRetroChessNotify) ; // , 3600 * 24 * 30 * 6); // 6 Months + + return mRetroChess ; +} + +void RetroChessPlugin::setPlugInHandler(RsPluginHandler *pgHandler) +{ + mPlugInHandler = pgHandler; +} + +QIcon *RetroChessPlugin::qt_icon() const +{ + if (mIcon == NULL) { + Q_INIT_RESOURCE(RetroChess_images); + + mIcon = new QIcon(IMAGE_RetroChess); + } + + return mIcon; +} +MainPage *RetroChessPlugin::qt_page() const +{ + if(mainpage == NULL){ + mainpage = new NEMainpage(0, mRetroChessNotify);//mPeers, mFiles) ; + //tpage = new NEMainpage( ); + //mainpage = tpage; + } + + return mainpage ; +} + +std::string RetroChessPlugin::getShortPluginDescription() const +{ + return "RetroChess"; +} + +std::string RetroChessPlugin::getPluginName() const +{ + return "RetroChess"; +} + +QTranslator* RetroChessPlugin::qt_translator(QApplication */*app*/, const QString& languageCode, const QString& externalDir) const +{ + return NULL; +} + +void RetroChessPlugin::qt_sound_events(SoundEvents &/*events*/) const +{ +// events.addEvent(QApplication::translate("RetroChess", "RetroChess"), QApplication::translate("RetroChess", "Incoming call"), RetroChess_SOUND_INCOMING_CALL); +} + +/*ToasterNotify *RetroChessPlugin::qt_toasterNotify(){ + if (!mRetroChessToasterNotify) { + mRetroChessToasterNotify = new RetroChessToasterNotify(mRetroChess, mRetroChessNotify); + } + return mRetroChessToasterNotify; +}*/ diff --git a/plugins/RetroChess/RetroChessPlugin.h b/plugins/RetroChess/RetroChessPlugin.h new file mode 100644 index 000000000..5d2940aa7 --- /dev/null +++ b/plugins/RetroChess/RetroChessPlugin.h @@ -0,0 +1,76 @@ +/* this is the central part of the plugin */ +/**************************************************************** + * RetroShare is distributed under the following license: + * + * Copyright (C) 2015 + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ****************************************************************/ +#pragma once + +/*RetroChess*/ +#include "services/p3RetroChess.h" + +/*libretroshare"*/ +#include + +#include "gui/NEMainpage.h" + +class RetroChessGUIHandler ; +class RetroChessNotify ; + +class RetroChessPlugin: public RsPlugin +{ + public: + RetroChessPlugin() ; + virtual ~RetroChessPlugin() {} + + virtual p3Service *p3_service() const ; + virtual uint16_t rs_service_id() const { return RS_SERVICE_TYPE_RetroChess_PLUGIN ; } + //virtual ConfigPage *qt_config_page() const ; + virtual QDialog *qt_about_page() const ; + virtual ChatWidgetHolder *qt_get_chat_widget_holder(ChatWidget *chatWidget) const ; + + virtual QIcon *qt_icon() const; + virtual QTranslator *qt_translator(QApplication *app, const QString& languageCode, const QString& externalDir) const; + virtual void qt_sound_events(SoundEvents &events) const; + + virtual void getPluginVersion(int& major, int& minor, int &build, int& svn_rev) const ; + virtual void setPlugInHandler(RsPluginHandler *pgHandler); + + virtual std::string configurationFileName() const { return "RetroChess.cfg" ; } + + virtual std::string getShortPluginDescription() const ; + virtual std::string getPluginName() const; + virtual void setInterfaces(RsPlugInInterfaces& interfaces); + + //================================== RsPlugin Notify ==================================// + //virtual ToasterNotify *qt_toasterNotify(); + + virtual MainPage *qt_page() const ; + + private: + mutable p3RetroChess *mRetroChess ; + mutable RsPluginHandler *mPlugInHandler; + mutable RsPeers* mPeers; + mutable ConfigPage *config_page ; + mutable QIcon *mIcon; + mutable MainPage* mainpage ; + + RetroChessNotify *mRetroChessNotify ; + RetroChessGUIHandler *mRetroChessGUIHandler ; +}; + diff --git a/plugins/RetroChess/cptest b/plugins/RetroChess/cptest new file mode 100644 index 000000000..6a98e93b4 --- /dev/null +++ b/plugins/RetroChess/cptest @@ -0,0 +1,3 @@ +kdesudo -u retrotester cp lib*.so* /home/retrotester/.retroshare/extensions6/ +kdesudo -u retrotester /home/chozabu/git/RetroShare/retroshare-gui/src/RetroShare + diff --git a/plugins/RetroChess/gui/NEMainpage.cpp b/plugins/RetroChess/gui/NEMainpage.cpp new file mode 100644 index 000000000..ae45c49f8 --- /dev/null +++ b/plugins/RetroChess/gui/NEMainpage.cpp @@ -0,0 +1,225 @@ +#include "NEMainpage.h" +#include "ui_NEMainpage.h" + +//#include "services/p3RetroChess.h" +#include "interface/rsRetroChess.h" +#include "services/rsRetroChessItems.h" +#include "retroshare/rsservicecontrol.h" +#include "gui/notifyqt.h" +#include +#include + +#include +#include +#include +#include + +#include "gui/chat/ChatDialog.h" + + +NEMainpage::NEMainpage(QWidget *parent, RetroChessNotify *notify) : + MainPage(parent), + mNotify(notify), + ui(new Ui::NEMainpage) +{ + ui->setupUi(this); + setupMenuActions(); + + connect(mNotify, SIGNAL(NeMsgArrived(RsPeerId,QString)), this , SLOT(NeMsgArrived(RsPeerId,QString))); + connect(mNotify, SIGNAL(chessStart(RsPeerId)), this , SLOT(chessStart(RsPeerId))); + connect(ui->friendSelectionWidget, SIGNAL(itemSelectionChanged()), this, SLOT(friendSelectionChanged())); + + ui->friendSelectionWidget->start(); + ui->friendSelectionWidget->setModus(FriendSelectionWidget::MODUS_SINGLE); + ui->friendSelectionWidget->setShowType(FriendSelectionWidget::SHOW_SSL); + + connect(ui->friendSelectionWidget, SIGNAL(contentChanged()), this, SLOT(on_filterPeersButton_clicked())); + connect(NotifyQt::getInstance(), SIGNAL(peerStatusChanged(const QString&,int)), this, SLOT(on_filterPeersButton_clicked())); + + QString welcomemessage = QTime::currentTime().toString() +" "; + welcomemessage+= tr("Welcome to RetroChess lobby"); + ui->listWidget->addItem(welcomemessage); + +} + +NEMainpage::~NEMainpage() +{ + delete ui; +} + + +void NEMainpage::on_pingAllButton_clicked() +{ + +} + + +void NEMainpage::chessStart(const RsPeerId &peer_id){ + + create_chess_window(peer_id.toStdString(), 0); +} + +void NEMainpage::NeMsgArrived(const RsPeerId &peer_id, QString str) +{ + QJsonDocument jdoc = QJsonDocument::fromJson(str.toUtf8()); + QVariantMap vmap = jdoc.toVariant().toMap(); + std::cout << "GUI got Packet from: " << peer_id; + std::cout << " saying " << str.toStdString(); + std::cout << std::endl; + QString type = vmap.value("type").toString(); + if (type == "chat"){ + QString output = QTime::currentTime().toString() +" "; + output+= QString::fromStdString(rsPeers->getPeerName(peer_id)); + output+=": "; + output+=vmap.value("message").toString(); + ui->listWidget->addItem(output); + }else if (type == "chessclick"){ + int row = vmap.value("row").toInt(); + int col = vmap.value("col").toInt(); + int count = vmap.value("count").toInt(); + RetroChessWindow* rcw = activeGames.value(peer_id.toStdString()); + rcw->validate_tile(row,col,count); + }else if (type == "chess_init"){ + create_chess_window(peer_id.toStdString(), 1); + }else if (type == "chess_invite"){ + ChatDialog::chatFriend(ChatId(peer_id)); + rsRetroChess->gotInvite(peer_id); + mNotify->notifyChessInvite(peer_id); + }else if (type == "chess_accept"){ + if (rsRetroChess->hasInviteTo(peer_id)){ + create_chess_window(peer_id.toStdString(), 1); + rsRetroChess->acceptedInvite(peer_id); + } + }else{ + QString output = QTime::currentTime().toString() +" "; + output+= QString::fromStdString(rsPeers->getPeerName(peer_id)); + output+=": "; + output+=str; + ui->listWidget->addItem(output); + } + + { + QString output = QTime::currentTime().toString() +" "; + output+= QString::fromStdString(rsPeers->getPeerName(peer_id)); + output+=": "; + output+=str; + ui->netLogWidget->addItem(output); + } +} + +void NEMainpage::on_broadcastButton_clicked() +{ + rsRetroChess->msg_all(ui->msgInput->text().toStdString()); + NeMsgArrived(rsPeers->getOwnId(),ui->msgInput->text()); + ui->msgInput->clear(); +} + +void NEMainpage::create_chess_window(std::string peer_id, int player_id){ + RetroChessWindow *rcw = new RetroChessWindow(peer_id, player_id); + rcw->show(); + + activeGames.insert(peer_id, rcw); + ui->active_games->addItem(QString::fromStdString(peer_id)); +} + +void NEMainpage::on_playButton_clicked() +{ + //get peer + FriendSelectionWidget::IdType idType; + std::string fid = ui->friendSelectionWidget->selectedId(idType); + //make_board(); + create_chess_window(fid, 0); + + QVariantMap map; + map.insert("type", "chess_init"); + + rsRetroChess->qvm_msg_peer(RsPeerId(fid),map); + + std::cout << fid; +} + +void NEMainpage::on_filterPeersButton_clicked() +{ + std::cout << "\n\n filter peers \n"; + + std::list ssllist ; + rsPeers->getFriendList(ssllist); + + + RsPeerServiceInfo ownServices; + rsServiceControl->getOwnServices(ownServices); + + std::vector peer_ids ; + std::vector service_ids ; + + for(std::list::const_iterator it(ssllist.begin());it!=ssllist.end();++it) + peer_ids.push_back(*it) ; + service_ids.clear() ; + uint32_t service_id; + for(std::map::const_iterator sit(ownServices.mServiceList.begin());sit!=ownServices.mServiceList.end();++sit) + { + RsServiceInfo rsi = sit->second; + service_ids.push_back(sit->first) ; + std::cout << rsi.mServiceName << rsi.mServiceType << "\n"; + if (strcmp(rsi.mServiceName.c_str(), "RetroChess") == 0){ + service_id = rsi.mServiceType; + std::cout << "setting service ID\n"; + } + } + + for(std::list::const_iterator it(ssllist.begin());it!=ssllist.end();++it) + { + RsPeerServiceInfo local_service_perms ; + RsPeerServiceInfo remote_service_perms ; + RsPeerId id = *it; + + rsServiceControl->getServicesAllowed (*it, local_service_perms) ; + rsServiceControl->getServicesProvided(*it,remote_service_perms) ; + + bool local_allowed = local_service_perms.mServiceList.find(service_id) != local_service_perms.mServiceList.end() ; + bool remote_allowed = remote_service_perms.mServiceList.find(service_id) != remote_service_perms.mServiceList.end() ; + bool allowed = (local_allowed && remote_allowed); + //QString la = + QString serviceinfos = QString("peerlocal: ") + QString(local_allowed?"yes":"no") + QString(" remote: ") + QString(remote_allowed?"yes":"no"); + ui->netLogWidget->addItem(serviceinfos); + std::cout << serviceinfos.toStdString() << "\n"; + //if (allowed){ + QList items; + ui->friendSelectionWidget->itemsFromId(FriendSelectionWidget::IDTYPE_SSL,id.toStdString(),items); + + std::cout << items.size() << "\n"; + if (items.size()){ + QTreeWidgetItem* item = items.first(); + item->setHidden(!allowed); + } + } + + + // +} + +void NEMainpage::setupMenuActions() +{ + mActionPlayChess = new QAction(QIcon(), tr("Play Chess"), this); + connect(mActionPlayChess, SIGNAL(triggered(bool)), this, SLOT(on_playButton_clicked())); + + ui->friendSelectionWidget->addContextMenuAction(mActionPlayChess); + +} + +void NEMainpage::friendSelectionChanged() +{ + std::set peerIds; + ui->friendSelectionWidget->selectedIds(peerIds, false); + + std::set gxsIds; + ui->friendSelectionWidget->selectedIds(gxsIds, false); + + int selectedCount = peerIds.size() + gxsIds.size(); + + mActionPlayChess->setEnabled(selectedCount); + + FriendSelectionWidget::IdType idType; + ui->friendSelectionWidget->selectedId(idType); + +} diff --git a/plugins/RetroChess/gui/NEMainpage.h b/plugins/RetroChess/gui/NEMainpage.h new file mode 100644 index 000000000..ac5e29fdd --- /dev/null +++ b/plugins/RetroChess/gui/NEMainpage.h @@ -0,0 +1,52 @@ +/* This is the main page displayed by the plugin */ +#ifndef NEMAINPAGE_H +#define NEMAINPAGE_H + +#include +#include +#include +#include "gui/RetroChessNotify.h" + +#include "gui/chess.h" + +#include + +class QAction; + +namespace Ui { +class NEMainpage; +} + +class NEMainpage : public MainPage +{ + Q_OBJECT + +public: + explicit NEMainpage(QWidget *parent, RetroChessNotify *notify); + ~NEMainpage(); + +private slots: + void setupMenuActions(); + void friendSelectionChanged(); + void on_pingAllButton_clicked(); + void NeMsgArrived(const RsPeerId &peer_id, QString str); + void chessStart(const RsPeerId &peer_id); + + void on_broadcastButton_clicked(); + + void on_playButton_clicked(); + + void on_filterPeersButton_clicked(); + +private: + Ui::NEMainpage *ui; + RetroChessNotify *mNotify; + + QAction *mActionPlayChess; + //RetroChessWindow *tempwindow; + + QMap activeGames; + void create_chess_window(std::string peer_id, int player_id); +}; + +#endif // NEMAINPAGE_H diff --git a/plugins/RetroChess/gui/NEMainpage.ui b/plugins/RetroChess/gui/NEMainpage.ui new file mode 100644 index 000000000..0c06d9b5b --- /dev/null +++ b/plugins/RetroChess/gui/NEMainpage.ui @@ -0,0 +1,268 @@ + + + NEMainpage + + + + 0 + 0 + 684 + 578 + + + + Form + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + 0 + 0 + + + + QFrame::NoFrame + + + QFrame::Raised + + + + 2 + + + 2 + + + 2 + + + 2 + + + + + + 24 + 24 + + + + + + + :/images/retrochess.png + + + true + + + + + + + RetroChess + + + + + + + Qt::Horizontal + + + + 123 + 13 + + + + + + + + Qt::NoFocus + + + + :/icons/help_64.png:/icons/help_64.png + + + true + + + true + + + + + + + + + + 0 + + + + Chess Lobby + + + + + + Qt::Horizontal + + + + Invite Friends + + + + + + Qt::CustomContextMenu + + + + + + + CheckPeers + + + + + + + Qt::Horizontal + + + + 41 + 20 + + + + + + + + + + + + + + Play Chess + + + + + + + Qt::Horizontal + + + + 475 + 20 + + + + + + + + + + + + + Type a message here + + + + + + + Send + + + + + + + + + + + + Net log + + + + + + + + + + + + + + 0 + 0 + + + + + 16777215 + 150 + + + + Active Games + + + + + + + + + + + + + StyledLabel + QLabel +
gui/common/StyledLabel.h
+
+ + FriendSelectionWidget + QWidget +
gui/common/FriendSelectionWidget.h
+ 1 +
+
+ + + + +
diff --git a/plugins/RetroChess/gui/RetroChessChatWidgetHolder.cpp b/plugins/RetroChess/gui/RetroChessChatWidgetHolder.cpp new file mode 100644 index 000000000..c5bdcc3df --- /dev/null +++ b/plugins/RetroChess/gui/RetroChessChatWidgetHolder.cpp @@ -0,0 +1,147 @@ + + +#include +#include +#include +#include + + +#include "interface/rsRetroChess.h" + +#include "gui/chat/ChatWidget.h" + +#include "RetroChessChatWidgetHolder.h" + +#include +#include + +#define IMAGE_RetroChess ":/images/chess.png" + +RetroChessChatWidgetHolder::RetroChessChatWidgetHolder(ChatWidget *chatWidget, RetroChessNotify *notify) + : QObject(), ChatWidgetHolder(chatWidget), mRetroChessNotify(notify) +{ + QIcon icon ; + icon.addPixmap(QPixmap(IMAGE_RetroChess)) ; + + + playChessButton = new QToolButton ; + playChessButton->setIcon(icon) ; + playChessButton->setToolTip(tr("Invite Friend to Chess")); + playChessButton->setIconSize(QSize(35,35)) ; + playChessButton->setAutoRaise(true) ; + + mChatWidget->addChatBarWidget(playChessButton); + connect(playChessButton, SIGNAL(clicked()), this , SLOT(chessPressed())); + connect(notify, SIGNAL(chessInvited(RsPeerId)), this , SLOT(chessnotify(RsPeerId))); + +} + +RetroChessChatWidgetHolder::~RetroChessChatWidgetHolder() +{ + + button_map::iterator it = buttonMapTakeChess.begin(); + while (it != buttonMapTakeChess.end()) { + it = buttonMapTakeChess.erase(it); + } +} + +void RetroChessChatWidgetHolder::chessnotify(RsPeerId from_peer_id) +{ + RsPeerId peer_id = mChatWidget->getChatId().toPeerId();//TODO support GXSID + //if (peer_id!=from_peer_id)return;//invite from another chat + if (rsRetroChess->hasInviteFrom(peer_id)){ + if (mChatWidget) { + QString buttonName = QString::fromUtf8(rsPeers->getPeerName(peer_id).c_str()); + if (buttonName.isEmpty()) buttonName = "Chess";//TODO maybe change all with GxsId + //disable old buttons + button_map::iterator it = buttonMapTakeChess.begin(); + while (it != buttonMapTakeChess.end()) { + it = buttonMapTakeChess.erase(it); + } + //button_map::iterator it = buttonMapTakeChess.find(buttonName); + //if (it == buttonMapTakeChess.end()){ + mChatWidget->addChatMsg(true, tr("Chess Status"), QDateTime::currentDateTime(), QDateTime::currentDateTime() + , tr("%1 inviting you to start Chess. Do you want to accept or decline the invitation?").arg(buttonName), ChatWidget::MSGTYPE_SYSTEM); + RSButtonOnText *button = mChatWidget->getNewButtonOnTextBrowser(tr("Accept")); + button->setToolTip(tr("Accept")); + button->setStyleSheet(QString("border: 1px solid #199909;") + .append("font-size: 12pt; color: white;") + .append("min-width: 128px; min-height: 24px;") + .append("border-radius: 6px;") + .append("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 0.67, " + "stop: 0 #22c70d, stop: 1 #116a06);") + + ); + + button->updateImage(); + + connect(button,SIGNAL(clicked()),this,SLOT(chessStart())); + connect(button,SIGNAL(mouseEnter()),this,SLOT(botMouseEnter())); + connect(button,SIGNAL(mouseLeave()),this,SLOT(botMouseLeave())); + + buttonMapTakeChess.insert(buttonName, button); + //} + } + + + } +} + +void RetroChessChatWidgetHolder::chessPressed() +{ + RsPeerId peer_id = mChatWidget->getChatId().toPeerId();//TODO support GXSID + if (rsRetroChess->hasInviteFrom(peer_id)){ + + rsRetroChess->acceptedInvite(peer_id); + mRetroChessNotify->notifyChessStart(peer_id); + return; + + } + rsRetroChess->sendInvite(peer_id); + + QString peerName = QString::fromUtf8(rsPeers->getPeerName(peer_id).c_str()); + mChatWidget->addChatMsg(true, tr("Chess Status"), QDateTime::currentDateTime(), QDateTime::currentDateTime() + , tr("You're now inviting %1 to play Chess").arg(peerName), ChatWidget::MSGTYPE_SYSTEM); + +} + +void RetroChessChatWidgetHolder::chessStart() +{ + RsPeerId peer_id = mChatWidget->getChatId().toPeerId();//TODO support GXSID + + rsRetroChess->acceptedInvite(peer_id); + mRetroChessNotify->notifyChessStart(peer_id); + return; +} + +void RetroChessChatWidgetHolder::botMouseEnter() +{ + RSButtonOnText *source = qobject_cast(QObject::sender()); + if (source){ + source->setStyleSheet(QString("border: 1px solid #333333;") + .append("font-size: 12pt; color: white;") + .append("min-width: 128px; min-height: 24px;") + .append("border-radius: 6px;") + .append("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 0.67, " + "stop: 0 #444444, stop: 1 #222222);") + + ); + //source->setDown(true); + } +} + +void RetroChessChatWidgetHolder::botMouseLeave() +{ + RSButtonOnText *source = qobject_cast(QObject::sender()); + if (source){ + source->setStyleSheet(QString("border: 1px solid #199909;") + .append("font-size: 12pt; color: white;") + .append("min-width: 128px; min-height: 24px;") + .append("border-radius: 6px;") + .append("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 0.67, " + "stop: 0 #22c70d, stop: 1 #116a06);") + + ); + //source->setDown(false); + } +} diff --git a/plugins/RetroChess/gui/RetroChessChatWidgetHolder.h b/plugins/RetroChess/gui/RetroChessChatWidgetHolder.h new file mode 100644 index 000000000..d98b3b262 --- /dev/null +++ b/plugins/RetroChess/gui/RetroChessChatWidgetHolder.h @@ -0,0 +1,35 @@ +#ifndef RETROCHESSCHATWIDGETHOLDER_H +#define RETROCHESSCHATWIDGETHOLDER_H + +#include "RetroChessChatWidgetHolder.h" +#include +#include +#include + +class RetroChessChatWidgetHolder : public QObject, public ChatWidgetHolder +{ + Q_OBJECT + +public: + RetroChessChatWidgetHolder(ChatWidget *chatWidget, RetroChessNotify *notify); + virtual ~RetroChessChatWidgetHolder(); + +public slots: + void chessPressed(); + void chessStart(); + void chessnotify(RsPeerId from_peer_id); + + +private slots: + void botMouseEnter(); + void botMouseLeave(); + +protected: + QToolButton *playChessButton ; + RetroChessNotify *mRetroChessNotify; + + typedef QMap button_map; + button_map buttonMapTakeChess; +}; + +#endif // RETROCHESSCHATWIDGETHOLDER_H diff --git a/plugins/RetroChess/gui/RetroChessNotify.cpp b/plugins/RetroChess/gui/RetroChessNotify.cpp new file mode 100644 index 000000000..07db46225 --- /dev/null +++ b/plugins/RetroChess/gui/RetroChessNotify.cpp @@ -0,0 +1,33 @@ +#include "RetroChessNotify.h" + +RetroChessNotify::RetroChessNotify(QObject *parent) : QObject(parent) +{ + +} + +void RetroChessNotify::notifyReceivedPaint(const RsPeerId &peer_id, int x, int y) +{ + std::cout << "pNotify Recvd paint from: " << peer_id; + std::cout << " at " << x << " , " << y; + std::cout << std::endl; +} + + +void RetroChessNotify::notifyReceivedMsg(const RsPeerId& peer_id, QString str) +{ + std::cout << "pNotify Recvd Packet from: " << peer_id; + std::cout << " saying " << str.toStdString(); + std::cout << std::endl; + emit NeMsgArrived(peer_id, str) ; +} + +void RetroChessNotify::notifyChessStart(const RsPeerId &peer_id) +{ + emit chessStart(peer_id) ; + +} +void RetroChessNotify::notifyChessInvite(const RsPeerId &peer_id) +{ + emit chessInvited(peer_id) ; + +} diff --git a/plugins/RetroChess/gui/RetroChessNotify.h b/plugins/RetroChess/gui/RetroChessNotify.h new file mode 100644 index 000000000..3581515a8 --- /dev/null +++ b/plugins/RetroChess/gui/RetroChessNotify.h @@ -0,0 +1,54 @@ +/**************************************************************** + * RetroShare is distributed under the following license: + * + * Copyright (C) 2015 + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ****************************************************************/ + +// This class is a Qt object to get notification from the plugin's service threads, +// and responsible to pass the info the the GUI part. +// +// Because the GUI part is async-ed with the service, it is crucial to use the +// QObject connect system to communicate between the p3Service and the gui part (handled by Qt) +// +#ifndef NETEXAMPLENOTIFY_H +#define NETEXAMPLENOTIFY_H + +#include + +#include + +class RetroChessNotify : public QObject +{ + Q_OBJECT +public: + explicit RetroChessNotify(QObject *parent = 0); + void notifyReceivedPaint(const RsPeerId &peer_id, int x, int y) ; + void notifyReceivedMsg(const RsPeerId &peer_id, QString str) ; + void notifyChessStart(const RsPeerId &peer_id) ; + void notifyChessInvite(const RsPeerId &peer_id) ; + +signals: + void NeMsgArrived(const RsPeerId &peer_id, QString str) ; // emitted when the peer gets a msg + + void chessStart(const RsPeerId &peer_id) ; + void chessInvited(const RsPeerId &peer_id) ; + +public slots: +}; + +#endif // NETEXAMPLENOTIFY_H diff --git a/plugins/RetroChess/gui/RetroChess_images.qrc b/plugins/RetroChess/gui/RetroChess_images.qrc new file mode 100644 index 000000000..9cffad0ad --- /dev/null +++ b/plugins/RetroChess/gui/RetroChess_images.qrc @@ -0,0 +1,21 @@ + + + images/bishop_black.svg + images/bishop_white.svg + images/king_black.svg + images/king_white.svg + images/knight_black.svg + images/knight_white.svg + images/pawn_black.svg + images/pawn_white.svg + images/queen_black.svg + images/queen_white.svg + images/rook_black.svg + images/rook_white.svg + images/chess-icon.png + images/chess-icon-32.png + images/chess.png + images/profile.png + images/retrochess.png + + diff --git a/plugins/RetroChess/gui/chess.cpp b/plugins/RetroChess/gui/chess.cpp new file mode 100644 index 000000000..98e38446b --- /dev/null +++ b/plugins/RetroChess/gui/chess.cpp @@ -0,0 +1,873 @@ +#include +#include "chess.h" +#include "gui/common/AvatarDefs.h" + +RetroChessWindow::RetroChessWindow(std::string peerid, int player, QWidget *parent) : + QWidget(parent), + mPeerId(peerid) + //ui(new Ui::RetroChessWindow) +{ + + //tile = { { NULL } }; + count=0; + turn=1; + max=0; + texp = new int[60]; + setGeometry(0,0,1370,700); + + QString player_str; + if (player ){ + p1id = rsPeers->getOwnId(); + p2id = RsPeerId(peerid); + player_str = " (1)"; + }else{ + p1id = RsPeerId(peerid); + p2id = rsPeers->getOwnId(); + player_str = " (2)"; + } + + p1name = rsPeers->getPeerName(p1id); + p2name = rsPeers->getPeerName(p2id); + + QString title = QString::fromStdString(p2name); + title += " Playing Chess against "; + title += QString::fromStdString(p1name); + title+=player_str; + + + this->setWindowTitle(title); + + accessories(); + chessBoard(); +} + +RetroChessWindow::~RetroChessWindow() +{ +} + +class Border +{ +public: + Border(); + void outline(QWidget *baseWidget, int xPos, int yPos, int Pos) + { + QLabel *outLabel = new QLabel(baseWidget); + + if(!Pos) + outLabel->setGeometry(xPos,yPos,552,20); //Horizontal Borders + + else + outLabel->setGeometry(xPos,yPos,20,512); //Vertical Borders + + outLabel->setStyleSheet("QLabel { background-color :rgb(170, 170, 127); color : black; }"); + } +}; + +void RetroChessWindow::accessories() +{ + QWidget *baseWidget = this; + QLabel *player2 = new QLabel(baseWidget); + QLabel *name2 = new QLabel(p2name.c_str(), baseWidget); + QLabel *time2 = new QLabel("00:00:00", baseWidget); + + QLabel *player1 = new QLabel(baseWidget); + QLabel *name1 = new QLabel(p1name.c_str(), baseWidget); + QLabel *time1 = new QLabel("00:00:00", baseWidget); + + QLabel *moves = new QLabel(baseWidget); + + name1->setGeometry(125,610,80,20); + time1->setGeometry(120,635,80,20); + player1->setGeometry(100,500,100,100); + QPixmap p1avatar; + AvatarDefs::getAvatarFromSslId(p1id, p1avatar); + player1->setPixmap(p1avatar);//QPixmap(":/images/profile.png")); + + + name2->setGeometry(125,210,80,20); + time2->setGeometry(120,235,80,20); + player2->setGeometry(100,100,100,100); + QPixmap p2avatar; + AvatarDefs::getAvatarFromSslId(p2id, p2avatar); + player2->setPixmap(p2avatar);//QPixmap(":/images/profile.png")); + + moves->setGeometry(1000,105,250,550); + moves->setStyleSheet("QLabel {background-color: white;}"); + +} + +void RetroChessWindow::disOrange() +{ + int i; + + for(i=0;itileDisplay(); + +} + +void RetroChessWindow::validate_tile(int row, int col, int c){ + Tile *clickedtile = tile[col][row]; + //if (!click1)click1=clickedtile; + clickedtile->validate(++count); +} + +void RetroChessWindow::chessBoard() +{ + //QWidget *baseWidget, Tile *tile[8][8] + QWidget *baseWidget = this; + int i,j,k=0,hor,ver; + Border *border[4]={ NULL }; + + //borderDisplay + { + border[0]->outline(baseWidget,330,105,0); + border[1]->outline(baseWidget,330,637,0); + border[2]->outline(baseWidget,330,125,1); + border[2]->outline(baseWidget,862,125,1); + } + + //Create 64 tiles (allocating memories to the objects of Tile class) + ver=125; + for(i=0;i<8;i++) + { + hor=350; + for(j=0;j<8;j++) + { + tile[i][j] = new Tile(baseWidget); + tile[i][j]->tileColor=(i+j)%2; + tile[i][j]->piece=0; + tile[i][j]->row=i; + tile[i][j]->col=j; + tile[i][j]->tileNum=k++; + tile[i][j]->tileDisplay(); + tile[i][j]->setGeometry(hor,ver,64,64); + hor+=64; + } + ver+=64; + } + + //white pawns + for(j=0;j<8;j++) + { + tile[1][j]->piece=1; + tile[1][j]->pieceColor=0; + tile[1][j]->display('P'); + } + + //black pawns + for(j=0;j<8;j++) + { + tile[6][j]->piece=1; + tile[6][j]->pieceColor=1; + tile[6][j]->display('P'); + } + + //white and black remaining elements + for(j=0;j<8;j++) + { + tile[0][j]->piece=1; + tile[0][j]->pieceColor=0; + tile[7][j]->piece=1; + tile[7][j]->pieceColor=1; + } + + { + tile[0][0]->display('R'); + tile[0][1]->display('H'); + tile[0][2]->display('B'); + tile[0][3]->display('Q'); + tile[0][4]->display('K'); + tile[0][5]->display('B'); + tile[0][6]->display('H'); + tile[0][7]->display('R'); + } + + + { + tile[7][0]->display('R'); + tile[7][1]->display('H'); + tile[7][2]->display('B'); + tile[7][3]->display('Q'); + tile[7][4]->display('K'); + tile[7][5]->display('B'); + tile[7][6]->display('H'); + tile[7][7]->display('R'); + } + + wR=7; + wC=4; + + bR=0; + bC=4; + + +} + + + +int RetroChessWindow::chooser(Tile *temp) +{ + switch(temp->pieceName) + { + case 'P': flag=validatePawn(temp); + break; + + case 'R': flag=validateRook(temp); + break; + + case 'H': flag=validateHorse(temp); + break; + + case 'K': flag=validateKing(temp); + break; + + case 'Q': flag=validateQueen(temp); + break; + + case 'B': flag=validateBishop(temp); + break; + + } + + orange(); + + return flag; +} + +//PAWN +int RetroChessWindow::validatePawn(Tile *temp) +{ + int row,col; + + row=temp->row; + col=temp->col; + retVal=0; + + //White Pawn + if(temp->pieceColor) + { + if(row-1>=0 && !tile[row-1][col]->piece) + { + /*int tnum = tile[row-1][col]->tileNum; + std::cout << "tile: " << texp[max] << std::endl; + int a = texp[max]; + texp[max] = tnum; + max++;*/ + texp[max++]=tile[row-1][col]->tileNum; + retVal=1; + } + + if(row==6 && !tile[5][col]->piece && !tile[4][col]->piece) + { + texp[max++]=tile[row-2][col]->tileNum; + retVal=1; + } + + if(row-1>=0 && col-1>=0) + { + if(tile[row-1][col-1]->pieceColor!=temp->pieceColor && tile[row-1][col-1]->piece) + { + texp[max++]=tile[row-1][col-1]->tileNum; + retVal=1; + } + } + + if(row-1>=0 && col+1<=7) + { + if(tile[row-1][col+1]->pieceColor!=temp->pieceColor && tile[row-1][col+1]->piece) + { + texp[max++]=tile[row-1][col+1]->tileNum; + retVal=1; + } + } + } + else + { + if(row+1<=7 && !tile[row+1][col]->piece) + { + texp[max++]=tile[row+1][col]->tileNum; + retVal=1; + } + + if(row==1 && !tile[2][col]->piece && !tile[3][col]->piece) + { + texp[max++]=tile[row+2][col]->tileNum; + retVal=1; + } + + if(row+1<=7 && col-1>=0) + { + if(tile[row+1][col-1]->pieceColor!=temp->pieceColor && tile[row+1][col-1]->piece) + { + texp[max++]=tile[row+1][col-1]->tileNum; + retVal=1; + } + } + + if(row+1<=7 && col+1<=7) + { + if(tile[row+1][col+1]->pieceColor!=temp->pieceColor && tile[row+1][col+1]->piece) + { + texp[max++]=tile[row+1][col+1]->tileNum; + retVal=1; + } + } + } + + return retVal; +} + + +//ROOK +int RetroChessWindow::validateRook(Tile *temp) +{ + int r,c; + + retVal=0; + + r=temp->row; + c=temp->col; + while(r-->0) + { + if(!tile[r][c]->piece) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + } + + else if(tile[r][c]->pieceColor==temp->pieceColor) + break; + + else if(tile[r][c]->pieceColor!=temp->pieceColor) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + break; + } + } + + r=temp->row; + c=temp->col; + while(r++<7) + { + if(!tile[r][c]->piece) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + } + + else if(tile[r][c]->pieceColor==temp->pieceColor) + break; + + else if(tile[r][c]->pieceColor!=temp->pieceColor) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + break; + } + } + + r=temp->row; + c=temp->col; + while(c++<7) + { + if(!tile[r][c]->piece) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + } + + else if(tile[r][c]->pieceColor==temp->pieceColor) + break; + + else if(tile[r][c]->pieceColor!=temp->pieceColor) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + break; + } + } + + r=temp->row; + c=temp->col; + while(c-->0) + { + if(!tile[r][c]->piece) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + } + + else if(tile[r][c]->pieceColor==temp->pieceColor) + break; + + else if(tile[r][c]->pieceColor!=temp->pieceColor) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + break; + } + } + + + return retVal; +} + + +//HORSE +int RetroChessWindow::validateHorse(Tile *temp) +{ + int r,c; + retVal=0; + + r=temp->row; + c=temp->col; + + if(r-2>=0 && c-1>=0) + { + if(tile[r-2][c-1]->pieceColor!=temp->pieceColor || !tile[r-2][c-1]->piece) + { + texp[max++]=tile[r-2][c-1]->tileNum; + retVal=1; + } + } + + if(r-2>=0 && c+1<=7) + { + if(tile[r-2][c+1]->pieceColor!=temp->pieceColor || !tile[r-2][c+1]->piece) + { + texp[max++]=tile[r-2][c+1]->tileNum; + retVal=1; + } + } + + if(r-1>=0 && c-2>=0) + { + if(tile[r-1][c-2]->pieceColor!=temp->pieceColor || !tile[r-1][c-2]->piece) + { + texp[max++]=tile[r-1][c-2]->tileNum; + retVal=1; + } + } + + if(r-1>=0 && c+2<=7) + { + if(tile[r-1][c+2]->pieceColor!=temp->pieceColor || !tile[r-1][c+2]->piece) + { + texp[max++]=tile[r-1][c+2]->tileNum; + retVal=1; + } + } + + if(r+2<=7 && c+1<=7) + { + if(tile[r+2][c+1]->pieceColor!=temp->pieceColor || !tile[r+2][c+1]->piece) + { + texp[max++]=tile[r+2][c+1]->tileNum; + retVal=1; + } + } + + if(r+2<=7 && c-1>=0) + { + if(tile[r+2][c-1]->pieceColor!=temp->pieceColor || !tile[r+2][c-1]->piece) + { + texp[max++]=tile[r+2][c-1]->tileNum; + retVal=1; + } + } + + if(r+1<=7 && c-2>=0) + { + if(tile[r+1][c-2]->pieceColor!=temp->pieceColor || !tile[r+1][c-2]->piece) + { + texp[max++]=tile[r+1][c-2]->tileNum; + retVal=1; + } + } + + if(r+1<=7 && c+2<=7) + { + if(tile[r+1][c+2]->pieceColor!=temp->pieceColor || !tile[r+1][c+2]->piece) + { + texp[max++]=tile[r+1][c+2]->tileNum; + retVal=1; + } + } + + return retVal; +} + + +//KING +int RetroChessWindow::validateKing(Tile *temp) +{ + int r,c; + retVal=0; + + r=temp->row; + c=temp->col; + + if(r-1>=0) + { + if(!tile[r-1][c]->piece || tile[r-1][c]->pieceColor!=temp->pieceColor) + { + texp[max++]=tile[r-1][c]->tileNum; + retVal=1; + } + } + + if(r+1<=7) + { + if(!tile[r+1][c]->piece || tile[r+1][c]->pieceColor!=temp->pieceColor) + { + texp[max++]=tile[r+1][c]->tileNum; + retVal=1; + } + } + + if(c-1>=0) + { + if(!tile[r][c-1]->piece || tile[r][c-1]->pieceColor!=temp->pieceColor) + { + texp[max++]=tile[r][c-1]->tileNum; + retVal=1; + } + } + + if(c+1<=7) + { + if(!tile[r][c+1]->piece || tile[r][c+1]->pieceColor!=temp->pieceColor) + { + texp[max++]=tile[r][c+1]->tileNum; + retVal=1; + } + } + + if(r-1>=0 && c-1>=0) + { + if(!tile[r-1][c-1]->piece || tile[r-1][c-1]->pieceColor!=temp->pieceColor) + { + texp[max++]=tile[r-1][c-1]->tileNum; + retVal=1; + } + } + + if(r-1>=0 && c+1<=7) + { + if(!tile[r-1][c+1]->piece || tile[r-1][c+1]->pieceColor!=temp->pieceColor) + { + texp[max++]=tile[r-1][c+1]->tileNum; + retVal=1; + } + } + + if(r+1<=7 && c-1>=0) + { + if(!tile[r+1][c-1]->piece || tile[r+1][c-1]->pieceColor!=temp->pieceColor) + { + texp[max++]=tile[r+1][c-1]->tileNum; + retVal=1; + } + } + + if(r+1<=7 && c+1<=7) + { + if(!tile[r+1][c+1]->piece || tile[r+1][c+1]->pieceColor!=temp->pieceColor) + { + texp[max++]=tile[r+1][c+1]->tileNum; + retVal=1; + } + } + + return retVal; +} + + +//QUEEN +int RetroChessWindow::validateQueen(Tile *temp) +{ + int r,c; + + retVal=0; + + r=temp->row; + c=temp->col; + while(r-->0) + { + if(!tile[r][c]->piece) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + } + + else if(tile[r][c]->pieceColor==temp->pieceColor) + break; + + else if(tile[r][c]->pieceColor!=temp->pieceColor) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + break; + } + } + + r=temp->row; + c=temp->col; + while(r++<7) + { + if(!tile[r][c]->piece) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + } + + else if(tile[r][c]->pieceColor==temp->pieceColor) + break; + + else if(tile[r][c]->pieceColor!=temp->pieceColor) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + break; + } + } + + r=temp->row; + c=temp->col; + while(c++<7) + { + if(!tile[r][c]->piece) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + } + + else if(tile[r][c]->pieceColor==temp->pieceColor) + break; + + else if(tile[r][c]->pieceColor!=temp->pieceColor) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + break; + } + } + + r=temp->row; + c=temp->col; + while(c-->0) + { + if(!tile[r][c]->piece) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + } + + else if(tile[r][c]->pieceColor==temp->pieceColor) + break; + + else if(tile[r][c]->pieceColor!=temp->pieceColor) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + break; + } + } + + r=temp->row; + c=temp->col; + while(r-->0 && c++<7) + { + if(!tile[r][c]->piece) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + } + + else if(tile[r][c]->pieceColor==temp->pieceColor) + break; + + else if(tile[r][c]->pieceColor!=temp->pieceColor) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + break; + } + } + + r=temp->row; + c=temp->col; + while(r-->0 && c-->0) + { + if(!tile[r][c]->piece) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + } + + else if(tile[r][c]->pieceColor==temp->pieceColor) + break; + + else if(tile[r][c]->pieceColor!=temp->pieceColor) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + break; + } + } + + r=temp->row; + c=temp->col; + while(r++<7 && c++<7) + { + if(!tile[r][c]->piece) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + } + + else if(tile[r][c]->pieceColor==temp->pieceColor) + break; + + else if(tile[r][c]->pieceColor!=temp->pieceColor) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + break; + } + } + + r=temp->row; + c=temp->col; + while(r++<7 && c-->0) + { + if(!tile[r][c]->piece) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + } + + else if(tile[r][c]->pieceColor==temp->pieceColor) + break; + + else if(tile[r][c]->pieceColor!=temp->pieceColor) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + break; + } + } + + + return retVal; +} + +//BISHOP +int RetroChessWindow::validateBishop(Tile *temp) +{ + int r,c; + retVal=0; + + r=temp->row; + c=temp->col; + while(r-->0 && c++<7) + { + if(!tile[r][c]->piece) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + } + + else if(tile[r][c]->pieceColor==temp->pieceColor) + break; + + else if(tile[r][c]->pieceColor!=temp->pieceColor) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + break; + } + } + + r=temp->row; + c=temp->col; + while(r-->0 && c-->0) + { + if(!tile[r][c]->piece) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + } + + else if(tile[r][c]->pieceColor==temp->pieceColor) + break; + + else if(tile[r][c]->pieceColor!=temp->pieceColor) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + break; + } + } + + r=temp->row; + c=temp->col; + while(r++<7 && c++<7) + { + if(!tile[r][c]->piece) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + } + + else if(tile[r][c]->pieceColor==temp->pieceColor) + break; + + else if(tile[r][c]->pieceColor!=temp->pieceColor) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + break; + } + } + + r=temp->row; + c=temp->col; + while(r++<7 && c-->0) + { + if(!tile[r][c]->piece) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + } + + else if(tile[r][c]->pieceColor==temp->pieceColor) + break; + + else if(tile[r][c]->pieceColor!=temp->pieceColor) + { + texp[max++]=tile[r][c]->tileNum; + retVal=1; + break; + } + } + + return retVal; +} + +int RetroChessWindow::check(Tile *temp) +{ + int r,c,flag; + retVal=0; + + return retVal; +} + +void RetroChessWindow::orange() +{ + int i,n; + + for(i=0;isetStyleSheet("QLabel {background-color: orange;}"); +} diff --git a/plugins/RetroChess/gui/chess.h b/plugins/RetroChess/gui/chess.h new file mode 100644 index 000000000..79e0289f4 --- /dev/null +++ b/plugins/RetroChess/gui/chess.h @@ -0,0 +1,57 @@ +#ifndef CHESS_H +#define CHESS_H + +//#include "tile.h" +#include "validation.h" +#include "qwidget.h" + +#include "retroshare/rspeers.h" + +class RetroChessWindow : public QWidget +{ + Q_OBJECT + + void accessories(); + void chessBoard(); + RsPeerId p1id; + RsPeerId p2id; + std::string p1name; + std::string p2name; +public: + std::string mPeerId; + explicit RetroChessWindow(std::string peerid, int player = 0, QWidget *parent = 0); + ~RetroChessWindow(); + int currentplayer; + int myid; + + //from global + + int wR,wC,bR,bC; + Tile *click1; + + Tile *tile[8][8]; + + int count,turn,max; + int *texp; + + + void disOrange(); + void validate_tile(int row, int col, int c); + + + int flag,retVal; + int chooser(Tile *temp); + int validateBishop(Tile *temp); + int validateQueen(Tile *temp); + int validateKing(Tile *temp); + int validateHorse(Tile *temp); + int validateRook(Tile *temp); + int validatePawn(Tile *temp); + void orange(); + int check(Tile *temp); +}; + + +QWidget* make_board(); + +#endif // CHESS_H diff --git a/plugins/RetroChess/gui/images/bishop_black.svg b/plugins/RetroChess/gui/images/bishop_black.svg new file mode 100644 index 000000000..7cceeb06a --- /dev/null +++ b/plugins/RetroChess/gui/images/bishop_black.svg @@ -0,0 +1,116 @@ + + + + + + + + image/svg+xml + + + + + + + + diff --git a/plugins/RetroChess/gui/images/bishop_white.svg b/plugins/RetroChess/gui/images/bishop_white.svg new file mode 100644 index 000000000..2c3bfbda3 --- /dev/null +++ b/plugins/RetroChess/gui/images/bishop_white.svg @@ -0,0 +1,124 @@ + + + + + + + + image/svg+xml + + + + + + + + diff --git a/plugins/RetroChess/gui/images/chess-icon-32.png b/plugins/RetroChess/gui/images/chess-icon-32.png new file mode 100644 index 000000000..7a0790261 Binary files /dev/null and b/plugins/RetroChess/gui/images/chess-icon-32.png differ diff --git a/plugins/RetroChess/gui/images/chess-icon.png b/plugins/RetroChess/gui/images/chess-icon.png new file mode 100644 index 000000000..a2468977d Binary files /dev/null and b/plugins/RetroChess/gui/images/chess-icon.png differ diff --git a/plugins/RetroChess/gui/images/chess-notify.png b/plugins/RetroChess/gui/images/chess-notify.png new file mode 100644 index 000000000..2ed5d11af Binary files /dev/null and b/plugins/RetroChess/gui/images/chess-notify.png differ diff --git a/plugins/RetroChess/gui/images/chess-notify.svg b/plugins/RetroChess/gui/images/chess-notify.svg new file mode 100644 index 000000000..90ebcef9a --- /dev/null +++ b/plugins/RetroChess/gui/images/chess-notify.svg @@ -0,0 +1,56 @@ + + + +image/svg+xml \ No newline at end of file diff --git a/plugins/RetroChess/gui/images/chess.png b/plugins/RetroChess/gui/images/chess.png new file mode 100644 index 000000000..63c15ea76 Binary files /dev/null and b/plugins/RetroChess/gui/images/chess.png differ diff --git a/plugins/RetroChess/gui/images/chess.svg b/plugins/RetroChess/gui/images/chess.svg new file mode 100644 index 000000000..a00ab6aaf --- /dev/null +++ b/plugins/RetroChess/gui/images/chess.svg @@ -0,0 +1,56 @@ + + + +image/svg+xml \ No newline at end of file diff --git a/plugins/RetroChess/gui/images/king_black.svg b/plugins/RetroChess/gui/images/king_black.svg new file mode 100644 index 000000000..6af78a1ac --- /dev/null +++ b/plugins/RetroChess/gui/images/king_black.svg @@ -0,0 +1,135 @@ + + + + + + + + image/svg+xml + + + + + + + + diff --git a/plugins/RetroChess/gui/images/king_white.svg b/plugins/RetroChess/gui/images/king_white.svg new file mode 100644 index 000000000..3bf6c2bdf --- /dev/null +++ b/plugins/RetroChess/gui/images/king_white.svg @@ -0,0 +1,143 @@ + + + + + + + + image/svg+xml + + + + + + + + diff --git a/plugins/RetroChess/gui/images/knight_black.svg b/plugins/RetroChess/gui/images/knight_black.svg new file mode 100644 index 000000000..ec8d31af1 --- /dev/null +++ b/plugins/RetroChess/gui/images/knight_black.svg @@ -0,0 +1,101 @@ + + + + + + + + image/svg+xml + + + + + + + + diff --git a/plugins/RetroChess/gui/images/knight_white.svg b/plugins/RetroChess/gui/images/knight_white.svg new file mode 100644 index 000000000..05700612a --- /dev/null +++ b/plugins/RetroChess/gui/images/knight_white.svg @@ -0,0 +1,123 @@ + + + + + + + + image/svg+xml + + + + + + + + diff --git a/plugins/RetroChess/gui/images/muted_self.svg b/plugins/RetroChess/gui/images/muted_self.svg new file mode 100644 index 000000000..0a31bdc1e --- /dev/null +++ b/plugins/RetroChess/gui/images/muted_self.svg @@ -0,0 +1,1999 @@ + + + + + muted_self + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + muted_self + + + + Martin Skilnand + + + + + Martin Skilnand + + + 2009.08.17 + + + Mumble team + + + Icon for voice chat program mumble + + + mumble muted self + + + git://mumble.git.sourceforge.net/gitroot/mumble + muted_self.svg + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/plugins/RetroChess/gui/images/pawn_black.svg b/plugins/RetroChess/gui/images/pawn_black.svg new file mode 100644 index 000000000..5bcc2c783 --- /dev/null +++ b/plugins/RetroChess/gui/images/pawn_black.svg @@ -0,0 +1,76 @@ + + + + + + + + image/svg+xml + + + + + + + + diff --git a/plugins/RetroChess/gui/images/pawn_white.svg b/plugins/RetroChess/gui/images/pawn_white.svg new file mode 100644 index 000000000..35bfc8a6e --- /dev/null +++ b/plugins/RetroChess/gui/images/pawn_white.svg @@ -0,0 +1,92 @@ + + + + + + + + image/svg+xml + + + + + + + + diff --git a/plugins/RetroChess/gui/images/profile.png b/plugins/RetroChess/gui/images/profile.png new file mode 100644 index 000000000..385482c54 Binary files /dev/null and b/plugins/RetroChess/gui/images/profile.png differ diff --git a/plugins/RetroChess/gui/images/queen_black.svg b/plugins/RetroChess/gui/images/queen_black.svg new file mode 100644 index 000000000..4116cfca8 --- /dev/null +++ b/plugins/RetroChess/gui/images/queen_black.svg @@ -0,0 +1,141 @@ + + + + + + + + image/svg+xml + + + + + + + + diff --git a/plugins/RetroChess/gui/images/queen_white.svg b/plugins/RetroChess/gui/images/queen_white.svg new file mode 100644 index 000000000..9ed7066a6 --- /dev/null +++ b/plugins/RetroChess/gui/images/queen_white.svg @@ -0,0 +1,139 @@ + + + + + + + + image/svg+xml + + + + + + + + diff --git a/plugins/RetroChess/gui/images/retrochess.png b/plugins/RetroChess/gui/images/retrochess.png new file mode 100644 index 000000000..c525231a3 Binary files /dev/null and b/plugins/RetroChess/gui/images/retrochess.png differ diff --git a/plugins/RetroChess/gui/images/rook_black.svg b/plugins/RetroChess/gui/images/rook_black.svg new file mode 100644 index 000000000..cd94e4c40 --- /dev/null +++ b/plugins/RetroChess/gui/images/rook_black.svg @@ -0,0 +1,73 @@ + + + + + + + + image/svg+xml + + + + + + + + diff --git a/plugins/RetroChess/gui/images/rook_white.svg b/plugins/RetroChess/gui/images/rook_white.svg new file mode 100644 index 000000000..a30485b4e --- /dev/null +++ b/plugins/RetroChess/gui/images/rook_white.svg @@ -0,0 +1,83 @@ + + + + + + + + image/svg+xml + + + + + + + + diff --git a/plugins/RetroChess/gui/tile.cpp b/plugins/RetroChess/gui/tile.cpp new file mode 100644 index 000000000..e2f698a00 --- /dev/null +++ b/plugins/RetroChess/gui/tile.cpp @@ -0,0 +1,157 @@ +#include "tile.h" +#include "validation.h" +#include "chess.h" +#include "../interface/rsRetroChess.h" + +validation *valid = new validation(); + +/*extern int count,turn; +extern QWidget *myWidget; +extern Tile *click1; +extern Tile *tile[8][8]; +*/ +void validate(Tile *temp,int c); +void disOrange(); + + +void Tile::mousePressEvent(QMouseEvent *event) +{ + validate(++((RetroChessWindow*)parentWidget())->count); + std::string peer_id = ((RetroChessWindow*)parentWidget())->mPeerId; + rsRetroChess->chess_click(peer_id, this->row,this->col,((RetroChessWindow*)parentWidget())->count); +} + +void Tile::display(char elem) +{ + this->pieceName=elem; + + if(this->pieceColor && this->piece) + { + switch(elem) + { + case 'P': this->setPixmap(QPixmap(":/images/pawn_white.svg")); + break; + case 'R': this->setPixmap(QPixmap(":/images/rook_white.svg")); + break; + case 'H': this->setPixmap(QPixmap(":/images/knight_white.svg")); + break; + case 'K': this->setPixmap(QPixmap(":/images/king_white.svg")); + break; + case 'Q': this->setPixmap(QPixmap(":/images/queen_white.svg")); + break; + case 'B': this->setPixmap(QPixmap(":/images/bishop_white.svg")); + break; + } + } + + else if(this->piece) + { + switch(elem) + { + case 'P': this->setPixmap(QPixmap(":/images/pawn_black.svg")); + break; + case 'R': this->setPixmap(QPixmap(":/images/rook_black.svg")); + break; + case 'H': this->setPixmap(QPixmap(":/images/knight_black.svg")); + break; + case 'K': this->setPixmap(QPixmap(":/images/king_black.svg")); + break; + case 'Q': this->setPixmap(QPixmap(":/images/queen_black.svg")); + break; + case 'B': this->setPixmap(QPixmap(":/images/bishop_black.svg")); + break; + } + } + else + this->clear(); +} + +void Tile::validate(int c) +{ + Tile *temp = this; + int retValue,i; + + if(c==1) + { + if(temp->piece && (temp->pieceColor==((RetroChessWindow*)parentWidget())->turn)) + { + //texp[max++]=temp->tileNum; + retValue=((RetroChessWindow*)parentWidget())->chooser(temp); + + if(retValue) + { + ((RetroChessWindow*)parentWidget())->click1= new Tile(); + temp->setStyleSheet("QLabel {background-color: green;}"); + ((RetroChessWindow*)parentWidget())->click1=temp; + } + else + { + //temp->setStyleSheet("QLabel {background-color: red;}"); + ((RetroChessWindow*)parentWidget())->count=0; + } + } + else + { + //qDebug()<<"Rascel, clicking anywhere"; + ((RetroChessWindow*)parentWidget())->count=0; + } + } + + else + { + + if(temp->tileNum==((RetroChessWindow*)parentWidget())->click1->tileNum) + { + ((RetroChessWindow*)parentWidget())->click1->tileDisplay(); + ((RetroChessWindow*)parentWidget())->disOrange(); + ((RetroChessWindow*)parentWidget())->max=0; + ((RetroChessWindow*)parentWidget())->count=0; + } + + for(i=0;i<((RetroChessWindow*)parentWidget())->max;i++) + { + if(temp->tileNum==((RetroChessWindow*)parentWidget())->texp[i]) + { + ((RetroChessWindow*)parentWidget())->click1->piece=0; + temp->piece=1; + + temp->pieceColor=((RetroChessWindow*)parentWidget())->click1->pieceColor; + temp->pieceName=((RetroChessWindow*)parentWidget())->click1->pieceName; + + ((RetroChessWindow*)parentWidget())->click1->display(((RetroChessWindow*)parentWidget())->click1->pieceName); + temp->display(((RetroChessWindow*)parentWidget())->click1->pieceName); + + ((RetroChessWindow*)parentWidget())->click1->tileDisplay(); + temp->tileDisplay(); + + retValue=((RetroChessWindow*)parentWidget())->check(((RetroChessWindow*)parentWidget())->click1); + /* + if(retValue) + { + tile[wR][wC]->setStyleSheet("QLabel {background-color: red;}"); + } + */ + + ((RetroChessWindow*)parentWidget())->disOrange(); + + ((RetroChessWindow*)parentWidget())->max=0; + + ((RetroChessWindow*)parentWidget())->turn=(((RetroChessWindow*)parentWidget())->turn+1)%2; + ((RetroChessWindow*)parentWidget())->count=0; + } + + else + ((RetroChessWindow*)parentWidget())->count=1; + } + } +} + +void Tile::tileDisplay() +{ + + if(this->tileColor) + this->setStyleSheet("QLabel {background-color: rgb(120, 120, 90);}:hover{background-color: rgb(170,85,127);}"); + else + this->setStyleSheet("QLabel {background-color: rgb(211, 211, 158);}:hover{background-color: rgb(170,95,127);}"); +} + diff --git a/plugins/RetroChess/gui/tile.h b/plugins/RetroChess/gui/tile.h new file mode 100644 index 000000000..59c008c4e --- /dev/null +++ b/plugins/RetroChess/gui/tile.h @@ -0,0 +1,27 @@ +#ifndef TILE_H +#define TILE_H +#include +#include + +class Tile: public QLabel +{ +public: + + //Fields + int tileColor,piece,pieceColor,row,col,tileNum; + char pieceName; + + //Constructors + Tile(QWidget* pParent=0, Qt::WindowFlags f=0) : QLabel(pParent, f) {}; + Tile(const QString& text, QWidget* pParent = 0, Qt::WindowFlags f = 0) : QLabel(text, pParent, f){}; + + //Methods + void mousePressEvent(QMouseEvent *event); + void display(char elem); + void tileDisplay(); + void validate(int c); +}; + +void validate_tile(int row, int col, int c); + +#endif // TILE_H diff --git a/plugins/RetroChess/gui/validation.cpp b/plugins/RetroChess/gui/validation.cpp new file mode 100644 index 000000000..9dfc332ed --- /dev/null +++ b/plugins/RetroChess/gui/validation.cpp @@ -0,0 +1,14 @@ +#include "validation.h" +#include +#include "string" +#include "chess.h" + + + +validation::validation() +{ + //Nothing here +// tile = ((RetroChessWindow*)parentWidget())->tile; + +} + diff --git a/plugins/RetroChess/gui/validation.h b/plugins/RetroChess/gui/validation.h new file mode 100644 index 000000000..9c49456d1 --- /dev/null +++ b/plugins/RetroChess/gui/validation.h @@ -0,0 +1,15 @@ +#ifndef VALIDATION_H +#define VALIDATION_H +#include "tile.h" + + +class validation +{ + + int *tile; + +public: + validation(); +}; + +#endif // VALIDATION_H diff --git a/plugins/RetroChess/interface/rsRetroChess.h b/plugins/RetroChess/interface/rsRetroChess.h new file mode 100644 index 000000000..63d1a8ccb --- /dev/null +++ b/plugins/RetroChess/interface/rsRetroChess.h @@ -0,0 +1,57 @@ +/* this is a simple class to make it easy for any part of the plugin to call its services */ +/**************************************************************** + * RetroShare is distributed under the following license: + * + * Copyright (C) 2015 + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ****************************************************************/ + +// interface class for p3RetroChess service +// + +#pragma once + +#include +#include +#include +#include +#include + +class RsRetroChess ; +extern RsRetroChess *rsRetroChess; + +static const uint32_t CONFIG_TYPE_RetroChess_PLUGIN = 0xe001 ; + +class RsRetroChess +{ + public: + + virtual void ping_all() = 0; + virtual void broadcast_paint(int x, int y) = 0; + virtual void msg_all(std::string msg) = 0; + virtual void chess_click(std::string peer_id, int col, int row, int count) = 0; + virtual void qvm_msg_peer(RsPeerId peerID, QVariantMap data) = 0; + virtual void str_msg_peer(RsPeerId peerID, QString strdata) = 0; + virtual void raw_msg_peer(RsPeerId peerID, std::string msg) = 0; + virtual bool hasInviteFrom(RsPeerId peerID) = 0; + virtual bool hasInviteTo(RsPeerId peerID) = 0; + virtual void acceptedInvite(RsPeerId peerID) = 0; + virtual void gotInvite(RsPeerId peerID) = 0; + virtual void sendInvite(RsPeerId peerID) = 0; +}; + + diff --git a/plugins/RetroChess/rename_plugin.sh b/plugins/RetroChess/rename_plugin.sh new file mode 100644 index 000000000..c63f6baab --- /dev/null +++ b/plugins/RetroChess/rename_plugin.sh @@ -0,0 +1,10 @@ +#!/bin/bash +newname=$1 +oldname="RetroChess" +echo "$newname" +echo "$oldname" +find . -not -path '*/\.*' -type f -print0 | xargs -0 sed -i "s/$oldname/$newname/g" +#find . -type f -exec rename "s/$oldname/$newname/' '{}" \; +find . | sed -e "p;s/$oldname/$newname/" | xargs -n2 git mv + +echo "now change 0xb00b5 in services/*items.h to a unique value of your choice to identify your plugin!" diff --git a/plugins/RetroChess/services/p3RetroChess.cc b/plugins/RetroChess/services/p3RetroChess.cc new file mode 100644 index 000000000..afff87ab2 --- /dev/null +++ b/plugins/RetroChess/services/p3RetroChess.cc @@ -0,0 +1,411 @@ +/**************************************************************** + * RetroShare is distributed under the following license: + * + * Copyright (C) 2015 + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ****************************************************************/ + +#include "util/rsdir.h" +#include "retroshare/rsiface.h" +#include "pqi/pqibin.h" +#include "pqi/pqistore.h" +#include "pqi/p3linkmgr.h" +#include +#include + +#include // for std::istringstream + +#include "services/p3RetroChess.h" +#include "services/rsRetroChessItems.h" + +#include + +#include "gui/RetroChessNotify.h" + + +//#define DEBUG_RetroChess 1 + + +/* DEFINE INTERFACE POINTER! */ +RsRetroChess *rsRetroChess = NULL; + + + +#ifdef WINDOWS_SYS +#include +#include +#endif + +static double getCurrentTS() +{ + +#ifndef WINDOWS_SYS + struct timeval cts_tmp; + gettimeofday(&cts_tmp, NULL); + double cts = (cts_tmp.tv_sec) + ((double) cts_tmp.tv_usec) / 1000000.0; +#else + struct _timeb timebuf; + _ftime( &timebuf); + double cts = (timebuf.time) + ((double) timebuf.millitm) / 1000.0; +#endif + return cts; +} + +static uint64_t convertTsTo64bits(double ts) +{ + uint32_t secs = (uint32_t) ts; + uint32_t usecs = (uint32_t) ((ts - (double) secs) * 1000000); + uint64_t bits = (((uint64_t) secs) << 32) + usecs; + return bits; +} + +static double convert64bitsToTs(uint64_t bits) +{ + uint32_t usecs = (uint32_t) (bits & 0xffffffff); + uint32_t secs = (uint32_t) ((bits >> 32) & 0xffffffff); + double ts = (secs) + ((double) usecs) / 1000000.0; + + return ts; +} + +p3RetroChess::p3RetroChess(RsPluginHandler *handler,RetroChessNotify *notifier) + : RsPQIService(RS_SERVICE_TYPE_RetroChess_PLUGIN,0,handler), mRetroChessMtx("p3RetroChess"), mServiceControl(handler->getServiceControl()) , mNotify(notifier) +{ + addSerialType(new RsRetroChessSerialiser()); + + + //plugin default configuration + +} +RsServiceInfo p3RetroChess::getServiceInfo() +{ + const std::string TURTLE_APP_NAME = "RetroChess"; + const uint16_t TURTLE_APP_MAJOR_VERSION = 1; + const uint16_t TURTLE_APP_MINOR_VERSION = 0; + const uint16_t TURTLE_MIN_MAJOR_VERSION = 1; + const uint16_t TURTLE_MIN_MINOR_VERSION = 0; + + return RsServiceInfo(RS_SERVICE_TYPE_RetroChess_PLUGIN, + TURTLE_APP_NAME, + TURTLE_APP_MAJOR_VERSION, + TURTLE_APP_MINOR_VERSION, + TURTLE_MIN_MAJOR_VERSION, + TURTLE_MIN_MINOR_VERSION); +} + +int p3RetroChess::tick() +{ +#ifdef DEBUG_RetroChess + std::cerr << "ticking p3RetroChess" << std::endl; +#endif + + //processIncoming(); + //sendPackets(); + + return 0; +} + +int p3RetroChess::status() +{ + return 1; +} +#include +void p3RetroChess::str_msg_peer(RsPeerId peerID, QString strdata){ + QVariantMap map; + map.insert("type", "chat"); + map.insert("message", strdata); + + qvm_msg_peer(peerID,map); +} + +void p3RetroChess::qvm_msg_peer(RsPeerId peerID, QVariantMap data){ + QJsonDocument jsondoc = QJsonDocument::fromVariant(data); + std::string msg = jsondoc.toJson().toStdString(); + raw_msg_peer(peerID, msg); +} + +void p3RetroChess::chess_click(std::string peer_id, int col, int row, int count) +{ + QVariantMap map; + map.insert("type", "chessclick"); + map.insert("col", col); + map.insert("row", row); + map.insert("count", count); + + RsPeerId peerID = RsPeerId(peer_id); + qvm_msg_peer(peerID,map); + +} + +bool p3RetroChess::hasInviteFrom(RsPeerId peerID) +{ + return invitesFrom.find(peerID)!=invitesFrom.end(); +} +bool p3RetroChess::hasInviteTo(RsPeerId peerID) +{ + return invitesTo.find(peerID)!=invitesTo.end(); +} + +void p3RetroChess::acceptedInvite(RsPeerId peerID) +{ + std::set::iterator it =invitesTo.find(peerID); + if (it != invitesTo.end()){ + invitesTo.erase(it); + } + + it =invitesFrom.find(peerID); + if (it != invitesFrom.end()){ + invitesFrom.erase(it); + } + raw_msg_peer(peerID, "{\"type\":\"chess_accept\"}"); +} + +void p3RetroChess::gotInvite(RsPeerId peerID) +{ + + std::set::iterator it =invitesFrom.find(peerID); + if (it == invitesFrom.end()){ + invitesFrom.insert(peerID); + } +} +void p3RetroChess::sendInvite(RsPeerId peerID) +{ + + std::set::iterator it =invitesTo.find(peerID); + if (it == invitesTo.end()){ + invitesTo.insert(peerID); + } + raw_msg_peer(peerID, "{\"type\":\"chess_invite\"}"); +} + +/*void p3RetroChess::set_peer(RsPeerId peer) +{ + mPeerID = peer; +}*/ +void p3RetroChess::raw_msg_peer(RsPeerId peerID, std::string msg){ + std::cout << "MSging: " << peerID.toStdString() << "\n"; + std::cout << "MSging: " << msg << "\n"; + /* create the packet */ + RsRetroChessDataItem *pingPkt = new RsRetroChessDataItem(); + pingPkt->PeerId(peerID); + pingPkt->m_msg = msg; + pingPkt->data_size = msg.size(); + //pingPkt->mSeqNo = mCounter; + //pingPkt->mPingTS = convertTsTo64bits(ts); + + //storePingAttempt(*it, ts, mCounter); + +#ifdef DEBUG_RetroChess + std::cerr << "p3RetroChess::msg_all() With Packet:"; + std::cerr << std::endl; + pingPkt->print(std::cerr, 10); +#endif + + sendItem(pingPkt); +} + +void p3RetroChess::msg_all(std::string msg){ + /* we ping our peers */ + //if(!mServiceControl) + // return ; + + //std::set onlineIds; + std::list< RsPeerId > onlineIds; + // mServiceControl->getPeersConnected(getServiceInfo().mServiceType, onlineIds); + rsPeers->getOnlineList(onlineIds); + + double ts = getCurrentTS(); + +#ifdef DEBUG_RetroChess + std::cerr << "p3RetroChess::msg_all() @ts: " << ts; + std::cerr << std::endl; +#endif + + std::cout << "READY TO BCast: " << onlineIds.size() << "\n"; + /* prepare packets */ + std::list::iterator it; + for(it = onlineIds.begin(); it != onlineIds.end(); it++) + { + str_msg_peer(RsPeerId(*it),QString::fromStdString(msg)); + } +} + +void p3RetroChess::ping_all(){ + //TODO ping all! +} + +void p3RetroChess::broadcast_paint(int x, int y) +{ + std::list< RsPeerId > onlineIds; + // mServiceControl->getPeersConnected(getServiceInfo().mServiceType, onlineIds); + rsPeers->getOnlineList(onlineIds); + + double ts = getCurrentTS(); + + + std::cout << "READY TO PAINT: " << onlineIds.size() << "\n"; + /* prepare packets */ + std::list::iterator it; + for(it = onlineIds.begin(); it != onlineIds.end(); it++) + { + + std::cout << "painting to: " << (*it).toStdString() << "\n"; + QVariantMap map; + map.insert("type", "paint"); + map.insert("x", x); + map.insert("y", y); + + qvm_msg_peer(RsPeerId(*it),map); + /* create the packet */ + //TODO send paint packets + } +} + +//TODO mNotify->notifyReceivedPaint(item->PeerId(), item->x,item->y); + + + +void p3RetroChess::handleData(RsRetroChessDataItem *item) +{ + RsStackMutex stack(mRetroChessMtx); /****** LOCKED MUTEX *******/ + + // store the data in a queue. + + + mNotify->notifyReceivedMsg(item->PeerId(), QString::fromStdString(item->m_msg)); +} + +bool p3RetroChess::recvItem(RsItem *item) +{ + std::cout << "recvItem type: " << item->PacketSubType() << "\n"; + /* pass to specific handler */ + bool keep = false ; + + switch(item->PacketSubType()) + { + case RS_PKT_SUBTYPE_RetroChess_DATA: + handleData(dynamic_cast(item)); + keep = true ; + break; + /*case RS_PKT_SUBTYPE_RetroChess_INVITE: + if (invites.find(item->PeerId()!=invites.end())){ + invites.insert(item->PeerId()); + } + mNotify-> + + //keep = true ; + break;*/ + + default: + break; + } + + /* clean up */ + if(!keep) + delete item; + return true ; +} + + + +RsTlvKeyValue p3RetroChess::push_int_value(const std::string& key,int value) +{ + RsTlvKeyValue kv ; + kv.key = key ; + rs_sprintf(kv.value, "%d", value); + + return kv ; +} +int p3RetroChess::pop_int_value(const std::string& s) +{ + std::istringstream is(s) ; + + int val ; + is >> val ; + + return val ; +} + +bool p3RetroChess::saveList(bool& cleanup, std::list& lst) +{ + cleanup = true ; + + RsConfigKeyValueSet *vitem = new RsConfigKeyValueSet ; + + /*vitem->tlvkvs.pairs.push_back(push_int_value("P3RetroChess_CONFIG_ATRANSMIT",_atransmit)) ; + vitem->tlvkvs.pairs.push_back(push_int_value("P3RetroChess_CONFIG_VOICEHOLD",_voice_hold)) ; + vitem->tlvkvs.pairs.push_back(push_int_value("P3RetroChess_CONFIG_VADMIN" ,_vadmin)) ; + vitem->tlvkvs.pairs.push_back(push_int_value("P3RetroChess_CONFIG_VADMAX" ,_vadmax)) ; + vitem->tlvkvs.pairs.push_back(push_int_value("P3RetroChess_CONFIG_NOISE_SUP",_noise_suppress)) ; + vitem->tlvkvs.pairs.push_back(push_int_value("P3RetroChess_CONFIG_MIN_LOUDN",_min_loudness)) ; + vitem->tlvkvs.pairs.push_back(push_int_value("P3RetroChess_CONFIG_ECHO_CNCL",_echo_cancel)) ;*/ + + lst.push_back(vitem) ; + + return true ; +} +bool p3RetroChess::loadList(std::list& load) +{ + for(std::list::const_iterator it(load.begin());it!=load.end();++it) + { +#ifdef P3TURTLE_DEBUG + assert(item!=NULL) ; +#endif + RsConfigKeyValueSet *vitem = dynamic_cast(*it) ; + /* + if(vitem != NULL) + for(std::list::const_iterator kit = vitem->tlvkvs.pairs.begin(); kit != vitem->tlvkvs.pairs.end(); ++kit) + if(kit->key == "P3RetroChess_CONFIG_ATRANSMIT") + _atransmit = pop_int_value(kit->value) ; + else if(kit->key == "P3RetroChess_CONFIG_VOICEHOLD") + _voice_hold = pop_int_value(kit->value) ; + else if(kit->key == "P3RetroChess_CONFIG_VADMIN") + _vadmin = pop_int_value(kit->value) ; + else if(kit->key == "P3RetroChess_CONFIG_VADMAX") + _vadmax = pop_int_value(kit->value) ; + else if(kit->key == "P3RetroChess_CONFIG_NOISE_SUP") + _noise_suppress = pop_int_value(kit->value) ; + else if(kit->key == "P3RetroChess_CONFIG_MIN_LOUDN") + _min_loudness = pop_int_value(kit->value) ; + else if(kit->key == "P3RetroChess_CONFIG_ECHO_CNCL") + _echo_cancel = pop_int_value(kit->value) ; + + delete vitem ; + */ + } + + return true ; +} + +RsSerialiser *p3RetroChess::setupSerialiser() +{ + RsSerialiser *rsSerialiser = new RsSerialiser(); + rsSerialiser->addSerialType(new RsRetroChessSerialiser()); + rsSerialiser->addSerialType(new RsGeneralConfigSerialiser()); + + return rsSerialiser ; +} + + + + + + + + + + diff --git a/plugins/RetroChess/services/p3RetroChess.h b/plugins/RetroChess/services/p3RetroChess.h new file mode 100644 index 000000000..b86b87dd7 --- /dev/null +++ b/plugins/RetroChess/services/p3RetroChess.h @@ -0,0 +1,120 @@ +/* this handles the networking service of this plugin */ +/**************************************************************** + * RetroShare is distributed under the following license: + * + * Copyright (C) 2015 + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ****************************************************************/ + +#pragma once + +#include +#include +#include + +#include "services/rsRetroChessItems.h" +#include "services/p3service.h" +#include "serialiser/rstlvbase.h" +#include "rsitems/rsconfigitems.h" +#include "plugins/rspqiservice.h" +#include + +class p3LinkMgr; +class RetroChessNotify ; + + + +//!The RS VoIP Test service. + /** + * + * This is only used to test Latency for the moment. + */ + +class p3RetroChess: public RsPQIService, public RsRetroChess +// Maybe we inherit from these later - but not needed for now. +//, public p3Config, public pqiMonitor +{ + public: + p3RetroChess(RsPluginHandler *cm,RetroChessNotify *); + + /***** overloaded from rsRetroChess *****/ + + + /***** overloaded from p3Service *****/ + /*! + * This retrieves all chat msg items and also (important!) + * processes chat-status items that are in service item queue. chat msg item requests are also processed and not returned + * (important! also) notifications sent to notify base on receipt avatar, immediate status and custom status + * : notifyCustomState, notifyChatStatus, notifyPeerHasNewAvatar + * @see NotifyBase + */ + virtual int tick(); + virtual int status(); + virtual bool recvItem(RsItem *item); + + /*************** pqiMonitor callback ***********************/ + //virtual void statusChange(const std::list &plist); + + + /************* from p3Config *******************/ + virtual RsSerialiser *setupSerialiser() ; + + /*! + * chat msg items and custom status are saved + */ + virtual bool saveList(bool& cleanup, std::list&) ; + virtual bool loadList(std::list& load) ; + virtual std::string configurationFileName() const { return "RetroChess.cfg" ; } + + virtual RsServiceInfo getServiceInfo() ; + + void ping_all(); + + void broadcast_paint(int x, int y); + void msg_all(std::string msg); + void str_msg_peer(RsPeerId peerID, QString strdata); + void raw_msg_peer(RsPeerId peerID, std::string msg); + void qvm_msg_peer(RsPeerId peerID, QVariantMap data); + + void chess_click(std::string peer_id, int col, int row, int count); + //void set_peer(RsPeerId peer); + + bool hasInviteFrom(RsPeerId peerID); + bool hasInviteTo(RsPeerId peerID); + void gotInvite(RsPeerId peerID); + void acceptedInvite(RsPeerId peerID); + void sendInvite(RsPeerId peerID); +private: + + + std::set invitesTo; + std::set invitesFrom; + void handleData(RsRetroChessDataItem*) ; + + RsMutex mRetroChessMtx; + + //RsPeerId mPeerID; + + + static RsTlvKeyValue push_int_value(const std::string& key,int value) ; + static int pop_int_value(const std::string& s) ; + + + RsServiceControl *mServiceControl; + RetroChessNotify *mNotify ; + +}; diff --git a/plugins/RetroChess/services/rsRetroChessItems.cc b/plugins/RetroChess/services/rsRetroChessItems.cc new file mode 100644 index 000000000..9a9a6327e --- /dev/null +++ b/plugins/RetroChess/services/rsRetroChessItems.cc @@ -0,0 +1,175 @@ +/**************************************************************** + * RetroShare is distributed under the following license: + * + * Copyright (C) 2015 + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ****************************************************************/ + +#include +#include "serialiser/rsbaseserial.h" +#include "serialiser/rstlvbase.h" + +#include "services/rsRetroChessItems.h" + +/*** +#define RSSERIAL_DEBUG 1 +***/ + +#include + +#define HOLLERITH_LEN_SPEC 4 +/*************************************************************************/ + +std::ostream& RsRetroChessDataItem::print(std::ostream &out, uint16_t indent) +{ + printRsItemBase(out, "RsRetroChessDataItem", indent); + uint16_t int_Indent = indent + 2; + printIndent(out, int_Indent); + out << "flags: " << flags << std::endl; + + printIndent(out, int_Indent); + out << "data size: " << std::hex << data_size << std::dec << std::endl; + + printRsItemEnd(out, "RsRetroChessDataItem", indent); + return out; +} + +/*************************************************************************/ +uint32_t RsRetroChessDataItem::serial_size() const +{ + uint32_t s = 8; /* header */ + s += 4; /* flags */ + s += 4; /* data_size */ + //s += m_msg.length()+HOLLERITH_LEN_SPEC; /* data */ + s += getRawStringSize(m_msg); + + return s; +} + +/* serialise the data to the buffer */ +bool RsRetroChessDataItem::serialise(void *data, uint32_t& pktsize) +{ + uint32_t tlvsize = serial_size() ; + uint32_t offset = 0; + + if (pktsize < tlvsize) + return false; /* not enough space */ + + pktsize = tlvsize; + + bool ok = true; + + ok &= setRsItemHeader(data, tlvsize, PacketId(), tlvsize); + +#ifdef RSSERIAL_DEBUG + std::cerr << "RsRetroChessSerialiser::serialiseRetroChessDataItem() Header: " << ok << std::endl; + std::cerr << "RsRetroChessSerialiser::serialiseRetroChessDataItem() Size: " << tlvsize << std::endl; +#endif + + /* skip the header */ + offset += 8; + + /* add mandatory parts first */ + ok &= setRawUInt32(data, tlvsize, &offset, flags); + ok &= setRawUInt32(data, tlvsize, &offset, data_size); + + + ok &= setRawString(data, tlvsize, &offset, m_msg ); + std::cout << "string sizes: " << getRawStringSize(m_msg) << " OR " << m_msg.size() << "\n"; + + if (offset != tlvsize) + { + ok = false; + std::cerr << "RsRetroChessSerialiser::serialiseRetroChessPingItem() Size Error! " << std::endl; + std::cerr << "expected " << tlvsize << " got " << offset << std::endl; + std::cerr << "m_msg looks like " << m_msg << std::endl; + } + + return ok; +} +/* serialise the data to the buffer */ + +/*************************************************************************/ +/*************************************************************************/ + +RsRetroChessDataItem::RsRetroChessDataItem(void *data, uint32_t pktsize) + : RsRetroChessItem(RS_PKT_SUBTYPE_RetroChess_DATA) +{ + /* get the type and size */ + uint32_t rstype = getRsItemId(data); + uint32_t rssize = getRsItemSize(data); + + uint32_t offset = 0; + + if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) || (RS_SERVICE_TYPE_RetroChess_PLUGIN != getRsItemService(rstype)) || (RS_PKT_SUBTYPE_RetroChess_DATA != getRsItemSubType(rstype))) + throw std::runtime_error("Wrong packet subtype") ; + + if (pktsize < rssize) /* check size */ + throw std::runtime_error("Not enough space") ; + + bool ok = true; + + /* skip the header */ + offset += 8; + + /* get mandatory parts first */ + ok &= getRawUInt32(data, rssize, &offset, &flags); + ok &= getRawUInt32(data, rssize, &offset, &data_size); + + + ok &= getRawString(data, rssize, &offset, m_msg ); + + if (offset != rssize) + throw std::runtime_error("Serialization error.") ; + + if (!ok) + throw std::runtime_error("Serialization error.") ; +} +/*************************************************************************/ + +RsItem* RsRetroChessSerialiser::deserialise(void *data, uint32_t *pktsize) +{ +#ifdef RSSERIAL_DEBUG + std::cerr << "RsRetroChessSerialiser::deserialise()" << std::endl; +#endif + + /* get the type and size */ + uint32_t rstype = getRsItemId(data); + + if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) || (RS_SERVICE_TYPE_RetroChess_PLUGIN != getRsItemService(rstype))) + return NULL ; + + try + { + switch(getRsItemSubType(rstype)) + { + case RS_PKT_SUBTYPE_RetroChess_DATA: return new RsRetroChessDataItem(data, *pktsize); + + default: + return NULL; + } + } + catch(std::exception& e) + { + std::cerr << "RsRetroChessSerialiser: deserialization error: " << e.what() << std::endl; + return NULL; + } +} + + +/*************************************************************************/ + diff --git a/plugins/RetroChess/services/rsRetroChessItems.h b/plugins/RetroChess/services/rsRetroChessItems.h new file mode 100644 index 000000000..b07ca5b7b --- /dev/null +++ b/plugins/RetroChess/services/rsRetroChessItems.h @@ -0,0 +1,125 @@ +/* this describes the datatypes sent over the network, and how to (de)serialise them */ +/**************************************************************** + * RetroShare is distributed under the following license: + * + * Copyright (C) 2015 + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + ****************************************************************/ + +#pragma once + +/* + * libretroshare/src/serialiser: rsRetroChessItems.h + * + * RetroShare Serialiser. + * + * Copyright 2011 by Robert Fernie. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License Version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + * + * Please report all bugs and problems to "retroshare@lunamutt.com". + * + */ + +#include + +#include "rsitems/rsserviceids.h" +#include "serialiser/rsserial.h" +#include "rsitems/rsitem.h" + + +/**************************************************************************/ + +const uint16_t RS_SERVICE_TYPE_RetroChess_PLUGIN = 0xc4e55; + +const uint8_t RS_PKT_SUBTYPE_RetroChess_DATA = 0x01; + +const uint8_t QOS_PRIORITY_RS_RetroChess = 9 ; + + +class RsRetroChessItem: public RsItem +{ + public: + RsRetroChessItem(uint8_t RetroChess_subtype) + : RsItem(RS_PKT_VERSION_SERVICE,RS_SERVICE_TYPE_RetroChess_PLUGIN,RetroChess_subtype) + { + setPriorityLevel(QOS_PRIORITY_RS_RetroChess) ; + } + + virtual ~RsRetroChessItem() {}; + virtual void clear() {}; + virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) = 0 ; + + virtual bool serialise(void *data,uint32_t& size) = 0 ; // Isn't it better that items can serialise themselves ? + virtual uint32_t serial_size() const = 0 ; // deserialise is handled using a constructor +}; + + +class RsRetroChessDataItem: public RsRetroChessItem +{ + public: + RsRetroChessDataItem() :RsRetroChessItem(RS_PKT_SUBTYPE_RetroChess_DATA) {} + RsRetroChessDataItem(void *data,uint32_t size) ; // de-serialization + + virtual bool serialise(void *data,uint32_t& size) ; + virtual uint32_t serial_size() const ; + + virtual ~RsRetroChessDataItem() + { + } + virtual std::ostream& print(std::ostream &out, uint16_t indent = 0); + + uint32_t flags ; + uint32_t data_size ; + std::string m_msg; +}; + + +class RsRetroChessSerialiser: public RsSerialType +{ + public: + RsRetroChessSerialiser() + :RsSerialType(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_RetroChess_PLUGIN) + { + } + virtual ~RsRetroChessSerialiser() {} + + virtual uint32_t size (RsItem *item) + { + return dynamic_cast(item)->serial_size() ; + } + + virtual bool serialise (RsItem *item, void *data, uint32_t *size) + { + return dynamic_cast(item)->serialise(data,*size) ; + } + virtual RsItem *deserialise(void *data, uint32_t *size); +}; + +/**************************************************************************/