Subversion Repositories QTron

Compare Revisions

Regard whitespace Rev 36 → Rev 35

/bike.h
11,6 → 11,7
#include <cmath>
#include <time.h>
#include <stdio.h>
#include <pthread.h>
 
using namespace std;
 
43,12 → 44,13
float abpool;
float velocity;
float speed;
 
private:
QTcpSocket *socket;
bool haveSentAlready;
 
private slots:
void readyRead();
void disconnected();
void readyRead();
 
signals:
void chat(QString name, QString message);
/mainwindow.h
29,7 → 29,6
QTimer *timer;
int id;
QTimer *suddenDeathTimer;
QTimer *timer2;
bool suddenDeath;
 
private slots:
37,7 → 36,6
void checkClients();
void chat(QString name, QString message);
void activateSuddenDeath();
void clientTimeOut();
};
 
#endif // MAINWINDOW_H
/main.cpp
7,4 → 7,5
MainWindow w;
w.show();
return a.exec();
pthread_exit(NULL);
}
/bike.cpp
105,11 → 105,8
 
void Bike::run()
{
if(!haveSentAlready){
for (int i = 0; i < bikes.count(); i++)
{
 
 
Bike *bike = bikes[i];
if (bike->isReady && bike->show)
{
137,11 → 134,9
}
}
}
 
linePoints.append(QPoint(x, y));
}
 
 
 
if (dead)
{
show = false;
152,18 → 147,14
 
if (!dead)
{
if(!haveSentAlready){
socket->write("G\n");
socket->flush();
}
 
hadGo = false;
if (!socket->waitForReadyRead(1))
if (!socket->waitForReadyRead(10000))
{
// socket->disconnectFromHost();
// dead = true;
haveSentAlready = true;
return;
socket->disconnectFromHost();
dead = true;
}
if (!hadGo)
{
175,6 → 166,7
 
time_t result = time(NULL);
cout << result << ": Recieved next move from " << name.toStdString().c_str() << endl;
pthread_exit(NULL);
}
 
int sign(int x){
269,7 → 261,6
 
void Bike::reset()
{
cout << "Reset was called\n";
x = rand() % 800;
y = rand() % 600;
linePoints.clear();
/mainwindow.cpp
12,36 → 12,14
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(50);
 
timer2 = new QTimer(this);
connect(timer2, SIGNAL(timeout()),this,SLOT(clientTimeOut()));
timer2->setSingleShot(true);
 
suddenDeath = false;
suddenDeathTimer = new QTimer(this);
connect(suddenDeathTimer, SIGNAL(timeout()), this, SLOT(activateSuddenDeath()));
suddenDeathTimer->start(300000);
 
 
 
id = 0;
}
 
 
void MainWindow::clientTimeOut()
{
cout << "Timeout function called\n";
Bike *bike;
foreach(bike,bikes)
{
if(!bike->hasHadGo)
{
bike->socket->disconnectFromHost();
bike->dead = true;
bike->hasHadGo = true;
}
}
}
 
MainWindow::~MainWindow()
{
delete server;
59,6 → 37,12
}
}
 
void *runtherun(void *bike){
Bike *realbike = (Bike *)bike;
realbike->run();
pthread_exit(NULL);
}
 
void MainWindow::paintEvent(QPaintEvent *e)
{
if (suddenDeath)
81,33 → 65,28
{
ready = false;
}
bikes[i]->hasHadGo = false;
bikes[i]->haveSentAlready = false;
}
 
if (ready)
{
// cout << ":: Sent move request..." << endl;
bool alldone = false;
timer2->start(10000);
while(!alldone){
cout << ":: Sent move request..." << endl;
pthread_t threads[bikes.count()];
for (int i = 0; i < bikes.count(); i++)
{
if (bikes[i]->show && !bikes[i]->hasHadGo)
if (bikes[i]->show)
{
bikes[i]->hasHadGo = false;
bikes[i]->bikes = bikes;
bikes[i]->run();
pthread_create(&threads[i],NULL,runtherun,(void *)bikes[i]);
}
}
alldone = true;
for (int i = 0; i < bikes.count(); i++){
if(bikes[i]->show && !bikes[i]->hasHadGo){
alldone = false;
Bike *bike;
foreach(bike,bikes)
{
while(!bike->hasHadGo)
{
}
}
}
timer2->stop();
}
 
for (int i = 0; i < bikes.count(); i++)
{
116,6 → 95,7
bikes[i]->draw(&painter,bikes);
}
}
}
 
checkClients();
}
128,7 → 108,6
Bike *bike = new Bike(socket, id);
connect(bike, SIGNAL(chat(QString,QString)), this, SLOT(chat(QString,QString)));
id += 1;
cout << "Resetting from here\n";
reset();
 
bikes.append(bike);
159,16 → 138,15
{
everyoneDied = false;
}
// else if (bikes[i]->dead)
// {
// everyoneDied = false;
// }
else if (bikes[i]->dead)
{
everyoneDied = false;
}
}
}
 
if (everyoneDied)
{
cout << "resetting from other place\n";
reset();
}
}