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 |
{
|
635
|
29 |
http = new QHttp(this);
|
|
30 |
http->setHost("www.hedgewars.org", 80);
|
|
31 |
connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(onClientRead(int, bool)));
|
|
32 |
|
|
33 |
QString request = QString("game[title]=%1&game[port]=%2&game[password]=%3&game[protocol_version]=%4")
|
|
34 |
.arg(descr)
|
|
35 |
.arg(port)
|
|
36 |
.arg(false ? "true" : "false")
|
|
37 |
.arg(*cProtoVer);
|
|
38 |
http->post("/games/create", request.toUtf8());
|
633
|
39 |
}
|
|
40 |
|
635
|
41 |
void HWNetWwwServer::onClientRead(int id, bool error)
|
633
|
42 |
{
|
635
|
43 |
if (error)
|
|
44 |
{
|
|
45 |
QMessageBox::critical(0,
|
|
46 |
tr("Error"),
|
|
47 |
tr("Server registration error") + "\n" +
|
|
48 |
http->errorString());
|
|
49 |
return;
|
|
50 |
}
|
633
|
51 |
|
635
|
52 |
QString str = http->readAll();
|
|
53 |
|
|
54 |
if (!str.size()) return; // ??
|
|
55 |
|
|
56 |
if (str[1] == QChar('0')) return; // error on server
|
|
57 |
if (!timer)
|
|
58 |
{
|
|
59 |
QStringList sl = str.split(',');
|
|
60 |
if (sl.size() != 2) return;
|
|
61 |
servid = sl[0];
|
|
62 |
servkey = sl[1];
|
|
63 |
|
|
64 |
timer = new QTimer(this);
|
|
65 |
connect(timer, SIGNAL(timeout()), this, SLOT(updateInList()));
|
|
66 |
timer->start(60000);
|
|
67 |
}
|
633
|
68 |
}
|
635
|
69 |
|
|
70 |
void HWNetWwwServer::updateInList()
|
|
71 |
{
|
|
72 |
QString request = QString("id=%1&key=%2")
|
|
73 |
.arg(servid)
|
|
74 |
.arg(servkey);
|
|
75 |
http->post("/games/update_game", request.toUtf8());
|
|
76 |
}
|
|
77 |
|
|
78 |
void HWNetWwwServer::unregister()
|
|
79 |
{
|
|
80 |
QString request = QString("id=%1&key=%2")
|
|
81 |
.arg(servid)
|
|
82 |
.arg(servkey);
|
|
83 |
http->post("/games/destroy_game", request.toUtf8());
|
|
84 |
}
|