//must be called after setWindow void GuiGameController::setNetworkInterface(){ if( net == nullptr){ net = new NetworkInterface(this); qDebug() << "connecting network signals"; //gameController -> network connect( this, SIGNAL(gameIsOver()), net, SLOT( tellMeGameIsOver()) ); //network -> GUI connect( net, SIGNAL(opponentDisconnectedEarly()), gui, SIGNAL(opponentDisconnected()) ); connect( net, SIGNAL(connectionReestablished()), gui, SIGNAL(opponentReconnected()) ); connect( net, SIGNAL( challengeReceived(QVariant, QVariant) ), gui, SIGNAL(challengeReceivedFromNetwork(QVariant, QVariant)), Qt::QueuedConnection ); connect( net, SIGNAL( challengeResponseReceived(bool)), this, SLOT(challengeResponseReceivedFromNetwork(bool)), Qt::QueuedConnection ); connect( net, SIGNAL( networkTurnReceived(int,int,int,int) ), this, SLOT(networkTurnReceivedFromNetwork(int,int,int,int)), Qt::QueuedConnection ); connect( this, SIGNAL( challengeAccepted()), gui, SIGNAL(challengeWasAccepted() ), Qt::QueuedConnection ); connect( this, SIGNAL( challengeDeclined()), gui, SIGNAL(challengeWasDeclined()) , Qt::QueuedConnection ); connect( gui, SIGNAL( challengeTimedOutAsIfDeclined()), net, SLOT(forceNotBusy()), Qt::QueuedConnection ); connect( net, SIGNAL( playerJoinedNetwork(QVariant, QVariant, int, bool )), gui, SIGNAL(playerEnteredLobby(QVariant, QVariant, int, bool )), Qt::QueuedConnection ); connect( net, SIGNAL( playerLeftNetwork(int)), gui, SIGNAL(playerLeftLobby(int))); connect( net, SIGNAL( networkPlayerBecameBusy(QVariant)), gui, SIGNAL(networkPlayerBecameBusy(QVariant)), Qt::QueuedConnection ); connect( net, SIGNAL( networkPlayerNoLongerBusy(QVariant)), gui, SIGNAL(networkPlayerNoLongerBusy(QVariant)), Qt::QueuedConnection ); //GUI -> network connect(gui, SIGNAL(sendThisChallenge(QVariant)), net, SLOT(sendChallenge(QVariant)), Qt::QueuedConnection ); connect(gui, SIGNAL(sendThisChallengeResponse(bool)), this, SLOT(forwardChallengeResponse(bool)), Qt::QueuedConnection ); }else{
void Server::textRead(){ QTcpSocket *source = dynamic_cast<QTcpSocket*>(sender()); QByteArray data = source->readAll(); QByteArray namePrefix = "/Name::"; QByteArray challengePrefix = "/Challenge::"; QByteArray challengeAcceptedPrefix = "/ChallengeAccepted::"; QByteArray settingsPrefix = "/Settings::"; QByteArray startPrefix = "/Start::"; QByteArray turnPrefix = "/Turn::"; QByteArray locationPrefix = "/Location::"; QByteArray multiplayerMatchPrefix = "/MultiplayerMatch::"; if (data.startsWith(namePrefix)){ QString tempNick(data); QStringRef name(&tempNick, namePrefix.length(), tempNick.length() - namePrefix.length()); QString validName = isAvailable(name.toString()); nickContainer->append(validName); if (name != validName){ source->write("/ValidName::" + validName.toLatin1()); } sendClientList(); } else if (data.startsWith(challengePrefix)){ QString tempNick(data); QStringRef name(&tempNick, challengePrefix.length(), tempNick.length() - challengePrefix.length()); sendChallenge(source, name); } else if (data.startsWith(challengeAcceptedPrefix)){ QString tempNick(data); QStringRef name(&tempNick, challengeAcceptedPrefix.length(), tempNick.length() - challengeAcceptedPrefix.length()); sendChallengeAccepted(name); } else if (data.startsWith(settingsPrefix)){ QString tempString(data); QStringRef values(&tempString, settingsPrefix.length(), tempString.length() - settingsPrefix.length()); sendSettingsData(values.toString()); } else if (data.startsWith(startPrefix)){ QString tempString(data); QStringRef values(&tempString, startPrefix.length(), tempString.length() - startPrefix.length()); sendStartMessage(values); } else if (data.startsWith(turnPrefix)){ QString tempString(data); QStringRef name(&tempString, turnPrefix.length(), tempString.length() - turnPrefix.length()); sendTurnChangeMessage(name); } else if (data.startsWith(locationPrefix)){ QString tempString(data); QStringRef values(&tempString, locationPrefix.length(), tempString.length() - locationPrefix.length()); sendLocationMessage(values); } else if (data.startsWith(multiplayerMatchPrefix)){ QString tempString(data); sendPrivateMessage(tempString, multiplayerMatchPrefix); } else { QTime time = QTime::currentTime(); QString timeString = time.toString() + " "; for (int i = 0; i < clientContainer->size(); ++i){ clientContainer->at(i)->write(timeString.toLatin1() + data); } } }
int Logic::logIn() { std::string msg = security.receive(); if(msg.at(0) != 0) std::runtime_error ("Expected username"); std::string username(msg.data()+1,msg.size()-1); if(model->findByUsername(username)) { std::string challenge = sendChallenge(); std::string msg = security.receive(); if(msg.at(0) != 2) std::runtime_error ("Expected response"); std::string hash = model->getPassword(username) + challenge; std::string md5string = md5(hash); std::string response(msg.data()+1,msg.size()-1); if(response.compare(md5string)==0) { if(model->isActive(username)) { state = LOGGED; model->setUserName(username); std::string msg; msg.push_back(3); security.send(msg, 1); } else { std::cout<<"User account has not been activated."<<std::endl; std::string msg; msg.push_back(4); msg.push_back(2); security.send(msg, 2); return -1; } } else { std::cout<<"Wrong password"<<std::endl; std::string msg; msg.push_back(4); msg.push_back(1); security.send(msg, 2); return -1; } } else { std::cout<<"User: "******" doesn't exist"<<std::endl; std::string msg; msg.push_back(4); msg.push_back(0); security.send(msg, 2); return -1; } }