initial commit, lordsawar source, slightly modified
[lordsawar] / src / game-client-decoder.cpp
1 // Copyright (C) 2008 Ole Laursen
2 // Copyright (C) 2008 Ben Asselstine
3 //
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.
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 Library 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., 51 Franklin Street, Fifth Floor, Boston, MA 
17 //  02110-1301, USA.
18
19 #include <iostream>
20 #include <fstream>
21
22 #include "game-client-decoder.h"
23
24 #include "File.h"
25 #include "action.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"
32
33
34
35 GameClientDecoder::GameClientDecoder()
36 {
37 }
38
39 GameClientDecoder::~GameClientDecoder()
40 {
41 }
42
43 void GameClientDecoder::gotScenario(const std::string &payload)
44 {
45   std::string file = "clientnetwork.sav";
46   std::string path = File::getSavePath();
47   path += file;
48   
49   std::ofstream f(path.c_str());
50   f << payload;
51   f.close();
52   
53   game_scenario_received.emit(path);
54 }
55
56 int GameClientDecoder::decodeActions(std::list<NetworkAction*> actions,
57                                      Player *player)
58 {
59   int count = 0;
60   for (std::list<NetworkAction *>::iterator i = actions.begin(),
61        end = actions.end(); i != end; ++i)
62   {
63     NetworkAction *action = *i;
64     std::string desc = action->toString();
65     
66     Player *p = action->getOwner();
67     //if (p != player && player)
68       //continue;
69     std::cerr << "decoding action: " << desc << std::endl;
70     NetworkPlayer *np = static_cast<NetworkPlayer *>(p);
71
72     if (!np) {
73       std::cerr << "warning: ignoring action for player " << p << std::endl;
74       continue;
75     }
76
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());
82     count++;
83   }
84
85   for (std::list<NetworkAction *>::iterator i = actions.begin(),
86        end = actions.end(); i != end; ++i)
87     delete *i;
88   return count;
89 }
90
91 void GameClientDecoder::gotActions(const std::string &payload)
92 {
93   std::istringstream is(payload);
94
95   ActionLoader loader;
96   
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));
100   helper.parse();
101
102   decodeActions(loader.actions, Playerlist::getActiveplayer());
103 }
104
105
106 int GameClientDecoder::decodeHistories(std::list<NetworkHistory *> histories)
107 {
108   int count = 0;
109   for (std::list<NetworkHistory *>::iterator i = histories.begin(),
110        end = histories.end(); i != end; ++i)
111   {
112     NetworkHistory *history = *i;
113     std::string desc = history->toString();
114     std::cerr << "received history: " << desc << std::endl;
115     
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()));
119     count++;
120     if (history->getHistory()->getType() == History::PLAYER_VANQUISHED)
121       remote_player_died.emit(history->getOwner());
122   }
123
124   for (std::list<NetworkHistory *>::iterator i = histories.begin(),
125        end = histories.end(); i != end; ++i)
126     delete *i;
127   return count;
128 }
129
130 void GameClientDecoder::gotHistories(const std::string &payload)
131 {
132   std::istringstream is(payload);
133
134   HistoryLoader loader;
135   
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));
139   helper.parse();
140
141   decodeHistories(loader.histories);
142 }