Rev 40 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | muzer | 1 | #include "mainwindow.h" |
2 | |||
3 | MainWindow::MainWindow(QWidget *parent) : |
||
3 | tom | 4 | QGLWidget(parent) |
1 | muzer | 5 | { |
9 | muzer | 6 | this->setFixedSize(800, 600); |
7 | server = new QTcpServer(this); |
||
8 | connect(server, SIGNAL(newConnection()), this, SLOT(newConnection())); |
||
9 | server->listen(QHostAddress::Any, 4567); |
||
1 | muzer | 10 | |
9 | muzer | 11 | timer = new QTimer(this); |
12 | connect(timer, SIGNAL(timeout()), this, SLOT(update())); |
||
13 | timer->start(50); |
||
1 | muzer | 14 | |
36 | muzer | 15 | timer2 = new QTimer(this); |
16 | connect(timer2, SIGNAL(timeout()),this,SLOT(clientTimeOut())); |
||
17 | timer2->setSingleShot(true); |
||
32 | tom | 18 | |
36 | muzer | 19 | suddenDeath = false; |
20 | suddenDeathTimer = new QTimer(this); |
||
21 | connect(suddenDeathTimer, SIGNAL(timeout()), this, SLOT(activateSuddenDeath())); |
||
22 | suddenDeathTimer->start(300000); |
||
23 | |||
24 | |||
25 | |||
9 | muzer | 26 | id = 0; |
1 | muzer | 27 | } |
28 | |||
36 | muzer | 29 | |
30 | void MainWindow::clientTimeOut() |
||
31 | { |
||
32 | cout << "Timeout function called\n"; |
||
33 | Bike *bike; |
||
34 | foreach(bike,bikes) |
||
35 | { |
||
36 | if(!bike->hasHadGo) |
||
37 | { |
||
38 | bike->socket->disconnectFromHost(); |
||
39 | bike->dead = true; |
||
40 | bike->hasHadGo = true; |
||
41 | } |
||
42 | } |
||
43 | } |
||
44 | |||
1 | muzer | 45 | MainWindow::~MainWindow() |
46 | { |
||
9 | muzer | 47 | delete server; |
1 | muzer | 48 | } |
49 | |||
50 | void MainWindow::reset() |
||
51 | { |
||
36 | muzer | 52 | suddenDeathTimer->stop(); |
53 | suddenDeathTimer->start(300000); |
||
54 | suddenDeath = false; |
||
32 | tom | 55 | |
9 | muzer | 56 | for (int i = 0; i < bikes.count(); i++) |
57 | { |
||
58 | bikes[i]->reset(); |
||
59 | } |
||
1 | muzer | 60 | } |
61 | |||
62 | void MainWindow::paintEvent(QPaintEvent *e) |
||
63 | { |
||
36 | muzer | 64 | if (suddenDeath) |
65 | { |
||
66 | for (int i = 0; i < bikes.count(); i++) |
||
67 | { |
||
68 | bikes[i]->speed *= 1.01; |
||
69 | } |
||
70 | } |
||
32 | tom | 71 | |
9 | muzer | 72 | e->accept(); |
73 | QPainter painter(this); |
||
1 | muzer | 74 | |
9 | muzer | 75 | painter.fillRect(0, 0, 800, 600, Qt::white); |
1 | muzer | 76 | |
9 | muzer | 77 | bool ready = true; |
78 | for (int i = 0; i < bikes.count(); i++) |
||
79 | { |
||
80 | if (!bikes[i]->isReady) |
||
81 | { |
||
82 | ready = false; |
||
83 | } |
||
36 | muzer | 84 | bikes[i]->hasHadGo = false; |
85 | bikes[i]->haveSentAlready = false; |
||
9 | muzer | 86 | } |
1 | muzer | 87 | |
9 | muzer | 88 | if (ready) |
89 | { |
||
36 | muzer | 90 | // cout << ":: Sent move request..." << endl; |
91 | bool alldone = false; |
||
40 | muzer | 92 | time_t start = time(NULL); |
36 | muzer | 93 | while(!alldone){ |
94 | for (int i = 0; i < bikes.count(); i++) |
||
9 | muzer | 95 | { |
36 | muzer | 96 | if (bikes[i]->show && !bikes[i]->hasHadGo) |
97 | { |
||
98 | bikes[i]->bikes = bikes; |
||
99 | bikes[i]->run(); |
||
100 | } |
||
9 | muzer | 101 | } |
36 | muzer | 102 | alldone = true; |
103 | for (int i = 0; i < bikes.count(); i++){ |
||
104 | if(bikes[i]->show && !bikes[i]->hasHadGo){ |
||
105 | alldone = false; |
||
106 | } |
||
9 | muzer | 107 | } |
40 | muzer | 108 | if(time(NULL) - 10 > start) |
109 | clientTimeOut(); |
||
9 | muzer | 110 | } |
36 | muzer | 111 | } |
13 | tom | 112 | |
41 | muzer | 113 | if(ready) |
36 | muzer | 114 | { |
41 | muzer | 115 | for (int i = 0; i < bikes.count(); i++) |
14 | muzer | 116 | { |
41 | muzer | 117 | if (bikes[i]->show) |
118 | { |
||
119 | bikes[i]->draw(&painter,bikes); |
||
120 | } |
||
14 | muzer | 121 | } |
9 | muzer | 122 | } |
1 | muzer | 123 | |
9 | muzer | 124 | checkClients(); |
1 | muzer | 125 | } |
126 | |||
127 | void MainWindow::newConnection() |
||
128 | { |
||
9 | muzer | 129 | while (server->hasPendingConnections()) |
130 | { |
||
131 | QTcpSocket *socket = server->nextPendingConnection(); |
||
132 | Bike *bike = new Bike(socket, id); |
||
36 | muzer | 133 | connect(bike, SIGNAL(chat(QString,QString)), this, SLOT(chat(QString,QString))); |
9 | muzer | 134 | id += 1; |
36 | muzer | 135 | cout << "Resetting from here\n"; |
9 | muzer | 136 | reset(); |
1 | muzer | 137 | |
9 | muzer | 138 | bikes.append(bike); |
139 | cout << ":: New Bike: " << socket->peerAddress().toString().toStdString() << endl; |
||
140 | } |
||
1 | muzer | 141 | } |
142 | |||
143 | void MainWindow::checkClients() |
||
144 | { |
||
36 | muzer | 145 | bool everyoneDied = true; |
27 | tom | 146 | |
9 | muzer | 147 | for (int i = bikes.count() - 1; i >= 0; i--) |
148 | { |
||
149 | if (bikes[i]->isDisconnected) |
||
150 | { |
||
36 | muzer | 151 | for (int j = 0; j < bikes.count(); j++) |
152 | { |
||
153 | bikes[j]->setText(QString("DISCO ") + bikes[i]->name + QString("\n")); |
||
154 | } |
||
30 | tom | 155 | |
9 | muzer | 156 | delete bikes[i]; |
157 | bikes.removeAt(i); |
||
158 | cout << ":: Removed Bike: " << i << endl; |
||
159 | } |
||
36 | muzer | 160 | else |
161 | { |
||
162 | if (!bikes[i]->collided) |
||
163 | { |
||
164 | everyoneDied = false; |
||
165 | } |
||
166 | // else if (bikes[i]->dead) |
||
167 | // { |
||
168 | // everyoneDied = false; |
||
169 | // } |
||
170 | } |
||
9 | muzer | 171 | } |
27 | tom | 172 | |
36 | muzer | 173 | if (everyoneDied) |
174 | { |
||
175 | cout << "resetting from other place\n"; |
||
176 | reset(); |
||
177 | } |
||
1 | muzer | 178 | } |
31 | tom | 179 | |
180 | void MainWindow::chat(QString name, QString message) |
||
181 | { |
||
36 | muzer | 182 | QString packet = "CHAT "; |
183 | packet.append(name); |
||
184 | packet.append(" "); |
||
185 | packet.append(message); |
||
186 | packet.append("\n"); |
||
31 | tom | 187 | |
36 | muzer | 188 | for (int i = 0; i < bikes.count(); i++) |
189 | { |
||
190 | bikes[i]->setText(packet); |
||
191 | } |
||
31 | tom | 192 | } |
32 | tom | 193 | |
194 | void MainWindow::activateSuddenDeath() |
||
195 | { |
||
36 | muzer | 196 | if (!suddenDeath) |
197 | { |
||
198 | suddenDeath = true; |
||
40 | muzer | 199 | cout << "SUDDEN DEATH ACTIVATED!" << endl; |
36 | muzer | 200 | chat("Server", "SUDDEN DEATH ACTIVATED!"); |
201 | } |
||
32 | tom | 202 | } |