* 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:
parent
f977376ac3
commit
24c9fae4f5
7 changed files with 59 additions and 62 deletions
|
@ -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
|
||||
{
|
||||
IState CurrentState { get; }
|
||||
Socket Connection { get; }
|
||||
List<byte> Buffer { get; set; }
|
||||
|
||||
string Name { get; set; }
|
||||
void Disconnect();
|
||||
void SwitchState(IState state);
|
||||
}
|
||||
}
|
|
@ -2,8 +2,7 @@
|
|||
{
|
||||
public interface IState
|
||||
{
|
||||
void Render(int index);
|
||||
void Render(IPlayer player);
|
||||
ICommand GetCommand();
|
||||
|
||||
}
|
||||
}
|
|
@ -7,16 +7,18 @@ namespace WinPC.Engine.Commands
|
|||
{
|
||||
private ServerDirector Director { get; set; }
|
||||
private IState NewState { get; set; }
|
||||
private int Index { get; set; }
|
||||
public SwitchStateCommand(ServerDirector director, IState newState, int index)
|
||||
private IPlayer player { get; set; }
|
||||
|
||||
public SwitchStateCommand(ServerDirector director, IState newState, IPlayer connectedPlayer)
|
||||
{
|
||||
Director = director;
|
||||
NewState = newState;
|
||||
player = connectedPlayer;
|
||||
}
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
Director.ConnectedPlayers[Index].SwitchState(NewState);
|
||||
player.SwitchState(NewState);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -9,7 +9,7 @@ namespace WinPC.Engine.Core
|
|||
public Socket Connection { 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; }
|
||||
|
||||
|
@ -18,6 +18,8 @@ namespace WinPC.Engine.Core
|
|||
Connection = connection;
|
||||
CurrentState = initialState;
|
||||
Name = "Player";
|
||||
|
||||
Buffer = new List<byte>();
|
||||
}
|
||||
|
||||
public void Disconnect()
|
||||
|
|
|
@ -31,6 +31,7 @@ namespace WinPC.Engine.Directors
|
|||
|
||||
public void AddConnection(Socket connection)
|
||||
{
|
||||
//TODO: Allow support for custom Player Types from scripts.
|
||||
var player = new Player(new ConnectState(this), connection);
|
||||
ConnectedPlayers.Add(player);
|
||||
|
||||
|
@ -42,20 +43,18 @@ namespace WinPC.Engine.Directors
|
|||
ConnectionThreads.Add(userThread);
|
||||
var index = ConnectionThreads.Count - 1;
|
||||
|
||||
ConnectionThreads[index].Name = "Player";
|
||||
ConnectionThreads[index].Start(index);
|
||||
|
||||
|
||||
player.Name = "Player";
|
||||
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)
|
||||
{
|
||||
player.CurrentState.Render((int)index);
|
||||
var command = player.CurrentState.GetCommand();
|
||||
connectedPlayer.CurrentState.Render(connectedPlayer);
|
||||
var command = connectedPlayer.CurrentState.GetCommand();
|
||||
command.Execute();
|
||||
}
|
||||
}
|
||||
|
@ -78,7 +77,7 @@ namespace WinPC.Engine.Directors
|
|||
}
|
||||
|
||||
|
||||
public String RecieveInput(int index)
|
||||
public String RecieveInput(IPlayer player)
|
||||
{
|
||||
string input = String.Empty;
|
||||
|
||||
|
@ -87,23 +86,24 @@ namespace WinPC.Engine.Directors
|
|||
try
|
||||
{
|
||||
byte[] buf = new byte[1];
|
||||
Int32 recved = ConnectedPlayers[index].Connection.Receive(buf);
|
||||
|
||||
Int32 recved = player.Connection.Receive(buf);
|
||||
|
||||
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')
|
||||
ConnectedPlayers[index].buffer.RemoveAt(ConnectedPlayers[index].buffer.Count - 1);
|
||||
if (player.Buffer[player.Buffer.Count - 1] == '\r')
|
||||
player.Buffer.RemoveAt(player.Buffer.Count - 1);
|
||||
|
||||
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
|
||||
input = enc.GetString(ConnectedPlayers[index].buffer.ToArray());
|
||||
ConnectedPlayers[index].buffer.Clear();
|
||||
input = enc.GetString(player.Buffer.ToArray());
|
||||
player.Buffer.Clear();
|
||||
//Return a trimmed string.
|
||||
return input;
|
||||
}
|
||||
else
|
||||
ConnectedPlayers[index].buffer.Add(buf[0]);
|
||||
player.Buffer.Add(buf[0]);
|
||||
}
|
||||
else if (recved == 0) //Disconnected
|
||||
{
|
||||
|
|
|
@ -11,46 +11,37 @@ namespace WinPC.Engine.States
|
|||
public class ConnectState : IState
|
||||
{
|
||||
public ServerDirector Director { get; private set; }
|
||||
private Socket Connection { get; set; }
|
||||
private ASCIIEncoding Encoding { get; set; }
|
||||
private int Index;
|
||||
|
||||
|
||||
private Socket connection;
|
||||
private ASCIIEncoding encoding;
|
||||
private IPlayer player;
|
||||
|
||||
public ConnectState(ServerDirector director)
|
||||
{
|
||||
Director = director;
|
||||
Encoding = new ASCIIEncoding();
|
||||
encoding = new ASCIIEncoding();
|
||||
|
||||
}
|
||||
public void Render(int index)
|
||||
public void Render(IPlayer connectedPlayer)
|
||||
{
|
||||
Index = index;
|
||||
|
||||
Connection = Director.ConnectedPlayers[index].Connection;
|
||||
connection = connectedPlayer.Connection;
|
||||
player = connectedPlayer;
|
||||
|
||||
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));
|
||||
connection.Send(encoding.GetBytes("Welcome to Scionwest's Mud Engine!" + "\n\r"));
|
||||
connection.Send(encoding.GetBytes("Please enter your name" + "\n\r"));
|
||||
}
|
||||
|
||||
public ICommand GetCommand()
|
||||
{
|
||||
|
||||
var input = Director.RecieveInput(Index);
|
||||
var input = Director.RecieveInput(player);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -10,38 +10,34 @@ namespace WinPC.Engine.States
|
|||
{
|
||||
|
||||
public ServerDirector Director { get; private set; }
|
||||
private Socket Connection { get; set; }
|
||||
private ASCIIEncoding Encoding { get; set; }
|
||||
private int Index;
|
||||
|
||||
private Socket connection { get; set; }
|
||||
private ASCIIEncoding encoding { get; set; }
|
||||
private IPlayer player { get; set; }
|
||||
|
||||
public MainMenuState(ServerDirector director)
|
||||
{
|
||||
Director = director;
|
||||
Encoding = new ASCIIEncoding();
|
||||
encoding = new ASCIIEncoding();
|
||||
|
||||
}
|
||||
public void Render(int index)
|
||||
public void Render(IPlayer connectedPlayer)
|
||||
{
|
||||
Index = index;
|
||||
|
||||
Connection = Director.ConnectedPlayers[index].Connection;
|
||||
connection = connectedPlayer.Connection;
|
||||
player = connectedPlayer;
|
||||
|
||||
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()
|
||||
{
|
||||
|
||||
var input = Director.RecieveInput(Index);
|
||||
var input = Director.RecieveInput(player);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue