This repository has been archived by the owner on Jun 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.cxx
executable file
·243 lines (214 loc) · 6.31 KB
/
controller.cxx
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// -*- Mode: C++ -*-
//
// Filename: controller.cxx
// Author: Frederic DELBOS - fred.delbos@gmail.com
// Created: Mon Jul 4 12:15:47 2011 (+0200)
// Last-Updated: Mon Jul 4 19:38:07 2011 (+0200)
// By: Frederic DELBOS - fred.delbos@gmail.com
// Update #: 63
//
//
#include "controller.hxx"
#include <QTextStream>
#include <iostream>
#include <QMessageBox>
Controller::Controller(QWidget *parent)
{
(void)parent;
_uidialog = new Ui_Dialog;
_uidialog->setupUi(this);
disableCommands();
_clock = 0;
_socket = new QTcpSocket(this);
connect(_uidialog->connection, SIGNAL(pressed()), this, SLOT(newConnection()));
connect(_socket, SIGNAL(connected()), this, SLOT(sockConnected()));
connect(_socket, SIGNAL(disconnected()), this, SLOT(connectionClosedByServer()));
connect(_socket, SIGNAL(readyRead()), this, SLOT(sockRead()));
connect(_socket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(sockError(QAbstractSocket::SocketError)));
connect(_uidialog->avance, SIGNAL(pressed()), this, SLOT(sendAvance()));
connect(_uidialog->gauche, SIGNAL(pressed()), this, SLOT(sendGauche()));
connect(_uidialog->droite, SIGNAL(pressed()), this, SLOT(sendDroite()));
connect(_uidialog->voir, SIGNAL(pressed()), this, SLOT(sendVoir()));
connect(_uidialog->inventaire, SIGNAL(pressed()), this, SLOT(sendInventaire()));
connect(_uidialog->fork, SIGNAL(pressed()), this, SLOT(sendFork()));
connect(_uidialog->incantation, SIGNAL(pressed()), this, SLOT(sendIncantation()));
connect(_uidialog->expulse, SIGNAL(pressed()), this, SLOT(sendExpulse()));
connect(_uidialog->prend, SIGNAL(pressed()), this, SLOT(sendPrend()));
connect(_uidialog->pose, SIGNAL(pressed()), this, SLOT(sendPose()));
connect(_uidialog->connectNbr, SIGNAL(pressed()), this, SLOT(sendConnectNbr()));
connect(_uidialog->broadcast, SIGNAL(pressed()), this, SLOT(sendBroadcast()));
connect(_uidialog->commandeBidon, SIGNAL(pressed()), this, SLOT(sendBidon()));
}
void Controller::newConnection()
{
if(_uidialog->connection->text() == tr("Connexion"))
{
_socket->abort();
_socket->connectToHost(_uidialog->host->text(), _uidialog->port->text().toInt());
}
else
{
_socket->abort();
_uidialog->connection->setText(tr("Connexion"));
disableCommands();
}
}
void Controller::sockRead()
{
QString msg = "";
char buf[2048];
qint64 lineLength = 0;
while((lineLength = _socket->readLine(buf, sizeof(buf))) > 0)
msg += tr(buf);
addReceivedText(msg);
if(!msg.compare("BIENVENUE\n"))
sendMsg(_uidialog->team->text()); // send team name
}
QString Controller::getTimeElapsed()
{
if(!_clock)
return tr("");
int time = _clock->elapsed();
int sec = time / 1000;
int milli = time % 1000;
QString str = QString("%1").arg(milli);
while(milli < 100)
{
str = tr("0") + str;
milli *= 10;
}
str = QString("%1").arg(sec) + tr(",") + str;
str = tr("<i style='color:green'>") + str + tr(" -></i> ");
return str;
}
void Controller::addReceivedText(QString text)
{
text = tr("<hr /><div style='color:red'>") + getTimeElapsed() + text + tr("</div>");
_uidialog->shell->setHtml(_uidialog->shell->toHtml() + text);
QTextCursor c = _uidialog->shell->textCursor();
c.movePosition(QTextCursor::End);
_uidialog->shell->setTextCursor(c);
}
void Controller::addSendText(QString text)
{
text = tr("<hr /><div style='color:blue'>") + getTimeElapsed() + text + tr("</div>");
_uidialog->shell->setHtml(_uidialog->shell->toHtml() + text);
QTextCursor c = _uidialog->shell->textCursor();
c.movePosition(QTextCursor::End);
_uidialog->shell->setTextCursor(c);
}
void Controller::sockError(QAbstractSocket::SocketError error)
{
disableCommands();
QMessageBox msgBox;
switch(error)
{
case QAbstractSocket::ConnectionRefusedError:
msgBox.setText("Connection refused"); break;
case QAbstractSocket::RemoteHostClosedError:
msgBox.setText("Remote Host closed connection"); break;
case QAbstractSocket::HostNotFoundError:
msgBox.setText("Host not found"); break;
case QAbstractSocket::SocketAccessError:
msgBox.setText("Socket access error"); break;
case QAbstractSocket::SocketTimeoutError:
msgBox.setText("Connection timeout"); break;
case QAbstractSocket::DatagramTooLargeError:
msgBox.setText("Datagram too large error"); break;
default:
msgBox.setText("Unknow network error"); break;
}
msgBox.exec();
}
void Controller::sockConnected()
{
enableCommands();
}
void Controller::connectionClosedByServer()
{
disableCommands();
QMessageBox msgBox;
msgBox.setText("Connection closed by server");
}
void Controller::sendMsg(QString msg)
{
addSendText(msg);
msg += '\n';
QByteArray ba = msg.toLocal8Bit();
const char *c_str = ba.data();
_socket->write(c_str, msg.size());
}
void Controller::enableCommands()
{
if(_clock)
delete _clock;
_clock = new QTime;
_clock->start();
_uidialog->host->setEnabled(false);
_uidialog->port->setEnabled(false);
_uidialog->team->setEnabled(false);
_uidialog->shell->setHtml("");
_uidialog->connection->setText(tr("Deconnexion"));
_uidialog->commandsBox->setEnabled(true);
}
void Controller::disableCommands()
{
_uidialog->host->setEnabled(true);
_uidialog->port->setEnabled(true);
_uidialog->team->setEnabled(true);
_uidialog->connection->setText(tr("Connexion"));
_uidialog->commandsBox->setEnabled(false);
}
void Controller::sendAvance()
{
sendMsg(tr("avance"));
}
void Controller::sendGauche()
{
sendMsg(tr("gauche"));
}
void Controller::sendDroite()
{
sendMsg(tr("droite"));
}
void Controller::sendVoir()
{
sendMsg(tr("voir"));
}
void Controller::sendInventaire()
{
sendMsg(tr("inventaire"));
}
void Controller::sendFork()
{
sendMsg(tr("fork"));
}
void Controller::sendPrend()
{
sendMsg(tr("prend ") + _uidialog->prendSelect->currentText());
}
void Controller::sendPose()
{
sendMsg(tr("pose ") + _uidialog->poseSelect->currentText());
}
void Controller::sendExpulse()
{
sendMsg(tr("expulse"));
}
void Controller::sendIncantation()
{
sendMsg(tr("incantation"));
}
void Controller::sendConnectNbr()
{
sendMsg(tr("connect_nbr"));
}
void Controller::sendBroadcast()
{
sendMsg(tr("broadcast ") + _uidialog->broadcastMsg->text());
}
void Controller::sendBidon()
{
sendMsg(_uidialog->commandeBidonMsg->text());
}