Merge pull request #55 from progtime-net/ServerBranchMultiPlayer

Server branch multi player
This commit is contained in:
SergoDobro 2024-08-18 10:48:35 +03:00 committed by GitHub
commit d111c2f8cd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 115 additions and 43 deletions

View file

@ -13,6 +13,8 @@ using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient;
using System.Drawing;
using System.Reflection;
using ZoFo.GameCore.GameObjects.Entities;
using System.Net.Sockets;
using System.Net;
using ZoFo.GameCore.GameManagers;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ClientToServer;
using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player;
@ -30,6 +32,8 @@ namespace ZoFo.GameCore
ClientNetworkManager networkManager;
public bool IsConnected { get { return networkManager.IsConnected; } }
public IPEndPoint InfoConnect => networkManager.InfoConnect;
public Client()
{
networkManager = new ClientNetworkManager();

View file

@ -26,25 +26,27 @@ public class SelectingServerGUI : AbstractGUI
Elements.Add(menuBackground);
menuBackground.LoadTexture(AppManager.Instance.Content);
Elements.Add(new Label(Manager) { rectangle = new Rectangle(width / 2 - (int)(width / 8), height / 7, (int)(width / 4), (int)(height / 20)), text = "Select server", fontColor = Color.White, mainColor = Color.Transparent, scale = 0.9f, fontName = "Fonts/Font"});
Elements.Add(new Label(Manager) { rectangle = new Rectangle(width / 2 - (int)(width / 8), height / 7, (int)(width / 4), (int)(height / 20)), text = "Select server", fontColor = Color.White, mainColor = Color.Transparent, scale = 0.9f, fontName = "Fonts/Font" });
TextBox ipBox = new TextBox(Manager)
{
rectangle = new Rectangle(width / 4 - (width / 4) / 2, height / 4, (int)(width / 4), (int)(height / 20)),
text = "ip",
scale = 0.2f,
scale = 0.5f,
fontColor = Color.White,
mainColor = Color.Gray,
textAligment = MonogameLibrary.UI.Enums.TextAligment.Left,
fontName = "Fonts/Font"
fontName = "Fonts/Font3"
};
ipBox.TextChanged += input => {
ipBox.TextChanged += input =>
{
if (input == "ip")
{
ipBox.text = ""; ipBox.fontColor = Color.White;
}
};
ipBox.StopChanging += input => {
ipBox.StopChanging += input =>
{
if (input.Length == 0)
{
ipBox.fontColor = Color.White;
@ -66,14 +68,23 @@ public class SelectingServerGUI : AbstractGUI
// join
Client client = new Client();
var endpoint = ipBox.text.Split(':');
var endpoint = ipBox.text.Split(':');
int port;
if (int.TryParse(endpoint[1], out port))
try
{
client.JoinRoom(endpoint[0], port);
AppManager.Instance.SetClient(client);
AppManager.Instance.SetGUI(new WaitingForPlayersGUI(false));
if (int.TryParse(endpoint[1], out port))
{
client.JoinRoom(endpoint[0], port);
AppManager.Instance.SetClient(client);
AppManager.Instance.SetGUI(new WaitingForPlayersGUI(false));
}
}
catch (Exception)
{
// throw;
}
// ваш код здесь
};
Elements.Add(joinButton);
@ -91,8 +102,11 @@ public class SelectingServerGUI : AbstractGUI
// host
Server server = new Server(); //Server Logic MultiPlayer
server.CreateRoom(5);
Client client = new Client();
server.CreateRoom(2);
client.JoinYourself(server.MyIp.Port);
AppManager.Instance.SetServer(server);
AppManager.Instance.SetClient(client);
string key = server.MyIp.ToString();
AppManager.Instance.debugHud.Set(key, "MultiPlayer");
// ваш код здесь
@ -101,7 +115,7 @@ public class SelectingServerGUI : AbstractGUI
Elements.Add(hostButton);
Button bTExit = new Button(Manager)
{ fontName = "Fonts/Font3", scale = 0.4f, text = "<-", fontColor = Color.Black, mainColor = Color.Transparent, rectangle = new Rectangle(width / 30, height / 30, width / 40, width / 40), textureName = "Textures/GUI/checkboxs_off"};
{ fontName = "Fonts/Font3", scale = 0.4f, text = "<-", fontColor = Color.Black, mainColor = Color.Transparent, rectangle = new Rectangle(width / 30, height / 30, width / 40, width / 40), textureName = "Textures/GUI/checkboxs_off" };
Elements.Add(bTExit);
bTExit.LeftButtonPressed += () =>
{

View file

@ -31,11 +31,15 @@ public class WaitingForPlayersGUI : AbstractGUI
menuBackground = new DrawableUIElement(Manager) { rectangle = new Rectangle(0, 0, width, height), mainColor = Color.White, textureName = "Textures/GUI/background/Waiting" };
Elements.Add(menuBackground);
menuBackground.LoadTexture(AppManager.Instance.Content);
// string pcIp =
// string pcIp =
ip = new Label(Manager) { rectangle = new Rectangle(width / 2 - (int)(width / 8), height / 7, (int)(width / 4), (int)(height / 20)), text = AppManager.Instance.server.MyIp.ToString(), fontColor = Color.White, mainColor = Color.Transparent, scale = 0.9f, fontName = "Fonts/Font3" };
Elements.Add(ip);
if (isHost)
{
ip = new Label(Manager) { rectangle = new Rectangle(width / 2 - (int)(width / 8), height / 7, (int)(width / 4), (int)(height / 20)), text = AppManager.Instance.server.MyIp.ToString(), fontColor = Color.White, mainColor = Color.Transparent, scale = 0.9f, fontName = "Fonts\\Font3" };
Elements.Add(ip);
Button startButton = new Button(Manager)
{
rectangle = new Rectangle(width / 2 - (width / 15) / 2, height / 2 + height / 4, (int)(width / 15), (int)(height / 20)),
@ -48,11 +52,31 @@ public class WaitingForPlayersGUI : AbstractGUI
startButton.LeftButtonPressed += () =>
{
// start
AppManager.Instance.ChangeState(GameState.HostPlaying);
// ваш код здесь
};
Elements.Add(startButton);
}
else {
ip = new Label(Manager) { rectangle = new Rectangle(width / 2 - (int)(width / 8), height / 7, (int)(width / 4), (int)(height / 20)), text = AppManager.Instance.client.InfoConnect.ToString(), fontColor = Color.White, mainColor = Color.Transparent, scale = 0.9f, fontName = "Fonts\\Font3" };
Elements.Add(ip);
Button waitButton = new Button(Manager)
{
rectangle = new Rectangle(width / 2 - (width / 15) / 2, height / 2 + height / 4, (int)(width / 15), (int)(height / 20)),
text = "WAITING",
scale = 0.3f,
fontColor = Color.White,
mainColor = Color.Gray,
fontName = "Fonts/Font"
};
waitButton.LeftButtonPressed += () =>
{
// start
AppManager.Instance.ChangeState(GameState.ClientPlaying);
// ваш код здесь
};
Elements.Add(waitButton);
}
Button bTExit = new Button(Manager)
{ fontName = "Fonts/Font3", scale = 0.4f, text = "<-", fontColor = Color.Black, mainColor = Color.Transparent, rectangle = new Rectangle(width / 30, height / 30, width / 40, width / 40), textureName = "Textures/GUI/checkboxs_off"};

View file

@ -94,7 +94,7 @@ namespace ZoFo.GameCore.GameManagers
Keyboard.GetState().IsKeyDown(Keys.Escape)) { server.CloseConnection(); Exit(); }
debugHud.Set("key", "value");
// debugHud.Set("key", "value");
InputManager.Update();
currentGUI.Update(gameTime);
@ -107,7 +107,7 @@ namespace ZoFo.GameCore.GameManagers
client.Update(gameTime);
break;
case GameState.ClientPlaying:
server.Update(gameTime);
client.Update(gameTime);
break;
default:
break;

View file

@ -108,7 +108,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
while(socket.Connected)
{
byte[] bytes = new byte[2048];
var countAnsw = socket.Receive(bytes);
var countAnsw = socket.Receive(bytes); //Вылетает если кто то закрыл
string update = Encoding.UTF8.GetString(bytes, 0, countAnsw); // обновление отосланные сервером
GetDataSent(update);
}

View file

@ -53,7 +53,8 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
public static IPAddress GetIp()
{
string hostName = Dns.GetHostName(); // Retrive the Name of HOST
string myIP = Dns.GetHostByName(hostName).AddressList[1].ToString();// Get the IP
var ipList = Dns.GetHostByName(hostName).AddressList;
string myIP = ipList[ipList.Count()-1].ToString();// Get the IP
return IPAddress.Parse(myIP);
}
@ -69,7 +70,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
}
updates.Clear();
return; //TODO TODO REMOVE TO ADD NETWORK TODO REMOVE TO ADD NETWORK TODO REMOVE TO ADD NETWORK TODO REMOVE TO ADD NETWORK
//Что это?
//по 10 паков за раз TODO FIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXIT
List<UpdateData> datasToSend = new List<UpdateData>();
for (int i = 0; i < 5 && i<updates.Count; i++)
@ -141,13 +142,16 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
for (int i = 0; i < playNumber; i++)
{
Socket client = socket.Accept();
AppManager.Instance.debugHud.Log($"Connect {client.LocalEndPoint.ToString()}");
Thread thread = new Thread(StartListening);
thread.IsBackground = true;
thread.Start(client);
managerThread.Add(client, thread);
clients.Add(client); //добавляем клиентов в лист
clients.Add(client);
//AppManager.Instance.ChangeState(GameState.HostPlaying);
//добавляем клиентов в лист
}
AppManager.Instance.ChangeState(GameState.HostPlaying);
}
/// <summary>

View file

@ -5,6 +5,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient;
/// </summary>
public class UpdateInteraction : UpdateData
{
public UpdateInteraction() { UpdateType = "UpdateInteraction"; }
public UpdateInteraction(int id)
{
IdEntity = id;

View file

@ -9,6 +9,6 @@ public class UpdateInteractionReady(int idEntity, bool isReady)
: UpdateData
{
public int IdEntity { get; set; } = idEntity;
public string UpdateType { get; set; }
public string UpdateType { get; set; } = "UpdateInteractionReady";
public bool IsReady { get; set; } = isReady;
}

View file

@ -28,7 +28,6 @@ namespace ZoFo.GameCore
private ServerNetworkManager networkManager;
private int ticks = 0;
public IPEndPoint MyIp { get { return networkManager.InfoConnect; } }
public Server()
{
networkManager = new ServerNetworkManager();
@ -56,6 +55,32 @@ namespace ZoFo.GameCore
{
//ТУТ Switch case будет честное слово
switch (updateData.UpdateType)
{
case "UpdateAnimation":
break;
case "UpdateEntityHealth":
break;
case "UpdateGameEnded":
break;
case "UpdateGameObjectCreated":
break;
case "UpdateGameObjectDeleted":
break;
case "UpdateInteraction":
break;
case "UpdateInteractionReady":
break;
case "UpdateLoot":
break;
case "UpdatePlayerParametrs":
break;
case "UpdatePosition":
break;
case "UpdateTileCreated":
break;
}
}
public void CloseConnection()