1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Sandro 'smerkli' Merkli |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "ChatHistory.h" |
---|
30 | |
---|
31 | #ifndef TEST |
---|
32 | namespace orxonox |
---|
33 | { |
---|
34 | #endif |
---|
35 | |
---|
36 | /* constructor */ |
---|
37 | #ifndef TEST |
---|
38 | ChatHistory(BaseObject* creator) : BaseObject(creator) |
---|
39 | #else |
---|
40 | ChatHistory::ChatHistory() |
---|
41 | #endif |
---|
42 | { |
---|
43 | /* register the object */ |
---|
44 | #ifndef TEST |
---|
45 | RegisterObject(ChatHistory); |
---|
46 | #endif |
---|
47 | |
---|
48 | this->hist_log_enabled = true; |
---|
49 | |
---|
50 | /* Read setting for logfiles */ |
---|
51 | if( hist_log_enabled ) /* NOTE Make this a check for the logfile setting */ |
---|
52 | { this->chat_hist_openlog(); |
---|
53 | |
---|
54 | /* push starting line */ |
---|
55 | this->chat_hist_logline( "--- Logfile opened ---" ); |
---|
56 | } |
---|
57 | |
---|
58 | /* Read setting for maximum number of lines and set limit */ |
---|
59 | this->hist_maxlines = 200; /* NOTE to be changed, 200 is just for testing */ |
---|
60 | } |
---|
61 | |
---|
62 | /* destructor */ |
---|
63 | ChatHistory::~ChatHistory() |
---|
64 | { |
---|
65 | chat_hist_closelog(); |
---|
66 | |
---|
67 | /* clear list */ |
---|
68 | this->hist_buffer.clear(); |
---|
69 | } |
---|
70 | |
---|
71 | /* react to incoming chat */ |
---|
72 | void ChatHistory::incomingChat(const std::string& message, |
---|
73 | unsigned int senderID) |
---|
74 | { |
---|
75 | /* --> a) look up the actual name of the sender */ |
---|
76 | std::string text; |
---|
77 | |
---|
78 | #ifndef TEST |
---|
79 | if (senderID != CLIENTID_UNKNOWN) |
---|
80 | { |
---|
81 | std::string name = "unknown"; |
---|
82 | PlayerInfo* player = PlayerManager::getInstance().getClient(senderID); |
---|
83 | if (player) |
---|
84 | name = player->getName(); |
---|
85 | |
---|
86 | text = name + ": " + message; |
---|
87 | } |
---|
88 | else |
---|
89 | text = message; |
---|
90 | #else |
---|
91 | text = message; |
---|
92 | #endif |
---|
93 | |
---|
94 | /* add the line to the history */ |
---|
95 | this->chat_hist_addline( text ); |
---|
96 | |
---|
97 | /* add the line to the log */ |
---|
98 | this->chat_hist_logline( text ); |
---|
99 | } |
---|
100 | |
---|
101 | /* Synchronize logfile onto the hard drive */ /* MARK MARK */ |
---|
102 | int ChatHistory::syncLog() |
---|
103 | { |
---|
104 | //if( this->hist_logfile ) |
---|
105 | //this->hist_logfile.sync(); |
---|
106 | } |
---|
107 | |
---|
108 | /* add a line to this history */ |
---|
109 | int ChatHistory::chat_hist_addline( const std::string& toadd ) |
---|
110 | { |
---|
111 | /* crop history at the end if it's too large */ |
---|
112 | while( this->hist_buffer.size() > this->hist_maxlines+1 ) |
---|
113 | this->hist_buffer.pop_front(); |
---|
114 | |
---|
115 | /* push to the front of the history */ |
---|
116 | this->hist_buffer.push_back( toadd ); |
---|
117 | } |
---|
118 | |
---|
119 | /* log a line to a logfile */ |
---|
120 | int ChatHistory::chat_hist_logline( const std::string& toadd ) |
---|
121 | { |
---|
122 | /* output the line to the file if logging is enabled */ |
---|
123 | if( this->hist_log_enabled ) |
---|
124 | this->hist_logfile << toadd << std::endl; |
---|
125 | } |
---|
126 | |
---|
127 | /* open logfile */ |
---|
128 | int ChatHistory::chat_hist_openlog() |
---|
129 | { |
---|
130 | /* TODO: find out the name of the file to log to via settings |
---|
131 | * and set the this->hist_logfile_path variable to it |
---|
132 | */ |
---|
133 | #ifndef TEST |
---|
134 | this->hist_logfile.open( PathConfig::getInstance().getLogPathString() + |
---|
135 | "chatlog.log", |
---|
136 | std::fstream::out | std::fstream::app ); |
---|
137 | #else |
---|
138 | this->hist_logfile.open( "/tmp/chatlog.log", |
---|
139 | std::fstream::out | std::fstream::app ); |
---|
140 | #endif |
---|
141 | |
---|
142 | /* TODO check whether this works (not sure how you'd like it?) */ |
---|
143 | if( !this->hist_logfile ) |
---|
144 | { this->hist_log_enabled = false; |
---|
145 | #ifndef TEST |
---|
146 | COUT(2) << "Warning: Could not open logfile." << std::endl; |
---|
147 | #endif |
---|
148 | } |
---|
149 | |
---|
150 | /* if it worked */ |
---|
151 | return 0; |
---|
152 | } |
---|
153 | |
---|
154 | /* close logfile */ |
---|
155 | void ChatHistory::chat_hist_closelog() |
---|
156 | { |
---|
157 | if( this->hist_logfile ) |
---|
158 | { this->chat_hist_logline( "--- Logfile closed ---" ); |
---|
159 | this->hist_logfile.close(); |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | /* output history for debugging */ |
---|
164 | void ChatHistory::debug_printhist() |
---|
165 | { |
---|
166 | /* create deque iterator */ |
---|
167 | std::deque<std::string>::iterator it; |
---|
168 | |
---|
169 | /* output all the strings */ |
---|
170 | for( it = this->hist_buffer.begin(); it != this->hist_buffer.end(); |
---|
171 | ++it ) |
---|
172 | std::cout << *it << std::endl; |
---|
173 | |
---|
174 | /* output size */ |
---|
175 | std::cout << "Size: " << hist_buffer.size() << std::endl; |
---|
176 | } |
---|
177 | |
---|
178 | #ifndef TEST |
---|
179 | } |
---|
180 | #endif |
---|