/*********************************************************** * WindowMud - Window Mud Server * * File: Player.cpp * * Usage: Manages players * * * * No warranty is given or implied. * * There is no license associated with this code. * * There are no restrictions on the use of this code. * * This code may be used in any way the reader wishes. * * No credit or credits are given and none are required. * * The reader may take this code and claim they wrote it. * ************************************************************/ /*********************************************************** * Includes * ************************************************************/ #include "stdafx.h" // precompiled headers #include "Player.h" // Server #include "Dnode.h" #include "Log.h" #include "Utility.h" /*********************************************************** * Globals * ************************************************************/ extern Dnode *pDnodeActor; extern CString ErrorMsg; int Player::Count = 0; /*********************************************************** * Constructor * ************************************************************/ Player::Player() { // Player variables Count++; Output = ""; Stuff = ""; // Player file variables Name = ""; Password = ""; Sex = ""; } /*********************************************************** * Destructor * ************************************************************/ Player::~Player() { Count--; } //////////////////////////////////////////////////////////// // Public functions static // //////////////////////////////////////////////////////////// /*********************************************************** * Is this a valid Player? * ************************************************************/ bool Player::IsPlayer(CString PlayerName) { CString PlayerFileName; CStdioFile PlayerFile; int Success; PlayerFileName = PLAYER_DIR; PlayerFileName += PlayerName + ".txt"; Success = PlayerFile.Open(PlayerFileName, CFile::modeRead | CFile::typeText); if(Success) { return true; PlayerFile.Close(); } else { return false; } } /*********************************************************** * Return player count * ************************************************************/ int Player::GetCount() { return Count; } /*********************************************************** * Validate player name * ************************************************************/ bool Player::IsNameValid(CString Name) { CString NameIn; int Success; CStdioFile ValidNameFile; CString ValidNamesFileName; ValidNamesFileName = VALID_NAMES_DIR; ValidNamesFileName += "ValidNames.txt"; Success = ValidNameFile.Open(ValidNamesFileName, CFile::modeRead | CFile::typeText); if(!Success) { // Ok, who deleted the valid names file? ErrorMsg = "Player::IsNameValid - Error opening valid name file, it may not exist"; Utility::BlowUp(); } Name.MakeLower(); ValidNameFile.ReadString(NameIn); NameIn.MakeLower(); while (NameIn != "") { // Read all names if (Name == NameIn) { // Name is valid ValidNameFile.Close(); return true; } ValidNameFile.ReadString(NameIn); } ValidNameFile.Close(); return false; } //////////////////////////////////////////////////////////// // Public functions // //////////////////////////////////////////////////////////// /*********************************************************** * Create player prompt * ************************************************************/ void Player::CreatePrompt() { CString TmpStr; Output = "\r\n"; Output += "> "; } /*********************************************************** * Get player output * ************************************************************/ CString Player::GetOutput() { return Output; } /*********************************************************** * Parse player stuff * ************************************************************/ void Player::ParsePlayerStuff() { CString LogBuf; CString Name; CString TmpStr; Name = pDnodeActor->PlayerName; if (!OpenFile(Name, "Read")) { ErrorMsg = "Player::Save - Error opening player file for read, Players directory may not exist"; Utility::BlowUp(); } ReadLine(); while (Stuff != "") { // Name if (Stuff.Left(5) == "Name:") { TmpStr = ""; // Already got the name } else // Password if (Stuff.Left(9) == "Password:") { Password = Stuff.Right(Stuff.GetLength()-9); } else // Sex if (Stuff.Left(4) == "Sex:") { Sex = Stuff.Right(Stuff.GetLength()-4); Sex.MakeUpper(); } else // Unidentified field in the player's file { LogBuf = Name; LogBuf += " has an unidentified player file field"; Log::LogIt(LogBuf); LogBuf = Stuff; Log::LogIt(LogBuf); } // Read the next line ReadLine(); } CloseFile(); } /*********************************************************** * Save player stuff * ************************************************************/ void Player::Save() { CString TmpStr; if (!OpenFile(Name, "Write")) { ErrorMsg = "Player::Save - Error opening player file for write, Players directory may not exist"; Utility::BlowUp(); } // Name Stuff = "Name:" + Name; WriteLine(Stuff); // Password Stuff = "Password:" + Password; WriteLine(Stuff); // Sex Stuff = "Sex:" + Sex; WriteLine(Stuff); // Done CloseFile(); } //////////////////////////////////////////////////////////// // Private functions // //////////////////////////////////////////////////////////// /*********************************************************** * Close player file * ************************************************************/ void Player::CloseFile() { PlayerFile.Close(); } /*********************************************************** * Open player file * ************************************************************/ bool Player::OpenFile(CString Name, CString Mode) { CString PlayerFileName; int Success; PlayerFileName = PLAYER_DIR; PlayerFileName += Name + ".txt"; if (Mode == "Read") { Success = PlayerFile.Open(PlayerFileName, CFile::modeRead | CFile::typeText); if(!Success) { return false; } else { return true; } } else if (Mode == "Write") { Success = PlayerFile.Open(PlayerFileName, CFile::modeCreate | CFile::modeWrite | CFile::typeText); if(!Success) { return false; } else { return true; } } else { ErrorMsg = "Player::OpenFile - Mode is not 'Read' or 'Write'"; Utility::BlowUp(); return false; // Never will execute this return, but stops warning message } } /*********************************************************** * Read a line from player file * ************************************************************/ void Player::ReadLine() { PlayerFile.ReadString(Stuff); } /*********************************************************** * Write a line to player file * ************************************************************/ void Player::WriteLine(CString Stuff) { Stuff = Stuff + "\n"; PlayerFile.WriteString(Stuff); PlayerFile.Flush(); }