mirror of
https://github.com/vcmi/vcmi.git
synced 2025-04-21 12:06:49 +02:00
Continue development
This commit is contained in:
parent
b58f70ac5e
commit
e54c9af24e
@ -1,7 +1,8 @@
|
|||||||
#include "lobby_moc.h"
|
#include "lobby_moc.h"
|
||||||
#include "ui_lobby_moc.h"
|
#include "ui_lobby_moc.h"
|
||||||
|
#include "../lib/GameConstants.h"
|
||||||
|
|
||||||
SocketTest::SocketTest(QObject *parent) :
|
SocketLobby::SocketLobby(QObject *parent) :
|
||||||
QObject(parent)
|
QObject(parent)
|
||||||
{
|
{
|
||||||
socket = new QTcpSocket(this);
|
socket = new QTcpSocket(this);
|
||||||
@ -11,43 +12,52 @@ SocketTest::SocketTest(QObject *parent) :
|
|||||||
connect(socket, SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWritten(qint64)));
|
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,..";
|
const int connectionTimeout = 1000;
|
||||||
emit text("Connecting to 127.0.0.1:5002...");
|
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());
|
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));
|
socket->write(qPrintable(msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
void SocketTest::connected()
|
void SocketLobby::connected()
|
||||||
{
|
{
|
||||||
isConnected = true;
|
isConnected = true;
|
||||||
emit text("Connected!");
|
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;
|
isConnected = false;
|
||||||
emit text("Disconnected!");
|
emit text("Disconnected!");
|
||||||
}
|
}
|
||||||
|
|
||||||
void SocketTest::bytesWritten(qint64 bytes)
|
void SocketLobby::bytesWritten(qint64 bytes)
|
||||||
{
|
{
|
||||||
qDebug() << "We wrote: " << bytes;
|
qDebug() << "We wrote: " << bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SocketTest::readyRead()
|
void SocketLobby::readyRead()
|
||||||
{
|
{
|
||||||
qDebug() << "Reading...";
|
qDebug() << "Reading...";
|
||||||
emit text(socket->readAll());
|
emit text(socket->readAll());
|
||||||
@ -55,15 +65,14 @@ void SocketTest::readyRead()
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Lobby::Lobby(QWidget *parent) :
|
Lobby::Lobby(QWidget *parent) :
|
||||||
QWidget(parent),
|
QWidget(parent),
|
||||||
ui(new Ui::Lobby)
|
ui(new Ui::Lobby)
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
|
|
||||||
connect(&mTest, SIGNAL(text(QString)), this, SLOT(text(QString)));
|
connect(&socketLobby, SIGNAL(text(QString)), this, SLOT(text(QString)));
|
||||||
|
|
||||||
mTest.Test();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Lobby::~Lobby()
|
Lobby::~Lobby()
|
||||||
@ -71,10 +80,10 @@ Lobby::~Lobby()
|
|||||||
delete ui;
|
delete ui;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Lobby::on_lineEdit_returnPressed()
|
void Lobby::on_messageEdit_returnPressed()
|
||||||
{
|
{
|
||||||
mTest.send(ui->lineEdit->text());
|
socketLobby.send(ProtocolStrings[MESSAGE].arg(ui->messageEdit->text()));
|
||||||
ui->lineEdit->clear();
|
ui->messageEdit->clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Lobby::text(QString txt)
|
void Lobby::text(QString txt)
|
||||||
@ -84,3 +93,15 @@ void Lobby::text(QString txt)
|
|||||||
curs.insertText(txt + "\n");
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -5,12 +5,26 @@
|
|||||||
#include <QTcpSocket>
|
#include <QTcpSocket>
|
||||||
#include <QAbstractSocket>
|
#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
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit SocketTest(QObject *parent = 0);
|
explicit SocketLobby(QObject *parent = 0);
|
||||||
void Test();
|
void connectServer(const QString & host, int port, const QString & username);
|
||||||
|
void disconnectServer();
|
||||||
|
|
||||||
void send(const QString &);
|
void send(const QString &);
|
||||||
|
|
||||||
@ -28,6 +42,7 @@ public slots:
|
|||||||
private:
|
private:
|
||||||
QTcpSocket *socket;
|
QTcpSocket *socket;
|
||||||
bool isConnected = false;
|
bool isConnected = false;
|
||||||
|
QString username;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -44,13 +59,15 @@ public:
|
|||||||
~Lobby();
|
~Lobby();
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void on_lineEdit_returnPressed();
|
void on_messageEdit_returnPressed();
|
||||||
|
|
||||||
void text(QString);
|
void text(QString);
|
||||||
|
|
||||||
|
void on_connectButton_toggled(bool checked);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Ui::Lobby *ui;
|
Ui::Lobby *ui;
|
||||||
SocketTest mTest;
|
SocketLobby socketLobby;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // LOBBY_MOC_H
|
#endif // LOBBY_MOC_H
|
||||||
|
@ -14,15 +14,153 @@
|
|||||||
<string>Form</string>
|
<string>Form</string>
|
||||||
</property>
|
</property>
|
||||||
<layout class="QGridLayout" name="gridLayout">
|
<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">
|
<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">
|
<widget class="QPlainTextEdit" name="chat">
|
||||||
<property name="readOnly">
|
<property name="readOnly">
|
||||||
<bool>true</bool>
|
<bool>true</bool>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="1" column="0">
|
<item row="3" column="0" colspan="4">
|
||||||
<widget class="QLineEdit" name="lineEdit"/>
|
<widget class="QLineEdit" name="messageEdit"/>
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user