~ubuntu-branches/ubuntu/wily/hedgewars/wily

« back to all changes in this revision

Viewing changes to QTfrontend/newnetclient.cpp

  • Committer: Package Import Robot
  • Author(s): Dmitry E. Oboukhov
  • Date: 2011-09-23 10:16:55 UTC
  • mfrom: (1.2.11 upstream)
  • Revision ID: package-import@ubuntu.com-20110923101655-3977th2gc5n0a3pv
Tags: 0.9.16-1
* New upstream version.
 + Downloadable content! Simply click to install any content.
   New voices, hats, maps, themes, translations, music, scripts...
   Hedgewars is now more customisable than ever before! As time goes
   by we will be soliciting community content to feature on this page,
   so remember to check it from time to time. If you decide you want
   to go back to standard Hedgewars, just remove the Data directory
   from your Hedgewars config directory.
 + 3-D rendering! Diorama-like rendering of the game in a variety
   of 3D modes. Let us know which ones work best for you, we didn't
   really have the equipment to test them all.
 + Resizable game window.
 + New utilities! The Time Box will remove one of your hedgehogs
   from the game for a while, protecting from attack until it returns,
   somewhere else on the map. Land spray will allow you to build bridges,
   seal up holes, or just make life unpleasant for your enemies.
 + New single player: Bamboo Thicket, That Sinking Feeling, Newton and
   the Tree and multi-player: The Specialists, Space Invaders,
   Racer - scripts! And a ton more script hooks for scripters
 + New twists on old weapons. Drill strike, seduction and fire have
   been adjusted. Defective mines have been added, rope can attach to
   hogs/crates/barrels again, grenades now have variable bounce (use
   precise key + 1-5). Portal gun is now more usable in flight and
   all game actions are a lot faster.
 + New theme - Golf, dozens of new community hats and a new
   localised Default voice, Ukranian.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * Hedgewars, a free turn based strategy game
3
 
 * Copyright (c) 2006-2008 Ulyanov Igor <iulyanov@gmail.com>
4
 
 * Copyright (c) 2010 Andrey Korotaev <unC0Rr@gmail.com>
 
3
 * Copyright (c) 2006-2008 Igor Ulyanov <iulyanov@gmail.com>
 
4
 * Copyright (c) 2008-2011 Andrey Korotaev <unC0Rr@gmail.com>
5
5
 *
6
6
 * This program is free software; you can redistribute it and/or modify
7
7
 * it under the terms of the GNU General Public License as published by
29
29
#include "gamecfgwidget.h"
30
30
#include "teamselect.h"
31
31
#include "misc.h"
 
32
/* only to get the ignoreList from the chat widget */
 
33
#include "hwform.h"
 
34
#include "pageroomslist.h"
 
35
#include "chatwidget.h"
32
36
 
33
37
char delimeter='\n';
34
38
 
66
70
 
67
71
void HWNewNet::Connect(const QString & hostName, quint16 port, const QString & nick)
68
72
{
69
 
    mynick = nick.isEmpty() ? QLineEdit::tr("unnamed") : nick;
 
73
    mynick = nick;
 
74
    while (mynick.isEmpty()) {
 
75
        mynick = QInputDialog::getText(m_pGameCFGWidget,
 
76
                 QObject::tr("Nickname"),
 
77
                 QObject::tr("Please enter your nickname"),
 
78
                 QLineEdit::Normal,
 
79
                 QDir::home().dirName());
 
80
        config->setValue("net/nick",mynick);
 
81
        config->updNetNick();
 
82
    }
70
83
    myhost = hostName + QString(":%1").arg(port);
71
84
    NetSocket.connectToHost(hostName, port);
72
85
}
185
198
void HWNewNet::displayError(QAbstractSocket::SocketError socketError)
186
199
{
187
200
    emit Disconnected();
 
201
    m_game_connected = false;
188
202
 
189
203
    switch (socketError) {
190
204
        case QAbstractSocket::RemoteHostClosedError:
236
250
    }
237
251
 
238
252
    if (lst[0] == "CONNECTED") {
 
253
        if(lst.size() < 3 || lst[2].toInt() < cMinServerVersion)
 
254
        {
 
255
            // TODO: Warn user, disconnect
 
256
            qWarning() << "Server too old";
 
257
        }
 
258
 
239
259
        RawSendNet(QString("NICK%1%2").arg(delimeter).arg(mynick));
240
260
        RawSendNet(QString("PROTO%1%2").arg(delimeter).arg(*cProtoVer));
241
261
        netClientState = 1;
276
296
            return;
277
297
        }
278
298
        if (netClientState == 2)
279
 
            emit chatStringLobby(HWProto::formatChatMsg(lst[1], lst[2]));
 
299
            emit chatStringLobby(lst[1], HWProto::formatChatMsgForFrontend(lst[2]));
280
300
        else
281
301
            emit chatStringFromNet(HWProto::formatChatMsg(lst[1], lst[2]));
282
302
        return;
312
332
        return;
313
333
    }
314
334
 
315
 
    if (lst[0] == "READY") {
316
 
        if(lst.size() < 2)
317
 
        {
318
 
            qWarning("Net: Malformed READY message");
319
 
            return;
320
 
        }
321
 
        for(int i = 1; i < lst.size(); ++i)
322
 
        {
323
 
            if (lst[i] == mynick)
324
 
                emit setMyReadyStatus(true);
325
 
            emit setReadyStatus(lst[i], true);
326
 
        }
327
 
        return;
328
 
    }
329
 
 
330
 
    if (lst[0] == "NOT_READY") {
331
 
        if(lst.size() < 2)
332
 
        {
333
 
            qWarning("Net: Malformed NOT_READY message");
334
 
            return;
335
 
        }
336
 
        for(int i = 1; i < lst.size(); ++i)
337
 
        {
338
 
            if (lst[i] == mynick)
339
 
                emit setMyReadyStatus(false);
340
 
            emit setReadyStatus(lst[i], false);
341
 
        }
 
335
    if (lst[0] == "CLIENT_FLAGS")
 
336
    {
 
337
        if(lst.size() < 3 || lst[1].size() < 2)
 
338
        {
 
339
            qWarning("Net: Malformed CLIENT_FLAGS message");
 
340
            return;
 
341
        }
 
342
 
 
343
        QString flags = lst[1];
 
344
        bool setFlag = flags[0] == '+';
 
345
 
 
346
        while(flags.size() > 1)
 
347
        {
 
348
            flags.remove(0, 1);
 
349
            char c = flags[0].toAscii();
 
350
 
 
351
            switch(c)
 
352
            {
 
353
            case 'r':
 
354
                {
 
355
                    for(int i = 2; i < lst.size(); ++i)
 
356
                    {
 
357
                        if (lst[i] == mynick)
 
358
                            emit setMyReadyStatus(setFlag);
 
359
                        emit setReadyStatus(lst[i], setFlag);
 
360
                    }
 
361
                }
 
362
            }
 
363
        }
 
364
 
342
365
        return;
343
366
    }
344
367
 
395
418
                if (isChief)
396
419
                    emit configAsked();
397
420
            }
398
 
            emit nickAdded(lst[i], isChief && (lst[i] != mynick));
399
 
            emit chatStringFromNet(tr("%1 *** %2 has joined the room").arg('\x03').arg(lst[i]));
 
421
            if (lst[i] != mynick && isChief && config->Form->ui.pageRoomsList->chatWidget->ignoreList.contains(lst[i], Qt::CaseInsensitive) && !config->Form->ui.pageRoomsList->chatWidget->friendsList.contains(lst[i], Qt::CaseInsensitive))
 
422
            {
 
423
                kickPlayer(lst[i]);
 
424
            }
 
425
            else
 
426
            {
 
427
                emit nickAdded(lst[i], isChief && (lst[i] != mynick));
 
428
                emit chatStringFromNet(tr("%1 *** %2 has joined the room").arg('\x03').arg(lst[i]));
 
429
            }
400
430
        }
401
431
        return;
402
432
    }
418
448
            }
419
449
 
420
450
            emit nickAddedLobby(lst[i], false);
421
 
            emit chatStringLobby(tr("%1 *** %2 has joined").arg('\x03').arg(lst[i]));
 
451
            emit chatStringLobby(lst[i], tr("%1 *** %2 has joined").arg('\x03').arg("|nick|"));
422
452
        }
423
453
        return;
424
454
    }
471
501
        bool ok = false;
472
502
        int passLength = config->value("net/passwordlength", 0).toInt();
473
503
        QString hash = config->value("net/passwordhash", "").toString();
474
 
        QString password = QInputDialog::getText(0, tr("Password"), tr("Your nickname %1 is\nregistered on Hedgewars.org\nPlease provide your password below\nor pick another nickname in game config:").arg(mynick), QLineEdit::Password, passLength==0?NULL:QString(passLength,'\0'), &ok);
475
 
 
476
 
        if (!ok) {
477
 
            Disconnect();
478
 
            emit Disconnected();
479
 
            return;
480
 
        }
481
 
 
482
 
        if (!passLength || password!=QString(passLength, '\0')) {
 
504
        
 
505
        // If the password is blank, ask the user to enter one in
 
506
        if (passLength == 0)
 
507
        {
 
508
            QString password = QInputDialog::getText(m_pGameCFGWidget, tr("Password"), tr("Your nickname %1 is\nregistered on Hedgewars.org\nPlease provide your password below\nor pick another nickname in game config:").arg(mynick), QLineEdit::Password, passLength==0?NULL:QString(passLength,'\0'), &ok);
 
509
 
 
510
            if (!ok) {
 
511
                Disconnect();
 
512
                emit Disconnected();
 
513
                return;
 
514
            }
 
515
            
483
516
            hash = QCryptographicHash::hash(password.toLatin1(), QCryptographicHash::Md5).toHex();
484
517
            config->setValue("net/passwordhash", hash);
485
518
            config->setValue("net/passwordlength", password.size());
 
519
            config->setNetPasswordLength(password.size());
486
520
        }
487
521
 
488
522
        RawSendNet(QString("PASSWORD%1%2").arg(delimeter).arg(hash));
489
523
        return;
490
524
    }
491
525
 
 
526
    if (lst[0] == "NOTICE") {
 
527
        if(lst.size() < 2)
 
528
        {
 
529
            qWarning("Net: Bad NOTICE message");
 
530
            return;
 
531
        }
 
532
 
 
533
        bool ok;
 
534
        int n = lst[1].toInt(&ok);
 
535
        if(!ok)
 
536
        {
 
537
            qWarning("Net: Bad NOTICE message");
 
538
            return;
 
539
        }
 
540
 
 
541
        handleNotice(n);
 
542
 
 
543
        return;
 
544
    }
 
545
 
492
546
    if (lst[0] == "TEAM_ACCEPTED") {
493
547
        if (lst.size() != 2)
494
548
        {
560
614
            qWarning("Net: Bad BYE message");
561
615
            return;
562
616
        }
 
617
        if (lst[1] == "Authentication failed")
 
618
        {
 
619
            // Set the password blank if case the user tries to join and enter his password again
 
620
            config->setValue("net/passwordlength", 0);
 
621
            config->setNetPasswordLength(0);
 
622
        }
563
623
        emit showMessage(HWNewNet::tr("Quit reason: ") + lst[1]);
564
624
        return;
565
625
    }
673
733
    return isChief;
674
734
}
675
735
 
676
 
void HWNewNet::gameFinished()
 
736
void HWNewNet::gameFinished(bool correctly)
677
737
{
678
738
    if (netClientState == 5) netClientState = 3;
679
 
    RawSendNet(QString("ROUNDFINISHED"));
 
739
    RawSendNet(QString("ROUNDFINISHED%1%2").arg(delimeter).arg(correctly ? "1" : "0"));
680
740
}
681
741
 
682
742
void HWNewNet::banPlayer(const QString & nick)
707
767
    RawSendNet(QString("START_GAME"));
708
768
}
709
769
 
 
770
void HWNewNet::updateRoomName(const QString & name)
 
771
{
 
772
    RawSendNet(QString("ROOM_NAME%1%2").arg(delimeter).arg(name));
 
773
}
 
774
 
 
775
 
710
776
void HWNewNet::toggleRestrictJoins()
711
777
{
712
778
    RawSendNet(QString("TOGGLE_RESTRICT_JOINS"));
752
818
{
753
819
    RawSendNet(QString("GET_SERVER_VAR"));
754
820
}
 
821
 
 
822
void HWNewNet::handleNotice(int n)
 
823
{
 
824
    switch(n)
 
825
    {
 
826
        case 0:
 
827
        {
 
828
            bool ok = false;
 
829
            QString newNick = QInputDialog::getText(0, tr("Nickname"), tr("Some one already uses\n your nickname %1\non the server.\nPlease pick another nickname:").arg(mynick), QLineEdit::Normal, mynick, &ok);
 
830
 
 
831
            if (!ok || newNick.isEmpty()) {
 
832
                Disconnect();
 
833
                emit Disconnected();
 
834
                return;
 
835
            }
 
836
 
 
837
            config->setValue("net/nick", newNick);
 
838
            config->updNetNick();
 
839
            mynick = newNick;
 
840
 
 
841
            RawSendNet(QString("NICK%1%2").arg(delimeter).arg(newNick));
 
842
 
 
843
            break;
 
844
        }
 
845
    }
 
846
}