1 // Copyright (C) 2008, 2011, 2014 Ben Asselstine
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU Library General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 // 02110-1301, USA.
17
18 #include <iostream>
19 #include <sstream>
20 #include <list>
21
22 #include "game-station.h"
23 #include "network-common.h"
24
25 #include "network-action.h"
26 #include "network-history.h"
27
GameStation()28 GameStation::GameStation()
29 {
30 }
31
clearNetworkActionlist(std::list<NetworkAction * > & a)32 void GameStation::clearNetworkActionlist(std::list<NetworkAction*> &a)
33 {
34 for (std::list<NetworkAction*>::iterator it = a.begin();
35 it != a.end(); it++)
36 {
37 delete (*it);
38 }
39 a.clear();
40 }
41
clearNetworkHistorylist(std::list<NetworkHistory * > & h)42 void GameStation::clearNetworkHistorylist(std::list<NetworkHistory*> &h)
43 {
44 for (std::list<NetworkHistory*>::iterator it = h.begin();
45 it != h.end(); it++)
46 {
47 delete (*it);
48 }
49 h.clear();
50 }
51
listenForLocalEvents(Player * p)52 void GameStation::listenForLocalEvents(Player *p)
53 {
54 sigc::connection connection;
55 connection = p->acting.connect(sigc::mem_fun(this,
56 &GameStation::onActionDone));
57 action_listeners[p->getId()] = connection;
58 connection = p->history_written.connect
59 (sigc::mem_fun(this, &GameStation::onHistoryDone));
60 history_listeners[p->getId()] = connection;
61 }
62
stopListeningForLocalEvents()63 void GameStation::stopListeningForLocalEvents()
64 {
65 std::map<guint32, sigc::connection>::iterator i = action_listeners.begin();
66 for (; i != action_listeners.end(); i++)
67 (*i).second.disconnect();
68 action_listeners.clear();
69 std::map<guint32, sigc::connection>::iterator j = history_listeners.begin();
70 for (; j != history_listeners.end(); j++)
71 (*j).second.disconnect();
72 history_listeners.clear();
73 }
74
stopListeningForLocalEvents(Player * p)75 void GameStation::stopListeningForLocalEvents(Player *p)
76 {
77 sigc::connection connection;
78 std::map<guint32, sigc::connection>::iterator it;
79 it = action_listeners.find(p->getId());
80 if (it != action_listeners.end())
81 {
82 connection = (*it).second;
83 connection.disconnect();
84 action_listeners.erase(it);
85 }
86 it = history_listeners.find(p->getId());
87 if (it != history_listeners.end())
88 {
89 connection = (*it).second;
90 connection.disconnect();
91 history_listeners.erase(it);
92 }
93 }
94
get_message_lobby_activity(Glib::ustring payload,guint32 & player_id,gint32 & action,bool & reported,Glib::ustring & remainder)95 bool GameStation::get_message_lobby_activity (Glib::ustring payload,
96 guint32 &player_id,
97 gint32 &action, bool &reported,
98 Glib::ustring &remainder)
99 {
100 std::stringstream spayload;
101 spayload.str(payload);
102 spayload >> player_id;
103 if (player_id >= MAX_PLAYERS + 1)
104 return false;
105 spayload >> action;
106 switch (action)
107 {
108 case LOBBY_MESSAGE_TYPE_SIT:
109 case LOBBY_MESSAGE_TYPE_CHANGE_NAME:
110 case LOBBY_MESSAGE_TYPE_STAND:
111 case LOBBY_MESSAGE_TYPE_CHANGE_TYPE:
112 break;
113 default:
114 return false;
115 }
116 spayload >> reported;
117 if (reported != 0 && reported != 1)
118 return false;
119 //okay, the rest of the stringstream is a nickname.
120 char buffer[1024];
121 memset (buffer, 0, sizeof (buffer));
122 spayload.get();
123 spayload.rdbuf()->sgetn(buffer, sizeof (buffer));
124 remainder = Glib::ustring (buffer);
125 return true;
126 }
127 // End of file
128