-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Communication object to receive chat messages
- Loading branch information
Showing
11 changed files
with
241 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
c_plus_plus/multiround/communicationobjects/receivechatmessagescommobj.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
49 changes: 49 additions & 0 deletions
49
c_plus_plus/multiround/communicationobjects/receivechatmessagescommobj.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
c_plus_plus/multiround/viewmodels/getchatmessagesviewmodel.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
c_plus_plus/multiround/viewmodels/receivedchatmessageviewmodel.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters