Logo
~Sockets~
~Examples~
~Contact~

Login.cpp

Go to the documentation of this file.
00001 // Login.cpp
00002 // released 2006-09-25
00003 /*
00004 Copyright (C) 2006  Anders Hedstrom (grymse@alhem.net)
00005 
00006 This program is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU General Public License
00008 as published by the Free Software Foundation; either version 2
00009 of the License, or (at your option) any later version.
00010 
00011 This program is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU General Public License for more details.
00015 
00016 You should have received a copy of the GNU General Public License
00017 along with this program; if not, write to the Free Software
00018 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 */
00020 #include <Utility.h>
00021 
00022 #include "Login.h"
00023 #include "InSocket.h"
00024 #include "Chunk.h"
00025 #include "Player.h"
00026 #include "Race.h"
00027 
00028 
00029 void Login::Execute(InSocket *,const std::string& params,Parse& pa)
00030 {
00031 }
00032 
00033 
00034 void Login::OnLine(InSocket *from,const std::string& line,int state)
00035 {
00036         Database& db = from -> GetDatabase();
00037         switch (state)
00038         {
00039         case 0: // name
00040                 if (line.size())
00041                 {
00042                         Player pl(db, line);
00043                         from -> SetCommandData(line);
00044                         if (pl.Exists())
00045                         {
00046                                 // %! stealth (echo off) password
00047                                 from -> SetPrompt(this, 1, "Password? ");
00048                         }
00049                         else
00050                         {
00051                                 from -> Send("Name does not exist.\n");
00052                                 from -> SetPrompt(this, 2, "Create a new player named '" + line + "' (y/N)? ");
00053                         }
00054                 }
00055                 else
00056                 {
00057                         from -> Send("Name required (bye).\n");
00058                         from -> SetCloseAndDelete();
00059                 }
00060                 break;
00061         case 1: // password
00062                 {
00063                         Player pl(db, from -> GetCommandData());
00064                         if (!strcmp(pl.GetPasswd().c_str(), line.c_str()))
00065                         {
00066                                 from -> Count("Number of successful logins");
00067                                 from -> GlobalEvent(pl.GetDisplayName() + " enters the game.");
00068                                 pl.VerifyPos();
00069                                 pl.LoadSteps();
00070                                 from -> SetAccountName(pl.GetName());
00071                                 from -> Send("Welcome, " + pl.GetDisplayName() + "!\n");
00072                                 if (!pl.GetRace())
00073                                 {
00074                                         int n = Race::DisplayRaces(from);
00075                                         from -> SetPrompt(this, 6, "Select a race (1-" + Utility::l2string(n) + ")? ");
00076                                 }
00077                                 else
00078                                 {
00079                                         from -> SetPrompt();
00080                                 }
00081                         }
00082                         else
00083                         {
00084                                 from -> Send("Name and/or password incorrect.\n");
00085                                 from -> SetPrompt(this, 0, "Name, please? ");
00086                         }
00087                 }
00088                 break;
00089         case 2: // create (y/N)?
00090                 if (line.size() && (line[0] == 'y' || line[0] == 'Y'))
00091                 {
00092                         from -> SetPrompt(this, 3, "Enter a password: ");
00093                 }
00094                 else
00095                 {
00096                         from -> SetPrompt(this, 0, "Name, please? ");
00097                 }
00098                 break;
00099         case 3: // enter new password
00100                 if (line.size() < 4)
00101                 {
00102                         from -> Send("Enter at least 4 characters.\n");
00103                         from -> SetPrompt(this, 3, "Enter a password: ");
00104                 }
00105                 else
00106                 {
00107                         from -> SetCommandData2(line);
00108                         from -> SetPrompt(this, 4, "Repeat password: ");
00109                 }
00110                 break;
00111         case 4: // repeat password
00112                 if (!strcmp(line.c_str(), from -> GetCommandData2().c_str()))
00113                 {
00114                         from -> Count("Number of created logins");
00115                         // wizard set by Player constructor
00116                         // first created is wizard
00117                         Player pl(db, from -> GetCommandData());
00118                         pl.SetName(from -> GetCommandData());
00119                         pl.SetPasswd(line);
00120                         pl.SetDisplayName(from -> GetCommandData());
00121                         pl.VerifyPos();
00122                         pl.Save();
00123                         from -> SetAccountName(from -> GetCommandData());
00124                         from -> SetPrompt(this, 5, "Enter your email: ");
00125                 }
00126                 else
00127                 {
00128                         from -> Send("Password mismatch, please try again.\n");
00129                         from -> SetPrompt(this, 3, "Enter a password: ");
00130                 }
00131                 break;
00132         case 5: // email
00133                 if (line.size())
00134                 {
00135                         Player pl(db, from -> GetAccountName());
00136                         pl.SetEmail(line);
00137                         pl.Save();
00138                 }
00139                 from -> GlobalEvent("New player " + from -> GetCommandData() + " enters the game.");
00140                 from -> Send("Welcome, new player " + from -> GetCommandData() + "!\n");
00141                 Race::DisplayRaces(from);
00142                 from -> SetPrompt(this, 6, "Select a race: ");
00143                 break;
00144         case 6: // race
00145                 {
00146                         Player pl(db, from -> GetAccountName());
00147                         long num = atol(line.c_str());
00148                         Race r(db, num);
00149                         if (r.Exists())
00150                         {
00151                                 pl.SetRace(num);
00152                                 pl.Save();
00153                                 //
00154                                 from -> SetPrompt();
00155                         }
00156                         else
00157                         if (pl.IsWizard())
00158                         {
00159                                 from -> Send("Skipping race selection, for now..\n");
00160                                 from -> SetPrompt();
00161                         }
00162                         else
00163                         {
00164                                 Race::DisplayRaces(from);
00165                                 from -> SetPrompt(this, 6, "Select a race: ");
00166                         }
00167                 }
00168                 break;
00169         }
00170 }
00171 
00172 
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