Logo
~Sockets~
~Examples~
~Contact~


SmallSocket Class Reference

Socket class for incoming telnet connections, command parsing, state management. More...

#include <SmallSocket.h>

Collaboration diagram for SmallSocket:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 SmallSocket (ISocketHandler &)
 ~SmallSocket ()
void OnAccept ()
void OnLine (const std::string &)
const std::string & GetName ()
bool IsAt (int x, int y)
int GetX ()
int GetY ()
WorldGetWorld ()
void try_move (const std::string &)
void SendPrompt ()
PlayerFactoryGetPlayerF ()
void Send (const std::string &)
void TryGet (SmallSocket *, const std::string &)
void TryDrop (SmallSocket *, const std::string &)

Private Attributes

int m_state
std::string m_name
int m_x
int m_y
PlayerFactory::PLAYERm_player
std::string m_passwd

Detailed Description

Socket class for incoming telnet connections, command parsing, state management.

Definition at line 41 of file SmallSocket.h.


Constructor & Destructor Documentation

SmallSocket::SmallSocket ( ISocketHandler &   ) 

Definition at line 31 of file SmallSocket.cpp.

00032 :TcpSocket(h)
00033 ,m_state(STATE_LOGIN)
00034 ,m_name("")
00035 ,m_x(0)
00036 ,m_y(0)
00037 ,m_player(NULL)
00038 {
00039         SetLineProtocol();
00040 }

SmallSocket::~SmallSocket (  ) 

Definition at line 43 of file SmallSocket.cpp.

00044 {
00045 }


Member Function Documentation

void SmallSocket::OnAccept (  ) 

Definition at line 56 of file SmallSocket.cpp.

References Send(), and SendPrompt().

00057 {
00058         Send("&WWelcome.&n\n");
00059         SendPrompt();
00060 }

void SmallSocket::OnLine ( const std::string &   ) 

Definition at line 63 of file SmallSocket.cpp.

References PlayerFactory::AddPlayer(), PlayerFactory::FindPlayer(), GetPlayerF(), World::GetRandomLocation(), GetWorld(), m_name, m_passwd, m_player, m_state, m_x, m_y, PlayerFactory::Save(), Send(), SendPrompt(), STATE_LOGIN, STATE_NEW_PLAYER_QUESTION, STATE_PASSWORD, STATE_PASSWORD_1, STATE_PASSWORD_2, STATE_PROMPT, STATE_QUIT, try_move(), TryDrop(), and TryGet().

00064 {
00065         switch (m_state)
00066         {
00067         case STATE_LOGIN:
00068                 // check player
00069                 //  exist - ask password STATE_PASSWORD
00070                 //  don't exist - ask new player question STATE_NEW_PLAYER_QUESTION
00071                 m_player = GetPlayerF().FindPlayer(line, "");
00072                 if (m_player)
00073                 {
00074                         m_state = STATE_PASSWORD;
00075                 }
00076                 else
00077                 {
00078                         m_name = line;
00079                         m_state = STATE_NEW_PLAYER_QUESTION;
00080                 }
00081                 SendPrompt();
00082                 break;
00083         case STATE_PROMPT:
00084                 if (line.size())
00085                 {
00086                         Parse pa(line);
00087                         std::string cmd = pa.getword();
00088 
00089                         if (line[0] == 34 || line[0] == '\'')
00090                         {
00091                                 static_cast<SmallHandler&>(Handler()).Event(m_x,m_y,m_name + " says '" + line.substr(1) + "'\n",this);
00092                                 Send("You say '" + line.substr(1) + "&n'\n");
00093                         }
00094                         else
00095                         if (line[0] == '!')
00096                         {
00097                                 static_cast<SmallHandler&>(Handler()).Event(m_x,m_y,m_name + " shouts '" + line.substr(1) + "'\n",this,true);
00098                                 Send("You shout '" + line.substr(1) + "&n'\n");
00099                         }
00100                         else
00101                         if (line == "n" || line == "s" || line == "e" || line == "w")
00102                         {
00103                                 try_move(line);
00104                                 static_cast<SmallHandler&>(Handler()).ShowCell(this);
00105                         }
00106                         else
00107                         if (line == "l")
00108                         {
00109                                 static_cast<SmallHandler&>(Handler()).ShowCell(this);
00110                         }
00111                         else
00112                         if (line == "who")
00113                         {
00114                                 static_cast<SmallHandler&>(Handler()).Who(this);
00115                         }
00116                         else
00117                         if (line == "quit")
00118                         {
00119                                 m_state = STATE_QUIT;
00120                         }
00121                         else
00122                         if (cmd == "get")
00123                         {
00124                                 TryGet(this, pa.getrest());
00125                         }
00126                         else
00127                         if (cmd == "drop")
00128                         {
00129                                 TryDrop(this, pa.getrest());
00130                         }
00131                         else
00132                         if (line == "i")
00133                         {
00134                                 m_player -> DisplayInventory(this);
00135                         }
00136                         else
00137                         if (line == "status")
00138                         {
00139                                 static_cast<SmallHandler&>(Handler()).ShowStatus(this);
00140                         }
00141                         else
00142                         if (line == "help")
00143                         {
00144                                 Send("n / s / e / w - move around\n");
00145                                 Send("l - look around\n");
00146                                 Send("get / drop <item>\n");
00147                                 Send("i - display inventory\n");
00148                                 Send("' / \" - talk ( as in >\"hi )\n");
00149                                 Send("! - shout ( as in >!Hello anyone??? )\n");
00150                                 Send("who - is online\n");
00151                                 Send("status - show game status\n");
00152                                 Send("help\nquit\n");
00153                         }
00154                         else
00155                         {
00156                                 Send("Command not recognized - try '&Whelp&n'\n");
00157                         }
00158                 }
00159                 SendPrompt();
00160                 break;
00161         case STATE_QUIT:
00162                 break;
00163         case STATE_PASSWORD:
00164                 if (line == m_player -> m_passwd)
00165                 {
00166                         std::string str;
00167                         m_name = m_player -> m_name;
00168                         m_state = STATE_PROMPT;
00169                         GetWorld().GetRandomLocation(m_x,m_y,str);
00170                         Send("Welcome back, " + m_player -> m_name + ".\n");
00171                         Send("You wake up in a room: " + str + "\n");
00172                 }
00173                 else
00174                 {
00175                         m_state = STATE_LOGIN;
00176                 }
00177                 SendPrompt();
00178                 break;
00179         case STATE_NEW_PLAYER_QUESTION:
00180                 if (!line.size() || line[0] == 'y' || line[0] == 'Y')
00181                 {
00182                         m_state = STATE_PASSWORD_1;
00183                 }
00184                 else
00185                 {
00186                         m_state = STATE_LOGIN;
00187                 }
00188                 SendPrompt();
00189                 break;
00190         case STATE_PASSWORD_1:
00191                 if (line.size() > 3)
00192                 {
00193                         m_passwd = line;
00194                         m_state = STATE_PASSWORD_2;
00195                 }
00196                 else
00197                 {
00198                         Send("Enter at least 4 characters.\n");
00199                 }
00200                 SendPrompt();
00201                 break;
00202         case STATE_PASSWORD_2:
00203                 if (line == m_passwd)
00204                 {
00205                         m_player = GetPlayerF().AddPlayer(m_name,m_passwd);
00206                         m_state = STATE_PASSWORD;
00207                         OnLine(line);
00208                         GetPlayerF().Save();
00209                 }
00210                 else
00211                 {
00212                         Send("Password mismatch - try again\n");
00213                         m_state = STATE_NEW_PLAYER_QUESTION;
00214                         SendPrompt();
00215                 }
00216                 break;
00217         }
00218 }

const std::string& SmallSocket::GetName (  )  [inline]

Definition at line 50 of file SmallSocket.h.

References m_name.

00050 { return m_name; }

bool SmallSocket::IsAt ( int  x,
int  y 
)

Definition at line 251 of file SmallSocket.cpp.

References m_x, and m_y.

00252 {
00253         if (m_x == x && m_y == y)
00254                 return true;
00255         return false;
00256 }

int SmallSocket::GetX (  )  [inline]

Definition at line 52 of file SmallSocket.h.

References m_x.

00052 { return m_x; }

int SmallSocket::GetY (  )  [inline]

Definition at line 53 of file SmallSocket.h.

References m_y.

00053 { return m_y; }

World& SmallSocket::GetWorld (  )  [inline]

Definition at line 55 of file SmallSocket.h.

Referenced by OnLine(), and try_move().

00055 { return static_cast<SmallHandler&>(Handler()).GetWorld(); }

void SmallSocket::try_move ( const std::string &   ) 

Definition at line 259 of file SmallSocket.cpp.

References World::GetAt(), GetWorld(), m_name, m_x, m_y, and Send().

Referenced by OnLine().

00260 {
00261         std::string name;
00262         bool n,s,e,w;
00263         GetWorld().GetAt(m_x,m_y,name,n,s,e,w);
00264         if (dir == "n")
00265         {
00266                 if (n && GetWorld().FindAt(m_x,m_y - 1,name))
00267                 {
00268                         static_cast<SmallHandler&>(Handler()).Event(m_x,m_y,m_name + " leaves north\n",this);
00269                         m_y--;
00270                         static_cast<SmallHandler&>(Handler()).Event(m_x,m_y,m_name + " enters from the south\n",this);
00271                         return;
00272                 }
00273         }
00274         else
00275         if (dir == "s")
00276         {
00277                 if (s && GetWorld().FindAt(m_x,m_y + 1,name))
00278                 {
00279                         static_cast<SmallHandler&>(Handler()).Event(m_x,m_y,m_name + " leaves south\n",this);
00280                         m_y++;
00281                         static_cast<SmallHandler&>(Handler()).Event(m_x,m_y,m_name + " enters from the north\n",this);
00282                         return;
00283                 }
00284         }
00285         else
00286         if (dir == "e")
00287         {
00288                 if (e && GetWorld().FindAt(m_x + 1,m_y,name))
00289                 {
00290                         static_cast<SmallHandler&>(Handler()).Event(m_x,m_y,m_name + " leaves east\n",this);
00291                         m_x++;
00292                         static_cast<SmallHandler&>(Handler()).Event(m_x,m_y,m_name + " enters from the west\n",this);
00293                         return;
00294                 }
00295         }
00296         else
00297         if (dir == "w")
00298         {
00299                 if (w && GetWorld().FindAt(m_x - 1,m_y,name))
00300                 {
00301                         static_cast<SmallHandler&>(Handler()).Event(m_x,m_y,m_name + " leaves west\n",this);
00302                         m_x--;
00303                         static_cast<SmallHandler&>(Handler()).Event(m_x,m_y,m_name + " enters from the east\n",this);
00304                         return;
00305                 }
00306         }
00307         Send("&ROuch!&n\n");
00308 }

void SmallSocket::SendPrompt (  ) 

Definition at line 221 of file SmallSocket.cpp.

References m_state, Send(), STATE_LOGIN, STATE_NEW_PLAYER_QUESTION, STATE_PASSWORD, STATE_PASSWORD_1, STATE_PASSWORD_2, STATE_PROMPT, and STATE_QUIT.

Referenced by OnAccept(), and OnLine().

00222 {
00223         switch (m_state)
00224         {
00225         case STATE_LOGIN:
00226                 Send("Enter name: ");
00227                 break;
00228         case STATE_PROMPT:
00229                 Send("> ");
00230                 break;
00231         case STATE_QUIT:
00232                 Send("Goodbye!\n");
00233                 SetCloseAndDelete();
00234                 break;
00235         case STATE_PASSWORD:
00236                 Send("Enter password: ");
00237                 break;
00238         case STATE_NEW_PLAYER_QUESTION:
00239                 Send("Create a new player (Y,n)? ");
00240                 break;
00241         case STATE_PASSWORD_1:
00242                 Send("Enter new player password: ");
00243                 break;
00244         case STATE_PASSWORD_2:
00245                 Send("Repeat password: ");
00246                 break;
00247         }
00248 }

PlayerFactory& SmallSocket::GetPlayerF (  )  [inline]

Definition at line 58 of file SmallSocket.h.

Referenced by OnLine().

00058 { return static_cast<SmallHandler&>(Handler()).GetPlayerF(); }

void SmallSocket::Send ( const std::string &   ) 

Definition at line 48 of file SmallSocket.cpp.

References cstring::c_str().

Referenced by OnAccept(), OnLine(), SendPrompt(), try_move(), TryDrop(), and TryGet().

00049 {
00050         cstring q;
00051         q = str;
00052         TcpSocket::Send( q.c_str() );
00053 }

void SmallSocket::TryGet ( SmallSocket ,
const std::string &   
)

Definition at line 311 of file SmallSocket.cpp.

References m_name, m_player, m_x, m_y, and Send().

Referenced by OnLine().

00312 {
00313         item_v& ref = static_cast<SmallHandler&>(Handler()).GetItems(m_x,m_y);
00314         item_v& ref_to = m_player -> GetInventory();
00315         for (item_v::iterator it = ref.begin(); it != ref.end(); it++)
00316         {
00317                 ITEM *i = *it;
00318                 bool ok;
00319                 if (i -> m_amount > 1)
00320                 {
00321                         ok = !strcmp(i -> m_plural.uc_str(), item.c_str());
00322                 }
00323                 else
00324                 {
00325                         ok = !strcmp(i -> m_name.uc_str(), item.c_str());
00326                 }
00327                 if (ok)
00328                 {
00329                         static_cast<SmallHandler&>(Handler()).Event(m_x,m_y,m_name + " picks up " + i -> GetDescription() + "\n",this);
00330                         Send("You pick up " + i -> GetDescription() + "\n");
00331                         static_cast<SmallHandler&>(Handler()).MergeItem(ref_to,i);
00332                         ref.erase(it);
00333                         break;
00334                 }
00335         }
00336 }

void SmallSocket::TryDrop ( SmallSocket ,
const std::string &   
)

Definition at line 339 of file SmallSocket.cpp.

References m_name, m_player, m_x, m_y, and Send().

Referenced by OnLine().

00340 {
00341         item_v& ref = m_player -> GetInventory();
00342         item_v& ref_to = static_cast<SmallHandler&>(Handler()).GetItems(m_x,m_y);
00343         for (item_v::iterator it = ref.begin(); it != ref.end(); it++)
00344         {
00345                 ITEM *i = *it;
00346                 bool ok;
00347                 if (i -> m_amount > 1)
00348                 {
00349                         ok = !strcmp(i -> m_plural.uc_str(), item.c_str());
00350                 }
00351                 else
00352                 {
00353                         ok = !strcmp(i -> m_name.uc_str(), item.c_str());
00354                 }
00355                 if (ok)
00356                 {
00357                         static_cast<SmallHandler&>(Handler()).Event(m_x,m_y,m_name + " drops " + i -> GetDescription() + "\n",this);
00358                         Send("You drop " + i -> GetDescription() + "\n");
00359                         static_cast<SmallHandler&>(Handler()).MergeItem(ref_to,i);
00360                         ref.erase(it);
00361                         break;
00362                 }
00363         }
00364 }


Member Data Documentation

int SmallSocket::m_state [private]

Definition at line 64 of file SmallSocket.h.

Referenced by OnLine(), and SendPrompt().

std::string SmallSocket::m_name [private]

Definition at line 65 of file SmallSocket.h.

Referenced by GetName(), OnLine(), try_move(), TryDrop(), and TryGet().

int SmallSocket::m_x [private]

Definition at line 66 of file SmallSocket.h.

Referenced by GetX(), IsAt(), OnLine(), try_move(), TryDrop(), and TryGet().

int SmallSocket::m_y [private]

Definition at line 67 of file SmallSocket.h.

Referenced by GetY(), IsAt(), OnLine(), try_move(), TryDrop(), and TryGet().

Definition at line 68 of file SmallSocket.h.

Referenced by OnLine(), TryDrop(), and TryGet().

std::string SmallSocket::m_passwd [private]

Definition at line 69 of file SmallSocket.h.

Referenced by OnLine().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2006 by Anders Hedström
Generated on Mon Aug 29 20:21:47 2005 for C++ Sockets by  doxygen 1.4.4