1 /* |
|
2 * Hedgewars, a free turn based strategy game |
|
3 * Copyright (c) 2007, 2008 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 |
|
19 #include <QHttp> |
|
20 #include <QMessageBox> |
|
21 #include <QTimer> |
|
22 #include <QStringList> |
|
23 #include "netwwwserver.h" |
|
24 #include "hwconsts.h" |
|
25 |
|
26 HWNetWwwServer::HWNetWwwServer(QObject *parent, const QString & descr, quint16 port) : |
|
27 HWNetRegisterServer(parent, descr, port), timer(0) |
|
28 { |
|
29 destroyPosted = false; |
|
30 destroyPostId = 0; |
|
31 |
|
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()); |
|
42 } |
|
43 |
|
44 void HWNetWwwServer::onClientRead(int id, bool error) |
|
45 { |
|
46 if (destroyPosted && (id == destroyPostId)) |
|
47 { |
|
48 deleteLater(); |
|
49 return; |
|
50 } |
|
51 |
|
52 if (error) |
|
53 { |
|
54 QMessageBox::critical(0, |
|
55 tr("Error"), |
|
56 tr("Server registration error") + "\n" + |
|
57 http->errorString()); |
|
58 return; |
|
59 } |
|
60 |
|
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 } |
|
77 } |
|
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 { |
|
89 QString request = QString("id=%1&key=%2") |
|
90 .arg(servid) |
|
91 .arg(servkey); |
|
92 destroyPostId = http->post("/games/destroy_game", request.toUtf8()); |
|
93 destroyPosted = true; |
|
94 } |
|