1 // Copyright (C) 2008 Ole Laursen
2 // Copyright (C) 2008 Ben Asselstine
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 3 of the License, or
7 // (at your option) any later version.
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 Library General Public License for more details.
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., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include "game-client-decoder.h"
26 #include "network-action.h"
27 #include "network-history.h"
28 #include "network_player.h"
29 #include "playerlist.h"
30 #include "xmlhelper.h"
31 #include "GameScenario.h"
35 GameClientDecoder::GameClientDecoder()
39 GameClientDecoder::~GameClientDecoder()
43 void GameClientDecoder::gotScenario(const std::string &payload)
45 std::string file = "clientnetwork.sav";
46 std::string path = File::getSavePath();
49 std::ofstream f(path.c_str());
53 game_scenario_received.emit(path);
56 int GameClientDecoder::decodeActions(std::list<NetworkAction*> actions,
60 for (std::list<NetworkAction *>::iterator i = actions.begin(),
61 end = actions.end(); i != end; ++i)
63 NetworkAction *action = *i;
64 std::string desc = action->toString();
66 Player *p = action->getOwner();
67 //if (p != player && player)
69 std::cerr << "decoding action: " << desc << std::endl;
70 NetworkPlayer *np = static_cast<NetworkPlayer *>(p);
73 std::cerr << "warning: ignoring action for player " << p << std::endl;
77 np->decodeAction(action->getAction());
78 if (action->getAction()->getType() == Action::PLAYER_RENAME)
79 remote_player_named.emit(action->getOwner());
80 else if (action->getAction()->getType() == Action::END_TURN)
81 remote_player_moved.emit((*actions.back()).getOwner());
85 for (std::list<NetworkAction *>::iterator i = actions.begin(),
86 end = actions.end(); i != end; ++i)
91 void GameClientDecoder::gotActions(const std::string &payload)
93 std::istringstream is(payload);
97 XML_Helper helper(&is);
98 helper.registerTag(Action::d_tag, sigc::mem_fun(loader, &ActionLoader::loadAction));
99 helper.registerTag(NetworkAction::d_tag, sigc::mem_fun(loader, &ActionLoader::loadAction));
102 decodeActions(loader.actions, Playerlist::getActiveplayer());
106 int GameClientDecoder::decodeHistories(std::list<NetworkHistory *> histories)
109 for (std::list<NetworkHistory *>::iterator i = histories.begin(),
110 end = histories.end(); i != end; ++i)
112 NetworkHistory *history = *i;
113 std::string desc = history->toString();
114 std::cerr << "received history: " << desc << std::endl;
116 //just add it to the player's history list.
117 Player *p = Playerlist::getInstance()->getActiveplayer();
118 p->getHistorylist()->push_back(History::copy(history->getHistory()));
120 if (history->getHistory()->getType() == History::PLAYER_VANQUISHED)
121 remote_player_died.emit(history->getOwner());
124 for (std::list<NetworkHistory *>::iterator i = histories.begin(),
125 end = histories.end(); i != end; ++i)
130 void GameClientDecoder::gotHistories(const std::string &payload)
132 std::istringstream is(payload);
134 HistoryLoader loader;
136 XML_Helper helper(&is);
137 helper.registerTag(History::d_tag, sigc::mem_fun(loader, &HistoryLoader::loadHistory));
138 helper.registerTag(NetworkHistory::d_tag, sigc::mem_fun(loader, &HistoryLoader::loadHistory));
141 decodeHistories(loader.histories);