initial commit, lordsawar source, slightly modified
[lordsawar] / src / game-server.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_SERVER_H
20 #define GAME_SERVER_H
21
22 #include "config.h"
23
24 #include <memory>
25 #include <list>
26 #include <sigc++/trackable.h>
27 #include <sigc++/signal.h>
28
29 #include "network-common.h"
30 #include "game-station.h"
31
32 class NetworkServer;
33 class Participant;
34 class NetworkAction;
35 class NetworkHistory;
36 class Player;
37 class XML_Helper;
38 class GameScenario;
39
40 class GameServer: public GameStation
41 {
42 public:
43         
44   //! Returns the singleton instance.  Creates a new one if neccessary.
45   static GameServer * getInstance();
46
47   //! Deletes the singleton instance.
48   static void deleteInstance();
49
50   bool isListening();
51   void start(GameScenario *game_scenario, int port, std::string nick);
52
53   void sit_down (Player *player);
54   void stand_up (Player *player);
55   void chat(std::string message);
56   void sendTurnOrder();
57   void sendKillPlayer(Player *player);
58   sigc::signal<void> remote_participant_connected;
59   sigc::signal<void> remote_participant_disconnected;
60
61   void setGameScenario(GameScenario *scenario) {d_game_scenario = scenario;};
62
63 protected:
64   GameServer();
65   ~GameServer();
66
67 private:
68   GameScenario *d_game_scenario;
69   void onActionDone(NetworkAction *action);
70   void onHistoryDone(NetworkHistory *history);
71
72   void join(void *conn, std::string payload);
73   void notifyJoin (std::string nickname);
74   void depart(void *conn);
75   void notifyDepart (void *conn, std::string nickname);
76   void sit(void *conn, Player *player, std::string nickname);
77   void notifySit(Player *player, std::string nickname);
78   void stand(void *conn, Player *player, std::string nickname);
79   void notifyStand(Player *player, std::string nickname);
80   void gotRemoteActions(void *conn, const std::string &payload);
81   void gotRemoteHistory(void *conn, const std::string &payload);
82   void notifyChat(std::string message);
83
84   void sendMap(Participant *part);
85   void sendSeats(void *conn);
86   void sendChatRoster(void *conn);
87
88   void checkRoundOver();
89
90   void sendActions(Participant *part);
91   void sendHistories(Participant *part);
92
93   std::auto_ptr<NetworkServer> network_server;
94
95   std::list<Participant *> participants;
96   std::list<guint32> players_seated_locally;
97
98   Participant * play_by_mail_participant;
99
100   Participant *findParticipantByConn(void *conn);
101   
102   void onGotMessage(void *conn, MessageType type, std::string message);
103   void onConnectionLost(void *conn);
104   void onConnectionMade(void *conn);
105   bool dumpActionsAndHistories(XML_Helper *helper);
106   bool dumpActionsAndHistories(XML_Helper *helper, Player *player);
107
108   void gotChat(void *conn, std::string message);
109   void gotRoundOver(void *conn);
110
111   bool player_already_sitting(Player *p);
112
113   bool add_to_player_list(std::list<guint32> &list, guint32 id);
114   bool remove_from_player_list(std::list<guint32> &list, guint32 id);
115   //! A static pointer for the singleton instance.
116   static GameServer * s_instance;
117 };
118
119 #endif