* StandardCharacter now automatically invokes the Login command upon connection to the server. Login command not implemented completely yet.

* Telnet client header information is now successfully (finally) stripped out of the first stream received.
* Command System underwent some optimizations along with now always returning a Boolean value once execution of a command is completed.
* ICommand interface now forces all ICommand.Execute methods to return a Boolean value.
* DataPaths class re-wrote and is now simi in-use by StandardGame and the Login command
* Minor adjustments to how characters are initialized upon connection in various classes.
This commit is contained in:
Scionwest_cp 2012-03-03 20:57:47 -08:00
parent 0720393626
commit ec5a674062
10 changed files with 185 additions and 74 deletions

View file

@ -58,12 +58,7 @@ namespace MudEngine.Core
/// <returns></returns>
public List<ICommand> GetCommands()
{
List<ICommand> collection = new List<ICommand>();
foreach (ICommand c in CommandSystem.Commands.Values)
collection.Add(c);
return collection;
return CommandSystem.Commands.Values.ToList();
}
/// <summary>
@ -101,7 +96,7 @@ namespace MudEngine.Core
/// </summary>
/// <param name="command"></param>
/// <param name="character"></param>
public void Execute(string command, StandardCharacter character)
public Boolean Execute(string command, StandardCharacter character)
{
//All Types that implement ICommand must have their class name begin with Command.
//We must insert the 'Command' string into the beginning of the users Command
@ -119,20 +114,20 @@ namespace MudEngine.Core
try
{
//Execute the command
cmd.Execute(command, character);
return cmd.Execute(command, character);
}
catch (Exception ex)
{
Logger.WriteLine("Error: " + ex.Message);
Console.WriteLine("Error: " + ex.Message);
}
return;
}
}
//Let the player know that it was not a valid command.
//TODO: Implement another way of performing this. I don't want game related classes tied to the system.
character.SendMessage("Invalid Command Used.");
return false;
}
/// <summary>

View file

@ -13,6 +13,6 @@ namespace MudEngine.Core.Interface
string Description { get; set; }
List<string> Help { get; set; }
void Execute(string command, StandardCharacter character);
Boolean Execute(string command, StandardCharacter character);
}
}

View file

@ -2,32 +2,62 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
namespace MudEngine.DAL
{
public enum DataTypes
{
Players,
Environments,
Characters,
Equipment
}
/// <summary>
/// Contains the paths for the engines file storage.
/// </summary>
public struct DataPaths
public class DataPaths
{
/// <summary>
/// Path to the engines Script directory
/// </summary>
public String Scripts { get; set; }
public DataPaths()
{
String path = Assembly.GetExecutingAssembly().Location;
String assemblyFile = Path.GetFileName(path);
this._InstallRoot = path.Substring(0, path.Length - assemblyFile.Length);
/// <summary>
/// Path to the engines Environment files.
/// </summary>
public String Environments { get; set; }
this.SetAbsolutePath(Path.Combine(this._InstallRoot, "Players"), DataTypes.Players);
}
/// <summary>
/// Gets the Path to the Characters save directory
/// </summary>
public String Characters { get; set; }
public void SetAbsolutePath(String path, DataTypes objectType)
{
if (!path.EndsWith(@"\"))
path = path.Insert(path.Length, @"\");
/// <summary>
/// Gets the path to the saved players directory.
/// </summary>
public String Players { get; set; }
switch (objectType)
{
case DataTypes.Players:
this._Players = path;
break;
}
}
public void SetRelativePath(String path, DataTypes objectType)
{
}
public String GetPath(DataTypes objectType)
{
if (objectType == DataTypes.Players)
return this._Players;
else
return String.Empty;
}
private String _InstallRoot;
private String _Players;
private String _Environments;
private String _Characters;
private String _Equipment;
}
}

View file

@ -46,8 +46,28 @@ namespace MudEngine.Game.Characters
/// </summary>
public Boolean Immovable { get; set; }
/// <summary>
/// Gets or Sets if this character is enabled.
/// </summary>
public Boolean Enabled { get; set; }
/// <summary>
/// Gets or Sets if this client is fully logged into the account.
/// </summary>
public Boolean LoggedIn
{
get
{
return this._LoggedIn;
}
private set
{
if (value)
this.OnLoginEvent();
this._LoggedIn = value;
}
}
//TODO: Add current location to characters
//public IEnvironment CurrentLocation
@ -76,7 +96,6 @@ namespace MudEngine.Game.Characters
this._Writer = new StreamWriter(new NetworkStream(this._Connection, true));
this._Writer.AutoFlush = true; //Flushes the stream automatically.
this._InitialMessage = true; //Strips Telnet client garbage text from initial message sent from client.
}
public void Initialize()
@ -111,21 +130,27 @@ namespace MudEngine.Game.Characters
base.Load(filename);
}
internal void ExecuteCommand(string command)
internal Boolean ExecuteCommand(string command)
{
if (this.Enabled)
{
Commands.Execute(command, this);
Boolean result = Commands.Execute(command, this);
SendMessage("");
SendMessage("Command:", false);
return result;
}
else
return false;
}
public void Connect(Socket connection)
{
this._Connection = connection;
//this.Initialize();
OnConnectEvent();
}
@ -186,7 +211,8 @@ namespace MudEngine.Game.Characters
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
input = enc.GetString(buffer.ToArray());
buffer.Clear();
return input;
//Return a trimmed string.
return CleanString(input);
}
else
buffer.Add(buf[0]);
@ -206,6 +232,10 @@ namespace MudEngine.Game.Characters
}
}
public void FlushBuffer()
{
}
String CleanString(String line)
{
/*
@ -224,22 +254,22 @@ namespace MudEngine.Game.Characters
else
return String.Empty;
* */
Regex invalidChars = new Regex(
@"(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F\uFEFF\uFFFE\uFFFF]",
RegexOptions.Compiled);
if (String.IsNullOrEmpty(line))
return "";
else
return invalidChars.Replace(line, "");
Match m = Regex.Match(line, @"\w+"); // Regex.Replace(line, @"[^\u0000-\u007F]", "");
line = m.Value;
return line.Trim();
}
public delegate void OnConnectHandler();
public event OnConnectHandler OnConnectEvent;
public void OnConnect()
{
this.SendMessage(this.Game.Server.MOTD);
//Greet the user with the game information
this.SendMessage(this.Game.Name);
this.SendMessage(this.Game.Description);
this.SendMessage(String.Empty);
//Log the user in.
this.LoggedIn = this.ExecuteCommand("Login");
}
public delegate void OnDisconnectHandler();
@ -252,13 +282,13 @@ namespace MudEngine.Game.Characters
public event OnLoginHandler OnLoginEvent;
public void OnLogin()
{
this.SendMessage(this.Game.Server.MOTD);
}
private Socket _Connection;
private StreamReader _Reader;
private StreamWriter _Writer;
private Boolean _InitialMessage;
private Boolean _LoggedIn;
private List<byte> buffer = new List<byte>();
}
}

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.IO;
using MudEngine.Networking;
using MudEngine.Core;
@ -108,12 +109,10 @@ namespace MudEngine.Game
//Setup default save paths.
DataPaths paths = new DataPaths();
paths.Environments = @"\Environment";
paths.Characters = @"\Characters";
paths.Players = @"\SavedPlayer";
paths.Scripts = @"\Scripts";
this.SavePaths = paths;
SetupPaths();
}
/// <summary>
@ -169,5 +168,11 @@ namespace MudEngine.Game
{
}
private void SetupPaths()
{
if (!Directory.Exists(this.SavePaths.GetPath(DataTypes.Players)))
Directory.CreateDirectory(this.SavePaths.GetPath(DataTypes.Players));
}
}
}

View file

@ -66,7 +66,7 @@ namespace MudEngine.GameScripts
if (!File.Exists(filename))
return;
XElement data = XElement.Load(filename);
//XElement data = XElement.Load(filename);
}
catch
{

View file

@ -28,13 +28,24 @@ namespace MudEngine.GameScripts.Commands
Description = "Account login command.";
}
public void Execute(string command, Game.Characters.StandardCharacter character)
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.SendMessage("Enter your character name: ", false);
@ -42,36 +53,73 @@ namespace MudEngine.GameScripts.Commands
name = String.Empty;
Boolean isFound = false;
while (String.IsNullOrEmpty(name))
//Get the supplied name
name = character.GetInput();
//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.GetPath(DAL.DataTypes.Players) + name;
if (File.Exists(game.SavePaths.GetPath(DAL.DataTypes.Players) + name))
isFound = true;
//if the character name supplied exists, load it.
if (isFound)
{
name = character.GetInput();
//Perform a password check
character.SendMessage("Please enter a password for " + name);
String password = character.GetInput();
if (String.IsNullOrEmpty(name))
//If the password is empty, then restart the process.
if (String.IsNullOrEmpty(password))
{
name = String.Empty;
continue;
}
//Look if the file exists.
if (File.Exists(game.SavePaths.Players + @"\" + name))
isFound = true;
//Load the character from file.
character.Load(game.SavePaths.GetPath(DAL.DataTypes.Players) + name);
//Check if the characters password matches that of the saved player password
if ("1234" != 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")
{
return CreateCharacter(name);
}
else
{
character.SendMessage("Enter your password: ", false);
String password = character.GetInput();
if (String.IsNullOrEmpty(password))
{
//Reset the process if no password supplied.
name = String.Empty;
continue;
}
else
{
}
continue;
}
}
}
return false;
}
private Boolean CreateCharacter(String name)
{
return false;
}
private StandardCharacter _Character;
}
}

View file

@ -24,7 +24,7 @@ namespace MudEngine.GameScripts.Commands
this.Description = "Chat command that allows objects to communicate.";
}
public void Execute(string command, StandardCharacter character)
public Boolean Execute(string command, StandardCharacter character)
{
//Grab a reference to the character for simplifying access.
StandardGame game = character.Game;
@ -42,6 +42,8 @@ namespace MudEngine.GameScripts.Commands
//Send a different copy of the message to the broadcastor.
character.SendMessage("You say: " + message);
return true;
}
}
}

View file

@ -25,13 +25,14 @@ namespace MudEngine.GameScripts.Commands
this.Description = "Chat command that allows objects to communicate.";
}
public void Execute(string command, StandardCharacter character)
public Boolean Execute(string command, StandardCharacter character)
{
//Grab a reference to the character for simplifying access.
StandardGame game = character.Game;
//Stop the game.
new Thread(game.Stop).Start();
return true;
}
}
}

View file

@ -35,6 +35,7 @@ namespace MudEngine.Networking
StandardCharacter character = new StandardCharacter(game, "New Player", "New networked client.", connection);
//Invoke the Characters Server connection method
character.Initialize();
character.Connect(connection);
this._ConnectedCharacters.Add(character);
this._ConnectedThreads.Add(new Thread(ReceiveDataThread));
@ -51,7 +52,6 @@ namespace MudEngine.Networking
private void ReceiveDataThread(Object index)
{
StandardCharacter character = this._ConnectedCharacters[(Int32)index];
character.Initialize();
while (character.Game.Server.Status == ServerStatus.Running &&
character.Enabled)