* IPlayer - Connection and Buffer properties added along with SwitchState(IState) method

* Switched the ServerDirector over from using two List Collections to maintain Client Threads and Player instances into a single Dictionary.  Required adjusting the majority of the existing Types, but should now be functional.  Need to still add some safety checks for null references since we are now passing a reference Type around.
This commit is contained in:
Scionwest_cp 2012-06-06 20:56:30 -07:00
parent f977376ac3
commit 24c9fae4f5
7 changed files with 59 additions and 62 deletions

View file

@ -1,9 +1,16 @@
namespace WinPC.Engine.Abstract.Core using System.Net.Sockets;
using System.Collections.Generic;
namespace WinPC.Engine.Abstract.Core
{ {
public interface IPlayer public interface IPlayer
{ {
IState CurrentState { get; } IState CurrentState { get; }
Socket Connection { get; }
List<byte> Buffer { get; set; }
string Name { get; set; } string Name { get; set; }
void Disconnect(); void Disconnect();
void SwitchState(IState state);
} }
} }

View file

@ -2,8 +2,7 @@
{ {
public interface IState public interface IState
{ {
void Render(int index); void Render(IPlayer player);
ICommand GetCommand(); ICommand GetCommand();
} }
} }

View file

@ -7,16 +7,18 @@ namespace WinPC.Engine.Commands
{ {
private ServerDirector Director { get; set; } private ServerDirector Director { get; set; }
private IState NewState { get; set; } private IState NewState { get; set; }
private int Index { get; set; } private IPlayer player { get; set; }
public SwitchStateCommand(ServerDirector director, IState newState, int index)
public SwitchStateCommand(ServerDirector director, IState newState, IPlayer connectedPlayer)
{ {
Director = director; Director = director;
NewState = newState; NewState = newState;
player = connectedPlayer;
} }
public void Execute() public void Execute()
{ {
Director.ConnectedPlayers[Index].SwitchState(NewState); player.SwitchState(NewState);
} }
} }
} }

View file

@ -9,7 +9,7 @@ namespace WinPC.Engine.Core
public Socket Connection { get; private set; } public Socket Connection { get; private set; }
public IState CurrentState { get; private set; } public IState CurrentState { get; private set; }
public List<byte> buffer = new List<byte>(); public List<byte> Buffer { get; set; }
public string Name { get; set; } public string Name { get; set; }
@ -18,6 +18,8 @@ namespace WinPC.Engine.Core
Connection = connection; Connection = connection;
CurrentState = initialState; CurrentState = initialState;
Name = "Player"; Name = "Player";
Buffer = new List<byte>();
} }
public void Disconnect() public void Disconnect()

View file

@ -31,6 +31,7 @@ namespace WinPC.Engine.Directors
public void AddConnection(Socket connection) public void AddConnection(Socket connection)
{ {
//TODO: Allow support for custom Player Types from scripts.
var player = new Player(new ConnectState(this), connection); var player = new Player(new ConnectState(this), connection);
ConnectedPlayers.Add(player); ConnectedPlayers.Add(player);
@ -42,20 +43,18 @@ namespace WinPC.Engine.Directors
ConnectionThreads.Add(userThread); ConnectionThreads.Add(userThread);
var index = ConnectionThreads.Count - 1; var index = ConnectionThreads.Count - 1;
ConnectionThreads[index].Name = "Player"; player.Name = "Player";
ConnectionThreads[index].Start(index); userThread.Start(player);
} }
public void ReceiveDataThread(object index) public void ReceiveDataThread(object player)
{ {
var player = ConnectedPlayers[(int)index]; var connectedPlayer = (IPlayer)player;
while (Server.Enabled) while (Server.Enabled)
{ {
player.CurrentState.Render((int)index); connectedPlayer.CurrentState.Render(connectedPlayer);
var command = player.CurrentState.GetCommand(); var command = connectedPlayer.CurrentState.GetCommand();
command.Execute(); command.Execute();
} }
} }
@ -78,7 +77,7 @@ namespace WinPC.Engine.Directors
} }
public String RecieveInput(int index) public String RecieveInput(IPlayer player)
{ {
string input = String.Empty; string input = String.Empty;
@ -87,23 +86,24 @@ namespace WinPC.Engine.Directors
try try
{ {
byte[] buf = new byte[1]; byte[] buf = new byte[1];
Int32 recved = ConnectedPlayers[index].Connection.Receive(buf);
Int32 recved = player.Connection.Receive(buf);
if (recved > 0) if (recved > 0)
{ {
if (buf[0] == '\n' && ConnectedPlayers[index].buffer.Count > 0) if (buf[0] == '\n' && player.Buffer.Count > 0)
{ {
if (ConnectedPlayers[index].buffer[ConnectedPlayers[index].buffer.Count - 1] == '\r') if (player.Buffer[player.Buffer.Count - 1] == '\r')
ConnectedPlayers[index].buffer.RemoveAt(ConnectedPlayers[index].buffer.Count - 1); player.Buffer.RemoveAt(player.Buffer.Count - 1);
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding(); System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
input = enc.GetString(ConnectedPlayers[index].buffer.ToArray()); input = enc.GetString(player.Buffer.ToArray());
ConnectedPlayers[index].buffer.Clear(); player.Buffer.Clear();
//Return a trimmed string. //Return a trimmed string.
return input; return input;
} }
else else
ConnectedPlayers[index].buffer.Add(buf[0]); player.Buffer.Add(buf[0]);
} }
else if (recved == 0) //Disconnected else if (recved == 0) //Disconnected
{ {

View file

@ -11,46 +11,37 @@ namespace WinPC.Engine.States
public class ConnectState : IState public class ConnectState : IState
{ {
public ServerDirector Director { get; private set; } public ServerDirector Director { get; private set; }
private Socket Connection { get; set; }
private ASCIIEncoding Encoding { get; set; } private Socket connection;
private int Index; private ASCIIEncoding encoding;
private IPlayer player;
public ConnectState(ServerDirector director) public ConnectState(ServerDirector director)
{ {
Director = director; Director = director;
Encoding = new ASCIIEncoding(); encoding = new ASCIIEncoding();
} }
public void Render(int index) public void Render(IPlayer connectedPlayer)
{ {
Index = index; connection = connectedPlayer.Connection;
player = connectedPlayer;
Connection = Director.ConnectedPlayers[index].Connection; connection.Send(encoding.GetBytes("Welcome to Scionwest's Mud Engine!" + "\n\r"));
connection.Send(encoding.GetBytes("Please enter your name" + "\n\r"));
Connection.Send(Encoding.GetBytes("Welcome to Scionwest's Mud Engine!"+"\n\r"));
Connection.Send(Encoding.GetBytes("Please enter your name" + "\n\r"));
//Just used for testing ServerDirector.GetPlayer method
IPlayer player = null;
bool validPlayer = Director.GetPlayer("Player", out player);
//player = player ?? new IPlayer().Create();
if (validPlayer)
Connection.Send(Encoding.GetBytes("Welcome " + player.Name));
} }
public ICommand GetCommand() public ICommand GetCommand()
{ {
var input = Director.RecieveInput(Index); var input = Director.RecieveInput(player);
if (input == "menu") if (input == "menu")
{ {
return new SwitchStateCommand(Director, new MainMenuState(Director), Index); return new SwitchStateCommand(Director, new MainMenuState(Director), player);
} }
return new InvalidCommand(Connection); return new InvalidCommand(connection);
} }
} }
} }

View file

@ -10,38 +10,34 @@ namespace WinPC.Engine.States
{ {
public ServerDirector Director { get; private set; } public ServerDirector Director { get; private set; }
private Socket Connection { get; set; }
private ASCIIEncoding Encoding { get; set; } private Socket connection { get; set; }
private int Index; private ASCIIEncoding encoding { get; set; }
private IPlayer player { get; set; }
public MainMenuState(ServerDirector director) public MainMenuState(ServerDirector director)
{ {
Director = director; Director = director;
Encoding = new ASCIIEncoding(); encoding = new ASCIIEncoding();
} }
public void Render(int index) public void Render(IPlayer connectedPlayer)
{ {
Index = index; connection = connectedPlayer.Connection;
player = connectedPlayer;
Connection = Director.ConnectedPlayers[index].Connection;
Director.ConnectedPlayers[index].Connection.Send(Encoding.GetBytes("Your now in the Main Menu State Welcome!! !"+"\n\r"));
connection.Send(encoding.GetBytes("Your now in the Main Menu State Welcome!! !" + "\n\r"));
} }
public ICommand GetCommand() public ICommand GetCommand()
{ {
var input = Director.RecieveInput(player);
var input = Director.RecieveInput(Index);
if(input == "connect") if(input == "connect")
{ {
return new SwitchStateCommand(Director, new ConnectState(Director),Index); return new SwitchStateCommand(Director, new ConnectState(Director),player);
} }
return new InvalidCommand(Connection); return new InvalidCommand(connection);
} }
} }
} }