1 // Copyright (C) 2008 Ole Laursen
2 // Copyright (C) 2011, 2014, 2015, 2017 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_CLIENT_H
21 #define GAME_CLIENT_H
22 
23 #include <config.h>
24 
25 #include <list>
26 #include <glibmm.h>
27 #include <memory>
28 #include <sigc++/trackable.h>
29 #include <sigc++/signal.h>
30 class NetworkAction;
31 class NetworkHistory;
32 
33 #include "game-station.h"
34 
35 class NetworkConnection;
36 class Player;
37 
38 //! A remotely connected user in a networked game.
39 class GameClient: public GameStation
40 {
41 public:
42 
43   //! Returns the singleton instance.  Creates a new one if neccessary.
44   static GameClient* getInstance();
45 
46   //! Deletes the singleton instance.
47   static void deleteInstance();
48 
49   void start(Glib::ustring host, guint32 port, Glib::ustring profile_id, Glib::ustring nick);
50   void disconnect();
51   void request_seat_manifest();
52 
53   sigc::signal<void> client_connected;
54   sigc::signal<void> client_disconnected;
55   sigc::signal<void> client_forcibly_disconnected; //server went away
56   sigc::signal<void> client_could_not_connect;
57 
58   void sit_down (Player *player);
59   void stand_up (Player *player);
60   void change_name(Player *player, Glib::ustring name);
61   void change_type(Player *player, int type);
62   void chat(Glib::ustring message);
63 
getHost()64   Glib::ustring getHost() const{return d_host;};
getPort()65   guint32 getPort() const{return d_port;};
66 
67   void sendRoundOver();
68 
69 protected:
70   GameClient();
71   ~GameClient();
72 
73 private:
74   NetworkConnection* network_connection;
75   int player_id;
76 
77   void sit_or_stand (Player *player, bool sit);
78   void onConnected();
79   void onConnectionLost();
80   bool onGotMessage(int type, Glib::ustring message);
81   void on_torn_down();
82 
83   void onActionDone(Action *action, guint32 id);
84   void sendActions();
85 
86   void onHistoryDone(History *history, guint32 id);
87   void sendHistories();
88 
89   void gotTurnOrder (Glib::ustring payload);
90   void gotKillPlayer(Player *player);
91   void gotOffPlayer(Player *player);
92 
93   void sat_down(Player *player, Glib::ustring nickname);
94   void stood_up(Player *player, Glib::ustring nickname);
95   void name_changed (Player *player, Glib::ustring name);
96   void type_changed (Player *player, int type);
97 
98   bool on_ping_timeout();
99 
100   std::list<NetworkAction*> actions;
101   std::list<NetworkHistory*> histories;
102   //! A static pointer for the singleton instance.
103   static GameClient * s_instance;
104   bool d_connected;
105   Glib::ustring d_host;
106   guint32 d_port;
107   sigc::connection d_ping_timer;
108   bool first_ping;
109 };
110 
111 #endif
112