muddesigner/MudEngine/WinPC_Engine/GameScripts/Commands/CommandLogin.cs
Scionwest_cp f2c5b594c5 * DataPaths now has two new helper methods. GetFilePath for returning the path to a supplied filename for a specified object type. GetExtension which returns a file extension for the specified object type.
* XMLData now contains a GetData() method for returning data from the stored data collection.
* XMLData now contains a Load() method for loading a previously saved XML data file.
* StandardCharacter now automatically generates a filename.
* StandardCharacter.Connected added.  Use Connected to check if they are connected to the server regardless of the values for Enabled and LoggedIn.  LoggedIn is now true once the Login command is completed.
* Default Character Role is now Player.
* Server.ServerOwner property added.  When a character is logged in matching the ServerOwner name, it will automatically be assigned the Admin role.
* StandardCharacter.ExecuteSilentCommand() method added for executing a command and not having the "Command: " line printed to the screen when the command is completed.  Useful for daisy chained commands.
* StandardCharacter login code is now 100% completed.  Including save/load code and new character creation.
* StandardCharacter.SetRole() method added.  Admins can set the role of any other character in the game if they want to.
* BaseScript & StandardCharacter now have their Load() code fully implemented.  They can save and load their files now.
* Player creation command added.  Can only be executed from within the login command.  If it is executed from any other object it will bail.
* Stop command now only works when a Character with Role = Admin issues the command.  During development of a MUD Game, this would typically be the Server.ServerOwner character who will have Admin rights.
* ConnectionManager had some bugs fixed such as not removing Threads from the Thread collection when a character disconnected.  Also re-organized the character connection code some.
2012-03-03 23:29:58 -08:00

165 lines
6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using MudEngine.Core.Interface;
using MudEngine.Game;
using MudEngine.Game.Characters;
using MudEngine.Game.Environment;
using MudEngine.GameScripts;
namespace MudEngine.GameScripts.Commands
{
public class CommandLogin : ICommand
{
public string Name { get; set; }
public string Description { get; set; }
public List<string> Help { get; set; }
public CommandLogin()
{
Help = new List<string>();
Name = "Login";
Description = "Account login command.";
}
public Boolean Execute(string command, Game.Characters.StandardCharacter character)
{
//reference to the Characters Game.
StandardGame game = character.Game;
//Store a reference to this character for other methods within this class.
this._Character = character;
if (character.LoggedIn)
{
character.SendMessage("You are already logged in!");
return false;
}
character.SendMessage("Please enter character name: ");
String name = String.Empty;
//Repeat the login process until we get a valid name.
while (String.IsNullOrEmpty(name) && character.Connected)
{
character.SendMessage("Enter your character name: ", false);
name = String.Empty;
Boolean isFound = false;
//Get the supplied name
name = character.GetInput();
//Entering their username is the first time input has been
//made by the user. Expect their Telnet client to send Header
//information, so we strip it out by forcing only a whole
//word to be saved and everything else discarded.
//Note that this wouldn't work if first and last names were supported
//as this only returns the first word found and that is all.
Match m = Regex.Match(name, @"\w+");
name = m.Value;
//Make sure no illegal characters are in the name such as underbars or asteriks
if (!name.All(Char.IsLetterOrDigit))
{
character.SendMessage("Invalid character name supplied. Only letters and numbers are allowed.");
name = String.Empty;
continue;
}
//Uncomment this if first/last name combinations are used in
//the game. Note that this does support numbers and other
//special characters. If you do not want them, you will need
//to modify the Regular Expression Evaluator below.
/*
MatchCollection m = Regex.Matches(name, @"\w+");
name = String.Empty;
foreach (Match word in m)
{
name += word.Value + " ";
}
*/
//Check if the name entered is blank. Ensure that we remove leading and ending spaces
if (String.IsNullOrEmpty(name))
continue;
//Look if the file exists.
String filename = game.SavePaths.GetFilePath(DAL.DataTypes.Players, name);
if (File.Exists(filename))
isFound = true;
//if the character name supplied exists, load it.
if (isFound)
{
//Perform a password check
character.SendMessage("Please enter a password for " + name);
String password = character.GetInput();
//If the password is empty, then restart the process.
if (String.IsNullOrEmpty(password))
{
name = String.Empty;
continue;
}
//Load the character from file.
character.Load(filename);
//Check if the characters password matches that of the saved player password
if (character.Password != password)
{
//No match, bail.
character.SendMessage("Invalid password provided.");
name = String.Empty;
continue;
}
else //End our loading.
{
character.SendMessage("Welcome back " + character.Name + "!");
return true;
}
}
else
{
character.SendMessage("No character with that name was found. Create a new one? (Yes/No)");
String result = character.GetInput();
if (result.ToLower() == "yes")
{
//Store the character name.
character.Name = name;
if (!character.ExecuteSilentCommand("CreatePlayer"))
{
name = String.Empty;
continue;
}
else
{
//We complete the login process..
character.SendMessage(character.Name + " created!");
return true;
}
}
else
{
name = String.Empty;
continue;
}
}
}
return false;
}
private StandardCharacter _Character;
}
}