-
Notifications
You must be signed in to change notification settings - Fork 26
/
rooms.cpp
executable file
·418 lines (409 loc) · 13.6 KB
/
rooms.cpp
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include "rooms.h"
rooms::rooms(QWidget *parent) :
QMainWindow(parent)
{
fac=new Factory();
haveEmpty=false;
haveinitial=false;
newRoomCreated=false;
LeaveDone=false;
Ready=false;
initialSize=0;
flushRoomListTimer=new QTimer(this);
connect(flushRoomListTimer,SIGNAL(timeout()),this,SLOT(CheckLeaveStatus()));
initialmainWidget();
}
void rooms::initialmainWidget()
{
QWidget* mainWidget=new QWidget(this);
QWidget* roomWidget=new QWidget(mainWidget);
roomWidget->setMinimumSize(640,640);
QHBoxLayout* h_roomWidgetLayout=new QHBoxLayout();
h_roomWidgetLayout->setContentsMargins(0,0,0,0);//让布局紧贴主窗口,不留空白
h_roomWidgetLayout->setSpacing(0);//让两个控件之间的间隔为0
roomWidget->setLayout(h_roomWidgetLayout);
roomInfo=new QWidget(mainWidget);
roomInfo->setMinimumSize(640,640);
roomInfo->hide();
QVBoxLayout* vLayout=new QVBoxLayout();
vLayout->setContentsMargins(0,0,0,0);//让布局紧贴主窗口,不留空白
vLayout->setSpacing(0);//让两个控件之间的间隔为0
roomInfo->setLayout(vLayout);
roomPlayers=new QWidget(roomInfo);
roomPlayers->setMinimumSize(640,340);
intialRoomPlayersWidget();
chatRoom=new ChatRoom(roomInfo);
chatRoom->setMinimumSize(640,300);
vLayout->addWidget(roomPlayers);
vLayout->addWidget(chatRoom);
h_roomWidgetLayout->addWidget(roomInfo);
roomsList=new QWidget(mainWidget);
roomsList->setMinimumSize(640,640);
ListLayout=new QVBoxLayout();
ListLayout->setContentsMargins(0,0,0,0);//让布局紧贴主窗口,不留空白
ListLayout->setSpacing(0);//让两个控件之间的间隔为0
ListLayout->setAlignment(Qt::AlignTop);//设置顶部对齐
roomsList->setLayout(ListLayout);
h_roomWidgetLayout->addWidget(roomsList,0,0);
QWidget* infoWidget=new QWidget(mainWidget);
infoWidget->setStyleSheet("background-color:papayawhip");
infoWidget->setMinimumSize(200,640);
QString infoWidget_style="color:black;background-color:white";
playerLbl=fac->CreateQLabel(infoWidget,0,320,120,40,"",infoWidget_style);
QPushButton* playerBtn=fac->CreateQPushButton(infoWidget,0,380,120,40,"创建房间",infoWidget_style);
QPushButton* deleteRoomBtn=fac->CreateQPushButton(infoWidget,0,440,120,40,"删除房间",infoWidget_style);
initialCreateroomDialog();
connect(playerBtn,SIGNAL(clicked()),this,SLOT(CreateRoom()));
connect(deleteRoomBtn,SIGNAL(clicked()),this,SLOT(DeleteRoom()));
QHBoxLayout* h_mainLayout=new QHBoxLayout();
h_mainLayout->setContentsMargins(0,0,0,0);//让布局紧贴主窗口,不留空白
h_mainLayout->setSpacing(0);//让两个控件之间的间隔为0
mainWidget->setLayout(h_mainLayout);
h_mainLayout->addWidget(roomWidget);
h_mainLayout->addWidget(infoWidget);
this->setCentralWidget(mainWidget);
}
void rooms::intialRoomPlayersWidget()
{
ownerTbt=new QToolButton(roomPlayers);
ownerTbt->setGeometry(100,95,150,150);
ownerTbt->setIcon(QPixmap(":/info/image/information/MarioLeft.png"));
ownerTbt->setIconSize(QPixmap(":/info/image/information/MarioLeft.png").size());
ownerTbt->setAutoRaise(true);
ownerTbt->setEnabled(true);
ownerTbt->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
playerTbt=new QToolButton(roomPlayers);
playerTbt->setGeometry(390,95,150,150);
playerTbt->setIcon(QPixmap(":/info/image/information/MarioRight.png"));
playerTbt->setIconSize(QPixmap(":/info/image/information/MarioRight.png").size());
playerTbt->setAutoRaise(true);
playerTbt->setEnabled(true);
playerTbt->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
QFont font_VS("Microsoft YaHei" ,24, 75);
QString VS_style="color:orangered;background-color:transparent;";
QLabel *VS=fac->CreateQLabel(roomPlayers,280,130,100,80,"VS",VS_style,font_VS);
QString button_style="QPushButton{background-color:white;color:black;border-radius:5px;}"
"QPushButton:hover{background-color:palegreen; color: orangered;}"
"QPushButton:pressed{background-color:aquamarine;border-style:inset;}";
QPushButton* returnRoomListBtn=fac->CreateQPushButton(roomPlayers,100,290,120,40,"房间列表",button_style);
startGameBtn=fac->CreateQPushButton(roomPlayers,390,290,120,40,"",button_style);
connect(returnRoomListBtn,SIGNAL(clicked()),this,SLOT(returnRoomList()));
connect(startGameBtn,SIGNAL(clicked()),this,SLOT(GameStart()));
}
void rooms::initialRoomsListBtn()
{
if(haveEmpty)
{
emptyBtn->hide();
delete emptyBtn;
haveEmpty=false;
}
if(haveinitial&&initialSize!=0)
{
for(int i=0;i<initialSize;++i)
{
delete roomsListButtons[i];
}
delete [] roomsListButtons;
haveinitial=false;
initialSize=0;
}
if(newRoomCreated)
{
newRoomBtn->hide();
delete newRoomBtn;
newRoomCreated=false;
}
if(!roomsListQueue.isEmpty())
{
roomsListButtons=new QToolButton *[roomsListQueue.size()];
initialSize=roomsListQueue.size();
haveinitial=true;
}
int Num=0;
while(!roomsListQueue.isEmpty())
{
QString name=roomsListQueue.front().first;
QString owner=roomsListQueue.front().second;
roomsListQueue.dequeue();
QString text=name+" 房主: "+owner;
roomsListButtons[Num]=fac->CreateQToolButton(text,640,40,":/info/image/information/房间.png",true);
connect(roomsListButtons[Num],SIGNAL(clicked()),this,SLOT(EnterRoom()));
ListLayout->addWidget(roomsListButtons[Num]);
//qDebug()<<"set done";
Num++;
}
}
void rooms::initialCreateroomDialog()
{
CreateroomName=new QDialog();
QLabel* nameLabel = new QLabel("房间名:",CreateroomName);
nameEdit=new QLineEdit(CreateroomName);
QPushButton* EnterBtn = new QPushButton("确定",CreateroomName);
QGridLayout* gLayout=new QGridLayout();
gLayout->addWidget(nameLabel,0,0);
gLayout->addWidget(nameEdit,0,1);
gLayout->addWidget(EnterBtn,1,1);
CreateroomName->setLayout(gLayout);
connect(EnterBtn,SIGNAL(clicked()),this,SLOT(doCreateRoom()));
}
void rooms::CreateRoom()
{
CreateroomName->show();
}
void rooms::doCreateRoom()
{
if(nameEdit->text()=="")
{
QMessageBox::information(this, "创建失败", "房间名不能为空");
CreateroomName->raise();
return;
}
if(nameEdit->text().contains('\t')||nameEdit->text().contains(':'))
{
QMessageBox::information(this, "无效的用户名", "用户名中包含非法字符!");
return;
}
QString msg = CREATEROOM;
msg+=nameEdit->text()+'\t'+playerName;
msg+=ENDFLAG;
int length = tcpSocket->write(msg.toUtf8(), msg.toUtf8().length());
if(length != msg.toUtf8().length())
return;
}
void rooms::DeleteRoom()
{
QString msg = DELETEROOM;
msg+=playerName;
msg+=ENDFLAG;
int length = tcpSocket->write(msg.toUtf8(), msg.toUtf8().length());
if(length != msg.toUtf8().length())
return;
}
void rooms::EnterRoom()
{
QToolButton* curBtn=qobject_cast<QToolButton*>(sender());//通过sender()获取发送信号对象
QString text=curBtn->text();
QString str=" 房主: ";
int index=text.indexOf(str,0);
roomName=text.mid(0,index);
owner_=text.mid(index+5);
chatRoom->owner_=owner_;
qDebug()<<"roomName:"<<roomName;
QString msg = ENTERROOM;
msg+=roomName+'\t'+playerName;
msg+=ENDFLAG;
int length = tcpSocket->write(msg.toUtf8(), msg.toUtf8().length());
if(length != msg.toUtf8().length())
return;
}
void rooms::slotConnected()
{
QString msg = ENTERROOM;
msg+=roomName+'\t'+playerName;
msg+=ENDFLAG;
int length = tcpSocket->write(msg.toUtf8(), msg.toUtf8().length());
if(length != msg.toUtf8().length())
return;
}
void rooms::doEnterRoom()
{
if(owner_==playerName)
{
startGameBtn->setText("开始");
ownerTbt->setText(owner_);
playerTbt->setText("");
}
else
{
startGameBtn->setText("准备");
playerTbt->setText(playerName);
ownerTbt->setText("");
}
roomInfo->show();
roomsList->hide();
}
void rooms::LeaveRoom()
{
QString msg = LEAVEROOM;
msg+=playerName;
msg+=ENDFLAG;
int length = tcpSocket->write(msg.toUtf8(), msg.toUtf8().length());
if(length != msg.toUtf8().length())
return;
}
void rooms::CheckLeaveStatus()
{
if(LeaveDone)
{
flushRoomList();
flushRoomListTimer->stop();
LeaveDone=false;
}
}
void rooms::returnRoomList()
{
LeaveRoom();
flushRoomListTimer->start(10);
roomInfo->hide();
chatRoom->contentListWidget->clear();
roomsList->show();
}
void rooms::startRq()
{
QString msg = STARTRQ;
msg+=playerName+'\t'+roomName;
msg+=ENDFLAG;
int length = tcpSocket->write(msg.toUtf8(), msg.toUtf8().length());
if(length != msg.toUtf8().length())
return;
}
void rooms::beReady()
{
Ready=!Ready;
QString ready_str;
if(Ready)
{
startGameBtn->setText("取消准备");
ready_str="ready";
}
else
{
startGameBtn->setText("准备");
ready_str="unready";
}
QString msg = READYRQ;
msg+=playerName+'\t'+roomName+'\t'+ready_str;
msg+=ENDFLAG;
qDebug()<<"ready msg:"<<msg;
int length = tcpSocket->write(msg.toUtf8(), msg.toUtf8().length());
if(length != msg.toUtf8().length())
return;
}
void rooms::GameStart()
{
if(owner_==playerName)
startRq();
else
beReady();
}
void rooms::dataReceived()
{
QByteArray datagram;
datagram.resize(tcpSocket->bytesAvailable());
tcpSocket->read(datagram.data(), datagram.size());
QString msg = datagram.data();
qDebug()<<"rooms msg: "<<msg;
if(msg=="")
return;
if(msg=="rooms empty!")
{
emptyBtn=fac->CreateQToolButton("(空)",640,40,":/info/image/information/房间.png",false);
ListLayout->addWidget(emptyBtn);
haveEmpty=true;
return;
}
else if(msg=="duplicated name!")
{
QMessageBox::information(this, "创建失败", "房间名已被占用");
nameEdit->clear();
CreateroomName->raise();
return;
}
else if(msg=="you already have a room!")
{
QMessageBox::information(this, "创建失败", "你已经创建了一个房间了!");
nameEdit->clear();
CreateroomName->hide();
return;
}
else if(msg=="CreateRoom done!")
{
if(haveEmpty==true)
{
haveEmpty=false;
//ListLayout->removeWidget(emptyBtn);
emptyBtn->hide();
delete emptyBtn;
}
QString text=nameEdit->text()+" 房主: "+playerName;
newRoomBtn=fac->CreateQToolButton(text,640,40,":/info/image/information/房间.png",true);
connect(newRoomBtn,SIGNAL(clicked()),this,SLOT(EnterRoom()));
ListLayout->addWidget(newRoomBtn);
nameEdit->clear();
CreateroomName->hide();
newRoomCreated=true;
}
else if(msg=="No Room!")
{
QMessageBox::information(this, "删除房间失败", "你还没有创建房间");
return;
}
else if(msg=="Enter Refused!")
{
QMessageBox::information(this, "进入房间失败", "房主不能进入他人房间,可以删除自己的房间后进入");
return;
}
else if(msg.mid(0,9)=="EnterRoom")
{
doEnterRoom();
QString text=msg.mid(9);
int it=text.indexOf('\t');
QString owner=text.mid(0,it);
QString player=text.mid(it+1);
qDebug()<<"owner:"<<owner<<" plyer:"<<player;
ownerTbt->setText(owner);
playerTbt->setText(player);
}
else if(msg.mid(0,9)=="LeaveRoom")
{
QString player=msg.mid(9);
if(playerTbt->text()==player)
playerTbt->setText("");
else if(ownerTbt->text()==player)
ownerTbt->setText("");
LeaveDone=true;
}
else if(msg=="flush RoomList")
{
flushRoomList();
if(roomsListQueue.size()==0)
initialRoomsListBtn();
}
else
{
int count=msg.section('#',1,1).toInt();
qDebug()<<"count:"<<count;
QString str;
for(int i=0;i<count;i++)
{
str=msg.section(':',i,i);
//qDebug()<<"str:"<<str;
QString name=str.section(',',0,0);
//qDebug()<<"name:"<<name;
QString owner=str.section(',',1,1);
//qDebug()<<"owner:"<<owner;
roomsListQueue.push_back(std::pair<QString,QString>(name,owner));
}
//qDebug()<<"get done";
initialRoomsListBtn();
}
}
void rooms::flushRoomList()
{
QString msg = FLUSHROOMLIST;
msg+=ENDFLAG;
int length = tcpSocket->write(msg.toUtf8(), msg.toUtf8().length());
if(length != msg.toUtf8().length())
return;
}
void rooms::showEvent(QShowEvent *)
{
tcpSocket=fac->CreateQTcpSocket(SERVER_IP,SERVER_PORT);
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(dataReceived()));
connect(tcpSocket,SIGNAL(connected()),this,SLOT (slotConnected()));
playerLbl->setText(playerName);
chatRoom->playerName=playerName;
//qDebug()<<"flushRooms";
flushRoomList();
}