Subversion Repositories QTron

Rev

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