633
|
1 |
/*
|
|
2 |
* Hedgewars, a worms-like game
|
|
3 |
* Copyright (c) 2007 Andrey Korotaev <unC0Rr@gmail.com>
|
|
4 |
*
|
|
5 |
* This program is free software; you can redistribute it and/or modify
|
|
6 |
* it under the terms of the GNU General Public License as published by
|
|
7 |
* the Free Software Foundation; version 2 of the License
|
|
8 |
*
|
|
9 |
* This program is distributed in the hope that it will be useful,
|
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
* GNU General Public License for more details.
|
|
13 |
*
|
|
14 |
* You should have received a copy of the GNU General Public License
|
|
15 |
* along with this program; if not, write to the Free Software
|
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
17 |
*/
|
|
18 |
|
635
|
19 |
#include <QHttp>
|
|
20 |
#include <QMessageBox>
|
|
21 |
#include <QTimer>
|
|
22 |
#include <QStringList>
|
633
|
23 |
#include "netwwwserver.h"
|
635
|
24 |
#include "hwconsts.h"
|
633
|
25 |
|
|
26 |
HWNetWwwServer::HWNetWwwServer(QObject *parent, const QString & descr, quint16 port) :
|
635
|
27 |
HWNetRegisterServer(parent, descr, port), timer(0)
|
633
|
28 |
{
|
659
|
29 |
destroyPosted = false;
|
|
30 |
destroyPostId = 0;
|
|
31 |
|
635
|
32 |
http = new QHttp(this);
|
|
33 |
http->setHost("www.hedgewars.org", 80);
|
|
34 |
connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(onClientRead(int, bool)));
|
|
35 |
|
|
36 |
QString request = QString("game[title]=%1&game[port]=%2&game[password]=%3&game[protocol_version]=%4")
|
|
37 |
.arg(descr)
|
|
38 |
.arg(port)
|
|
39 |
.arg(false ? "true" : "false")
|
|
40 |
.arg(*cProtoVer);
|
|
41 |
http->post("/games/create", request.toUtf8());
|
633
|
42 |
}
|
|
43 |
|
635
|
44 |
void HWNetWwwServer::onClientRead(int id, bool error)
|
633
|
45 |
{
|
659
|
46 |
if (destroyPosted && (id == destroyPostId))
|
|
47 |
{
|
|
48 |
deleteLater();
|
|
49 |
return;
|
|
50 |
}
|
|
51 |
|
635
|
52 |
if (error)
|
|
53 |
{
|
|
54 |
QMessageBox::critical(0,
|
|
55 |
tr("Error"),
|
|
56 |
tr("Server registration error") + "\n" +
|
|
57 |
http->errorString());
|
|
58 |
return;
|
|
59 |
}
|
633
|
60 |
|
635
|
61 |
QString str = http->readAll();
|
|
62 |
|
|
63 |
if (!str.size()) return; // ??
|
|
64 |
|
|
65 |
if (str[1] == QChar('0')) return; // error on server
|
|
66 |
if (!timer)
|
|
67 |
{
|
|
68 |
QStringList sl = str.split(',');
|
|
69 |
if (sl.size() != 2) return;
|
|
70 |
servid = sl[0];
|
|
71 |
servkey = sl[1];
|
|
72 |
|
|
73 |
timer = new QTimer(this);
|
|
74 |
connect(timer, SIGNAL(timeout()), this, SLOT(updateInList()));
|
|
75 |
timer->start(60000);
|
|
76 |
}
|
633
|
77 |
}
|
635
|
78 |
|
|
79 |
void HWNetWwwServer::updateInList()
|
|
80 |
{
|
|
81 |
QString request = QString("id=%1&key=%2")
|
|
82 |
.arg(servid)
|
|
83 |
.arg(servkey);
|
|
84 |
http->post("/games/update_game", request.toUtf8());
|
|
85 |
}
|
|
86 |
|
|
87 |
void HWNetWwwServer::unregister()
|
|
88 |
{
|
660
|
89 |
qDebug("delete server");
|
635
|
90 |
QString request = QString("id=%1&key=%2")
|
|
91 |
.arg(servid)
|
|
92 |
.arg(servkey);
|
659
|
93 |
destroyPostId = http->post("/games/destroy_game", request.toUtf8());
|
|
94 |
destroyPosted = true;
|
635
|
95 |
}
|