Skip to content

Commit

Permalink
Communication object to receive chat messages
Browse files Browse the repository at this point in the history
  • Loading branch information
xxxcucus committed Jul 31, 2024
1 parent 6653337 commit a0d0b55
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 38 deletions.
6 changes: 5 additions & 1 deletion c_plus_plus/multiround/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ set(MULTIROUND_HEADR
viewmodels/unsentmovesviewmodel.h
viewmodels/deactivateuserviewmodel.h
viewmodels/getopponentemovesviewmodel.h
viewmodels/chatmessagerequestviewmodel.h
viewmodels/sendchatmessageviewmodel.h
viewmodels/receivedchatmessageviewmodel.h
viewmodels/getchatmessagesviewmodel.h
communicationobjects/basiscommobj.h
communicationobjects/creategamecommobj.h
communicationobjects/refreshgamestatuscommobj.h
Expand All @@ -54,6 +56,7 @@ set(MULTIROUND_HEADR
communicationobjects/playerslistcommobj.h
communicationobjects/deactivateusercommobj.h
communicationobjects/sendchatmessagecommobj.h
communicationobjects/receivechatmessagescommobj.h
communicationtools.h
gameinfo.h)

Expand All @@ -79,6 +82,7 @@ set(MULTIROUND_SRCS
communicationobjects/playerslistcommobj.cpp
communicationobjects/deactivateusercommobj.cpp
communicationobjects/sendchatmessagecommobj.cpp
communicationobjects/receivechatmessagescommobj.cpp
)


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include "receivechatmessagescommobj.h"

#include <QMessageBox>
#include <QJsonArray>
#include "multiplayerround.h"
#include "communicationtools.h"

bool ReceiveChatMessagesCommObj::makeRequest()
{
if (m_IsSinglePlayer) {
//qDebug() << "makeRequestBasis in single player modus";
return false;
}

if (m_GlobalData->m_UserData.m_UserName.isEmpty()) {
if (m_ParentWidget != nullptr) {
QMessageBox msgBox(m_ParentWidget);
msgBox.setText("No user logged in");
msgBox.exec();
}
return false;
}

m_RequestData = prepareViewModel().toJson();

makeRequestBasis(true);
return true;
}

GetChatMessagesViewModel ReceiveChatMessagesCommObj::prepareViewModel() {
GetChatMessagesViewModel chatMessagesViewModel;
chatMessagesViewModel.m_UserId = m_GlobalData->m_GameData.m_UserId;
chatMessagesViewModel.m_Username = m_GlobalData->m_UserData.m_UserName;

return chatMessagesViewModel;
}


void ReceiveChatMessagesCommObj::finishedRequest()
{
QJsonObject retJson;
if (!finishRequestHelper(retJson))
return;

processResponse(retJson);
}

void ReceiveChatMessagesCommObj::processResponse(const QJsonObject& retJson) {
QJsonValue messagesObject = retJson.value("messages");
QJsonArray messagesArray = messagesObject.toArray();

std::vector<ReceivedChatMessageViewModel> receivedMessages = std::vector<ReceivedChatMessageViewModel>();

for (int i = 0; i < messagesArray.size(); i++) {
QJsonValue messageValue = messagesArray.at(i);
QJsonObject messageObject = messageValue.toObject();
if (messageObject.contains("senderId") && messageObject.contains("senderName") && messageObject.contains("receiverId") &&
messageObject.contains("receiverName") && messageObject.contains("message") && messageObject.contains("createdAt"))
{
ReceivedChatMessageViewModel receivedMessage;
receivedMessage.m_SenderId = messageObject.value("senderId").toString().toLong();
receivedMessage.m_SenderName = messageObject.value("senderName").toString();
receivedMessage.m_ReceiverId = messageObject.value("receiverId").toString().toLong();
receivedMessage.m_ReceiverName = messageObject.value("receiverName").toString();
receivedMessage.m_Message = messageObject.value("message").toString();
receivedMessage.m_CreatedAt = CommunicationTools::parseDateFromString(messageObject.value("createdAt").toString());
}
}
}


bool ReceiveChatMessagesCommObj::validateReply(const QJsonObject& reply) {
if (!(reply.contains("messages"))) {
//qDebug() << "error 1";
return false;
}

QJsonValue messagesObject = reply.value("messages");
if (!messagesObject.isArray()) {
//qDebug() << "error 2";
return false;
}

QJsonArray messagesArray = messagesObject.toArray();
if (messagesArray.size() > 100) {
//qDebug() << "error 3 " << movesArray.size();
return false;
}

for (int i = 0; i < messagesArray.size(); i++) {
QJsonValue messageValue = messagesArray.at(i);
if (!messageValue.isObject()) {
//qDebug() << "error 4 " << i;
return false;
}

QJsonObject messageObject = messageValue.toObject();
if (!(messageObject.contains("senderId") && messageObject.contains("senderName")
&& messageObject.contains("receiverId") && messageObject.contains("receiverName")
&& messageObject.contains("message") && messageObject.contains("createdAt")))
return false;

if (!(checkLong(messageObject.value("senderId").toString())))
return false;

if (!(checkLong(messageObject.value("receiverId").toString())))
return false;

//TODO: other checks
}

return true;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#ifndef __RECEIVE_CHAT_MESSAGES__
#define __RECEIVE_CHAT_MESSAGES__

#if defined MAKE_MULTIPLAYERROUND_LIB
#define MULTIPLAYER_EXPORT Q_DECL_EXPORT
#else
#define MULTIPLAYER_EXPORT Q_DECL_IMPORT
#endif


#include "basiscommobj.h"
#include "viewmodels/getchatmessagesviewmodel.h"
#include "viewmodels/receivedchatmessageviewmodel.h"
#include <vector>

class MultiplayerRound;

class MULTIPLAYER_EXPORT ReceiveChatMessagesCommObj : public BasisCommObj {
Q_OBJECT

public:
ReceiveChatMessagesCommObj(const QString& requestPath, const QString& actionName, QWidget* parentWidget, QNetworkAccessManager* networkManager, QSettings* settings, bool isSinglePlayer, GlobalData* globalData, MultiplayerRound* mrd):
BasisCommObj(requestPath, actionName, parentWidget, networkManager, settings, isSinglePlayer, globalData), m_MultiRound(mrd) {}

bool makeRequest();
bool validateReply(const QJsonObject& retJson) override;

protected:
ReceiveChatMessagesCommObj() {}

public slots:
void finishedRequest() override;

signals:
void chatMessagesReceived(const std::vector<ReceivedChatMessageViewModel>& messages);

private:
GetChatMessagesViewModel prepareViewModel();
void processResponse(const QJsonObject& retJson);

protected:
MultiplayerRound* m_MultiRound;

};

#endif

//TODO: test

Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ void SendChatMessageCommObj::processResponse(const QJsonObject& retJson) {
//TODO:
}

ChatMessageRequestViewModel SendChatMessageCommObj::prepareViewModel(long int receiverId, const QString& message) {
SendChatMessageViewModel SendChatMessageCommObj::prepareViewModel(long int receiverId, const QString& message) {
QString shortenedMessage = message.left(m_MaxMessageLength);

if (shortenedMessage.size() != message.size()) {
qDebug() << "Message " << message << " was cut to ";
qDebug() << shortenedMessage;
}

ChatMessageRequestViewModel viewModel;
SendChatMessageViewModel viewModel;
viewModel.m_UserId = m_GlobalData->m_UserData.m_UserId;
viewModel.m_Username = m_GlobalData->m_UserData.m_UserName;
viewModel.m_ReceiverId = receiverId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include "basiscommobj.h"
#include "viewmodels/basisrequestviewmodel.h"
#include "viewmodels/chatmessagerequestviewmodel.h"
#include "viewmodels/sendchatmessageviewmodel.h"


class MULTIPLAYER_EXPORT SendChatMessageCommObj : public BasisCommObj {
Expand All @@ -33,7 +33,7 @@ public slots:

private:
void processResponse(const QJsonObject& retJson);
ChatMessageRequestViewModel prepareViewModel(long int receiverId, const QString& message);
SendChatMessageViewModel prepareViewModel(long int receiverId, const QString& message);
const int m_MaxMessageLength = 128;
};
#endif
Expand Down
34 changes: 34 additions & 0 deletions c_plus_plus/multiround/communicationtools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,37 @@ void CommunicationTools::treatCommunicationError(const QString& actionName, QNet
msgBox.setText("Error when " + actionName + " " + reply->errorString() + "\n" + registrationReplyQString);
msgBox.exec();
}

/**
* Parse QDateTime from format dd mm yyyy hh:mm:ss
*/
QDateTime CommunicationTools::parseDateFromString(const QString& dateString) {
QStringList dateElements = dateString.split(" ", Qt::SkipEmptyParts);
int day = 0;
int month = 0;
int year = 0;
if (dateElements.size() > 0)
day = dateElements[0].toInt();
if (dateElements.size() > 1)
month = dateElements[1].toInt();
if (dateElements.size() > 2)
year = dateElements[2].toInt();
QString timeString;
int hour = 0;
int minute = 0;
int second = 0;
if (dateElements.size() > 3) {
timeString = dateElements[3];
QStringList timeElements = timeString.split(":", Qt::SkipEmptyParts);
if (timeElements.size() > 0)
hour = timeElements[0].toInt();
if (timeElements.size() > 1)
minute = timeElements[1].toInt();
if (timeElements.size() > 2)
second = timeElements[2].toInt();
}

QDate retDate(year, month, day);
QTime retTime(hour, minute, second);
return QDateTime(retDate, retTime);
}
2 changes: 1 addition & 1 deletion c_plus_plus/multiround/communicationtools.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CommunicationTools {
static QNetworkReply* buildPostRequestWithAuth(const QString& routePath, const QString& serverPath, const QJsonObject& jsonObject, const QByteArray& authToken, QNetworkAccessManager* networkManager);
static QJsonObject objectFromString(const QString& in);
static void treatCommunicationError(const QString& actionName, QNetworkReply* reply, QWidget* parentWidget);

static QDateTime parseDateFromString(const QString& dateString);
};

#endif
15 changes: 15 additions & 0 deletions c_plus_plus/multiround/viewmodels/getchatmessagesviewmodel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef __GET_CHAT_MESSAGES_VIEWMODEL__
#define __GET_CHAT_MESSAGES_VIEWMODEL__

#include <QJsonObject>
#include "basisrequestviewmodel.h"

struct GetChatMessagesViewModel: BasisRequestViewModel {
QJsonObject toJson() {
QJsonObject retVal = BasisRequestViewModel::toJson();
return retVal;
}
};

#endif

16 changes: 16 additions & 0 deletions c_plus_plus/multiround/viewmodels/receivedchatmessageviewmodel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef __RECEIVED_CHAT_MESSAGE_VIEW_MODEL__
#define __RECEIVED_CHAT_MESSAGE_VIEW_MODEL__

#include <QString>
#include <QDateTime>

struct ReceivedChatMessageViewModel {
QString m_SenderName;
long int m_SenderId;
QString m_ReceiverName;
long int m_ReceiverId;
QString m_Message;
QDateTime m_CreatedAt;
};

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <QString>
#include <QJsonObject>

struct ChatMessageRequestViewModel: public BasisRequestViewModel {
struct SendChatMessageViewModel: public BasisRequestViewModel {
long int m_ReceiverId = 0;
QString m_Message;

Expand Down
33 changes: 2 additions & 31 deletions c_plus_plus/multiround/viewmodels/userwithlastloginviewmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <QString>
#include <QJsonObject>
#include "communicationtools.h"

struct UserWithLastLoginViewModel {
QString m_UserName;
Expand All @@ -11,7 +12,7 @@ struct UserWithLastLoginViewModel {
UserWithLastLoginViewModel(const QJsonObject& jsonObject) {
m_UserName = jsonObject.value("username").toString();
QString lastLoginString = jsonObject.value("lastLogin").toString();
m_LastLogin = parseDateFromString(lastLoginString);
m_LastLogin = CommunicationTools::parseDateFromString(lastLoginString);
}

explicit UserWithLastLoginViewModel(const QString& username) {
Expand All @@ -25,36 +26,6 @@ struct UserWithLastLoginViewModel {
}

private:
QDateTime parseDateFromString(const QString& dateString) {
QStringList dateElements = dateString.split(" ", Qt::SkipEmptyParts);
int day = 0;
int month = 0;
int year = 0;
if (dateElements.size() > 0)
day = dateElements[0].toInt();
if (dateElements.size() > 1)
month = dateElements[1].toInt();
if (dateElements.size() > 2)
year = dateElements[2].toInt();
QString timeString;
int hour = 0;
int minute = 0;
int second = 0;
if (dateElements.size() > 3) {
timeString = dateElements[3];
QStringList timeElements = timeString.split(":", Qt::SkipEmptyParts);
if (timeElements.size() > 0)
hour = timeElements[0].toInt();
if (timeElements.size() > 1)
minute = timeElements[1].toInt();
if (timeElements.size() > 2)
second = timeElements[2].toInt();
}

QDate retDate(year, month, day);
QTime retTime(hour, minute, second);
return QDateTime(retDate, retTime);
}

friend bool operator<(const UserWithLastLoginViewModel& user1, const UserWithLastLoginViewModel& user2) {
if (user1.m_UserName != user2.m_UserName) {
Expand Down

0 comments on commit a0d0b55

Please sign in to comment.