Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6688 was 6688, checked in by smerkli, 14 years ago

Completed and tested the code as far as I could, now preparing questions.

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