initial commit, lordsawar source, slightly modified
[lordsawar] / src / game-client-decoder.h
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 #ifndef GAME_CLIENT_DECODER_H
20 #define GAME_CLIENT_DECODER_H
21
22 #include "config.h"
23 #include "chat-client.h"
24
25 #include <list>
26 #include <memory>
27 #include <sigc++/trackable.h>
28 #include <sigc++/signal.h>
29 class XML_Helper;
30 #include "network-action.h"
31 #include "network-history.h"
32
33 class GameScenario;
34 class Player;
35
36 class GameClientDecoder: public ChatClient
37 {
38 public:
39   GameClientDecoder();
40   ~GameClientDecoder();
41
42   sigc::signal<void, std::string> game_scenario_received;
43   sigc::signal<void, Player *> remote_player_moved;
44   sigc::signal<void, Player *> remote_player_named;
45   sigc::signal<void, Player *> remote_player_died;
46
47 protected:
48   class ActionLoader 
49     {
50   public:
51       bool loadAction(std::string tag, XML_Helper* helper)
52         {
53           if (tag == Action::d_tag)
54             {
55               NetworkAction *action = &*actions.back();
56               action->setAction(Action::handle_load(helper));
57               return true;
58             }
59           if (tag == NetworkAction::d_tag) 
60             {
61               NetworkAction * action = new NetworkAction(helper);
62               actions.push_back(action);
63               return true;
64             }
65           return false;
66         }
67
68       std::list<NetworkAction *> actions;
69     };
70   class HistoryLoader 
71     {
72   public:
73       bool loadHistory(std::string tag, XML_Helper* helper)
74         {
75           if (tag == History::d_tag)
76             {
77               NetworkHistory *history = &*histories.back();
78               history->setHistory(History::handle_load(helper));
79               return true;
80             }
81           if (tag == NetworkHistory::d_tag) 
82             {
83               NetworkHistory* history = new NetworkHistory(helper);
84               histories.push_back(history);
85               return true;
86             }
87           return false;
88         }
89
90       std::list<NetworkHistory *> histories;
91     };
92
93
94 protected:
95   void gotActions(const std::string &payload);
96   void gotHistories(const std::string &payload);
97   void gotScenario(const std::string &payload);
98   int decodeActions(std::list<NetworkAction*> actions,
99                     Player *player);
100   int decodeHistories(std::list<NetworkHistory*> histories);
101
102 };
103
104 #endif