src/tron/gServerFavorites.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
00007 
00008 **************************************************************************
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00023   
00024 ***************************************************************************
00025 
00026 */
00027 
00028 #include "rSDL.h"
00029 
00030 #include "gServerFavorites.h"
00031 
00032 #include "gLogo.h"
00033 #include "gGame.h"
00034 
00035 #include "uMenu.h"
00036 
00037 #include "tConfiguration.h"
00038 #include "tDirectories.h"
00039 
00040 #include "gServerBrowser.h"
00041 #include "nServerInfo.h"
00042 
00043 #ifdef CONNECTION_STRESS
00044 static bool sg_ConnectionStress = false;
00045 #endif
00046 
00047 #include <sstream>
00048 
00049 enum { NUM_FAVORITES = 10 };
00050 
00051 /*
00053 gServerInfoFavorite::gServerInfoFavorite( tString const & connectionName, unsigned int port )
00054 {
00055     nServerInfoBase::SetConnectionName( connectionName );
00056     nServerInfoBase::SetPort( port );
00057 }
00058 */
00059 
00060 typedef nServerInfoRedirect gServerInfoFavorite;
00061 
00062 //********************************************************************************
00063 //********************************************************************************
00064 
00065 static tString sg_ConfName( int ID, char const * prefix, char const * name )
00066 {
00067     std::stringstream s;
00068     s << "BOOKMARK_" << prefix << ID+1 << name;
00069 
00070     return tString( s.str().c_str() );
00071 }
00072 
00074 class gServerFavorite
00075 {
00076 public:
00077     friend class gServerFavoritesHolder;
00078 
00080     gServerFavorite( int ID, char const * prefix )
00081             : name_( "" )
00082             , port_( sn_defaultPort )
00083             , index_( ID )
00084             , confName_( sg_ConfName( ID, prefix, "_NAME") ,name_ )
00085             , confAddress_( sg_ConfName( ID, prefix, "_ADDRESS"), address_ )
00086             , confPort( sg_ConfName( ID, prefix, "_PORT"), port_ )
00087     {
00088     };
00089 
00091     void Connect()
00092     {
00093         gServerInfoFavorite fav( address_, port_ );
00094 
00095         gLogo::SetDisplayed(false);
00096 
00097         ConnectToServer( &fav );
00098     }
00099 
00101     int GetIndex()
00102     {
00103         return index_;
00104     }
00105 public:
00106     tString         name_;        
00107     tString         address_;     
00108     int             port_;        
00109 
00110 private:
00111     int             index_;       
00112 
00113     tConfItemLine confName_;      
00114     tConfItemLine confAddress_;   
00115     tConfItem<int> confPort;      
00116 };
00117 
00119 class gServerFavoritesHolder
00120 {
00121 public:
00122     gServerFavoritesHolder( char const * prefix )
00123     :custom(-1, prefix)
00124     {
00125         // generate favorites
00126         for (int i = NUM_FAVORITES-1; i>=0; --i )
00127             favorites[i] = new gServerFavorite( i, prefix );
00128     }
00129 
00130     ~gServerFavoritesHolder()
00131     {
00132         // destroy favorites
00133         for (int i = NUM_FAVORITES-1; i>=0; --i )
00134             delete favorites[i];
00135     }
00136 
00138     gServerFavorite & GetFavorite( int index )
00139     {
00140         if ( index == -1 )
00141             return custom;
00142 
00143         tASSERT( index >=0 && index < NUM_FAVORITES );
00144         tASSERT( favorites[index] );
00145         return *favorites[index];
00146     }
00147 
00148     bool AddFavorite( nServerInfoBase const * server )
00149     {
00150         if ( !server )
00151             return false;
00152         
00153         for ( int i = NUM_FAVORITES-1; i>=0; --i )
00154         {
00155             gServerFavorite & fav = GetFavorite(i);
00156             
00157             if (fav.name_ == "" || fav.name_ == "Empty")
00158             {
00159                 fav.name_ = tColoredString::RemoveColors(server->GetName());
00160                 fav.address_ = server->GetConnectionName();
00161                 fav.port_ = server->GetPort();
00162                 
00163                 return true;
00164             }
00165         }
00166         
00167         return false;
00168 }
00169 
00170     bool IsFavorite( nServerInfoBase const * server )
00171     {
00172         if ( !server )
00173             return false;
00174         
00175         for ( int i = NUM_FAVORITES-1; i>=0; --i )
00176         {
00177             gServerFavorite & fav = GetFavorite(i);
00178             
00179             if (fav.name_ != "" && fav.name_ != "Empty" && fav.address_ == server->GetConnectionName() && fav.port_ == static_cast< int >( server->GetPort() ) )
00180             {
00181                 return true;
00182             }
00183         }
00184         
00185         return false;
00186     }
00187     
00188 private:
00189     // regular bookmarks
00190     gServerFavorite * favorites[NUM_FAVORITES];
00191 
00192     // custom connect server
00193     gServerFavorite custom;
00194 };
00195 
00196 // server bookmarks
00197 static gServerFavoritesHolder sg_favoriteHolder("");
00198 
00199 // alternative master servers
00200 static gServerFavoritesHolder sg_masterHolder("_MASTER");
00201 
00203 class gMenuItemEditSubmenu: public uMenuItemSubmenu
00204 {
00205 public:
00206     gMenuItemEditSubmenu(uMenu *M,uMenu *s,
00207                          const tOutput& help)
00208             : uMenuItemSubmenu( M, s, help )
00209     {}
00210 
00212     virtual void Enter()
00213     {
00214         // delegate to base
00215         uMenuItemSubmenu::Enter();
00216 
00217         // exit the parent menu (so we don't have to update the edit menu)
00218         menu->Exit();
00219     }
00220 };
00221 
00223 static void sg_ConnectFavorite( int ID )
00224 {
00225     sg_favoriteHolder.GetFavorite(ID).Connect();
00226 }
00227 
00229 static void sg_AlternativeMaster( int ID )
00230 {
00231     // generate suffix for filename
00232     std::stringstream suffix;
00233     suffix << "_" << ID;
00234 
00235     // fetch server info
00236     gServerFavorite & favorite = sg_masterHolder.GetFavorite(ID);
00237     gServerInfoFavorite fav( favorite.address_, favorite.port_ );
00238 
00239     // browse master info
00240     gServerBrowser::BrowseSpecialMaster( &fav, suffix.str().c_str() );
00241 }
00242 
00243 // current connection function
00244 static INTFUNCPTR sg_Connect = &sg_ConnectFavorite;
00245 // current server holder
00246 static gServerFavoritesHolder * sg_holder = &sg_favoriteHolder;
00247 // current minimal port accessible in the menu
00248 static int sg_lowPort = gServerBrowser::lowPort;
00249 // current language id prefix
00250 static char * sg_languageIDPrefix = "$bookmarks_";
00251 // yeah, this could all be more elegant.
00252 
00253 // generates a language string item fitting the current situation
00254 static void sg_AddBookmarkString( char const * suffix, tOutput & addTo )
00255 {
00256     std::ostringstream s;
00257     s << sg_languageIDPrefix;
00258     s << suffix;
00259     addTo << s.str().c_str();
00260 }
00261 
00262 // generates a language string item fitting the current situation
00263 static tOutput sg_GetBookmarkString( char const * suffix )
00264 {
00265     tOutput ret;
00266     sg_AddBookmarkString( suffix, ret );
00267     return ret;
00268 }
00269 
00271 class gCustomConnectEntries
00272 {
00273 public:
00274     void Generate( gServerFavorite & fav, uMenu * menu )
00275     {
00276         // prepare output reading "Edit <server name>"
00277         // create menu items (autodeleted when the edit menu is killed)
00278         tNEW(uMenuItemFunctionInt) ( menu, sg_GetBookmarkString( "menu_edit_connect_text" ), sg_GetBookmarkString( "menu_edit_connect_help" ), sg_Connect, fav.GetIndex() );
00279         tNEW(uMenuItemInt)         ( menu,"$network_custjoin_port_text","$network_custjoin_port_help", fav.port_, sg_lowPort, gServerBrowser::highPort );
00280         tNEW(uMenuItemString)      ( menu,sg_GetBookmarkString( "menu_address" ),sg_GetBookmarkString( "menu_address_help" ),fav.address_);
00281     }
00282 
00283     gCustomConnectEntries()
00284     {
00285     }
00286 
00287     gCustomConnectEntries( gServerFavorite & fav, uMenu * menu )
00288     {
00289         Generate( fav, menu );
00290     }
00291 
00292     ~gCustomConnectEntries()
00293     {
00294     }
00295 
00296 private:
00297 };
00298 
00300 class gServerFavoriteMenuEntries: public gCustomConnectEntries
00301 {
00302 public:
00303     gServerFavoriteMenuEntries( gServerFavorite & fav, uMenu & edit_menu )
00304     {
00305         // prepare output reading "Edit <server name>"
00306         tString serverName = tColoredString::RemoveColors(fav.name_);
00307         if ( serverName == "" || serverName == "Empty" )
00308         {
00309             std::stringstream s;
00310             s << "Server " << fav.GetIndex()+1;
00311 
00312             serverName = s.str().c_str();
00313         }
00314 
00315         tOutput fe;
00316         fe.SetTemplateParameter(1, serverName);
00317         sg_AddBookmarkString( "menu_edit_slot", fe );
00318 
00319         // create edit menu
00320         edit_     = tNEW(uMenu)                (fe);
00321         editmenu_ = tNEW(gMenuItemEditSubmenu) ( &edit_menu, edit_, fe);
00322 
00323         Generate( fav, edit_ );
00324 
00325         tNEW(uMenuItemString)      ( edit_,sg_GetBookmarkString( "menu_name" ),sg_GetBookmarkString( "menu_name_help" ),fav.name_);
00326     }
00327 
00328     ~gServerFavoriteMenuEntries()
00329     {
00330         delete editmenu_; editmenu_ = 0;
00331         delete edit_; edit_ = 0;
00332     }
00333 
00334 private:
00335     uMenu     * edit_;
00336     uMenuItem * editmenu_;
00337 };
00338 
00339 static void sg_GenerateConnectionItems();
00340 
00341 // Edit servers submenu funcion
00342 static void sg_EditServers()
00343 {
00344     int i;
00345 
00346     // create menu
00347     uMenu edit_menu(sg_GetBookmarkString( "menu_edit" ) );
00348 
00349     // create menu entries
00350     gServerFavoriteMenuEntries * entries[ NUM_FAVORITES ];
00351     for ( i = NUM_FAVORITES-1; i>=0; --i )
00352         entries[i] = tNEW( gServerFavoriteMenuEntries )( sg_holder->GetFavorite(i), edit_menu );
00353 
00354     // enter menu
00355     edit_menu.Enter();
00356 
00357     // delete menu entries
00358     for ( i = NUM_FAVORITES-1; i>=0; --i )
00359         delete entries[i];
00360 
00361     // regenerate parent menu
00362     sg_GenerateConnectionItems();
00363 }
00364 
00365 // ugly hack: functions clearing and filling the connection menu
00366 static uMenu * sg_connectionMenu = 0;
00367 static uMenuItem * sg_connectionMenuItemKeep = 0;
00368 static void sg_ClearConnectionItems()
00369 {
00370     tASSERT( sg_connectionMenu );
00371 
00372     // delete old connection items
00373     for ( int i = sg_connectionMenu->NumItems()-1; i>=0; --i )
00374     {
00375         uMenuItem * item = sg_connectionMenu->Item(i);
00376         if ( item != sg_connectionMenuItemKeep )
00377             delete item;
00378     }
00379 }
00380 static void sg_GenerateConnectionItems()
00381 {
00382     tASSERT( sg_connectionMenu );
00383 
00384     // delete old connection items
00385     sg_ClearConnectionItems();
00386 
00387     // create new connection items
00388     for ( int i = NUM_FAVORITES-1; i>=0; --i )
00389     {
00390         gServerFavorite & fav = sg_holder->GetFavorite(i);
00391 
00392         if (fav.name_ != "" && fav.name_ != "Empty" && fav.address_ != "")
00393         {
00394             tOutput fc;
00395             fc.SetTemplateParameter(1,tColoredString::RemoveColors(fav.name_) );
00396             sg_AddBookmarkString( "menu_connect", fc );
00397 
00398             tNEW(uMenuItemFunctionInt)(sg_connectionMenu ,fc ,sg_GetBookmarkString( "menu_edit_connect_help" ) , sg_Connect, i );
00399         }
00400     }
00401 }
00402 
00404 static tString sg_customServerName("");
00405 static tConfItemLine sg_serverName_ci("CUSTOM_SERVER_NAME",sg_customServerName);
00406 static int sg_clientPort = 4534;
00407 static tConfItem<int> sg_cport("CLIENT_PORT",sg_clientPort);
00408 
00410 static void sg_TransferCustomServer()
00411 {
00412     if ( sg_customServerName != "" )
00413     {
00414         // add custom connect server to favorites
00415         gServerInfoFavorite server( sg_customServerName, sg_clientPort );
00416         gServerFavorites::AddFavorite( &server );
00417 
00418         // clear custom connect server
00419         sg_customServerName = "";
00420     }
00421 }
00422 
00423 static void sg_FavoritesMenu( INTFUNCPTR connect, gServerFavoritesHolder & holder, int lowPort )
00424 {
00425     sg_Connect = connect;
00426     sg_holder  = &holder;
00427     sg_lowPort = lowPort;
00428 
00429     sg_TransferCustomServer();
00430 
00431     uMenu net_menu(sg_GetBookmarkString( "menu" ) );
00432     sg_connectionMenu = & net_menu;
00433 
00434     uMenuItemFunction edit(&net_menu,sg_GetBookmarkString( "menu_edit" ), sg_GetBookmarkString( "menu_edit_help" ),&sg_EditServers);
00435     sg_connectionMenuItemKeep = & edit;
00436 
00437     sg_GenerateConnectionItems();
00438     net_menu.Enter();
00439     sg_ClearConnectionItems();
00440 
00441     sg_connectionMenuItemKeep = NULL;
00442     sg_connectionMenu = NULL;
00443 }
00444 
00445 // *********************************************************************************************
00446 // *
00447 // *    FavoritesMenu
00448 // *
00449 // *********************************************************************************************
00452 // *********************************************************************************************
00453 
00454 void gServerFavorites::FavoritesMenu( void )
00455 {
00456     sg_languageIDPrefix = "$bookmarks_";
00457     sg_FavoritesMenu( &sg_ConnectFavorite, sg_favoriteHolder, gServerBrowser::lowPort );
00458 }
00459 
00460 // *********************************************************************************************
00461 // *
00462 // *    AlternativesMenu
00463 // *
00464 // *********************************************************************************************
00467 // *********************************************************************************************
00468 
00469 void gServerFavorites::AlternativesMenu( void )
00470 {
00471     // load default subcultures
00472     nServerInfo::DeleteAll();
00473     nServerInfo::Load( tDirectories::Config(), "subcultures.srv" );
00474 
00475     // add them
00476     nServerInfo * run = nServerInfo::GetFirstServer();
00477     while( run )
00478     {
00479         if ( !sg_masterHolder.IsFavorite( run ) )
00480         {
00481             sg_masterHolder.AddFavorite( run );
00482         }
00483 
00484         run = run->Next();
00485     }
00486 
00487     nServerInfo::DeleteAll();
00488 
00489     sg_languageIDPrefix = "$masters_";
00490     sg_FavoritesMenu( &sg_AlternativeMaster, sg_masterHolder, gServerBrowser::lowPort - 10 );
00491 }
00492 
00493 // *********************************************************************************************
00494 // *
00495 // *    CustomConnectMenu
00496 // *
00497 // *********************************************************************************************
00500 // *********************************************************************************************
00501 
00502 void gServerFavorites::CustomConnectMenu( void )
00503 {
00504     sg_TransferCustomServer();
00505 
00506     uMenu net_menu("$network_custjoin_text");
00507     sg_connectionMenu = & net_menu;
00508 
00509     gServerFavorite & fav = sg_favoriteHolder.GetFavorite(-1);
00510 
00511     // create menu entries
00512     gCustomConnectEntries submenu( fav, &net_menu );
00513 
00514     net_menu.Enter();
00515 }
00516 
00517 // *********************************************************************************************
00518 // *
00519 // *    AddFavorite
00520 // *
00521 // *********************************************************************************************
00526 // *********************************************************************************************
00527 
00528 bool gServerFavorites::AddFavorite( nServerInfoBase const * server )
00529 {
00530     return sg_favoriteHolder.AddFavorite( server );
00531 }
00532 
00533 // *********************************************************************************************
00534 // *
00535 // *    IsFavorite
00536 // *
00537 // *********************************************************************************************
00542 // *********************************************************************************************
00543 
00544 bool gServerFavorites::IsFavorite( nServerInfoBase const * server )
00545 {
00546     return sg_favoriteHolder.IsFavorite( server );
00547 }
00548 
00549 

Generated on Sat Mar 15 22:56:09 2008 for Armagetron Advanced by  doxygen 1.5.4