Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/chat/src/orxonox/ChatHistory.cc @ 6690

Last change on this file since 6690 was 6690, checked in by smerkli, 15 years ago

Few changes before the questions, completing doxy stuff.

  • Property svn:eol-style set to native
File size: 4.3 KB
Line 
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
32namespace orxonox
33{
34#endif
35  /* QUESTION */
36  /* is this necessary? if yes uncomment please :P */
37  //CreateFactory(ChatHistory);
38
39  /* constructor */
40  #ifndef TEST
41  ChatHistory(BaseObject* creator) : BaseObject(creator) 
42  #else
43  ChatHistory::ChatHistory()
44  #endif
45  {
46    /* register the object */
47    #ifndef TEST
48    RegisterObject(ChatHistory);
49    #endif
50
51    this->hist_log_enabled = true;
52
53    /* Read setting for logfiles */
54    if( hist_log_enabled ) /* NOTE Make this a check for the logfile setting */
55    { this->chat_hist_openlog();
56
57      /* push starting line */
58      this->chat_hist_logline( "--- Logfile opened ---" );
59    }
60
61    /* Read setting for maximum number of lines and set limit */
62    this->hist_maxlines = 200; /* NOTE to be changed, 200 is just for testing */
63  }
64
65  /* destructor */
66  ChatHistory::~ChatHistory()
67  {
68    chat_hist_closelog();
69   
70    /* clear list */
71    this->hist_buffer.clear();
72  }
73
74  /* react to incoming chat */
75  void ChatHistory::incomingChat(const std::string& message, 
76    unsigned int senderID)
77  {
78    /* QUESTION */
79    /* sanity - check for valid senderID */
80
81    /* TODO make a correct name */
82    std::string name = "" + senderID; /* NOTE to be changed */
83
84    /* TODO */
85    /* QUESTION */
86    /* --> a) look up the actual name of the sender */
87    /* --> b) add sender name and string up with a separator
88     *    to make up the actual message
89     */
90
91    /* add the line to the history */
92    this->chat_hist_addline( name + ": " + message );
93
94    /* add the line to the log */
95    this->chat_hist_logline( name + ": " + message );
96  } 
97
98  /* Synchronize logfile onto the hard drive */ /* MARK MARK */
99  int ChatHistory::syncLog()
100  {
101    //if( this->hist_logfile )
102      //this->hist_logfile.sync();
103  }
104
105  /* add a line to this history */
106  int ChatHistory::chat_hist_addline( const std::string& toadd )
107  {
108    /* crop history at the end if it's too large */
109    while( this->hist_buffer.size() > this->hist_maxlines+1 )
110      this->hist_buffer.pop_front();
111
112    /* push to the front of the history */
113    this->hist_buffer.push_back( toadd );
114  }
115
116  /* log a line to a logfile */
117  int ChatHistory::chat_hist_logline( const std::string& toadd )
118  { 
119    /* output the line to the file if logging is enabled */
120    if( this->hist_log_enabled )
121      this->hist_logfile << toadd << std::endl;
122  }
123
124  /* open logfile */
125  int ChatHistory::chat_hist_openlog()
126  {
127    /* QUESTION */
128    /* TODO: find out the name of the file to log to via settings
129     *       and set the this->hist_logfile_path variable to it
130     */
131    this->hist_logfile.open( "/tmp/setsomepath.txt", 
132      std::fstream::out | std::fstream::app );
133
134    /* QUESTION */
135    /* TODO check whether this works (not sure how you'd like it?) */
136
137    /* if it worked */
138    return 0;
139  }
140
141  /* close logfile */
142  void ChatHistory::chat_hist_closelog()
143  {
144    if( this->hist_logfile )
145    { this->chat_hist_logline( "--- Logfile closed ---" );
146      this->hist_logfile.close();
147    }
148  }
149
150  /* output history for debugging */
151  void ChatHistory::debug_printhist()
152  {
153    /* create deque iterator */
154    std::deque<std::string>::iterator it;
155
156    /* output all the strings */
157    for( it = this->hist_buffer.begin(); it != this->hist_buffer.end();
158      ++it )
159      std::cout << *it << std::endl;
160
161    /* output size */
162    std::cout << "Size: " << hist_buffer.size() << std::endl;
163  }
164
165#ifndef TEST
166}
167#endif
Note: See TracBrowser for help on using the repository browser.