1
// Copyright (C) 2008 Ole Laursen
3
// This program is free software; you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation; either version 2 of the License, or
6
// (at your option) any later version.
8
// This program is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
// GNU Library General Public License for more details.
13
// You should have received a copy of the GNU General Public License
14
// along with this program; if not, write to the Free Software
15
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21
#include "game-client.h"
23
#include "network-connection.h"
26
#include "playerlist.h"
27
#include "network_player.h"
28
#include "xmlhelper.h"
32
GameClient::GameClient()
36
GameClient::~GameClient()
40
void GameClient::start(std::string host, int port)
42
network_connection.reset(new NetworkConnection());
43
network_connection->connected.connect(
44
sigc::mem_fun(this, &GameClient::onConnected));
45
network_connection->connection_lost.connect(
46
sigc::mem_fun(this, &GameClient::onConnectionLost));
47
network_connection->got_message.connect(
48
sigc::mem_fun(this, &GameClient::onGotMessage));
50
network_connection->connectToHost(host, port);
53
void GameClient::onConnected()
55
std::cerr << "connected" << std::endl;
57
network_connection->send(MESSAGE_TYPE_PING, "");
60
void GameClient::onConnectionLost()
62
std::cerr << "connection lost" << std::endl;
65
void GameClient::onGotMessage(MessageType type, std::string payload)
67
std::cerr << "got message of type " << type << std::endl;
69
case MESSAGE_TYPE_PING:
70
network_connection->send(MESSAGE_TYPE_PONG, "PONGOGOGO");
73
case MESSAGE_TYPE_PONG:
74
std::cerr << "sending join" << std::endl;
75
network_connection->send(MESSAGE_TYPE_JOIN, "I wanna join");
78
case MESSAGE_TYPE_SENDING_ACTIONS:
82
case MESSAGE_TYPE_SENDING_MAP:
86
case MESSAGE_TYPE_JOIN:
87
// FIXME: faulty server
92
void GameClient::gotScenario(const std::string &payload)
94
std::string file = "network.sav";
95
std::string path = File::getSavePath();
98
std::ofstream f(path.c_str());
102
game_scenario_received.emit(path);
108
bool loadAction(std::string tag, XML_Helper* helper)
110
if (tag == "action") {
111
Action* action = Action::handle_load(helper);
112
actions.push_back(action);
117
std::list<Action *> actions;
121
void GameClient::gotActions(const std::string &payload)
123
std::istringstream is(payload);
127
XML_Helper helper(&is);
128
helper.registerTag("action", sigc::mem_fun(loader, &ActionLoader::loadAction));
131
for (std::list<Action *>::iterator i = loader.actions.begin(),
132
end = loader.actions.end(); i != end; ++i)
135
std::cerr << "decoding action " << action->getType() << std::endl;
137
Player *p = Playerlist::getInstance()->getPlayer(action->getPlayer());
138
NetworkPlayer *np = dynamic_cast<NetworkPlayer *>(p);
141
std::cerr << "warning: ignoring action for player " << p << std::endl;
145
np->decodeAction(action);
148
for (std::list<Action *>::iterator i = loader.actions.begin(),
149
end = loader.actions.end(); i != end; ++i)