Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions RetroChessPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ RetroChessPlugin::RetroChessPlugin()
mRetroChessNotify = new RetroChessNotify;
}

RetroChessPlugin::~RetroChessPlugin()
{
delete mRetroChessToasterNotify;
delete mRetroChessNotify;
delete mIcon;
}

void RetroChessPlugin::setInterfaces(RsPlugInInterfaces &interfaces)
{
mPeers = interfaces.mPeers;
Expand Down Expand Up @@ -150,18 +157,21 @@ ChatWidgetHolder *RetroChessPlugin::qt_get_chat_widget_holder(ChatWidget *chatWi
return NULL;
}

#include <mutex>

static std::once_flag mRetroChessInitOnce;

p3Service *RetroChessPlugin::p3_service() const
{
if(mRetroChess == NULL)
{
std::call_once(mRetroChessInitOnce, [this]() {
// Create the service
rsRetroChess = mRetroChess = new p3RetroChess(mPlugInHandler, mRetroChessNotify);

// Register it for GXS Tunnels immediately if the interface is available
if (mGxsTunnels) {
mRetroChess->connectToGxsTunnelService(mGxsTunnels);
}
}
});
return mRetroChess;
}

Expand Down
2 changes: 1 addition & 1 deletion RetroChessPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class RetroChessPlugin: public RsPlugin
{
public:
RetroChessPlugin() ;
virtual ~RetroChessPlugin() {}
virtual ~RetroChessPlugin();

virtual p3Service *p3_service() const ;
virtual uint16_t rs_service_id() const
Expand Down
4 changes: 3 additions & 1 deletion gui/NEMainpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,8 @@ const ChatLobbyId OFFICIAL_RETROCHESS_LOBBY_ID = 0x0174BD3E49231CDAULL;

void NEMainpage::autoJoinOfficialLobby()
{
if (!rsChats || !rsIdentity) return;

std::list<ChatLobbyId> subscribedLobbies;
rsChats->getChatLobbyList(subscribedLobbies);
if (std::find(subscribedLobbies.begin(), subscribedLobbies.end(),
Expand Down Expand Up @@ -786,7 +788,7 @@ void NEMainpage::chessRematchPeer(const RsPeerId &peer_id, int remoteColor)
}
const bool alreadyRequested = window->m_rematchRequested;
if (!alreadyRequested && QMessageBox::question(
window, tr("Rematch"), tr("Your opponent requests a rematch. Accept?"))
this, tr("Rematch"), tr("Your opponent requests a rematch. Accept?"))
!= QMessageBox::Yes) {
reply.insert("type", "game_action");
reply.insert("action", "rematch_decline");
Expand Down
13 changes: 8 additions & 5 deletions gui/RetroChessChatWidgetHolder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ RetroChessChatWidgetHolder::RetroChessChatWidgetHolder(ChatWidget *chatWidget, R
{
QIcon icon(cropTransparentPadding(QPixmap(IMAGE_RetroChess)));

playChessButton = new QToolButton ;
playChessButton = new QToolButton(mChatWidget);
playChessButton->setIcon(icon) ;
playChessButton->setToolTip(tr("Invite to Chess"));
playChessButton->setAutoRaise(true) ;
Expand Down Expand Up @@ -129,7 +129,7 @@ RetroChessChatWidgetHolder::~RetroChessChatWidgetHolder()
void RetroChessChatWidgetHolder::clearInviteButtons()
{
for (RSButtonOnText *button : buttonMapTakeChess)
if (button) button->clear();
if (button) button->deleteLater();
buttonMapTakeChess.clear();
}

Expand Down Expand Up @@ -166,8 +166,9 @@ void RetroChessChatWidgetHolder::inviteClearedGxs(const RsGxsId &gxs_id)

void RetroChessChatWidgetHolder::chessnotify(RsPeerId from_peer_id)
{
if (!mChatWidget->getChatId().isPeerId()) return;
RsPeerId peer_id = mChatWidget->getChatId().toPeerId();//TODO support GXSID
//if (peer_id!=from_peer_id)return;//invite from another chat
if (peer_id != from_peer_id) return;//invite from another chat
if (rsRetroChess->hasInviteFrom(peer_id))
{
if (mChatWidget)
Expand Down Expand Up @@ -356,9 +357,11 @@ void RetroChessChatWidgetHolder::chessPressed()
rsRetroChess->sendInvite(peer_id);

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);
} else {
return;
}
mChatWidget->addChatMsg(true, tr("Chess Status"), QDateTime::currentDateTime(), QDateTime::currentDateTime()
, tr("You're now inviting %1 to play Chess").arg(peerName), ChatWidget::MSGTYPE_SYSTEM);
}


Expand Down
2 changes: 1 addition & 1 deletion gui/RetroChessSessionService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void RetroChessSessionService::closeAll()
const auto sessions = m_sessions;
m_sessions.clear();
for (const Session &session : sessions)
if (session.window) session.window->deleteLater();
if (session.window) delete session.window;
}

bool RetroChessSessionService::routeMove(
Expand Down
73 changes: 38 additions & 35 deletions gui/chess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ RetroChessWindow::RetroChessWindow(const RsGxsId &gxsId, int player, QWidget *pa

// Use non-blocking lookup with fallback for unknown identities
RsIdentityDetails d1, d2;
if (rsIdentity->getIdDetails(mOwnGxsId, d1)) {
if (rsIdentity && rsIdentity->getIdDetails(mOwnGxsId, d1)) {
p1name = d1.mNickname;
} else {
p1name = mOwnGxsId.isNull() ? "Local GXS identity" : mOwnGxsId.toStdString().substr(0, 8) + "...";
}
if (rsIdentity->getIdDetails(gxsId, d2)) {
if (rsIdentity && rsIdentity->getIdDetails(gxsId, d2)) {
p2name = d2.mNickname;
} else {
p2name = gxsId.toStdString().substr(0, 8) + "...";
Expand All @@ -134,12 +134,12 @@ RetroChessWindow::RetroChessWindow(const RsGxsId &gxsId, int player, QWidget *pa
m_localplayer_turn = 1;

RsIdentityDetails d1, d2;
if (rsIdentity->getIdDetails(gxsId, d1)) {
if (rsIdentity && rsIdentity->getIdDetails(gxsId, d1)) {
p1name = d1.mNickname;
} else {
p1name = gxsId.toStdString().substr(0, 8) + "...";
}
if (rsIdentity->getIdDetails(mOwnGxsId, d2)) {
if (rsIdentity && rsIdentity->getIdDetails(mOwnGxsId, d2)) {
p2name = d2.mNickname;
} else {
p2name = mOwnGxsId.isNull() ? "Local GXS identity" : mOwnGxsId.toStdString().substr(0, 8) + "...";
Expand Down Expand Up @@ -212,7 +212,7 @@ RetroChessWindow::RetroChessWindow(std::string peerid, int player, QWidget *pare
QString player_str;
if (player ) // local player as black
{
p1id = rsPeers->getOwnId();
p1id = rsPeers ? rsPeers->getOwnId() : RsPeerId();
p2id = RsPeerId(peerid);
player_str = " (1)";

Expand All @@ -221,14 +221,14 @@ RetroChessWindow::RetroChessWindow(std::string peerid, int player, QWidget *pare
else // local player as white
{
p1id = RsPeerId(peerid);
p2id = rsPeers->getOwnId();
p2id = rsPeers ? rsPeers->getOwnId() : RsPeerId();
player_str = " (2)";

m_localplayer_turn = 1;
}

p1name = rsPeers->getPeerName(p1id);
p2name = rsPeers->getPeerName(p2id);
p1name = rsPeers ? rsPeers->getPeerName(p1id) : "";
p2name = rsPeers ? rsPeers->getPeerName(p2id) : "";

const std::string &localName = m_localplayer_turn == 0 ? p1name : p2name;
const std::string &opponentName = m_localplayer_turn == 0 ? p2name : p1name;
Expand Down Expand Up @@ -513,27 +513,29 @@ void RetroChessWindow::initAccessories()
// remote peers, while our own node avatar comes from getOwnAvatar().
QPixmap p1avatar;
QPixmap p2avatar;
const RsPeerId ownId = rsPeers->getOwnId();
auto loadPeerAvatar = [&ownId](const RsPeerId &id, QPixmap &avatar) {
if (id != ownId) {
AvatarDefs::getAvatarFromSslId(id, avatar);
return;
}

unsigned char *avatarData = nullptr;
int avatarSize = 0;
rsChats->getOwnNodeAvatarData(avatarData, avatarSize);
if (avatarData)
free(avatarData);
if (avatarSize > 0)
AvatarDefs::getOwnAvatar(avatar);
else
// Generate the familiar per-peer coloured fallback instead of
// RetroShare's static blue missing-avatar image.
AvatarDefs::getAvatarFromSslId(ownId, avatar);
};
loadPeerAvatar(p1id, p1avatar);
loadPeerAvatar(p2id, p2avatar);
if (rsPeers && rsChats) {
const RsPeerId ownId = rsPeers->getOwnId();
auto loadPeerAvatar = [&ownId](const RsPeerId &id, QPixmap &avatar) {
if (id != ownId) {
AvatarDefs::getAvatarFromSslId(id, avatar);
return;
}

unsigned char *avatarData = nullptr;
int avatarSize = 0;
rsChats->getOwnNodeAvatarData(avatarData, avatarSize);
if (avatarData)
free(avatarData);
if (avatarSize > 0)
AvatarDefs::getOwnAvatar(avatar);
else
// Generate the familiar per-peer coloured fallback instead of
// RetroShare's static blue missing-avatar image.
AvatarDefs::getAvatarFromSslId(ownId, avatar);
};
loadPeerAvatar(p1id, p1avatar);
loadPeerAvatar(p2id, p2avatar);
}

const QSize avatarSize(128, 128);
auto setPeerAvatar = [&avatarSize](QLabel *label, const QPixmap &avatar) {
Expand Down Expand Up @@ -825,6 +827,7 @@ void RetroChessWindow::activateBoardSquare(int square)

int RetroChessWindow::chooser(Tile *tile_p)
{
int flag = 0;
switch(tile_p->pieceName)
{
case 'P':
Expand Down Expand Up @@ -878,7 +881,7 @@ int RetroChessWindow::validatePawn(Tile *tile_p)

row=tile_p->row;
col=tile_p->col;
retVal=0;
int retVal=0;

//White Pawn
if(tile_p->pieceColor)
Expand Down Expand Up @@ -980,7 +983,7 @@ int RetroChessWindow::validateRook(Tile *tile_p)
{
int r,c;

retVal=0;
int retVal=0;

r=tile_p->row;
c=tile_p->col;
Expand Down Expand Up @@ -1075,7 +1078,7 @@ int RetroChessWindow::validateRook(Tile *tile_p)
int RetroChessWindow::validateHorse(Tile *tile_p)
{
int r,c;
retVal=0;
int retVal=0;

r=tile_p->row;
c=tile_p->col;
Expand Down Expand Up @@ -1160,7 +1163,7 @@ int RetroChessWindow::validateHorse(Tile *tile_p)
int RetroChessWindow::validateKing(Tile *tile_p)
{
int r,c;
retVal=0;
int retVal=0;

r=tile_p->row;
c=tile_p->col;
Expand Down Expand Up @@ -1257,7 +1260,7 @@ int RetroChessWindow::validateQueen(Tile *tile_p)
{
int r,c;

retVal=0;
int retVal=0;

r=tile_p->row;
c=tile_p->col;
Expand Down Expand Up @@ -1435,7 +1438,7 @@ int RetroChessWindow::validateQueen(Tile *tile_p)
int RetroChessWindow::validateBishop(Tile *tile_p)
{
int r,c;
retVal=0;
int retVal=0;

r=tile_p->row;
c=tile_p->col;
Expand Down
1 change: 0 additions & 1 deletion gui/chess.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ class RetroChessWindow : public QWidget
const QString &fen, uint32_t moveSequence,
QString *error = nullptr);

int flag,retVal;
int chooser(Tile *temp);
int validateBishop(Tile *temp);
int validateQueen(Tile *temp);
Expand Down
15 changes: 8 additions & 7 deletions services/p3RetroChess.cc
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ void p3RetroChess::raw_msg_peer(RsPeerId peerID, std::string msg)
RsRetroChessDataItem *pingPkt = new RsRetroChessDataItem();
pingPkt->PeerId(peerID);
pingPkt->m_msg = msg;
pingPkt->data_size = msg.size();
//pingPkt->mSeqNo = mCounter;
//pingPkt->mPingTS = convertTsTo64bits(ts);

Expand Down Expand Up @@ -264,7 +263,7 @@ void p3RetroChess::msg_all(std::string msg)

void p3RetroChess::ping_all()
{
//TODO ping all!
// Required override of RsRetroChess pure virtual — no-op.
}

void p3RetroChess::broadcast_paint(int x, int y)
Expand Down Expand Up @@ -319,7 +318,9 @@ bool p3RetroChess::recvItem(RsItem *item)
// handleData() only forwards the message string to the notifier and
// does not take ownership, so the item must not be kept: keeping it
// leaked one item per received message.
handleData(dynamic_cast<RsRetroChessDataItem*>(item));
if (RsRetroChessDataItem* chess_item = dynamic_cast<RsRetroChessDataItem*>(item)) {
handleData(chess_item);
}
break;
/*case RS_PKT_SUBTYPE_RetroChess_INVITE:
if (invites.find(item->PeerId()!=invites.end())){
Expand Down Expand Up @@ -984,12 +985,12 @@ void p3RetroChess::handleRawData(const RsGxsId& gxs_id,
}

// All messages are JSON
std::string msg((const char*)data, data_size);
#ifdef DEBUG_RetroChess
std::cout << "Chess::handleRawData: received from " << sender_id << ": " << msg << std::endl;
std::cout << "Chess::handleRawData: received from " << sender_id << ": "
<< std::string((const char*)data, data_size) << std::endl;
#endif

QJsonDocument jsondoc = QJsonDocument::fromJson(QByteArray::fromStdString(msg));
QJsonDocument jsondoc = QJsonDocument::fromJson(QByteArray((const char*)data, data_size));
QVariantMap map = jsondoc.toVariant().toMap();
QString type = map.value("type").toString();

Expand Down Expand Up @@ -1061,7 +1062,7 @@ void p3RetroChess::handleRawData(const RsGxsId& gxs_id,

} else {
// Chess move: format "col,row,count"
QStringList parts = QString::fromStdString(msg).split(",");
QStringList parts = QString::fromUtf8((const char*)data, data_size).split(",");
if (parts.size() == 3) {
int col = parts[0].toInt();
int row = parts[1].toInt();
Expand Down
7 changes: 3 additions & 4 deletions services/p3RetroChess.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,10 @@ class p3RetroChess: public RsPQIService, public RsRetroChess, public RsGxsTunnel
std::map<std::string, RsRetroChessGameSession> mGameSessions;
std::map<std::string, time_t> mLastSessionReconnect;

RsGxsTunnelService *mGxsTunnels;
RsMutex mRetroChessMtx;
RsServiceControl *mServiceControl;
RetroChessNotify *mNotify ;
RsGxsTunnelService *mGxsTunnels;

//RsPeerId mPeerID;

Expand All @@ -195,7 +197,4 @@ class p3RetroChess: public RsPQIService, public RsRetroChess, public RsGxsTunnel
static int pop_int_value(const std::string& s) ;


RsServiceControl *mServiceControl;
RetroChessNotify *mNotify ;

};
Loading