-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetriswindow.cpp
166 lines (133 loc) · 5.09 KB
/
tetriswindow.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
#include "tetriswindow.h"
#include "ui_tetriswindow.h"
#include <QtWidgets>
#include <QtNetwork>
#include <iostream>
#include <exception>
#include "savegame.h"
using namespace std;
TetrisWindow::TetrisWindow()
{
// basics
setWindowTitle("BLOCKS FROM PURGATORY");
this->setFixedSize(550,445);
board = new TetrisBoard;
//board->setFixedSize(220+2,420+2);
board->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
// muziek
player = new QMediaPlayer;
player->setMedia(QUrl::fromLocalFile("/Users/Jonas/Music/Blokken.mp3"));
player->setVolume(5); //volume nog aanpassen
// Gif
movie = new QMovie("/Users/Jonas/Music/giphy.gif");
QLabel *processLabel = new QLabel(this);
QSize const *size = new QSize(150,75);
processLabel->setMovie(movie);
movie->setScaledSize(*size);
// Buttons
startButton = new QPushButton("Start");
startButton->setFocusPolicy(Qt::NoFocus);
quitButton = new QPushButton("Quit");
quitButton->setFocusPolicy(Qt::NoFocus);
connect(startButton, SIGNAL(clicked()), board, SLOT(start()));
connect(startButton, SIGNAL(clicked()), player, SLOT(play()));
connect(startButton, SIGNAL(clicked()), movie, SLOT(start()));
connect(quitButton , SIGNAL(clicked()), qApp, SLOT(quit()));
// LCDS
scoreLcd = new QLCDNumber(2);
linesLcd = new QLCDNumber(2);
connect(board, SIGNAL(score_changed(int)), scoreLcd, SLOT(display(int)));
connect(board, SIGNAL(lines_changed(int)), linesLcd, SLOT(display(int)));
// Networking
statusLabel = new QLabel;
tcpServer= NULL;
networkSession = NULL;
session_opened();
connect(tcpServer, &QTcpServer::newConnection, this, &TetrisWindow::send_data);
//Saving the game
Saver = new Savegame;
Game_data = new QJsonObject;
QString Name = QHostInfo::localHostName(); //DNS
Saver->setName(Name);
Saver->write(*Game_data);
connect(board, &TetrisBoard::lines_changed, this, &TetrisWindow::save_data);
//Layout
QGridLayout *layout= new QGridLayout;
layout->addWidget(startButton, 4, 0);
layout->addWidget(quitButton, 5, 0);
layout->addWidget(statusLabel, 2, 0);
layout->addWidget(createLabel("Score"), 0, 2);
layout->addWidget(createLabel("# Lines Removed"), 2, 2);
layout->addWidget(board, 0, 1, 6, 1);
layout->addWidget(scoreLcd, 1, 2);
layout->addWidget(linesLcd, 3, 2);
layout->addWidget(processLabel,3,0);
setLayout(layout);
}
void TetrisWindow::save_data()
{
Saver->setScore(scoreLcd->intValue());
Saver->setLines(linesLcd->intValue());
Saver->setSpm(board->Get_Spm());
Saver->write(*Game_data);
bool check = Saver->storeGame(*Game_data);
if (check == FALSE)
{
QMessageBox::warning(this,"Tetris Server","save_tetris.json could not be written on.");
}
}
QLabel *TetrisWindow::createLabel(const QString &text)
{
QLabel *lbl = new QLabel(text);
return lbl;
}
void TetrisWindow::session_opened()
{
tcpServer = new QTcpServer(this);
if (!tcpServer->listen()) {
QMessageBox::critical(this, "Tetris Server",tr("Unable to start the Tetris server: %1.").arg(tcpServer->errorString()));
close();
return;
}
// on an arbitrary port
QString ipAddress;
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
for (int i = 0; i < ipAddressesList.size(); ++i)
{
if (ipAddressesList.at(i) != QHostAddress::LocalHost && ipAddressesList.at(i).toIPv4Address())
{
//use the first non-localhost (IPv4) that is not an 169.254 ("link local") adresses
cout << ipAddressesList.at(i).toString().toUtf8().constData() << endl;
ipAddress = ipAddressesList.at(i).toString();
QString str = ipAddress.left(7);
if(str != "169.254") break;
}
}
if (ipAddress.isEmpty())
{
QMessageBox::information(this, "Tetris Server", "No non-local IP Address found, used localhost.");
ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
}
statusLabel->setText(tr("Purgatory is located on\n\nIP: %1\nport: %2\n\n").arg(ipAddress).arg(tcpServer->serverPort()));
}
void TetrisWindow::send_data()
{
QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
connect(clientConnection, &QAbstractSocket::disconnected, clientConnection, &QObject::deleteLater);
QByteArray jSon_To_Send = Saver->datastream(*Game_data);
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out << quint16( 0 ) //space for a 16 bit int = the size of the data block
<< jSon_To_Send;
out.device()->seek( 0 );
out << quint16( block.size() - sizeof(quint16) );
try
{
clientConnection->write(block);
}
catch(exception& e)
{
QMessageBox::critical(this,"Tetris Server",tr("Writing failed: standard exception: %1.").arg(e.what()));
}
clientConnection->disconnectFromHost();
}