SerializableDTO

This commit is contained in:
AnloGames 2024-08-18 19:06:12 +03:00
parent 7244981e86
commit 30b3e650cc
8 changed files with 95 additions and 63 deletions

View file

@ -24,6 +24,8 @@ using ZoFo.GameCore.GUI;
using ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables;
using ZoFo.GameCore.GameObjects.MapObjects.StopObjects;
using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Enemies;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
namespace ZoFo.GameCore
{
public class Client
@ -54,13 +56,21 @@ namespace ZoFo.GameCore
public void OnDataSend(string data)
{
List<UpdateData> updateDatas = JsonSerializer.Deserialize<List<UpdateData>>(data);
//List<UpdateTileCreated> updateDatas = JsonSerializer.Deserialize<List<UpdateTileCreated>>(data);
JArray jToken = JsonConvert.DeserializeObject(data) as JArray;
//string[] brands = jToken.SelectToken("")?.ToObject<string[]>();
foreach (JToken update in jToken.Children())
{
string a = update.ToString();
UpdateTileCreated u = System.Text.Json.JsonSerializer.Deserialize<UpdateTileCreated>(a);
}
// тут будет switch
AppManager.Instance.debugHud.Log(data);
foreach (var item in updateDatas)
{
GotData(item);
}
//foreach (var item in updateDatas)
//{
// GotData(item);
//}
}
public void GameEndedUnexpectedly() { }
@ -112,22 +122,21 @@ namespace ZoFo.GameCore
mapObjects.Add(
new MapObject(
(update as UpdateTileCreated).Position,
(update as UpdateTileCreated).Size.ToVector2(),
(update as UpdateTileCreated).sourceRectangle,
(update as UpdateTileCreated).Size.GetPoint().ToVector2(),
(update as UpdateTileCreated).sourceRectangle.GetRectangle(),
(update as UpdateTileCreated).tileSetName
));
}
else if (update is UpdateStopObjectCreated)
{
stopObjects.Add(
new StopObject(
(update as UpdateStopObjectCreated).Position,
(update as UpdateStopObjectCreated).Size.ToVector2(),
(update as UpdateStopObjectCreated).sourceRectangle,
(update as UpdateStopObjectCreated).tileSetName,
(update as UpdateStopObjectCreated).collisions
));
}
//else if (update is UpdateStopObjectCreated)
//{
// stopObjects.Add(
// new StopObject(
// (update as UpdateStopObjectCreated).Position,
// (update as UpdateStopObjectCreated).Size.ToVector2(),
// (update as UpdateStopObjectCreated).sourceRectangle,
// (update as UpdateStopObjectCreated).tileSetName
// ));
//}
else if (update is UpdateGameObjectCreated)
{
GameObject created_gameObject;

View file

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

View file

@ -0,0 +1,18 @@
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO
{
public class SerializablePoint
{
public int X;
public int Y;
public SerializablePoint(Point point) { X = point.X; Y = point.Y;}
public Point GetPoint() { return new Point(X, Y);}
}
}

View file

@ -0,0 +1,33 @@
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO;
namespace ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO
{
[Serializable]
[JsonSerializable(typeof(SerializableRectangle))]
public class SerializableRectangle
{
public SerializablePoint Size { get; set; }
public SerializablePoint Location { get; set; }
public int X { get; set; }
public int Y { get; set; }
public SerializableRectangle()
{
}
public SerializableRectangle(Rectangle rectangle) { X = rectangle.X; Y = rectangle.Y;
Size = new SerializablePoint(rectangle.Size); Location = new SerializablePoint(rectangle.Location); }
public Rectangle GetRectangle()
{
return new Rectangle() { X = X, Y = Y, Size = Size.GetPoint(), Location = Location.GetPoint() };
}
}
}

View file

@ -53,7 +53,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
public static IPAddress GetIp()
{
string hostName = Dns.GetHostName(); // Retrive the Name of HOST
var ipList = Dns.GetHostByName(hostName).AddressList;
var ipList = Dns.GetHostEntry(hostName).AddressList;
foreach (var ip in ipList)
{
@ -80,15 +80,15 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
//Что это?
//по 10 паков за раз TODO FIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXIT
List<UpdateData> datasToSend = new List<UpdateData>();
for (int i = 0; i < 5 && i<updates.Count; i++)
for (int i = 0; i < 200 && i<updates.Count; i++)
datasToSend.Add(updates[i]);
string data = JsonSerializer.Serialize(datasToSend);
var databytes = Encoding.UTF8.GetBytes(data);
foreach (var item in clients)
foreach (Socket socket in clients)
{
item.SendAsync(databytes);
clients[0].SendAsync(databytes, SocketFlags.Partial);
}
for (int i = 0; i < 5 && i< datasToSend.Count; i++)
for (int i = 0; i < 200 && i< datasToSend.Count; i++)
updates.RemoveAt(0);
}
@ -171,8 +171,8 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
Socket client = (Socket)socket;
while (client.Connected)
{
var buff = new byte[1024];
var answ = client.Receive(buff);
var buff = new byte[65535];
var answ = client.Receive(buff, SocketFlags.Partial);
string response = Encoding.UTF8.GetString(buff, 0, answ);
GetDataSend(response);
}

View file

@ -6,6 +6,7 @@ using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO;
using ZoFo.GameCore.GameManagers.NetworkManager.Updates.ClientToServer;
namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient
@ -18,22 +19,9 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient
{
public UpdateTileCreated() { UpdateType = "UpdateTileCreated"; }
public Texture2D TextureTile { get; set; }
public Vector2 Position { get; set; }
public Point Size { get; set; }
public Rectangle sourceRectangle { get; set; }
public Vector2 Position { get; set; }
public SerializablePoint Size { get; set; }
public SerializableRectangle sourceRectangle { get; set; }
public string tileSetName { get; set; }
}
/// <summary>
/// При создании тайла TODO move to another file
/// </summary>
public class UpdateStopObjectCreated : UpdateData
{
public UpdateStopObjectCreated() { UpdateType = "UpdateStopObjectCreated"; }
public Texture2D TextureTile { get; set; }
public Vector2 Position { get; set; }
public Point Size { get; set; }
public Rectangle sourceRectangle { get; set; }
public string tileSetName { get; set; }
public Rectangle[] collisions { get; set; }
}
}

View file

@ -28,7 +28,7 @@ namespace ZoFo.GameCore.GameManagers
if (k.Length > 0)
{
string[] soundFiles = k.Select(x => x.Split("/").Last().Replace(".xnb", "")).ToArray();// папка со звуками там где exe
string[] soundFiles = k.Select(x => x.Split("\\").Last().Split("/").Last().Replace(".xnb", "")).ToArray();// папка со звуками там где exe
foreach (var soundFile in soundFiles)
{
Sounds.Add(soundFile, AppManager.Instance.Content.Load<SoundEffect>(Path.Combine("sounds", soundFile)));

View file

@ -21,6 +21,7 @@ using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player;
using ZoFo.GameCore.GameObjects.MapObjects;
using ZoFo.GameCore.GameObjects.MapObjects.StopObjects;
using ZoFo.GameCore.Graphics;
using ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO;
namespace ZoFo.GameCore
{
@ -172,30 +173,13 @@ namespace ZoFo.GameCore
{
gameObjects.Add(gameObject);
if (gameObject is StopObject)
{
AddData(new UpdateStopObjectCreated()
{
Position = (gameObject as StopObject).position,
sourceRectangle = (gameObject as StopObject).sourceRectangle,
Size = (gameObject as StopObject).graphicsComponent.ObjectDrawRectangle.Size,
collisions = (gameObject as StopObject).collisionComponents.Select(x=>x.stopRectangle).ToArray(),
tileSetName = ((gameObject as StopObject).graphicsComponent as StaticGraphicsComponent)._textureName
});//TODO
foreach (var item in (gameObject as StopObject).collisionComponents)
{
collisionManager.Register(item);
}
return;
}
if (gameObject is MapObject)
{
AddData(new UpdateTileCreated()
{
Position = (gameObject as MapObject).position,
sourceRectangle = (gameObject as MapObject).sourceRectangle,
Size = (gameObject as MapObject).graphicsComponent.ObjectDrawRectangle.Size,
sourceRectangle = new SerializableRectangle((gameObject as MapObject).sourceRectangle),
Size = new SerializablePoint((gameObject as MapObject).graphicsComponent.ObjectDrawRectangle.Size),
tileSetName = ((gameObject as MapObject).graphicsComponent as StaticGraphicsComponent)._textureName
});//TODO
return;