/* * ORXONOX - the hottest 3D action shooter ever to exist * > www.orxonox.net < * * * License notice: * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Author: * Sandro 'smerkli' Merkli * Co-authors: * ... * */ #include "ChatHistory.h" #ifndef TEST namespace orxonox { #endif /* is this necessary? if yes uncomment please :P */ //CreateFactory(ChatHistory); /* constructor */ #ifndef TEST ChatHistory(BaseObject* creator) : BaseObject(creator) #else ChatHistory::ChatHistory() #endif { /* register the object */ #ifndef TEST RegisterObject(ChatHistory); #endif this->hist_log_enabled = true; /* Read setting for logfiles */ if( hist_log_enabled ) /* NOTE Make this a check for the logfile setting */ { this->chat_hist_openlog(); /* push starting line */ this->chat_hist_logline( "--- Logfile opened ---" ); } /* Read setting for maximum number of lines and set limit */ this->hist_maxlines = 200; /* NOTE to be changed, 200 is just for testing */ } /* destructor */ ChatHistory::~ChatHistory() { chat_hist_closelog(); /* TODO clear list */ } /* react to incoming chat */ void ChatHistory::incomingChat(const std::string& message, unsigned int senderID) { /* sanity - check for valid senderID */ /* sanity - check for valid string format */ /* format the message and senderID into a line */ std::string buf = "" + senderID; /* NOTE to be changed */ /* TODO */ /* --> a) look up the actual name of the sender */ /* --> b) add sender name and string up with a separator * to make up the actual message */ /* add the line to the history */ this->chat_hist_addline( buf + ": " + message ); /* add the line to the log */ this->chat_hist_logline( buf + ": " + message ); } /* Synchronize logfile onto the hard drive */ /* MARK MARK */ int ChatHistory::syncLog() { //if( this->hist_logfile ) //this->hist_logfile.sync(); } /* add a line to this history */ int ChatHistory::chat_hist_addline( const std::string& toadd ) { /* crop history at the end if it's too large */ while( this->hist_buffer.size() > this->hist_maxlines+1 ) this->hist_buffer.pop_front(); /* push to the front of the history */ this->hist_buffer.push_back( toadd ); } /* log a line to a logfile */ int ChatHistory::chat_hist_logline( const std::string& toadd ) { /* TODO use already written class to batch write to file * so not every line gets written on its own */ /* output the line to the file if logging is enabled */ if( this->hist_log_enabled ) this->hist_logfile << toadd << std::endl; } /* open logfile */ int ChatHistory::chat_hist_openlog() { /* TODO: find out the name of the file to log to via settings * and set the this->hist_logfile_path variable to it */ this->hist_logfile.open( "/tmp/setsomepath.txt", std::fstream::out | std::fstream::app ); /* TODO check whether this works (not sure how you'd like it?) */ /* if it worked */ return 0; } /* close logfile */ void ChatHistory::chat_hist_closelog() { if( this->hist_logfile ) { this->chat_hist_logline( "--- Logfile closed ---" ); this->hist_logfile.close(); } } /* output history for debugging */ void ChatHistory::debug_printhist() { std::deque::iterator it; for( it = this->hist_buffer.begin(); it != this->hist_buffer.end(); ++it ) std::cout << *it << std::endl; std::cout << "Size: " << hist_buffer.size() << std::endl; } #ifndef TEST } #endif