netpanzer-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[netPanzer-Devel] lobby patch


From: hankin
Subject: [netPanzer-Devel] lobby patch
Date: Sun, 02 Nov 2003 17:12:55 +1100
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5) Gecko/20030917

Here's a patch for 0.1.1 for a lobby via IRC.
To use...
Start a dedicated server, put in a valid IRC nick name for the server name.
Edit the ~/.netpanzer/config/netpanzer.xml and change your player name to your IRC nick name. Start netpanzer client like normal and if everything works you should be able to select
the server and press Ready to launch.
You may run into problems if the server's isp doesn't do a proper reverse dns.


diff -rc --new-file ../netpanzer-0.1.1/src/NetPanzer/Interfaces/GameManager.cpp 
./src/NetPanzer/Interfaces/GameManager.cpp
*** ../netpanzer-0.1.1/src/NetPanzer/Interfaces/GameManager.cpp Tue Oct 14 
00:30:16 2003
--- ./src/NetPanzer/Interfaces/GameManager.cpp  Sun Nov  2 17:05:39 2003
***************
*** 113,118 ****
--- 113,119 ----
  
  //winsock hack
  #include "IPAddressView.hpp"
+ #include "IRCLobbyView.hpp"
  
  //** Physics/Particle Stuff
  #include "Particle2D.hpp"
***************
*** 241,246 ****
--- 242,248 ----
  
      //winsock hack
      Desktop::add(new IPAddressView());
+     Desktop::add(new IRCLobbyView());
  
      Desktop::setVisibilityAllWindows(false);
      Desktop::setVisibility("MainView", true);
***************
*** 1388,1394 ****
--- 1390,1399 ----
  
      setNetPanzerGameOptions();
  
+     gameconfig->hostorjoin=_game_session_host;
+ 
      Particle2D::setCreateParticles(false);
+     IRCLobbyView::startIRC();
  
      ConsoleInterface::postMessage( "Game Launched, Server Ready...");
  
diff -rc --new-file 
../netpanzer-0.1.1/src/NetPanzer/Interfaces/PlayerInterface.cpp 
./src/NetPanzer/Interfaces/PlayerInterface.cpp
*** ../netpanzer-0.1.1/src/NetPanzer/Interfaces/PlayerInterface.cpp     Mon Sep 
22 23:53:52 2003
--- ./src/NetPanzer/Interfaces/PlayerInterface.cpp      Sat Nov  1 22:32:49 2003
***************
*** 231,236 ****
--- 231,247 ----
      return( &player_lists[ local_player_index ] );
  }
  
+ int PlayerInterface::countPlayers( void )
+ {
+       int count=0;
+     for ( int player_index = 0; player_index < max_players; player_index++ ) {
+         if ( player_lists[ player_index ].getStatus() != _player_state_free ) 
{
+               count++;
+       }
+     }
+       return count;
+ }
+ 
  PlayerState * PlayerInterface::allocateNewPlayer( void )
  {
      unsigned long player_index;
diff -rc --new-file 
../netpanzer-0.1.1/src/NetPanzer/Interfaces/PlayerInterface.hpp 
./src/NetPanzer/Interfaces/PlayerInterface.hpp
*** ../netpanzer-0.1.1/src/NetPanzer/Interfaces/PlayerInterface.hpp     Wed Sep 
17 06:16:12 2003
--- ./src/NetPanzer/Interfaces/PlayerInterface.hpp      Fri Oct 31 20:43:21 2003
***************
*** 128,133 ****
--- 128,134 ----
      static PlayerState * allocateLoopBackPlayer( void );
  
      static PlayerState * allocateNewPlayer( void );
+     static int countPlayers( void );
  
      static void spawnPlayer( unsigned short player_index, const iXY &location 
);
      static void spawnPlayer( const PlayerID &player, const iXY &location );
diff -rc --new-file 
../netpanzer-0.1.1/src/NetPanzer/Views/MainMenu/Multi/GameServer.hpp 
./src/NetPanzer/Views/MainMenu/Multi/GameServer.hpp
*** ../netpanzer-0.1.1/src/NetPanzer/Views/MainMenu/Multi/GameServer.hpp        
Thu Jan  1 10:00:00 1970
--- ./src/NetPanzer/Views/MainMenu/Multi/GameServer.hpp Sat Nov  1 23:17:58 2003
***************
*** 0 ****
--- 1,52 ----
+ /*
+ Copyright (C) 1998 Pyrosoft Inc. (www.pyrosoftgames.com), Matthew Bogue
+ 
+ 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
+ */
+ #ifndef __GameServer_h__
+ #define __GameServer_h__
+ 
+ #include <SDL_net.h>
+ 
+ #include "String.hpp"
+ 
+ //---------------------------------------------------------------------------
+ class GameServer
+ {
+ public:
+     String host;
+     String user;
+     String map;
+     time_t last_update;
+     int players;
+     int max_players;
+     GameServer *next,*prev;
+     GameServer() {
+       next=NULL;
+       prev=NULL;
+     }
+     void set(const char *h,const char *u,int p,int mp,const char *m) {
+       host=h;
+       user=u;
+       map=m;
+       time(&last_update);
+       players=p;
+       max_players=mp;
+     }
+ };
+ 
+ 
+ 
+ #endif
diff -rc --new-file 
../netpanzer-0.1.1/src/NetPanzer/Views/MainMenu/Multi/GameServerList.hpp 
./src/NetPanzer/Views/MainMenu/Multi/GameServerList.hpp
*** ../netpanzer-0.1.1/src/NetPanzer/Views/MainMenu/Multi/GameServerList.hpp    
Thu Jan  1 10:00:00 1970
--- ./src/NetPanzer/Views/MainMenu/Multi/GameServerList.hpp     Sun Nov  2 
13:19:55 2003
***************
*** 0 ****
--- 1,77 ----
+ /*
+ Copyright (C) 1998 Pyrosoft Inc. (www.pyrosoftgames.com), Matthew Bogue
+ 
+ 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
+ */
+ #ifndef __GameServerList_h__
+ #define __GameServerList_h__
+ 
+ #include <SDL.h>
+ #include <SDL_thread.h>
+ #include <SDL_net.h>
+ 
+ #include "View.hpp"
+ #include "Surface.hpp"
+ #include "BucketArrayTemplate.hpp"
+ #include "GameServer.hpp"
+ 
+ 
+ //---------------------------------------------------------------------------
+ class GameServerList
+ {
+       BucketArrayTemplate<GameServer> servers;
+       static int get_str_hash(const char *h) {
+               int n=0xaa;
+               while(*h) { n&=*h; h++; }
+               return n&0xf;
+       }
+ public:
+       static const int max_buckets=16;
+       GameServerList() {
+               servers.initialize(max_buckets);
+       }
+       ~GameServerList() {
+       }
+ 
+       LinkListDoubleTemplate<GameServer> *getBucket(int h) {
+               assert(this!=NULL);
+               assert(h<max_buckets);
+               LinkListDoubleTemplate<GameServer> *i= servers.getBucket(h);
+               return i;
+       }
+       GameServer *find(const char *addr) {
+               assert(this!=NULL);
+               assert(addr!=NULL);
+ 
+               int h=get_str_hash(addr);
+               LinkListDoubleTemplate<GameServer> *bucket=servers.getBucket(h);
+               GameServer *upto=bucket->getFront();
+               while(upto!=NULL) {
+                       if(strcmp((const char *)upto->host,addr)==0) {
+                               return upto;
+                       }
+                       upto=upto->next;
+               }
+       }
+       void add(GameServer *server) {
+               assert(this!=NULL);
+               assert(server!=NULL);
+ 
+               int h=get_str_hash(server->host);
+               servers.addObject(h,server);
+       }
+ };
+ 
+ #endif
diff -rc --new-file 
../netpanzer-0.1.1/src/NetPanzer/Views/MainMenu/Multi/GetSessionView.cpp 
./src/NetPanzer/Views/MainMenu/Multi/GetSessionView.cpp
*** ../netpanzer-0.1.1/src/NetPanzer/Views/MainMenu/Multi/GetSessionView.cpp    
Tue Oct 14 00:30:22 2003
--- ./src/NetPanzer/Views/MainMenu/Multi/GetSessionView.cpp     Fri Oct 31 
20:34:02 2003
***************
*** 24,29 ****
--- 24,30 ----
  #include "GameManager.hpp"
  #include "MouseInterface.hpp"
  #include "GameViewGlobals.hpp"
+ #include "IRCLobbyView.hpp"
  
  //winsock hack
  #include "IPAddressView.hpp"
***************
*** 140,145 ****
--- 141,148 ----
  
  
          Desktop::setVisibility("IPAddressView", true);
+         Desktop::setVisibility("IRCLobbyView", true);
+       IRCLobbyView::startIRC();
          //Desktop::setVisibility("JoinView", true);
          //Desktop::setVisibility("GetSessionHostView", true);
          //Desktop::setVisibility("UnitSelectionView", true);
diff -rc --new-file 
../netpanzer-0.1.1/src/NetPanzer/Views/MainMenu/Multi/HostJoinTemplateView.cpp 
./src/NetPanzer/Views/MainMenu/Multi/HostJoinTemplateView.cpp
*** 
../netpanzer-0.1.1/src/NetPanzer/Views/MainMenu/Multi/HostJoinTemplateView.cpp  
    Tue Oct 14 00:30:22 2003
--- ./src/NetPanzer/Views/MainMenu/Multi/HostJoinTemplateView.cpp       Sat Nov 
 1 23:03:23 2003
***************
*** 30,35 ****
--- 30,36 ----
  #include "FlagSelectionView.hpp"
  #include "Palette.hpp"
  #include "GameViewGlobals.hpp"
+ #include "IRCLobbyView.hpp"
  
  #include "Client.hpp"
  #include "Server.hpp"
***************
*** 118,123 ****
--- 119,125 ----
      MenuTemplateView::backgroundSurface.free();
      //MenuTemplateView::titleSurface.free();
  
+     IRCLobbyView::stopIRC();
      GameManager::launchNetPanzerGame();
  }
  /*
diff -rc --new-file 
../netpanzer-0.1.1/src/NetPanzer/Views/MainMenu/Multi/IRCChatMessage.hpp 
./src/NetPanzer/Views/MainMenu/Multi/IRCChatMessage.hpp
*** ../netpanzer-0.1.1/src/NetPanzer/Views/MainMenu/Multi/IRCChatMessage.hpp    
Thu Jan  1 10:00:00 1970
--- ./src/NetPanzer/Views/MainMenu/Multi/IRCChatMessage.hpp     Sun Nov  2 
14:27:12 2003
***************
*** 0 ****
--- 1,42 ----
+ /*
+ Copyright (C) 1998 Pyrosoft Inc. (www.pyrosoftgames.com), Matthew Bogue
+ 
+ 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
+ */
+ #ifndef __IRCChatMessage_h__
+ #define __IRCChatMessage_h__
+ 
+ #include <SDL.h>
+ #include <SDL_thread.h>
+ #include <SDL_net.h>
+ 
+ #include "View.hpp"
+ #include "Surface.hpp"
+ #include "String.hpp" 
+ 
+ //---------------------------------------------------------------------------
+ class IRCChatMessage
+ {
+ public:
+     String mess;
+     String user;
+     IRCChatMessage *prev,*next;
+     IRCChatMessage(const char *m,const char *u) : mess(m),user(u) {
+         prev=NULL;
+         next=NULL;
+     }
+ };
+ 
+ #endif
diff -rc --new-file 
../netpanzer-0.1.1/src/NetPanzer/Views/MainMenu/Multi/IRCLobby.cpp 
./src/NetPanzer/Views/MainMenu/Multi/IRCLobby.cpp
*** ../netpanzer-0.1.1/src/NetPanzer/Views/MainMenu/Multi/IRCLobby.cpp  Thu Jan 
 1 10:00:00 1970
--- ./src/NetPanzer/Views/MainMenu/Multi/IRCLobby.cpp   Sun Nov  2 16:22:31 2003
***************
*** 0 ****
--- 1,370 ----
+ /*
+ Copyright (C) 1998 Pyrosoft Inc. (www.pyrosoftgames.com), Matthew Bogue
+ 
+ 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
+ */
+ 
+ #include "Log.hpp"
+ #include "IRCLobby.hpp"
+ #include "PlayerInterface.hpp"
+ #include "GameConfig.hpp"
+ #include "NetworkGlobals.hpp" 
+ 
+ IRCLobby::IRCLobby(IPaddress *addr)
+ {
+     irc_server_sock=0;
+     channel_name="#netpanzerlob";
+     game_servers=new GameServerList();
+     memcpy(&irc_server,addr,sizeof(irc_server));
+     running_thread=NULL;
+     quit_thread=0;
+     game_servers_mutex=SDL_CreateMutex();
+ }
+ 
+ IRCLobby::~IRCLobby()
+ {
+     SDL_mutexP(game_servers_mutex);
+     delete game_servers;
+     game_servers=NULL;
+     SDL_mutexV(game_servers_mutex);
+     SDL_DestroyMutex(game_servers_mutex);
+     stopThread();
+ }
+ 
+ void IRCLobby::stopThread() {
+     int r;
+     quit_thread=1;
+     if(running_thread==NULL) { return; }
+     SDL_WaitThread(running_thread,&r);
+     running_thread=NULL;
+     if(irc_server_sock) {
+         char *quit="QUIT\n";
+         SDLNet_TCP_Send(irc_server_sock,quit,5);
+         SDLNet_TCP_Close(irc_server_sock);
+         irc_server_sock=0;
+     }
+ }
+ 
+ const char IRCLobby::server_running_mess[]="I'm running";
+ const char IRCLobby::ask_server_running_mess[]="Who's running a server?";
+ 
+ // send server info to someone
+ int IRCLobby::sendServerInfo(const char *dest)
+ {
+     char line[1024];
+     assert(this!=NULL);
+     char *l=line;
+ 
+     const char *map= ((const std::string&)(gameconfig->map)).c_str();
+     int running_players=PlayerInterface::countPlayers();
+     l+=snprintf(l,(line+sizeof(line))-l,"-%s %i/%i map:%s",
+         server_running_mess,
+         running_players,PlayerInterface::getMaxPlayers(),
+         map);
+ 
+ #if 0
+     int max_players=PlayerInterface::getMaxPlayers();
+     for(int p=0; p<max_players; p++) {
+             PlayerState *st=getPlayerState(p);
+             l+=snprintf(l,(line+sizeof(line))-l,"%s:",
+                     st->getName());
+     }
+     *l++=',';
+ #endif
+     return sendIRCMessageLine(line,dest);
+ }
+ 
+ 
+ int IRCLobby::connectToServer()
+ {
+     char line[1024];
+     char irc_playername[1024];
+     char *pl;
+     const char *pl_from;
+ 
+     assert(this!=NULL);
+     if(!(irc_server_sock=SDLNet_TCP_Open(&irc_server))) {
+         return -1;
+     }
+ 
+     // login
+     const char *playername=((const 
std::string&)(gameconfig->playername)).c_str();
+ 
+     pl=irc_playername;
+     pl_from=playername;
+     while(*pl_from) {
+         if(!isalnum(*pl_from) && *pl_from!='_') { pl_from++; continue; }
+         *pl++=*pl_from++;
+     }
+     *pl=0;
+ 
+     snprintf(line,sizeof(line),"NICK %s",irc_playername);
+     sendIRCLine(line);
+     snprintf(line,sizeof(line),"USER %s 0 * :%s",
+             irc_playername,irc_playername);
+     sendIRCLine(line);
+     // join channel
+     snprintf(line,sizeof(line),"JOIN %s",channel_name);
+     sendIRCLine(line);
+ 
+     if(gameconfig->hostorjoin== _game_session_host) {
+         // tell everyone the server just started
+         sendServerInfo(channel_name);
+     }
+     else if(gameconfig->hostorjoin== _game_session_join) {
+         refreshServerList();
+     }
+     return 0;
+ }
+ 
+ int IRCLobby::refreshServerList()
+ {
+     char line[256];
+ 
+     assert(this!=NULL);
+     // ask for known servers
+     snprintf(line,sizeof(line),"-%s",ask_server_running_mess);
+     sendIRCMessageLine(line,channel_name);
+     return 0;
+ }
+ 
+ static int messagesThreadEntry(IRCLobby *t)
+ {
+     t->processMessages();
+     LOG(("irc chat ended"));
+     return 0;
+ }
+ 
+ int IRCLobby::startMessagesThread()
+ {
+     assert(this!=NULL);
+     assert(running_thread==NULL);
+ 
+     quit_thread=0;
+     if(running_thread!=NULL) { return -1; }
+     running_thread=SDL_CreateThread( (int (*)(void *)) 
messagesThreadEntry,this);
+     return 0;
+ }
+ 
+ int IRCLobby::processMessages()
+ {
+     assert(this!=NULL);
+     if(connectToServer()<0) { return -1; }
+     while(!quit_thread) {
+         if(processMessage()<0) { return -1; }
+     }
+     return 0;
+ }
+ 
+ void IRCLobby::addChatMessage(const char *u,const char *m)
+ {
+     IRCChatMessage *chat_message=new IRCChatMessage(m,u);
+     int dels=chat_messages.getSize();
+     while(dels-->4) {
+         chat_messages.deleteFront();
+     }
+     chat_messages.addRear(chat_message);
+ }
+ 
+ // read a line of irc and process it.
+ int IRCLobby::processMessage()
+ {
+     char buf[1024];
+     char *host,*mess,*host_end,*user_end,*code;
+ 
+     assert(this!=NULL);
+     if(!irc_server_sock)  { return 0; }
+ 
+     if(readIRCLine(buf,sizeof(buf))<0) {
+       return 0;
+     }
+     if(buf[0]!=':') { return 0; }
+     code=buf+1;
+     while(*code && !isspace(*code)) {  code++; }
+     while(isspace(*code)) {  code++; }
+     int code_i=atoi(code);
+     if(code_i>=400 && code_i<500) {
+         LOG(("IRC:%s",buf));
+     }
+ 
+     // get remote user/host
+     // address@hidden PRIVMSG #netpanzerlob :asfd
+     if(
+       (host=strchr(buf,'@'))==NULL
+       || (mess=strchr(buf+1,':'))==NULL
+       || (user_end=strchr(buf,'!'))==NULL
+     ) {
+       return 0;
+     }
+     *host++=0;
+     *user_end++=0;
+     mess++;
+     if((host_end=strchr(host,' '))==NULL) {
+       return 0;
+     }
+     *host_end++=0;
+     while(isspace(*host_end)) host_end++;
+     if(strncmp(host_end,"PRIVMSG",7)!=0) {
+         return 0;
+     }
+ 
+     if(mess[0]=='#') {
+         // this is a chat message
+         addChatMessage(mess+1,buf+1);
+         if(gameconfig->hostorjoin== _game_session_host) {
+             LOG(("IRC message:%s:%s",buf+1,mess+1));
+         }
+ 
+         return 0;
+     }
+     if(mess[0]!='-') {
+         // this is not an internal message
+         return 0;
+     }
+ 
+     if(strcmp(mess+1,ask_server_running_mess)==0) {
+         if(gameconfig->hostorjoin== _game_session_host) {
+             // reply with server details
+             sendServerInfo(buf+1);
+         }
+     }
+     else 
if(strncmp(mess+1,server_running_mess,sizeof(server_running_mess)-1)==0) {
+         // add a server to the list
+         if(gameconfig->hostorjoin== _game_session_join) {
+             const char *p=mess+1+sizeof(server_running_mess);
+             const char *map;
+             int players=atoi(p);
+             if((p=strchr(p,'/'))==NULL) {
+                 LOG(("bad server description: %s\n",mess));
+                 return 0;
+             }
+             int max_players=atoi(++p);
+             if((map=strstr(p,"map:"))==NULL) {
+                 LOG(("no map name: %s\n",mess));
+                 return 0;
+             }
+             map+=4;
+ 
+ //                    IPaddress addr;
+ //                    
if(SDLNet_ResolveHost(&addr,host,_NETPANZER_DEFAULT_PORT_TCP)==0) {
+ 
+             GameServer *server=game_servers->find(host);
+             if(server==NULL) {
+                 server=new GameServer(); 
+                 server->set(host,buf+1,players,max_players,map);
+                 SDL_mutexP(game_servers_mutex);
+                 game_servers->add(server);
+                 SDL_mutexV(game_servers_mutex);
+             }
+             else {
+                 server->set(host,buf+1,players,max_players,map);
+             }
+ 
+ //                    }
+ //                    else { LOG(("cannot lookup: %s\n",host)); }
+         }
+     }
+     else {
+         LOG(("unknown chat cmd: %s\n",mess));
+     }
+     return 0;
+ }
+ 
+ int IRCLobby::sendChatMessage(const char *line)
+ {
+     assert(this!=NULL);
+     assert(line!=NULL);
+ 
+     char chat_line[1024];
+     snprintf(chat_line,sizeof(chat_line),"#%s",line);
+     addChatMessage("",line);
+     return sendIRCMessageLine(chat_line);
+ }
+ 
+ 
+ int IRCLobby::sendIRCMessageLine(char *line)
+ {
+     assert(this!=NULL);
+     assert(line!=NULL);
+     return sendIRCMessageLine(line,channel_name);
+ }
+ 
+ int IRCLobby::sendIRCMessageLine(char *line,const char *to)
+ {
+     assert(this!=NULL);
+     assert(line!=NULL);
+     assert(to!=NULL);
+     char channel_msg[256];
+     int channel_msg_len;
+ 
+     channel_msg_len=snprintf(channel_msg,sizeof(channel_msg),"PRIVMSG %s 
:",to);
+ 
+     
if(SDLNet_TCP_Send(irc_server_sock,channel_msg,channel_msg_len)!=channel_msg_len)
 {
+         return -1;
+     }
+     return sendIRCLine(line);
+ }
+ 
+ 
+ int IRCLobby::sendIRCLine(char *line)
+ {
+     assert(this!=NULL);
+     int len=strlen(line);
+     if(SDLNet_TCP_Send(irc_server_sock,line,len)!=len) {
+         return -1;
+     }
+     static char lf[]="\n";
+     SDLNet_TCP_Send(irc_server_sock,lf,1);
+     return 0;
+ }
+ 
+ int IRCLobby::readIRCLine(char *buf,int buf_len)
+ {
+     assert(this!=NULL);
+     char *buf_end=buf+buf_len-1;
+     char ch;
+     char *buf_upto=buf;
+     SDLNet_SocketSet sock_set=SDLNet_AllocSocketSet(1);
+     SDLNet_TCP_AddSocket(sock_set,irc_server_sock);
+ 
+     while( buf_upto<buf_end) {
+         if(quit_thread) {
+ err_ex:;
+             SDLNet_FreeSocketSet(sock_set);
+             return -1;
+         }
+         if(SDLNet_CheckSockets(sock_set,1000)<=0) {
+             const char *err=SDLNet_GetError();
+             if(err[0]!=0) {
+                 LOG(("irc socket err:%s\n",err));
+                 goto err_ex;
+             }
+             continue;
+         }
+         if(SDLNet_TCP_Recv(irc_server_sock,&ch,1)<=0) {
+             goto err_ex;
+         }
+         if(ch=='\r') { continue; }
+         if(ch=='\n') {
+                 break;
+         }
+         *buf_upto++=ch;
+     }
+     SDLNet_FreeSocketSet(sock_set);
+     *buf_upto=0;
+     return buf_upto-buf;
+ }
+ 
+ 
diff -rc --new-file 
../netpanzer-0.1.1/src/NetPanzer/Views/MainMenu/Multi/IRCLobby.hpp 
./src/NetPanzer/Views/MainMenu/Multi/IRCLobby.hpp
*** ../netpanzer-0.1.1/src/NetPanzer/Views/MainMenu/Multi/IRCLobby.hpp  Thu Jan 
 1 10:00:00 1970
--- ./src/NetPanzer/Views/MainMenu/Multi/IRCLobby.hpp   Sun Nov  2 15:36:59 2003
***************
*** 0 ****
--- 1,72 ----
+ /*
+ Copyright (C) 1998 Pyrosoft Inc. (www.pyrosoftgames.com), Matthew Bogue
+ 
+ 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
+ */
+ #ifndef __IRCLobby_h__
+ #define __IRCLobby_h__
+ 
+ #include <SDL.h>
+ #include <SDL_thread.h>
+ #include <SDL_net.h>
+ 
+ #include "View.hpp"
+ #include "Surface.hpp"
+ #include "GameServer.hpp"
+ #include "GameServerList.hpp"
+ #include "IRCChatMessage.hpp"
+ 
+ class IRCLobbyView;
+ 
+ //---------------------------------------------------------------------------
+ class IRCLobby
+ {
+     friend class IRCLobbyView;
+ 
+     static const char server_running_mess[];
+     static const char ask_server_running_mess[];
+     IPaddress irc_server;
+     TCPsocket irc_server_sock;
+     char *channel_name;
+     SDL_Thread *running_thread;
+     int quit_thread;
+     LinkListDoubleTemplate<IRCChatMessage> chat_messages;
+     GameServerList *game_servers;
+ 
+     int sendServerInfo(const char *dest);
+     // read any messages that need to be processed
+     int processMessage();
+     int sendIRCLine(char *line);
+     int sendIRCMessageLine(char *line,const char *to);
+     int readIRCLine(char *buf,int buf_len);
+     int connectToServer();
+     void addChatMessage(const char *m,const char *u);
+ 
+ 
+ public:
+     SDL_mutex *game_servers_mutex;
+ 
+     IRCLobby(IPaddress *addr);
+     ~IRCLobby();
+ 
+     int sendChatMessage(const char *line);
+     int sendIRCMessageLine(char *line);
+     void stopThread();
+     int refreshServerList();
+     int startMessagesThread();
+     int processMessages();
+ };
+ 
+ #endif
diff -rc --new-file 
../netpanzer-0.1.1/src/NetPanzer/Views/MainMenu/Multi/IRCLobbyView.cpp 
./src/NetPanzer/Views/MainMenu/Multi/IRCLobbyView.cpp
*** ../netpanzer-0.1.1/src/NetPanzer/Views/MainMenu/Multi/IRCLobbyView.cpp      
Thu Jan  1 10:00:00 1970
--- ./src/NetPanzer/Views/MainMenu/Multi/IRCLobbyView.cpp       Sun Nov  2 
16:15:08 2003
***************
*** 0 ****
--- 1,171 ----
+ /*
+ Copyright (C) 1998 Pyrosoft Inc. (www.pyrosoftgames.com), Matthew Bogue
+ 
+  
+ 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
+ */
+ #include <config.h>
+ #include <SDL_net.h>
+ 
+ #include "Log.hpp"
+ #include "IRCLobbyView.hpp"
+ #include "IPAddressView.hpp"
+ #include "Desktop.hpp"
+ #include "GameViewGlobals.hpp"
+ #include "GameConfig.hpp"
+ 
+ #include "Client.hpp"
+ 
+ 
+ 
+ IRCLobby *IRCLobbyView::lobby_connection=NULL;
+ 
+ cInputFieldString IRCLobbyView::szChat;
+ 
+ 
+ static void buttonRefresh(void)
+ {
+     IRCLobbyView::lobby_connection->refreshServerList();
+ }
+ 
+ static void buttonChat(void)
+ {
+     IRCLobbyView::lobby_connection->sendChatMessage(
+         IRCLobbyView::szChat.getString());
+     IRCLobbyView::szChat.setString("");
+ }
+ 
+ 
+ // IRCLobbyView
+ //---------------------------------------------------------------------------
+ IRCLobbyView::IRCLobbyView() : View()
+ {
+     lobby_view_height=160;
+     mouse_down_server=NULL;
+     total_displayed_servers=0;
+     setSearchName("IRCLobbyView");
+     setTitle("Lobby");
+     setSubTitle("");
+ 
+     setAllowResize(false);
+     setAllowMove(false);
+     setVisible(false);
+ 
+     moveTo(iXY(bodyTextRect.min.x, bodyTextRect.min.y + 200));
+ 
+     iXY  area_size = iXY(400, lobby_view_height);
+     resizeClientArea(area_size);
+ 
+     int chat_y=lobby_view_height-(Surface::getFontHeight()*2);
+     addButtonCenterText(iXY(320, chat_y ), 80,  "Chat", "", buttonChat);
+     addButtonCenterText(iXY(320, chat_y-(Surface::getFontHeight()*2) ), 80,  
"Refresh", "", buttonRefresh);
+     szChat.init("  ", 34);
+     addInputField(iXY(4, chat_y), &szChat, "", true);
+ } 
+ 
+ 
+ // doDraw
+ //---------------------------------------------------------------------------
+ void IRCLobbyView::doDraw(Surface &viewArea, Surface &clientArea)
+ {
+     assert(this!=NULL);
+     clientArea.fill(Color::black);
+     int y=0;
+     int bucket_upto=0;
+     int disp_server_upto=0;
+ 
+     if(lobby_connection==NULL) { return; }
+ 
+     LinkListDoubleTemplate<GameServer> *bucket=NULL;
+     GameServer *server=NULL;
+ 
+ //~~~ todo: scrollbar for large list of servers
+     SDL_mutexP(lobby_connection->game_servers_mutex);
+     int server_list_end_y=lobby_view_height-(Surface::getFontHeight()*6);
+     int chat_list_end_y=server_list_end_y+(Surface::getFontHeight()*4);
+     for(; y<server_list_end_y; y+=Surface::getFontHeight()) {
+         while(server==NULL && bucket_upto<GameServerList::max_buckets) {
+             bucket=lobby_connection->game_servers->getBucket(bucket_upto++);
+             server=bucket->getFront();
+         }
+         if(server==NULL) { break; }
+ 
+         char players_str[256];
+         sprintf(players_str,"%i/%i",server->players,server->max_players);
+         clientArea.bltString(iXY(0,y),server->user, Color::white);
+         clientArea.bltString(iXY(140,y),players_str, Color::white);
+         clientArea.bltString(iXY(200,y),server->map, Color::white);
+         displayed_servers[disp_server_upto++]=server;
+         server=server->next;
+     }
+     SDL_mutexV(lobby_connection->game_servers_mutex);
+     total_displayed_servers=disp_server_upto;
+ 
+     IRCChatMessage *cm=lobby_connection->chat_messages.getFront();
+     y=server_list_end_y;
+     for(; cm!=NULL && y<chat_list_end_y; y+=Surface::getFontHeight()) {
+         char mess_str[256];
+         snprintf(mess_str,sizeof(mess_str),"%s:%s",
+             (const char *)cm->user,(const char *)cm->mess);
+         clientArea.bltString(iXY(4,y),mess_str, Color::white);
+         cm=cm->next;
+     }
+     
+     View::doDraw(viewArea, clientArea);
+ }
+ 
+ 
+ 
+ 
+ int IRCLobbyView::lMouseUp(const iXY &down_pos,const iXY &up_pos)
+ {
+     assert(this!=NULL);
+     int idx_down=down_pos.y/Surface::getFontHeight();
+     int idx=up_pos.y/Surface::getFontHeight();
+     if(idx>=0 && idx_down==idx && idx<total_displayed_servers) {
+         GameServer *server=displayed_servers[idx];
+         assert(server!=NULL);
+ 
+         // connect to this game
+         IPAddressView::szServer.setString(server->host);
+     }
+     return View::lMouseUp(down_pos,up_pos);
+ }
+ 
+ 
+ 
+ // connect to remote irc
+ //---------------------------------------------------------------------------
+ void IRCLobbyView::startIRC()
+ {
+     IPaddress addr;
+     stopIRC();
+     if(SDLNet_ResolveHost(&addr,"irc.freenode.net",6667)==0) {
+       lobby_connection=new IRCLobby(&addr);
+       lobby_connection->startMessagesThread();
+     }
+     else { LOG(("cannot find irc host\n")); }
+ }
+ 
+ void IRCLobbyView::stopIRC()
+ {
+     if(lobby_connection!=NULL) {
+       lobby_connection->stopThread();
+       delete lobby_connection;
+       lobby_connection=NULL;
+         LOG(("stopped irc"));
+     }
+ }
+ 
diff -rc --new-file 
../netpanzer-0.1.1/src/NetPanzer/Views/MainMenu/Multi/IRCLobbyView.hpp 
./src/NetPanzer/Views/MainMenu/Multi/IRCLobbyView.hpp
*** ../netpanzer-0.1.1/src/NetPanzer/Views/MainMenu/Multi/IRCLobbyView.hpp      
Thu Jan  1 10:00:00 1970
--- ./src/NetPanzer/Views/MainMenu/Multi/IRCLobbyView.hpp       Sun Nov  2 
14:38:05 2003
***************
*** 0 ****
--- 1,46 ----
+ /*
+ Copyright (C) 1998 Pyrosoft Inc. (www.pyrosoftgames.com), Matthew Bogue
+ 
+ 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
+ */
+ #ifndef __IRCLobbyView_h__
+ #define __IRCLobbyView_h__
+ 
+ #include "IRCLobby.hpp"
+ #include "View.hpp"
+ #include "Surface.hpp"
+ #include "GameServer.hpp"
+ 
+ //---------------------------------------------------------------------------
+ class IRCLobbyView : public View
+ {
+  int lobby_view_height;
+  GameServer *mouse_down_server;
+  GameServer *displayed_servers[64];
+  int total_displayed_servers;
+ public:
+  static cInputFieldString szChat;
+  static IRCLobby *lobby_connection;
+  IRCLobbyView();
+ 
+  static void startIRC();
+  static void stopIRC();
+ 
+  virtual void doDraw(Surface &windowArea, Surface &clientArea); 
+  virtual int lMouseUp(const iXY &up_pos,const iXY &down_pos);
+ };
+ 
+ #endif
+ 
diff -rc --new-file 
../netpanzer-0.1.1/src/NetPanzer/Views/MainMenu/Multi/PlayerNameView.cpp 
./src/NetPanzer/Views/MainMenu/Multi/PlayerNameView.cpp
*** ../netpanzer-0.1.1/src/NetPanzer/Views/MainMenu/Multi/PlayerNameView.cpp    
Tue Oct 14 00:30:23 2003
--- ./src/NetPanzer/Views/MainMenu/Multi/PlayerNameView.cpp     Sun Nov  2 
11:52:47 2003
***************
*** 50,56 ****
  //---------------------------------------------------------------------------
  void PlayerNameView::init()
  {
!     playerName.init("Player", INPUT_FIELD_CHARACTERS);
      addInputField(iXY(BORDER_SPACE, BORDER_SPACE), &playerName, "", true);
  
  } // end PlayerNameView::init
--- 50,56 ----
  //---------------------------------------------------------------------------
  void PlayerNameView::init()
  {
!     playerName.init(((const std::string &)gameconfig->playername).c_str(), 
INPUT_FIELD_CHARACTERS);
      addInputField(iXY(BORDER_SPACE, BORDER_SPACE), &playerName, "", true);
  
  } // end PlayerNameView::init

reply via email to

[Prev in Thread] Current Thread [Next in Thread]