Skip to content

Commit

Permalink
Added noughtsandcrossesplugin to dev.
Browse files Browse the repository at this point in the history
This is a plugin to play noughts and crosses game.
Plugin was written by Kevin Smith in 2006. Ported from qt3 to qt4 by WADealer. Ported from qt3-qt4 to qt5 by VitoZz.
To start the game you need to send a message to opponent (text in options).
Plugin not yet properly tested.
  • Loading branch information
Vitozz committed Mar 8, 2022
1 parent 5b19092 commit 16e3b93
Show file tree
Hide file tree
Showing 7 changed files with 891 additions and 0 deletions.
1 change: 1 addition & 0 deletions dev/CMakeLists.txt
Expand Up @@ -2,6 +2,7 @@ cmake_minimum_required( VERSION 3.1.0 )

set( plugins_list
battleshipgameplugin
noughtsandcrossesplugin
)

message(STATUS "Plugins from dev subdirectory")
Expand Down
78 changes: 78 additions & 0 deletions dev/noughtsandcrossesplugin/CMakeLists.txt
@@ -0,0 +1,78 @@
unset(_HDRS)
unset(_UIS)
unset(_SRCS)
unset(_RSCS)
unset(PLUGIN)

set( PLUGIN noughtsandcrossesplugin )
project(${PLUGIN} LANGUAGES CXX)
cmake_minimum_required(VERSION 3.1.0)
if(POLICY CMP0071)
if(${CMAKE_VERSION} VERSION_LESS "3.10.0")
cmake_policy(SET CMP0071 OLD)
else()
cmake_policy(SET CMP0071 NEW)
endif()
endif()
if(POLICY CMP0074)
cmake_policy(SET CMP0074 NEW)
endif()
set( CMAKE_AUTOMOC TRUE )

get_filename_component(ABS_PLUGINS_ROOT_DIR "${CMAKE_CURRENT_LIST_DIR}/../.." ABSOLUTE)
set(PLUGINS_ROOT_DIR "${ABS_PLUGINS_ROOT_DIR}" CACHE STRING "Plugins root path. Path where include directory placed")
set( CMAKE_MODULE_PATH
${CMAKE_MODULE_PATH}
${PLUGINS_ROOT_DIR}/cmake/modules
)
find_package(PsiPluginsApi REQUIRED)
include(${PsiPluginsApi_DIR}/variables.cmake)
include_directories(
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_LIST_DIR}
${PsiPluginsApi_INCLUDE_DIR}
)
if(NOT PLUGINS_INSTALL_PATH)
set(PLUGINS_INSTALL_PATH "${CMAKE_INSTALL_PREFIX}/${PLUGINS_PATH}")
endif()

set(_HDRS
tictac.h
)

set( _SRCS
${PLUGIN}.cpp
tictac.cpp
)

find_package( Qt5 COMPONENTS Widgets Xml REQUIRED )
set(QT_DEPLIBS
Qt5::Widgets
Qt5::Xml
)

if(WIN32)
set(LIB_TYPE "MODULE")
else()
set(LIB_TYPE "SHARED")
endif()

add_library(
${PLUGIN}
${LIB_TYPE}
${_SRCS}
)
target_link_libraries(
${PLUGIN}
${QT_DEPLIBS}
)

install(
TARGETS
${PLUGIN}
LIBRARY
DESTINATION
${PLUGINS_INSTALL_PATH}
RUNTIME DESTINATION
${PLUGINS_INSTALL_PATH}
)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
247 changes: 247 additions & 0 deletions dev/noughtsandcrossesplugin/noughtsandcrossesplugin.cpp
@@ -0,0 +1,247 @@
/*
* noughtsandcrossesplugin.cpp - Psi plugin to play noughts and crosses
* Copyright (C) 2006 Kevin Smith
*
* 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, see <https://www.gnu.org/licenses/>.
*
*/

#include <QDebug>
#include <QGridLayout>
#include <QLabel>
#include <QtCore>
#include <QtGui>

#include "eventfilter.h"
#include "psiplugin.h"
#include "stanzasender.h"
#include "stanzasendinghost.h"

#include "tictac.h"

class NoughtsAndCrossesPlugin : public QObject, public PsiPlugin, public EventFilter, public StanzaSender {
Q_OBJECT
Q_PLUGIN_METADATA(IID "com.psi-plus.NoughtsAndCrosses" FILE "psiplugin.json")
Q_INTERFACES(PsiPlugin EventFilter StanzaSender)

public:
NoughtsAndCrossesPlugin();

virtual QString name() const;
virtual QWidget *options();
virtual bool enable();
virtual bool disable();

virtual void setStanzaSendingHost(StanzaSendingHost *host);
virtual bool processEvent(int account, QDomElement &e);
virtual bool processMessage(int account, const QString &fromJid, const QString &body, const QString &subject);
virtual bool processOutgoingMessage(int account, const QString &fromJid, QString &body, const QString &type,
QString &subject)
{
Q_UNUSED(account);
Q_UNUSED(fromJid);
Q_UNUSED(body);
Q_UNUSED(type);
Q_UNUSED(subject);
return false;
};
virtual void logout(int account) { Q_UNUSED(account); };
virtual void applyOptions() {};
virtual void restoreOptions() {};
virtual QString pluginInfo();

private slots:
void stopGame();
void myTurn(int space);
void theirTurn(int space);
void gameOver(TicTacGameBoard::State state);

private:
void startGame(QString jid, int size, bool meFirst, int account);

QWidget *optionsWid;
TicTacToe *game;
QString playingWith;
int account_;
bool enabled_;
StanzaSendingHost *stanzaSender_;
};

// Q_EXPORT_PLUGIN(NoughtsAndCrossesPlugin);

NoughtsAndCrossesPlugin::NoughtsAndCrossesPlugin()
{
game = NULL;
enabled_ = false;
stanzaSender_ = 0;
optionsWid = 0;
}

QString NoughtsAndCrossesPlugin::name() const { return "NoughtsAndCrosses Plugin"; }

QWidget *NoughtsAndCrossesPlugin::options()
{
if (!enabled_) {
return 0;
}
optionsWid = new QWidget();
QGridLayout *layout = new QGridLayout(optionsWid);
layout->addWidget(new QLabel(tr("Send command: noughtsandcrosses start")));
return optionsWid;
}

bool NoughtsAndCrossesPlugin::enable()
{
if (stanzaSender_) {
enabled_ = true;
}
return enabled_;
}

bool NoughtsAndCrossesPlugin::disable()
{
stopGame();
enabled_ = false;
return true;
}

void NoughtsAndCrossesPlugin::setStanzaSendingHost(StanzaSendingHost *host) { stanzaSender_ = host; }

bool NoughtsAndCrossesPlugin::processMessage(int account, const QString &fromJid, const QString &message,
const QString &subject)
{
// FIXME(mck)
QString fromDisplay = fromJid;

Q_UNUSED(subject);

if (!enabled_) {
return false;
}

QString reply;
qDebug("naughtsandcrosses message");
if (!message.startsWith("noughtsandcrosses", Qt::CaseInsensitive))
return false;
qDebug("message for us in noughtsandcrosses");
if (game && fromJid != playingWith) {
reply = QString("<message to=\"%1\" type=\"chat\"><body>already playing with %2, sorry</body></message>")
.arg(fromJid, playingWith);
stanzaSender_->sendStanza(account, reply);
return true;
}
QString command = QString(message).toLower();
command.remove(0, 18);
qDebug() << QString("noughtsandcrosses command string %1").arg(command);
if (command == QString("start")) {
if (game)
return true;
qWarning() << QString("Received message '%1', launching nac with %2").arg(message, fromDisplay);
QString reply;
reply = QString("<message to=\"%1\" type=\"chat\"><body>noughtsandcrosses starting</body></message>")
.arg(fromJid);
stanzaSender_->sendStanza(account, reply);
startGame(fromJid, 3, false, account);
} else if (command == QString("starting")) {
if (game)
return true;
qWarning() << QString("Received message '%1', launching nac with %2").arg(message, fromDisplay);
QString reply;
reply
= QString(
"<message to=\"%1\" type=\"chat\"><body>starting noughts and crosses, I go first :)</body></message>")
.arg(fromJid);
stanzaSender_->sendStanza(account, reply);
startGame(fromJid, 3, true, account);
} else if (!game) {
return true;
} else if (command.startsWith("move")) {
command.remove(0, 5);

int space = command.toInt();
qDebug() << QString("noughtsandcrosses move to space %1").arg(space);
theirTurn(space);
}
return true;
}

void NoughtsAndCrossesPlugin::startGame(QString jid, int size, bool meFirst, int account)
{
game = new TicTacToe(meFirst, size);
game->setWindowTitle(QString("Noughts and Crosses with %1").arg(jid));
playingWith = jid;
game->show();
account_ = account;
connect(game, &TicaTacToe::closing, this, &NoughtsAndCrossesPlugin::stopGame);
connect(game, &TicaTacToe::myMove, this, &NoughtsAndCrossesPlugin::myTurn);
connect(game, &TicaTacToe::gameOverSignal, this, &NoughtsAndCrossesPlugin::gameOver);
}

void NoughtsAndCrossesPlugin::stopGame()
{
delete game;
game = NULL;
}

void NoughtsAndCrossesPlugin::gameOver(TicTacGameBoard::State state)
{
QString reply;
QString winner;
switch (state) {
case TicTacGameBoard::HumanWon:
winner = "I";
break;
case TicTacGameBoard::ComputerWon:
winner = "You";
break;
case TicTacGameBoard::NobodyWon:
winner = "It was a draw, no-one";
break;
default:
winner = "ERROR!!!";
}
reply = QString("<message to=\"%1\" type=\"chat\"><body>%2 won. Good game.</body></message>")
.arg(playingWith, winner);
stanzaSender_->sendStanza(account_, reply);
}

void NoughtsAndCrossesPlugin::myTurn(int space)
{
qDebug() << QString("my turn: %1").arg(space);
if (!game)
return;
QString reply;
reply = QString("<message to=\"%1\" type=\"chat\"><body>noughtsandcrosses move %2</body></message>")
.arg(playingWith, space);
stanzaSender_->sendStanza(account_, reply);
}

void NoughtsAndCrossesPlugin::theirTurn(int space)
{
qDebug() << QString("their turn: %1").arg(space);
if (!game)
return;
game->theirMove(space);
}

bool NoughtsAndCrossesPlugin::processEvent(int acc, QDomElement &e)
{
Q_UNUSED(acc);
Q_UNUSED(e);
return false;
}

QString NoughtsAndCrossesPlugin::pluginInfo() { return tr("Psi plugin to play noughts and crosses"); }

#include "noughtsandcrossesplugin.moc"
26 changes: 26 additions & 0 deletions dev/noughtsandcrossesplugin/psiplugin.json
@@ -0,0 +1,26 @@
{
"name": "Noughts And Crosses",
"name:ru": "Крестики-нолики",
"shortname": "noughtsandcrosses",
"version": "0.2.0",
"priority": 2,
"icon": "base64:iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXBIWXMAAACAAAAAgAHPVpo3AAAA
GXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAAw9JREFUOI2dk01sVGUYhZ/73VuH
FvpjqT8tuBCjWIQo9Wc1CTHEn2ACKhpZSJS0FhkXogtlgZooUUmU1kDjwEwmqdVajUpYiFRNibGB
qFQFax2kdAZKKenM9M50yp3rne/e10XVGEJi4rM8yTl5c3JebNtxRpJZLSLa87Q+NnRBd3d3+yKi
S4W8djIZLSI6Ho/72WxRl0qeFhE9O+tq2551VU3NPKuzK2V+e2TSfGH7sGkXMD3PU5meqDnTlzCd
Q/vN87t3miKixiccszXyo2nbF82nn/3JnLZ9CxHxXNeTlvCAdHYlRUQkFotJ+sUt8jfpbRGJxWIi
IvLVwLgsbRmQ479kRES0AujoOknkqcUc/X6a30cLAJiWhZe30a4LQQCA7wd89Ol5Iq3X8F7vWQCM
9Jmcd/S7fMWGR5cwlbnIwf5xtDvIxofWkd//IUEQcOUDD/P+F19ybdPdhEKV3LO6ib5P0ixqVL4R
jUa1YRgm/2JwcJBwOMx/aUqpwPDLZe/CrlcrgupaglyGqzdF6DnYz8nULTzy4A1UL6hgbyLJ8puG
aW9v5xJ8a+bnH4yqxkXUbdxMcXIC54N9GHXXsePlO2l7ZgjDgHc776Cvb+RS819XVNcSFOy5uOks
Rk0dANmci8hcf9mce1kzgFWzdJlkTwwxHt1FhQgNmyL43T28+fYwb72+AqUMXtlxglub9WUDjEQi
ocvl8v8q0TTNwFq7bkPw9eGc+dj6xYylCpw6/QdKKR5fcx/5Ax/ji1B//1qUUrS1tZHvP4CTPs38
m5dTu+peUQvrKzlzdoZ3un7lpdd+4/aVCwDI7H6Dhie30Ni+lam9HQAUDvfj2zmaNj+PlxymePwY
CqD1ievp7s2wKlxPw8IqAAIBq7IKZVmgFACSHsVadhsAVzSvwE+NGpbjlHlu2wif9bbQsWeMzw/N
TXT+yruY3LMTMxRi3o3NEEBo9Rqm93VgnEtRPPINV23dLkxl3NK5Ceefdx5JFnU8HvdFRJc9T3ul
khYRHYvFfBHRulzWM2OntO/7WkTcPwGP+7S0UpsyDwAAAABJRU5ErkJggg==
",
"description": "Psi plugin to play noughts and crosses.",
"vendor": "Kevin Smith"
}

0 comments on commit 16e3b93

Please sign in to comment.