1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Continue development

This commit is contained in:
nordsoft 2022-10-21 20:29:27 +04:00
parent b58f70ac5e
commit e54c9af24e
3 changed files with 201 additions and 25 deletions

View File

@ -1,7 +1,8 @@
#include "lobby_moc.h"
#include "ui_lobby_moc.h"
#include "../lib/GameConstants.h"
SocketTest::SocketTest(QObject *parent) :
SocketLobby::SocketLobby(QObject *parent) :
QObject(parent)
{
socket = new QTcpSocket(this);
@ -11,43 +12,52 @@ SocketTest::SocketTest(QObject *parent) :
connect(socket, SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWritten(qint64)));
}
void SocketTest::Test()
void SocketLobby::connectServer(const QString & host, int port, const QString & usr)
{
qDebug() << "Connecting,..";
emit text("Connecting to 127.0.0.1:5002...");
const int connectionTimeout = 1000;
username = usr;
socket->connectToHost("127.0.0.1", 5002);
emit text("Connecting to " + host + ":" + QString::number(port));
if(!socket->waitForDisconnected(1000) && !isConnected)
socket->connectToHost(host, port);
if(!socket->waitForDisconnected(connectionTimeout) && !isConnected)
{
emit text("Error: " + socket->errorString());
}
}
void SocketTest::send(const QString & msg)
void SocketLobby::disconnectServer()
{
socket->disconnectFromHost();
}
void SocketLobby::send(const QString & msg)
{
socket->write(qPrintable(msg));
}
void SocketTest::connected()
void SocketLobby::connected()
{
isConnected = true;
emit text("Connected!");
const QString greetingConst = ProtocolStrings[GREETING].arg(username) + ProtocolStrings[VERSION].arg(QString::fromStdString(GameConstants::VCMI_VERSION));
send(greetingConst);
}
void SocketTest::disconnected()
void SocketLobby::disconnected()
{
isConnected = false;
emit text("Disconnected!");
}
void SocketTest::bytesWritten(qint64 bytes)
void SocketLobby::bytesWritten(qint64 bytes)
{
qDebug() << "We wrote: " << bytes;
}
void SocketTest::readyRead()
void SocketLobby::readyRead()
{
qDebug() << "Reading...";
emit text(socket->readAll());
@ -55,15 +65,14 @@ void SocketTest::readyRead()
Lobby::Lobby(QWidget *parent) :
QWidget(parent),
ui(new Ui::Lobby)
{
ui->setupUi(this);
connect(&mTest, SIGNAL(text(QString)), this, SLOT(text(QString)));
mTest.Test();
connect(&socketLobby, SIGNAL(text(QString)), this, SLOT(text(QString)));
}
Lobby::~Lobby()
@ -71,10 +80,10 @@ Lobby::~Lobby()
delete ui;
}
void Lobby::on_lineEdit_returnPressed()
void Lobby::on_messageEdit_returnPressed()
{
mTest.send(ui->lineEdit->text());
ui->lineEdit->clear();
socketLobby.send(ProtocolStrings[MESSAGE].arg(ui->messageEdit->text()));
ui->messageEdit->clear();
}
void Lobby::text(QString txt)
@ -84,3 +93,15 @@ void Lobby::text(QString txt)
curs.insertText(txt + "\n");
}
void Lobby::on_connectButton_toggled(bool checked)
{
if(checked)
{
socketLobby.connectServer(ui->hostEdit->text(), ui->portEdit->text().toInt(), ui->userEdit->text());
}
else
{
socketLobby.disconnectServer();
}
}

View File

@ -5,12 +5,26 @@
#include <QTcpSocket>
#include <QAbstractSocket>
class SocketTest : public QObject
enum ProtocolConsts
{
GREETING, USERNAME, MESSAGE, VERSION
};
const QMap<ProtocolConsts, QString> ProtocolStrings
{
{GREETING, "<GREETINGS>%1"},
{USERNAME, "<USER>%1"},
{MESSAGE, "<MSG>%1"},
{VERSION, "<VER>%1"}
};
class SocketLobby : public QObject
{
Q_OBJECT
public:
explicit SocketTest(QObject *parent = 0);
void Test();
explicit SocketLobby(QObject *parent = 0);
void connectServer(const QString & host, int port, const QString & username);
void disconnectServer();
void send(const QString &);
@ -28,6 +42,7 @@ public slots:
private:
QTcpSocket *socket;
bool isConnected = false;
QString username;
};
@ -44,13 +59,15 @@ public:
~Lobby();
private slots:
void on_lineEdit_returnPressed();
void on_messageEdit_returnPressed();
void text(QString);
void on_connectButton_toggled(bool checked);
private:
Ui::Lobby *ui;
SocketTest mTest;
SocketLobby socketLobby;
};
#endif // LOBBY_MOC_H

View File

@ -14,15 +14,153 @@
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="4">
<widget class="QLabel" name="label_4">
<property name="text">
<string>User</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Port</string>
</property>
</widget>
</item>
<item row="0" column="6">
<widget class="QPushButton" name="connectButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Connect</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Address</string>
</property>
</widget>
</item>
<item row="0" column="3">
<widget class="QLineEdit" name="portEdit">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="inputMethodHints">
<set>Qt::ImhDigitsOnly</set>
</property>
<property name="text">
<string>5002</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="hostEdit">
<property name="text">
<string>127.0.0.1</string>
</property>
</widget>
</item>
<item row="0" column="5">
<widget class="QLineEdit" name="userEdit"/>
</item>
<item row="2" column="4" rowspan="2" colspan="3">
<widget class="QStackedWidget" name="stackedWidget">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="sessionsPage">
<layout class="QGridLayout" name="gridLayout_2">
<item row="2" column="0">
<widget class="QPushButton" name="newButton">
<property name="text">
<string>New game</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="passwordInput"/>
</item>
<item row="2" column="1">
<widget class="QPushButton" name="joinButton">
<property name="text">
<string>Join game</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Password</string>
</property>
</widget>
</item>
<item row="0" column="0" colspan="2">
<widget class="QTableWidget" name="sessionsTable">
<column>
<property name="text">
<string>Session</string>
</property>
</column>
<column>
<property name="text">
<string>Players</string>
</property>
</column>
<column>
<property name="text">
<string>Private</string>
</property>
</column>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="roomPage">
<layout class="QGridLayout" name="gridLayout_3">
<item row="1" column="0">
<widget class="QPushButton" name="buttonLeave">
<property name="text">
<string>Leave</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QPushButton" name="buttonReady">
<property name="text">
<string>Ready</string>
</property>
</widget>
</item>
<item row="0" column="0" colspan="2">
<widget class="QPlainTextEdit" name="roomChat"/>
</item>
</layout>
</widget>
</widget>
</item>
<item row="2" column="0" colspan="4">
<widget class="QPlainTextEdit" name="chat">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLineEdit" name="lineEdit"/>
<item row="3" column="0" colspan="4">
<widget class="QLineEdit" name="messageEdit"/>
</item>
</layout>
</widget>