* ConnectionManager now has a helper method for returning a Connected Character that matches the supplied name

* Added a new Command. SetRole allows Admins to assign a Server Role (Admin, Player, GM etc) to a specified character.  Server.ServerOwner is the only Character that can use this command regardless of server role.  Any other Character must have a Role of Admin in order to use it.
* StandardCharacter now checks if the character is connected as well as being enabled during execution of commands.
* Re-organized how the StandardCharacter save code was invoked during disconnection from server.
* StandardCharacter.Role is now saved and loaded from file.
This commit is contained in:
Scionwest_cp 2012-03-04 00:29:02 -08:00
parent f2c5b594c5
commit 2622c2d8c7
4 changed files with 170 additions and 6 deletions

View file

@ -140,6 +140,7 @@ namespace MudEngine.Game.Characters
SaveData.AddSaveData("Immovable", Immovable.ToString());
SaveData.AddSaveData("Password", Password);
SaveData.AddSaveData("Role", this.Role.ToString());
if (this.SaveData.Save(filename))
{
@ -156,6 +157,29 @@ namespace MudEngine.Game.Characters
this.Immovable = Convert.ToBoolean(this.SaveData.GetData("Immovable"));
this.Password = this.SaveData.GetData("Password");
String role = this.SaveData.GetData("Role");
this.Role = GetRole(role);
}
private CharacterRoles GetRole(String role)
{
//Blow all of the available values up into an array.
Array values = Enum.GetValues(typeof(CharacterRoles));
//Loop through each available value, converting it into a string.
foreach (Int32 value in values)
{
//Get the string representation of the current value
String displayName = Enum.GetName(typeof(CharacterRoles), value);
//Check if this value matches that of the supplied one.
//If so, return it as a enum
if (displayName.ToLower() == role.ToLower())
return (CharacterRoles)Enum.Parse(typeof(CharacterRoles), displayName);
}
return CharacterRoles.Player;
}
/// <summary>
@ -165,7 +189,7 @@ namespace MudEngine.Game.Characters
/// <returns></returns>
public Boolean ExecuteCommand(string command)
{
if (this.Enabled)
if (this.Enabled && this.Connected)
{
Boolean result = Commands.Execute(command, this);
@ -207,6 +231,7 @@ namespace MudEngine.Game.Characters
public void Disconnect()
{
Console.WriteLine("Disconnecting...");
this.Save(this.Filename);
//Purge all of this characters commands.
this.Destroy();
@ -215,6 +240,7 @@ namespace MudEngine.Game.Characters
this._Connection.Close();
this.LoggedIn = false;
this.Connected = false;
this.OnDisconnectEvent();
}
@ -269,14 +295,16 @@ namespace MudEngine.Game.Characters
}
else if (recved == 0) //Disconnected
{
this.Enabled = false;
this.Connected = false;
this.LoggedIn = false;
return "Disconnected.";
}
}
catch (Exception e)
{
//Flag as disabled
this.Enabled = false;
this.Connected = false;
this.LoggedIn = false;
return e.Message;
}
}
@ -340,9 +368,27 @@ namespace MudEngine.Game.Characters
public void SetRole(StandardCharacter adminCharacter, StandardCharacter subordinateCharacter, CharacterRoles role)
{
//Check to make sure the admin character is truly a admin
if (adminCharacter.Role == CharacterRoles.Admin)
//The Server Owner can change anyone characters Roles at any time regardless of the ServerOwners current Role.
if (adminCharacter.Role == CharacterRoles.Admin || adminCharacter.Name == this.Game.Server.ServerOwner)
{
//Do not allow any other Admins on the server to remove the ServerOwner from Admin status.
//Server owners can not have any other Role but Admin unless they change it themselves.
if (subordinateCharacter.Name == this.Game.Server.ServerOwner && adminCharacter.Name != this.Game.Server.ServerOwner)
{
//Tell the admin that is attempting to change the server owner that it's not allowed.
adminCharacter.SendMessage("You can not change the role of the Master admin.");
//Warn the server owner in the event this is a malicious attempt to take over the server.
subordinateCharacter.SendMessage(adminCharacter.Name + " attempted to change your server role!");
//Exit with no changes.
return;
}
//This was not the server owner, so change the role.
else
{
subordinateCharacter.Role = role;
subordinateCharacter.SendMessage("Your role was changed to " + role.ToString());
adminCharacter.SendMessage("You changed player '" + subordinateCharacter.Name + "' to the role of " + role.ToString());
}
}
else
{

View file

@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using MudEngine.Core.Interface;
using MudEngine.Game;
using MudEngine.Game.Characters;
using MudEngine.Networking;
namespace MudEngine.GameScripts.Commands
{
public class CommandSetRole : ICommand
{
public string Name { get; set; }
public string Description { get; set; }
public List<string> Help { get; set; }
public CommandSetRole()
{
this.Name = "SetRole";
this.Description = "Chat command that allows objects to communicate.";
this.Help = new List<string>();
this.Help.Add("Usage: SetRole TargetCharacterNameHere");
}
public Boolean Execute(string command, StandardCharacter character)
{
//Grab a reference to the character for simplifying access.
StandardGame game = character.Game;
MatchCollection matches = Regex.Matches(command.Substring("setrole".Length).Trim(), @"\w+");
List<String> names = new List<string>();
foreach (Match match in matches)
{
names.Add(match.Value.ToLower());
}
if (names.Count < 1)
{
character.SendMessage("You must provide a target character name.");
ShowHelp(character);
return false;
}
if (character.Role == CharacterRoles.Admin || character.Name == game.Server.ServerOwner)
{
StandardCharacter target = game.Server.ConnectionManager.GetConnectedCharacter(names[0].ToLower());
this.SetRole(character, target);
}
else
{
character.SendMessage("Invalid Command Used.");
return true;
}
return false;
}
public void SetRole(StandardCharacter admin, StandardCharacter target)
{
admin.SendMessage("Please choose from one of the available Roles:");
//Blow all of the available values up into an array.
Array values = Enum.GetValues(typeof(CharacterRoles));
List<String> roles = new List<string>();
//Loop through each available value, converting it into a string.
foreach (Int32 value in values)
{
//Get the string representation of the current value
String displayName = Enum.GetName(typeof(CharacterRoles), value);
roles.Add(displayName);
admin.SendMessage(displayName);
}
admin.SendMessage("Selection: ", false);
String result = admin.GetInput();
if (String.IsNullOrEmpty(result))
{
admin.SendMessage("You did not select a valid Role.");
return;
}
if (roles.Contains(result))
{
target.SetRole(admin, target, (CharacterRoles)Enum.Parse(typeof(CharacterRoles), result));
}
}
public void ShowHelp(StandardCharacter character)
{
String help = String.Empty;
foreach (String entry in this.Help)
{
help += entry + "\n";
}
character.SendMessage(help);
}
}
}

View file

@ -39,9 +39,19 @@ namespace MudEngine.Networking
this._ConnectedThreads.Add(new Thread(ReceiveDataThread));
Int32 index = this._ConnectedThreads.Count - 1;
this._ConnectedThreads[index].Name = character.Name;
this._ConnectedThreads[index].Start(index);
}
public StandardCharacter GetConnectedCharacter(String characterName)
{
var v = from player in this._ConnectedCharacters
where characterName == player.Name.ToLower()
select player;
return v.First();
}
public StandardCharacter[] GetConnectedCharacters()
{
return this._ConnectedCharacters.ToArray();
@ -76,7 +86,6 @@ namespace MudEngine.Networking
/// <param name="character"></param>
public void RemoveConnection(StandardCharacter character)
{
character.Save(character.Filename);
character.Disconnect();
foreach (StandardCharacter c in this._ConnectedCharacters)
{

View file

@ -59,6 +59,7 @@
<Compile Include="GameScripts\Commands\CommandSay.cs" />
<Compile Include="GameScripts\Commands\CommandStop.cs" />
<Compile Include="GameScripts\Commands\CommandCreatePlayer.cs" />
<Compile Include="GameScripts\Commands\CommandSetRole.cs" />
<Compile Include="Game\Characters\CharacterRoles.cs" />
<Compile Include="Game\Characters\CharacterStats.cs" />
<Compile Include="Game\Characters\MyCharacter.cs" />