Merge Net part 3 done

This commit is contained in:
AnloGames 2024-08-20 14:03:23 +03:00
parent e57291cb4c
commit 549c7e34a3
6 changed files with 81 additions and 45 deletions

View file

@ -49,8 +49,8 @@ namespace ZoFo.GameCore
{
networkManager.AddData(new UpdateInput()
{
InputMovementDirection = AppManager.Instance.InputManager.InputMovementDirection,
InputAttackDirection = AppManager.Instance.InputManager.InputAttackDirection
InputMovementDirection = AppManager.Instance.InputManager.InputMovementDirection.Serialize(),
InputAttackDirection = AppManager.Instance.InputManager.InputAttackDirection.Serialize()
});
};

View file

@ -55,6 +55,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
Datagramm.updateDatas = updates;
byte[] bytes = Encoding.UTF8.GetBytes(System.Text.Json.JsonSerializer.Serialize(Datagramm)); //нужно сериализовать
socket.SendTo(bytes, sendingEP);
updates.Clear();
}
}
@ -84,6 +85,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
AppManager.Instance.ChangeState(GameState.ClientPlaying);
AppManager.Instance.SetGUI(new HUD());
}
SendAcknowledgement(Dgramm.DatagrammId);
}
if (Dgramm.isImportant)
{

View file

@ -15,6 +15,10 @@ using System.Threading.Tasks;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates;
using ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO;
using ZoFo.GameCore.GUI;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ClientToServer;
namespace ZoFo.GameCore.GameManagers.NetworkManager
{
@ -111,30 +115,38 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
arrivingDataId.Clear();
}
List<UpdateData> dataToSend;
if (importantUpdates.Count != 0 || sendedData.Count != 0)
{
dataToSend = new List<UpdateData>();
for (int i = 0; i < datapackSize && i < importantUpdates.Count; i++)
dataToSend.Add(importantUpdates[i]);
if (importantUpdates.Count > 0)
{
for (int i = 0; i < clientsEP.Count; i++)
{
dataToSend = new List<UpdateData>();
for (int j = 0; j < datapackSize && j < importantUpdates.Count; j++)
dataToSend.Add(importantUpdates[j]);
Datagramm impDgramm = new Datagramm();
impDgramm.DatagrammId = currentDatagrammId;
impDgramm.updateDatas = dataToSend;
impDgramm.isImportant = true;
impDgramm.PlayerId = i + 1;
sendedData.Add(impDgramm);
for (int j = 0; j < datapackSize && j < dataToSend.Count; j++)
importantUpdates.RemoveAt(0);
}
currentDatagrammId++;
}
if (sendedData.Count != 0)
{
for (int i = 0; i < clientsEP.Count; i++)
{
foreach (Datagramm Dgramm in sendedData.Where(x => x.PlayerId == i+1))
{
string impData = JsonSerializer.Serialize(Dgramm);
string impData = System.Text.Json.JsonSerializer.Serialize(Dgramm);
byte[] impBuffer = Encoding.UTF8.GetBytes(impData);
socket.SendTo(impBuffer, clientsEP[i]);
}
}
currentDatagrammId++;
for (int i = 0; i < datapackSize && i < dataToSend.Count; i++)
importantUpdates.RemoveAt(0);
}
Datagramm unImpDgramm = new Datagramm();
@ -143,7 +155,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
dataToSend.Add(commonUpdates[i]);
unImpDgramm.updateDatas = dataToSend;
string data = JsonSerializer.Serialize(unImpDgramm);
string data = System.Text.Json.JsonSerializer.Serialize(unImpDgramm);
byte[] buffer = Encoding.UTF8.GetBytes(data);
foreach (EndPoint sendingEP in clientsEP)
{
@ -187,7 +199,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
initDgramm.DatagrammId = currentDatagrammId;
initDgramm.PlayerId = i + 1;
sendedData.Add(initDgramm);
string data = JsonSerializer.Serialize(initDgramm);
string data = System.Text.Json.JsonSerializer.Serialize(initDgramm);
byte[] buffer = Encoding.UTF8.GetBytes(data);
socket.SendTo(buffer, clientsEP[i]);
}
@ -227,17 +239,48 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
}
public void AnalyzeData(string data)
{
Datagramm Dgramm = JsonSerializer.Deserialize<Datagramm>(data);
if (Dgramm.updateDatas == null)
JObject jObj = JsonConvert.DeserializeObject(data) as JObject;
JToken token = JToken.FromObject(jObj);
JToken updateDatas = token["updateDatas"];
Datagramm Dgramm = new Datagramm();
Dgramm.PlayerId = token["PlayerId"].ToObject<int>();
if (!updateDatas.HasValues)
{
//Обработка acknowledgement
Dgramm.DatagrammId = token["DatagrammId"].ToObject<int>();
arrivingDataId.Add(Dgramm);
}
else
{
//Настроить десериализацию и применять неважные апдейты
{
List<UpdateData> updates = GetSentUpdates(updateDatas);
AppManager.Instance.server.UpdatesList(updates);
}
}
public List<UpdateData> GetSentUpdates(JToken updatesToken)
{
List<UpdateData> data = new List<UpdateData>();
JArray updateDatas = updatesToken as JArray;
UpdateData update = new UpdateData();
foreach (JObject token in updateDatas.Children())
{
switch (token["UpdateType"].ToObject<string>())
{
case "UpdateInput":
update = token.ToObject<UpdateInput>();
data.Add(update);
break;
case "UpdateInputInteraction":
update = token.ToObject<UpdateInputInteraction>();
data.Add(update);
break;
case "UpdateInputShoot":
update = token.ToObject<UpdateInputShoot>();
data.Add(update);
break;
}
}
return data;
}
}
}

View file

@ -5,14 +5,15 @@ using Microsoft.Xna.Framework;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO;
namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ClientToServer
{
public class UpdateInput :UpdateData
{
// public int IdEntity { get; set; }
public Vector2 InputMovementDirection{get;set;}
public Vector2 InputAttackDirection {get;set;}
public SerializableVector2 InputMovementDirection{get;set;}
public SerializableVector2 InputAttackDirection {get;set;}
public UpdateInput()
{
UpdateType = "UpdateInput";

View file

@ -90,12 +90,13 @@ public class Player : LivingEntity
}
public void MovementLogic()
{
velocity += InputPlayerRotation * speed;
velocity += InputPlayerRotation * speed;
DebugHUD.DebugSet("player pos server", position.ToString());
}
public void HandleNewInput(UpdateInput updateInput)
{
InputPlayerRotation = updateInput.InputMovementDirection;
InputWeaponRotation = updateInput.InputAttackDirection;
InputPlayerRotation = updateInput.InputMovementDirection.GetVector2();
InputWeaponRotation = updateInput.InputAttackDirection.GetVector2();
}
public void HandleInteract(UpdateInputInteraction updateInputInteraction)

View file

@ -50,6 +50,13 @@ namespace ZoFo.GameCore
ProcessIUpdateData(updateDatas[i]);
}
}
internal void UpdatesList(List<UpdateData> updates)
{
foreach (var item in updates)
{
ProcessIUpdateData(item);
}
}
/// <summary>
/// Обработка апдейтсов, которые нам прислал клиент
/// </summary>
@ -60,24 +67,6 @@ 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 "UpdateInput":
if (players.Count > 0)
players[0].HandleNewInput(updateData as UpdateInput);//TODO id instead of 0
@ -137,9 +126,9 @@ namespace ZoFo.GameCore
//AppManager.Instance.server.RegisterGameObject(new EntittyForAnimationTests(new Vector2(0, 0)));
AppManager.Instance.server.RegisterGameObject(new Player(new Vector2(740, 140)));
for (int i = 0; i < 20; i++)
for (int j = 0; j < 20; j++)
AppManager.Instance.server.RegisterGameObject(new Zombie(new Vector2(1300 + i*70, 1000+j*70)));
//for (int i = 0; i < 20; i++)
// for (int j = 0; j < 20; j++)
// AppManager.Instance.server.RegisterGameObject(new Zombie(new Vector2(1300 + i*70, 1000+j*70)));
AppManager.Instance.server.RegisterGameObject(new Ammo(new Vector2(140, 440)));
AppManager.Instance.server.RegisterGameObject(new Ammo(new Vector2(240, 440)));