00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <Parse.h>
00022 #include "SmallHandler.h"
00023 #include "World.h"
00024 #include "cstring.h"
00025 #include "ItemFactory.h"
00026 #include "SmallSocket.h"
00027
00028
00029
00030
00031 SmallSocket::SmallSocket(ISocketHandler& h)
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 }
00041
00042
00043 SmallSocket::~SmallSocket()
00044 {
00045 }
00046
00047
00048 void SmallSocket::Send(const std::string& str)
00049 {
00050 cstring q;
00051 q = str;
00052 TcpSocket::Send( q.c_str() );
00053 }
00054
00055
00056 void SmallSocket::OnAccept()
00057 {
00058 Send("&WWelcome.&n\n");
00059 SendPrompt();
00060 }
00061
00062
00063 void SmallSocket::OnLine(const std::string& line)
00064 {
00065 switch (m_state)
00066 {
00067 case STATE_LOGIN:
00068
00069
00070
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 }
00219
00220
00221 void SmallSocket::SendPrompt()
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 }
00249
00250
00251 bool SmallSocket::IsAt(int x,int y)
00252 {
00253 if (m_x == x && m_y == y)
00254 return true;
00255 return false;
00256 }
00257
00258
00259 void SmallSocket::try_move(const std::string& dir)
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 }
00309
00310
00311 void SmallSocket::TryGet(SmallSocket *sendto,const std::string& item)
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 }
00337
00338
00339 void SmallSocket::TryDrop(SmallSocket *sendto,const std::string& item)
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 }
00365
00366