1 // Copyright (C) 2008 Ole Laursen
2 // Copyright (C) 2008, 2011, 2014 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 #pragma once
20 #ifndef GAME_SERVER_H
21 #define GAME_SERVER_H
22 
23 #include <config.h>
24 
25 #include <memory>
26 #include <list>
27 #include <sigc++/trackable.h>
28 #include <sigc++/signal.h>
29 
30 #include "game-station.h"
31 
32 class NetworkServer;
33 class Participant;
34 class Action;
35 class History;
36 class Player;
37 class XML_Helper;
38 class GameScenario;
39 class GameParameters;
40 
41 //! A networked game server.  Talks to GameClient objects.
42 class GameServer: public GameStation
43 {
44 public:
45 
46   //! Returns the singleton instance.  Creates a new one if neccessary.
47   static GameServer * getInstance();
48 
49   //! Deletes the singleton instance.
50   static void deleteInstance();
51 
52   bool isListening();
53   void start(GameScenario *game_scenario, int port, Glib::ustring profile_id, Glib::ustring nick);
54 
55   void sit_down (Player *player);
56   void stand_up (Player *player);
57   void name_change (Player *player, Glib::ustring name);
58   void type_change (Player *player, int type);
59   void chat(Glib::ustring message);
60   void sendTurnOrder();
61   void sendKillPlayer(Player *player);
62   void sendOffPlayer(Player *player);
63   void notifyClientsGameMayBeginNow();
64   void notifyRoundOver();
65   sigc::signal<void> remote_participant_connected;
66   sigc::signal<void> remote_participant_disconnected;
67   sigc::signal<Player*> get_next_player;
68   sigc::signal<void, int> port_in_use;
69 
setGameScenario(GameScenario * scenario)70   void setGameScenario(GameScenario *scenario) {d_game_scenario = scenario;};
71 
72   bool sendRoundStart();
73   bool sendNextPlayer();
74   bool gameHasBegun();
75 
76   void on_player_finished_turn(Player *player);
77   void on_turn_aborted();
78   bool check_end_of_round();
79 protected:
80   GameServer();
81   ~GameServer();
82 
83 private:
84   GameScenario *d_game_scenario;
85   bool d_game_has_begun;
86   void onActionDone(Action *action, guint32 id);
87   void onHistoryDone(History *history, guint32 id);
88 
89   void join(void *conn, Glib::ustring payload);
90   void notifyJoin (Glib::ustring nickname);
91   void depart(void *conn);
92   void notifyDepart (void *conn, Glib::ustring nickname);
93   void sit(void *conn, Player *player, Glib::ustring nickname);
94   void notifySit(Player *player, Glib::ustring nickname);
95   void stand(void *conn, Player *player, Glib::ustring nickname);
96   void notifyStand(Player *player, Glib::ustring nickname);
97   void change_name(void *conn, Player *player, Glib::ustring name);
98   void notifyNameChange(Player *player, Glib::ustring name);
99   void change_type(void *conn, Player *player, int type);
100   void notifyTypeChange(Player *player, int type);
101   void gotRemoteActions(void *conn, const Glib::ustring &payload);
102   void gotRemoteHistory(void *conn, const Glib::ustring &payload);
103   void notifyChat(Glib::ustring message);
104 
105   void sendMap(Participant *part);
106   void sendSeats(void *conn);
107   void sendSeat(void *conn, GameParameters::Player player, Glib::ustring nickname);
108   void sendChatRoster(void *conn);
109 
110   void sendActions(Participant *part);
111   void sendHistories(Participant *part);
112 
113   std::unique_ptr<NetworkServer> network_server;
114 
115   std::list<Participant *> participants;
116   std::list<GameParameters::Player> players_seated_locally;
117   std::map<guint32, bool> id_end_turn; //whether local players ended their turn
118 
119   Participant * play_by_mail_participant;
120 
121   Participant *findParticipantByConn(void *conn);
122   Participant *findParticipantByNick(Glib::ustring nickname);
123   Participant *findParticipantByPlayerId(guint32 id);
124 
125   bool onGotMessage(void *conn, int type, Glib::ustring message);
126   void onConnectionLost(void *conn);
127   void onConnectionMade(void *conn);
128   bool dumpActionsAndHistories(XML_Helper *helper);
129   bool dumpActionsAndHistories(XML_Helper *helper, Player *player);
130 
131   void gotChat(void *conn, Glib::ustring message);
132 
133   bool player_already_sitting(Player *p);
134 
135   bool add_to_player_list(std::list<GameParameters::Player> &list, guint32 id,
136                           Glib::ustring name, guint32 type);
137   bool remove_from_player_list(std::list<GameParameters::Player> &list, guint32 id);
138   bool update_player_type (std::list<GameParameters::Player> &list, guint32 id, guint32 type);
139   bool update_player_name (std::list<GameParameters::Player> &list, guint32 id, Glib::ustring name);
140 
141   void syncLocalPlayers();
142 
143   Glib::ustring make_nickname_unique(Glib::ustring nickname);
144 
145   void onLocalNonNetworkedActionDone(NetworkAction *action);
146   void onLocalNonNetworkedHistoryDone(NetworkHistory *history);
147   void onLocalNetworkedHistoryDone(NetworkHistory *history);
148 
149   bool nextTurn();
150 
151 
152   void remove_all_participants();
153 
154   bool d_stop;
155   //! A static pointer for the singleton instance.
156   static GameServer * s_instance;
157 };
158 
159 #endif
160