Subversion Repositories QTron

Rev

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