Rev 36 | 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; |
||
92 | timer2->start(10000); |
||
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 | } |
108 | } |
||
36 | muzer | 109 | timer2->stop(); |
110 | } |
||
13 | tom | 111 | |
36 | muzer | 112 | for (int i = 0; i < bikes.count(); i++) |
113 | { |
||
114 | if (bikes[i]->show) |
||
14 | muzer | 115 | { |
36 | muzer | 116 | bikes[i]->draw(&painter,bikes); |
14 | muzer | 117 | } |
9 | muzer | 118 | } |
1 | muzer | 119 | |
9 | muzer | 120 | checkClients(); |
1 | muzer | 121 | } |
122 | |||
123 | void MainWindow::newConnection() |
||
124 | { |
||
9 | muzer | 125 | while (server->hasPendingConnections()) |
126 | { |
||
127 | QTcpSocket *socket = server->nextPendingConnection(); |
||
128 | Bike *bike = new Bike(socket, id); |
||
36 | muzer | 129 | connect(bike, SIGNAL(chat(QString,QString)), this, SLOT(chat(QString,QString))); |
9 | muzer | 130 | id += 1; |
36 | muzer | 131 | cout << "Resetting from here\n"; |
9 | muzer | 132 | reset(); |
1 | muzer | 133 | |
9 | muzer | 134 | bikes.append(bike); |
135 | cout << ":: New Bike: " << socket->peerAddress().toString().toStdString() << endl; |
||
136 | } |
||
1 | muzer | 137 | } |
138 | |||
139 | void MainWindow::checkClients() |
||
140 | { |
||
36 | muzer | 141 | bool everyoneDied = true; |
27 | tom | 142 | |
9 | muzer | 143 | for (int i = bikes.count() - 1; i >= 0; i--) |
144 | { |
||
145 | if (bikes[i]->isDisconnected) |
||
146 | { |
||
36 | muzer | 147 | for (int j = 0; j < bikes.count(); j++) |
148 | { |
||
149 | bikes[j]->setText(QString("DISCO ") + bikes[i]->name + QString("\n")); |
||
150 | } |
||
30 | tom | 151 | |
9 | muzer | 152 | delete bikes[i]; |
153 | bikes.removeAt(i); |
||
154 | cout << ":: Removed Bike: " << i << endl; |
||
155 | } |
||
36 | muzer | 156 | else |
157 | { |
||
158 | if (!bikes[i]->collided) |
||
159 | { |
||
160 | everyoneDied = false; |
||
161 | } |
||
162 | // else if (bikes[i]->dead) |
||
163 | // { |
||
164 | // everyoneDied = false; |
||
165 | // } |
||
166 | } |
||
9 | muzer | 167 | } |
27 | tom | 168 | |
36 | muzer | 169 | if (everyoneDied) |
170 | { |
||
171 | cout << "resetting from other place\n"; |
||
172 | reset(); |
||
173 | } |
||
1 | muzer | 174 | } |
31 | tom | 175 | |
176 | void MainWindow::chat(QString name, QString message) |
||
177 | { |
||
36 | muzer | 178 | QString packet = "CHAT "; |
179 | packet.append(name); |
||
180 | packet.append(" "); |
||
181 | packet.append(message); |
||
182 | packet.append("\n"); |
||
31 | tom | 183 | |
36 | muzer | 184 | for (int i = 0; i < bikes.count(); i++) |
185 | { |
||
186 | bikes[i]->setText(packet); |
||
187 | } |
||
31 | tom | 188 | } |
32 | tom | 189 | |
190 | void MainWindow::activateSuddenDeath() |
||
191 | { |
||
36 | muzer | 192 | if (!suddenDeath) |
193 | { |
||
194 | suddenDeath = true; |
||
195 | chat("Server", "SUDDEN DEATH ACTIVATED!"); |
||
196 | } |
||
32 | tom | 197 | } |