-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
148 lines (130 loc) · 3.08 KB
/
client.cpp
File metadata and controls
148 lines (130 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "client.h"
#include <QHostAddress>
#include <QTcpSocket>
#include <iostream>
#include "clientwidget.h"
/**
* @brief Client::Client
* @param parent
*/
Client::Client(QObject* parent): Connexion(parent), blockSize(0)
{
client = new QTcpSocket(this);
connect(client, SIGNAL(error(QAbstractSocket::SocketError))
, this, SLOT(erreurConnexion()));
}
/**
* @brief Client::~Client
*/
Client::~Client()
{
client->close();
}
/**
* @brief Client::start
* @param address
* @param port
* @param pseudo
* essaye de se connecter au serveur
*/
void Client::start(QString address, quint16 port, QString pseudo)
{
QHostAddress addr(address);
client->connectToHost(addr, port);
connect(client, SIGNAL(readyRead()), this, SLOT(recevoirInstruction()));
this->pseudo = pseudo;
}
/**
* @brief Client::verifierConnexion
* @param buffer
* verifie le resultat de la connection
*/
void Client::verifierConnexion(const char* buffer) {
if (strcmp(buffer,"nop") == 0)
cw->setEtat("Connexion refusée, trop de joueurs");
else {
char buffer[1020] = {0};
strcpy(buffer,std::to_string(0).c_str());
strcat(buffer,";");
strcat(buffer,this->pseudo.toStdString().c_str());
client->write(buffer,sizeof(buffer));
cw->setEtat("Connexion réussie, en attente du lancement");
}
}
/**
* @brief Client::close
*/
void Client::close()
{
client->close();
}
/**
* @brief Client::erreurConnexion
*/
void Client::erreurConnexion()
{
std::cout << "erreur dans la connexion " << std::endl;
}
/**
* @brief Client::recevoirInstruction
* recupere une instruction
*/
void Client::recevoirInstruction()
{
char buffer[1020] = {0};
client->read(buffer, client->bytesAvailable());
analyserInstruction(QString::fromUtf8(buffer));
}
/**
* @brief Client::analyserInstruction
* @param instruction
* traite une instruction
*/
void Client::analyserInstruction(QString instruction)
{
QStringList list = instruction.split(";");
if (list.size() == 0)
return;
switch(list.at(0).toInt())
{
// instruction de connexion / envoie de pseudo
case 0:
verifierConnexion(list.at(1).toStdString().c_str());
break;
// instruction lancement partie / récupération numéro joueur
case 1:
lancerPartie(list.at(1).toInt());
break;
// instruction action levier / couleur levier
case 2:
break;
// instruction déplacement joueur / id joueur / nouveau x / nouveau y
case 3:
break;
case 4:
break;
}
}
/**
* @brief Client::lancerPartie
* @param numero
*/
void Client::lancerPartie(int numero)
{
gw = new GameWindow(0, this, numero);
gw->show();
}
/**
* @brief Client::envoyerDeplacement
* @param numero
* @param direction
* envoi le numero du joueur et sa direction
*/
void Client::envoyerDeplacement(int numero, string direction)
{
char buffer[1020] = {0};
strcpy(buffer,std::to_string(1).c_str());
strcat(buffer,";");
strcat(buffer,direction.c_str());
client->write(buffer,sizeof(buffer));
}