///////////////////////////////////////////////////////////////////////////// // Name: room.cpp // tag: Handle a room of aBridge tables. // Author: David Roundy // Modified by: // Copyright: (c) 2002 David Roundy // Licence: GPL //--------------------------------------------------------------------------- // Last modified: Feb 3, 2002 /* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ ///////////////////////////////////////////////////////////////////////////// // For compilers that support precompilation, includes . #include #ifdef __BORLANDC__ #pragma hdrstop #endif #ifndef WX_PRECOMP #include #include #include #endif #include "abridge.h" #include "colors.h" #include "irc.h" #include "room.h" #include "table.h" #include "globals.h" #include "tables.h" #include "speech.h" #include "prefmacros.h" #include "playerdata.h" #include "preferences.h" #include "ftpfilecache.h" #include "lasttrick.h" #include "scorewin.h" #include "bidreview.h" #include "bidwindow.h" #include "abridgemenus.h" #include "config.h" #include "debug.h" // include config.h for VERSION #include "config.h" BEGIN_EVENT_TABLE(aBridgeRoom, wxFrame) EVT_MENU(NEW_TABLE, aBridgeRoom::MenuNewTable) EVT_CLOSE(aBridgeRoom::OnCloseWindow) EVT_TEXT_ENTER(TALK, aBridgeRoom::Talk) EVT_SET_FOCUS(aBridgeRoom::SetTalkFocus) EVT_IRC_MESSAGE(aBridgeRoom::ProcessIRCmessage) EVT_BRIDGETABLES(aBridgeRoom::ProcessTablesEvent) EVT_IRC_JOINED(aBridgeRoom::HandleSomeoneJoined) EVT_IRC_IJOINED(aBridgeRoom::HandleIJoined) END_EVENT_TABLE() // My frame constructor aBridgeRoom::aBridgeRoom(wxFrame* frame, char* title, int x, int y, int w, int h, Irc *the_irc, wxString the_room) : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h)) { #ifdef __WXMAC__ // we need this in order to allow the about menu relocation, // since ABOUT is not the default id of the about menu // wxApp::s_macAboutMenuItemId = ABOUT ; #endif // set the icon #ifdef __WXMSW__ SetIcon(wxIcon("CardsIcon")); #else #ifdef GTK_TBD SetIcon(wxIcon(Cards_bits, Cards_width, Cards_height)); #endif #endif // Create the chat text windows... m_log = new wxTextCtrl( this, -1, "", wxPoint(0,0), wxSize(100,100), wxTE_READONLY | wxTE_MULTILINE ); delete wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) ); m_talk = new wxTextCtrl( this, TALK, "", wxPoint(0,0), wxSize(100,-1), wxTE_PROCESS_ENTER ); // Start up the IRC object itself... m_irc = the_irc->Join(the_room, this, m_log); // Make a menu bar wxMenu* helpMenu = new wxMenu; helpMenu->Append(ABOUT, "&About aBridge...", "Displays program version information"); wxMenu* gameMenu = new wxMenu; gameMenu->Append(NEW_TABLE, "&New Table\tCtrl-N", "Create a new table"); #ifdef __WXMAC__ gameMenu->Append(wxID_CLOSE, "Close Table\tCtrl-W", "Close window"); #else gameMenu->Append(EXIT, "E&xit\tCtrl-Q", "Exits aBridge"); #endif wxMenu* editMenu = new wxMenu; editMenu->Append(PREFERENCES, "P&references...", "Configure user preferences"); m_menuBar = new wxMenuBar; m_menuBar->Append(gameMenu, "&Game"); m_menuBar->Append(editMenu, "&Settings"); #ifdef __WXMAC__ wxMenu* tableMenu = new wxMenu; tableMenu->Append(SHOWBID, "Show &Bids", "Displays scores"); tableMenu->Append(SHOWSCORES, "Show Scores", "Displays scores"); tableMenu->Append(SHOWLAST, "Show Last Trick", "Displays last trick"); tableMenu->MacEnableMenu(false); m_menuBar->Append(tableMenu, "&Table"); wxMenu* actionMenu = new wxMenu; actionMenu->Append(CLAIM, "&Claim", "Claim tricks"); actionMenu->MacEnableMenu(false); m_menuBar->Append(actionMenu, "&Action"); #endif m_menuBar->Append(helpMenu, "&Help"); SetMenuBar(m_menuBar); #ifdef __WXMAC__ m_menuBar->MacInstallMenuBar(); #endif m_tables = new aBridgeTables(this, 0, 0, 820, 400); wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL ); sizer->Add( m_tables, 1, wxEXPAND ); sizer->Add( m_log, 0, wxEXPAND ); sizer->Add( m_talk, 0, wxEXPAND ); sizer->SetSizeHints(this); m_cache = new ftpFileCache(GetCacheDir(), "ftp.abridgegame.org", "abridge", "abridge"); aBridgePlayerData::SetPlayerInfo(player_name(), GetMyInfo()); m_player_pic_md5 = UploadPlayerPic(); SetAutoLayout( TRUE ); SetSizer( sizer ); LastTrick::InitStaticVariables(); ScoreWin::InitStaticVariables(); BidReview::InitStaticVariables(); BidWindow::InitStaticVariables(); m_talk->SetFocus(); } aBridgeRoom::~aBridgeRoom() { delete wxLog::SetActiveTarget( new wxLogTextCtrl( NULL ) ); delete m_irc; } void aBridgeRoom::SayString(wxString str) { m_irc->Send(str); } void aBridgeRoom::OnCloseWindow(wxCloseEvent& event) { wxWindowListNode *node = GetChildren().GetFirst(); while (node) { wxFrame *t = wxDynamicCast(node->GetData(), wxFrame); if (t) t->Destroy(); node = node->GetNext(); } this->Destroy(); } void aBridgeRoom::Preferences(wxCommandEvent&) { aBridgePreferences *prefs = new aBridgePreferences(this); prefs->Show(true); } void aBridgeRoom::MenuNewTable(wxCommandEvent&) { wxString the_table = "#abt-"; wxString name_part = player_name().BeforeFirst('_').Left(20); the_table << name_part << "-" << (wxGetUTCTime() % 1000); wxTextEntryDialog *dlg = new wxTextEntryDialog(NULL, "What shall I call the table?", "Table name", "aBridge Table", wxOK); dlg->ShowModal(); wxString my_topic = dlg->GetValue(); delete dlg; NewTable(the_table, my_topic); DebugMsg("Just called NewTable in aBridgeRoom::MenuNewTable"); } void aBridgeRoom::NewTable(wxString tn, wxString topic) { wxString tablename = tn; irc_name(tablename); if (!m_irc->Mother()->AmInChannel(tablename)) { JoinTable(tablename); m_irc->SetTopic(topic, tablename); m_irc->SendMsg("NEWTABLE", tablename); } else { // we already are in that table... this may be a bug, in which we // failed to delete the ircchannel, but better not to try to create // another if we're still in the first one. DebugMsg("You silly, we're already in that channel!"); } } void aBridgeRoom::JoinTable(wxString tn) { wxString tablename = tn; irc_name(tablename); if (!m_irc->Mother()->AmInChannel(tablename)) { aBridgeTable *this_table = new aBridgeTable(this,"aBridge--" + player_name(), -1, -1, 600, 650, m_irc->Mother(), tablename); this_table->Show(TRUE); SendTablesEvent("JOINEDTABLE", tablename); } else { // we already are in that table... this may be a bug, in which we // failed to delete the ircchannel, but better not to try to create // another if we're still in the first one. DebugMsg("You silly, we're already in that channel!"); } } void aBridgeRoom::SetTalkFocus(wxFocusEvent& evt) { m_talk->SetFocus(); } void aBridgeRoom::Talk(wxCommandEvent&) { wxString str = m_talk->GetLineText(0) + "\n";; SayString(m_talk->GetLineText(0) + "\n"); m_log->AppendText(player_name() + "> " + m_talk->GetLineText(0) + "\n"); m_talk->Clear(); } void aBridgeRoom::SendTablesEvent(const wxString &type, const wxString &msg) { TablesEvent my_event( this, type, msg); wxPostEvent(m_tables->GetEventHandler(), my_event); // if ( m_tables->GetEventHandler()->ProcessEvent( my_event ) == FALSE ) // wxLogWarning( "Room: Could not process sendtables event! "+ // type +" "+msg); } void aBridgeRoom::ProcessIRCmessage(IrcMsgEvent &evt) { wxString type = evt.GetType(); wxString from = evt.GetFrom(); wxString msg = evt.GetMyMessage(); if (type.IsSameAs("TABLE")) { // A new table has been created! SendTablesEvent("TABLE", msg); m_irc->AskTopic(msg); } else if (type.IsSameAs("TABLECLOSED")) { SendTablesEvent("TABLECLOSED", msg); } else if (type.IsSameAs("TOPIC")) { SendTablesEvent("TOPIC", from + ":" + msg); } else if (type.IsSameAs("SIT")) { SendTablesEvent("SIT", msg); } else if (type.IsSameAs("STAND")) { SendTablesEvent("STAND", msg); } else if (type.IsSameAs("PART")) { SendTablesEvent("PART", from); } else if (type.IsSameAs("PLAYERINFO")) { wxString name = msg.BeforeFirst(':'); wxString info = msg.AfterFirst(':'); info.Replace("\\n","\n"); aBridgePlayerData::SetPlayerInfo(name, info); } else if (type.IsSameAs("PLAYERPIC")) { wxString name = msg.BeforeFirst(':'); wxString pic = msg.AfterFirst(':'); m_cache->GetFile(pic); aBridgePlayerData::SetPlayerPic(name, pic); } else if (type.IsSameAs("VERSION")) { m_log->AppendText(from + "--" + msg + "\n"); from.Replace("_", " "); speak_string(from + " just joined us."); } } void aBridgeRoom::HandleIJoined(IrcMsgEvent &evt) { wxString info = GetMyInfo(); info.Replace("\n","\\n"); m_irc->SendMsg("PLAYERINFO", player_name() + ":" + info); if (! m_player_pic_md5.IsSameAs("")) m_irc->SendMsg("PLAYERPIC", player_name() + ":" + m_player_pic_md5); DebugMsg("Sending my info"); } void aBridgeRoom::HandleSomeoneJoined(IrcMsgEvent &evt) { wxString info = GetMyInfo(); wxString player = evt.GetFrom(); info.Replace("\n","\\n"); m_irc->SendMsg("PLAYERINFO", player_name() + ":" + info, player); if (! m_player_pic_md5.IsSameAs("")) m_irc->SendMsg("PLAYERPIC", player_name() + ":" + m_player_pic_md5, player); DebugMsg("Sending my info to " + player); } void aBridgeRoom::ProcessTablesEvent(TablesEvent &evt) { wxString type = evt.GetType(); wxString msg = evt.GetMyMessage(); if (type.IsSameAs("JOIN")) { DebugMsg("Joining " + msg); JoinTable(msg); } else if (type.IsSameAs("SIT")) { wxString room = msg.AfterFirst(':'); wxString seat = msg.BeforeFirst(':'); JoinTable(room); IrcChannel *the_room_channel = m_irc->Mother()->AmInChannel(room); if (the_room_channel) the_room_channel->SendMsg("SIT", seat, room); } }