- Added player array in game
- put server in Game - created start server function in Game - created CommandRestart There are two different 'player classes' which do I use? PlayerBasic and BasePlayer.
This commit is contained in:
parent
15de95ba65
commit
1baa067044
8 changed files with 116 additions and 21 deletions
|
@ -2,6 +2,8 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
|
||||||
using MUDGame.Environments;
|
using MUDGame.Environments;
|
||||||
|
|
||||||
|
@ -12,7 +14,7 @@ namespace MUDGame
|
||||||
//Setup our Fields
|
//Setup our Fields
|
||||||
static MudEngine.GameManagement.Game game;
|
static MudEngine.GameManagement.Game game;
|
||||||
static MudEngine.GameManagement.CommandEngine commands;
|
static MudEngine.GameManagement.CommandEngine commands;
|
||||||
static MudEngine.GameObjects.Characters.Controlled.PlayerBasic player;
|
static MudEngine.GameObjects.Characters.Controlled.PlayerBasic admin = new MudEngine.GameObjects.Characters.Controlled.PlayerBasic(true);
|
||||||
|
|
||||||
static List<MudEngine.GameObjects.Environment.Realm> realmCollection;
|
static List<MudEngine.GameObjects.Environment.Realm> realmCollection;
|
||||||
|
|
||||||
|
@ -37,6 +39,9 @@ namespace MUDGame
|
||||||
game.TimeOfDayTransition = 30;
|
game.TimeOfDayTransition = 30;
|
||||||
game.Version = "0.1";
|
game.Version = "0.1";
|
||||||
game.Website = "http://MudDesigner.Codeplex.com";
|
game.Website = "http://MudDesigner.Codeplex.com";
|
||||||
|
game.ServerType = ProtocolType.Tcp;
|
||||||
|
game.ServerPort = 555;
|
||||||
|
game.MaximumPlayers = 1000;
|
||||||
|
|
||||||
//Create the world
|
//Create the world
|
||||||
BuildRealms();
|
BuildRealms();
|
||||||
|
@ -53,8 +58,14 @@ namespace MUDGame
|
||||||
if (game.InitialRealm == null)
|
if (game.InitialRealm == null)
|
||||||
Console.WriteLine("Critical Error: No Initial Realm defined!");
|
Console.WriteLine("Critical Error: No Initial Realm defined!");
|
||||||
|
|
||||||
|
game.player = new MudEngine.GameObjects.Characters.Controlled.PlayerBasic[1000];
|
||||||
|
|
||||||
//Start the game.
|
//Start the game.
|
||||||
MudEngine.GameManagement.CommandEngine.LoadAllCommands();
|
MudEngine.GameManagement.CommandEngine.LoadAllCommands();
|
||||||
|
|
||||||
|
// Start the server thread.
|
||||||
|
game.StartServer();
|
||||||
|
|
||||||
game.IsRunning = true;
|
game.IsRunning = true;
|
||||||
|
|
||||||
while (game.IsRunning)
|
while (game.IsRunning)
|
||||||
|
@ -62,7 +73,7 @@ namespace MUDGame
|
||||||
Console.Write("Command: ");
|
Console.Write("Command: ");
|
||||||
string command = Console.ReadLine();
|
string command = Console.ReadLine();
|
||||||
|
|
||||||
MudEngine.GameManagement.CommandEngine.ExecuteCommand(command, player, game, null);
|
MudEngine.GameManagement.CommandEngine.ExecuteCommand(command, admin, game, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine("Press Enter to exit.");
|
Console.WriteLine("Press Enter to exit.");
|
||||||
|
|
38
MudEngine/Commands/CommandRestart.cs
Normal file
38
MudEngine/Commands/CommandRestart.cs
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
using MudEngine.GameObjects.Characters;
|
||||||
|
using MudEngine.GameManagement;
|
||||||
|
using MudEngine.GameObjects.Environment;
|
||||||
|
using MudEngine.GameObjects;
|
||||||
|
using MudEngine.FileSystem;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
|
||||||
|
namespace MudEngine.Commands
|
||||||
|
{
|
||||||
|
class CommandRestart : IGameCommand
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
public bool Override { get; set; }
|
||||||
|
|
||||||
|
public CommandResults Execute(string command, BaseCharacter player, Game project, Room room)
|
||||||
|
{
|
||||||
|
/*if (player.admin)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < project.player.Length; i++)
|
||||||
|
project.player[i].Save(project.player[i].Name + ".dat");
|
||||||
|
project.server.EndServer();
|
||||||
|
if (project.ServerType == ProtocolType.Tcp)
|
||||||
|
project.server.InitializeTCP(555, ref project.player);
|
||||||
|
else
|
||||||
|
project.server.InitializeUDP(555, ref project.player);
|
||||||
|
return new CommandResults("Server Restarted.");
|
||||||
|
}*/
|
||||||
|
return new CommandResults("Access Denied.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,6 +7,8 @@ using System.ComponentModel;
|
||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using System.Xml;
|
using System.Xml;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
|
||||||
//MUD Engine
|
//MUD Engine
|
||||||
using MudEngine.FileSystem;
|
using MudEngine.FileSystem;
|
||||||
|
@ -195,5 +197,18 @@ namespace MudEngine.GameManagement
|
||||||
{
|
{
|
||||||
InitialRealm = realm;
|
InitialRealm = realm;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public MudEngine.GameObjects.Characters.Controlled.PlayerBasic[] player;
|
||||||
|
public MudEngine.Networking.Server server = new MudEngine.Networking.Server();
|
||||||
|
public ProtocolType ServerType = ProtocolType.Tcp;
|
||||||
|
public int ServerPort = 555;
|
||||||
|
public int MaximumPlayers = 1000;
|
||||||
|
|
||||||
|
public void StartServer()
|
||||||
|
{
|
||||||
|
server.InitializeTCP(ServerPort, ref player);
|
||||||
|
server.Start();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,12 @@ namespace MudEngine.GameObjects.Characters.Controlled
|
||||||
{
|
{
|
||||||
public class PlayerBasic : BaseCharacter
|
public class PlayerBasic : BaseCharacter
|
||||||
{
|
{
|
||||||
|
public bool admin {get; private set;}
|
||||||
|
|
||||||
|
public PlayerBasic(bool a = false)
|
||||||
|
{
|
||||||
|
admin = a;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,6 +51,7 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Commands\CommandExit.cs" />
|
<Compile Include="Commands\CommandExit.cs" />
|
||||||
<Compile Include="Commands\CommandLook.cs" />
|
<Compile Include="Commands\CommandLook.cs" />
|
||||||
|
<Compile Include="Commands\CommandRestart.cs" />
|
||||||
<Compile Include="Commands\CommandWalk.cs" />
|
<Compile Include="Commands\CommandWalk.cs" />
|
||||||
<Compile Include="GameManagement\CommandEngine.cs" />
|
<Compile Include="GameManagement\CommandEngine.cs" />
|
||||||
<Compile Include="GameManagement\CommandResults.cs" />
|
<Compile Include="GameManagement\CommandResults.cs" />
|
||||||
|
|
|
@ -15,18 +15,16 @@ namespace MudEngine.Networking
|
||||||
public ClientSocket()
|
public ClientSocket()
|
||||||
{
|
{
|
||||||
type = 0;
|
type = 0;
|
||||||
ip = 0;
|
|
||||||
}
|
}
|
||||||
~ClientSocket()
|
~ClientSocket()
|
||||||
{
|
{
|
||||||
type = 0;
|
type = 0;
|
||||||
ip = 0;
|
|
||||||
}
|
}
|
||||||
public int Send(byte[] ba,int size,SocketFlags sf)
|
public int Send(byte[] ba)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
sock.Send(ba, size, sf);
|
sock.Send(ba);
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
|
@ -34,11 +32,11 @@ namespace MudEngine.Networking
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
public int Receive(ref byte[] ba, int size, SocketFlags sf)
|
public int Receive(byte[] ba)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
sock.Receive(ba, size, sf);
|
sock.Receive(ba);
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
|
@ -53,7 +51,6 @@ namespace MudEngine.Networking
|
||||||
sock.Disconnect(true);
|
sock.Disconnect(true);
|
||||||
sock.Close();
|
sock.Close();
|
||||||
sock.Dispose();
|
sock.Dispose();
|
||||||
ip = 0;
|
|
||||||
type = 0;
|
type = 0;
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
|
@ -64,7 +61,7 @@ namespace MudEngine.Networking
|
||||||
}
|
}
|
||||||
|
|
||||||
public ProtocolType type;
|
public ProtocolType type;
|
||||||
public long ip;
|
public IPAddress ip;
|
||||||
public Socket sock;
|
public Socket sock;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,11 +14,12 @@ using System.Threading;
|
||||||
|
|
||||||
namespace MudEngine.Networking
|
namespace MudEngine.Networking
|
||||||
{
|
{
|
||||||
class Server
|
public class Server
|
||||||
{
|
{
|
||||||
public Server()
|
public Server()
|
||||||
{
|
{
|
||||||
stage = 0;
|
stage = 0;
|
||||||
|
server = new ServerSocket();
|
||||||
}
|
}
|
||||||
~Server()
|
~Server()
|
||||||
{
|
{
|
||||||
|
@ -31,23 +32,29 @@ namespace MudEngine.Networking
|
||||||
}
|
}
|
||||||
server.CleanUp();
|
server.CleanUp();
|
||||||
}
|
}
|
||||||
public bool InitializeTCP(int port, int max_cons)
|
public bool InitializeTCP(int port, ref MudEngine.GameObjects.Characters.Controlled.PlayerBasic[] pbs)
|
||||||
{
|
{
|
||||||
if (stage != 0)
|
if (stage != 0)
|
||||||
return false;
|
return false;
|
||||||
if (server.Initialize(port, ProtocolType.Tcp) < 0)
|
if (server.Initialize(port, ProtocolType.Tcp) < 0)
|
||||||
return false;
|
return false;
|
||||||
numberOfClients = max_cons;
|
|
||||||
clients = new ClientSocket[max_cons];
|
numberOfClients = pbs.Length;
|
||||||
|
clients = new ClientSocket[pbs.Length];
|
||||||
|
players = pbs;
|
||||||
|
|
||||||
stage++;
|
stage++;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
public bool InitializeUDP(int port)
|
public bool InitializeUDP(int port, ref MudEngine.GameObjects.Characters.Controlled.PlayerBasic[] pbs)
|
||||||
{
|
{
|
||||||
if (stage != 0)
|
if (stage != 0)
|
||||||
return false;
|
return false;
|
||||||
if (server.Initialize(port, ProtocolType.Udp) < 0)
|
if (server.Initialize(port, ProtocolType.Udp) < 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
players = pbs;
|
||||||
|
|
||||||
stage++;
|
stage++;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -67,6 +74,17 @@ namespace MudEngine.Networking
|
||||||
serverThread.Start();
|
serverThread.Start();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
public void EndServer()
|
||||||
|
{
|
||||||
|
stage = 0;
|
||||||
|
if (server.type == ProtocolType.Tcp)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < numberOfClients; i++)
|
||||||
|
clients[i].CleanUp();
|
||||||
|
numberOfClients = 0;
|
||||||
|
}
|
||||||
|
server.CleanUp();
|
||||||
|
}
|
||||||
/*
|
/*
|
||||||
* ServerThread, if UDP: Accepts messages(ReceiveFrom) and sends in correspondence to the correct player
|
* ServerThread, if UDP: Accepts messages(ReceiveFrom) and sends in correspondence to the correct player
|
||||||
* if TCP: Accepts connection and opens a separate thread to receive a data stream between the clien
|
* if TCP: Accepts connection and opens a separate thread to receive a data stream between the clien
|
||||||
|
@ -75,14 +93,22 @@ namespace MudEngine.Networking
|
||||||
{
|
{
|
||||||
if (server.type == ProtocolType.Udp)
|
if (server.type == ProtocolType.Udp)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
/*while (stage == 2)
|
||||||
|
{
|
||||||
|
|
||||||
|
}*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private Thread serverThread;
|
private Thread serverThread;
|
||||||
private ServerSocket server;
|
private ServerSocket server;
|
||||||
private int stage;
|
private int stage;
|
||||||
|
|
||||||
|
MudEngine.GameObjects.Characters.Controlled.PlayerBasic[] players;
|
||||||
|
|
||||||
// TCP Stuff:
|
// TCP Stuff:
|
||||||
private ClientSocket[] clients;
|
private ClientSocket[] clients;
|
||||||
private int numberOfClients;
|
private int numberOfClients;
|
||||||
|
|
|
@ -105,7 +105,7 @@ namespace MudEngine.Networking
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
public int Accept(ref ClientSocket cs)
|
public int Accept(ClientSocket cs)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -128,7 +128,7 @@ namespace MudEngine.Networking
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
public int SendTo(byte[] ba, int size, SocketFlags sf, ref ClientSocket rcs)
|
public int SendTo(byte[] ba, ClientSocket rcs)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -140,7 +140,7 @@ namespace MudEngine.Networking
|
||||||
if (stage == 5)
|
if (stage == 5)
|
||||||
{
|
{
|
||||||
IPEndPoint ipep = new IPEndPoint(rcs.ip, port);
|
IPEndPoint ipep = new IPEndPoint(rcs.ip, port);
|
||||||
sock.SendTo(ba, size, sf, ipep);
|
sock.SendTo(ba, ipep);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return -2;
|
return -2;
|
||||||
|
@ -151,7 +151,7 @@ namespace MudEngine.Networking
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
public int ReceiveFrom(ref byte[] ba, int size, SocketFlags sf, ref ClientSocket rcs)
|
public int ReceiveFrom(byte[] ba, ClientSocket rcs)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -164,8 +164,9 @@ namespace MudEngine.Networking
|
||||||
{
|
{
|
||||||
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 0);
|
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 0);
|
||||||
EndPoint ep = (EndPoint)ipep;
|
EndPoint ep = (EndPoint)ipep;
|
||||||
sock.ReceiveFrom(ba, size, sf, ref ep);
|
sock.ReceiveFrom(ba, ref ep);
|
||||||
rcs.ip = ipep.Address.Address; // Why am I getting this warning?
|
|
||||||
|
rcs.ip = ipep.Address;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return -2;
|
return -2;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue