-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #421 from Jnick-24/main
Network optimizations for CTF mod
- Loading branch information
Showing
8 changed files
with
533 additions
and
370 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using Sandbox.ModAPI; | ||
using System; | ||
using System.CodeDom; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using VRage.Game.Components; | ||
using VRage.Utils; | ||
|
||
namespace Jnick_SCModRepository.StarCoreCTF.Data.Scripts.CTF | ||
{ | ||
[MySessionComponentDescriptor(MyUpdateOrder.AfterSimulation)] | ||
public class NetworkDebug : MySessionComponentBase | ||
{ | ||
const int AveragingPeriod = 60; | ||
|
||
private static NetworkDebug I; | ||
private int nextLog = 0; | ||
private Dictionary<string, List<int>> downLoad = new Dictionary<string, List<int>>(); | ||
private Dictionary<string, List<int>> upLoad = new Dictionary<string, List<int>>(); | ||
|
||
/// <summary> | ||
/// Use this method | ||
/// </summary> | ||
/// <param name="type"></param> | ||
/// <param name="obj"></param> | ||
/// <returns></returns> | ||
public static byte[] SerializeLogged(string type, object obj) | ||
{ | ||
byte[] serialized = MyAPIGateway.Utilities.SerializeToBinary(obj); | ||
I.LogUsageUp(type, serialized.Length); | ||
return serialized; | ||
} | ||
private void LogUsageUp(string type, int usage) | ||
{ | ||
if (!upLoad.ContainsKey(type)) | ||
upLoad.Add(type, new List<int>()); | ||
upLoad[type].Add(usage); | ||
} | ||
|
||
/// <summary> | ||
/// Use this method | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="type"></param> | ||
/// <param name="serialized"></param> | ||
/// <returns></returns> | ||
public static T DeserializeLogged<T>(string type, byte[] serialized) | ||
{ | ||
T obj = MyAPIGateway.Utilities.SerializeFromBinary<T>(serialized); | ||
I.LogUsageDown(type, serialized.Length); | ||
return obj; | ||
} | ||
private void LogUsageDown(string type, int usage) | ||
{ | ||
if (!downLoad.ContainsKey(type)) | ||
downLoad.Add(type, new List<int>()); | ||
downLoad[type].Add(usage); | ||
} | ||
|
||
public override void UpdateAfterSimulation() | ||
{ | ||
nextLog--; | ||
if (nextLog <= 0) | ||
{ | ||
foreach (var upKvp in upLoad) | ||
{ | ||
float averageUp = 0; | ||
long total = 0; | ||
foreach (var size in upKvp.Value) | ||
{ | ||
averageUp += size; | ||
total += size; | ||
} | ||
averageUp /= (AveragingPeriod/60f); | ||
MyLog.Default.WriteLineAndConsole($"NetworkDebug: [UP] {upKvp.Key} {Math.Round(averageUp/1000f, 2)}kb/s" + (upKvp.Value.Count > 0 ? $" (avg. {total / upKvp.Value.Count}b), ct. {upKvp.Value.Count / (AveragingPeriod / 60)})" : "")); | ||
upKvp.Value.Clear(); | ||
} | ||
foreach (var downKvp in downLoad) | ||
{ | ||
float averageDown = 0; | ||
long total = 0; | ||
foreach (var size in downKvp.Value) | ||
{ | ||
averageDown += size; | ||
total += size; | ||
} | ||
averageDown /= (AveragingPeriod / 60f); | ||
MyLog.Default.WriteLineAndConsole($"NetworkDebug: [DOWN] {downKvp.Key} {Math.Round(averageDown / 1000f, 2)}kb/s" + (downKvp.Value.Count > 0 ? $" (avg. {total / downKvp.Value.Count}b, ct. {downKvp.Value.Count / (AveragingPeriod / 60)})" : "")); | ||
downKvp.Value.Clear(); | ||
} | ||
nextLog = AveragingPeriod; | ||
} | ||
} | ||
|
||
public override void BeforeStart() | ||
{ | ||
I = this; | ||
} | ||
|
||
protected override void UnloadData() | ||
{ | ||
I = null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using ProtoBuf; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using static Klime.CTF.CTF; | ||
|
||
namespace Jnick_SCModRepository.StarCoreCTF.Data.Scripts.CTF.Packets | ||
{ | ||
[ProtoContract] | ||
public class EventInfo | ||
{ | ||
[ProtoMember(600)] | ||
public string info; | ||
[ProtoMember(601)] | ||
public InfoType infotype; | ||
|
||
public EventInfo() | ||
{ | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
using ProtoBuf; | ||
using Sandbox.Game.Entities; | ||
using Sandbox.ModAPI; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using static Klime.CTF.CTF; | ||
using VRage.Game.Entity; | ||
using VRage.Game.ModAPI; | ||
using VRageMath; | ||
|
||
namespace Jnick_SCModRepository.StarCoreCTF.Data.Scripts.CTF | ||
{ | ||
[ProtoContract] | ||
public class Flag | ||
{ | ||
public MyEntity flag_entity; | ||
public IMyPlayer carrying_player; | ||
public IMyFaction owning_faction; | ||
|
||
[ProtoMember(1)] | ||
public long entity_id; | ||
|
||
[ProtoMember(2)] | ||
public FlagState state; | ||
|
||
[ProtoMember(3)] | ||
public int lifetime; | ||
|
||
[ProtoMember(4)] | ||
public long carrying_player_id = -1; | ||
|
||
[ProtoMember(5)] | ||
public SerializableMatrix current_matrix; | ||
|
||
[ProtoMember(6)] | ||
public long owning_faction_id; | ||
|
||
[ProtoMember(7)] | ||
public int current_drop_life = 0; | ||
|
||
[ProtoMember(8)] | ||
public Vector3D homePos; | ||
|
||
[ProtoMember(9)] | ||
public Dictionary<long, SerializableMatrix> capture_positions = new Dictionary<long, SerializableMatrix>(); // This seems to be the majority of network load - fix? | ||
|
||
[ProtoMember(10)] | ||
public Color flag_color; | ||
|
||
[ProtoMember(11)] | ||
public FlagType flag_type; | ||
|
||
[ProtoMember(12)] | ||
public float grip_strength = 100; | ||
|
||
[ProtoMember(13)] | ||
public float regen_modifier = 0.2f; | ||
|
||
[ProtoMember(14)] | ||
public float lastTickAcceleration; | ||
|
||
[ProtoIgnore] | ||
public MyCubeGrid attachedGrid = null; | ||
|
||
[ProtoIgnore] | ||
public MatrixD attachedLocalMatrix = MatrixD.Identity; | ||
|
||
public Flag() | ||
{ | ||
|
||
} | ||
|
||
//Single | ||
public Flag(long entity_id, FlagState state, Vector3D homePos, Dictionary<long, SerializableMatrix> capture_positions, | ||
long owning_faction_id, Color flag_color, FlagType flag_type, float grip_strength, float regen_modifier, float lastTickAcceleration) | ||
{ | ||
this.entity_id = entity_id; | ||
this.state = state; | ||
this.homePos = homePos; | ||
this.current_matrix = MatrixD.CreateWorld(homePos); | ||
this.owning_faction_id = owning_faction_id; | ||
this.flag_color = flag_color; | ||
this.capture_positions = capture_positions; | ||
this.flag_type = flag_type; | ||
this.grip_strength = grip_strength; | ||
this.regen_modifier = regen_modifier; | ||
this.lastTickAcceleration = lastTickAcceleration; | ||
} | ||
|
||
//Double | ||
public Flag(long entity_id, FlagState state, Vector3D homePos, long owning_faction_id, | ||
Color flag_color, FlagType flag_type, float grip_strength, float regen_modifier, float lastTickAcceleration) | ||
{ | ||
this.entity_id = entity_id; | ||
this.state = state; | ||
this.homePos = homePos; | ||
this.current_matrix = MatrixD.CreateWorld(homePos); | ||
this.owning_faction_id = owning_faction_id; | ||
this.flag_color = flag_color; | ||
this.flag_type = flag_type; | ||
this.grip_strength = grip_strength; | ||
this.regen_modifier = regen_modifier; | ||
this.lastTickAcceleration = lastTickAcceleration; | ||
} | ||
|
||
public void Init() | ||
{ | ||
flag_entity = MyAPIGateway.Entities.GetEntityById(entity_id) as MyEntity; | ||
if (owning_faction_id != 0) | ||
{ | ||
owning_faction = MyAPIGateway.Session.Factions.TryGetFactionById(owning_faction_id); | ||
} | ||
} | ||
|
||
|
||
|
||
public List<IMyPlayer> GetNearbyPlayers(ref List<IMyPlayer> all_players, ref List<IMyPlayer> return_list, bool cockpit_allowed, double flagPickupRadius) | ||
{ | ||
return_list.Clear(); | ||
foreach (var player in all_players) | ||
{ | ||
if (player.Character != null && !player.Character.IsDead) | ||
{ | ||
double distance = Vector3D.Distance(player.Character.WorldMatrix.Translation, flag_entity.WorldMatrix.Translation); | ||
|
||
if (cockpit_allowed && distance <= flagPickupRadius && player.Controller?.ControlledEntity?.Entity is IMyCockpit) //distance <= [number] is the pickup radius | ||
{ | ||
return_list.Add(player); | ||
} | ||
|
||
//disabled pickup from suit | ||
// else | ||
// { | ||
// if (player.Controller?.ControlledEntity?.Entity is IMyCharacter && distance <= 40) | ||
// { | ||
// return_list.Add(player); | ||
// } | ||
// } | ||
} | ||
} | ||
return return_list; | ||
} | ||
|
||
public void UpdateFromNetwork(Flag incoming_flag) | ||
{ | ||
if (this.flag_entity != null) | ||
{ | ||
this.flag_entity.WorldMatrix = incoming_flag.current_matrix; | ||
} | ||
this.state = incoming_flag.state; | ||
this.lifetime = incoming_flag.lifetime; | ||
this.carrying_player_id = incoming_flag.carrying_player_id; | ||
this.current_matrix = incoming_flag.current_matrix; | ||
this.owning_faction_id = incoming_flag.owning_faction_id; | ||
this.current_drop_life = incoming_flag.current_drop_life; | ||
this.homePos = incoming_flag.homePos; | ||
this.flag_color = incoming_flag.flag_color; | ||
this.capture_positions = incoming_flag.capture_positions; | ||
this.flag_type = incoming_flag.flag_type; | ||
this.grip_strength = incoming_flag.grip_strength; | ||
this.regen_modifier = incoming_flag.regen_modifier; | ||
this.lastTickAcceleration = incoming_flag.lastTickAcceleration; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using ProtoBuf; | ||
using Sandbox.ModAPI; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using static Klime.CTF.CTF; | ||
using VRage.Game.ModAPI; | ||
|
||
namespace Jnick_SCModRepository.StarCoreCTF.Data.Scripts.CTF | ||
{ | ||
[ProtoContract] | ||
public class GameState | ||
{ | ||
[ProtoMember(50)] | ||
public CurrentGameState currentgamestate; | ||
|
||
[ProtoMember(51)] | ||
public Dictionary<long, int> faction_scores = new Dictionary<long, int>(); | ||
|
||
[ProtoMember(52)] | ||
public string winning_tag = ""; | ||
|
||
[ProtoMember(53)] | ||
public List<string> ordered_faction_tags = new List<string>(); | ||
|
||
public GameState() | ||
{ | ||
|
||
} | ||
|
||
public GameState(CurrentGameState currentgamestate, List<Flag> currentflags) | ||
{ | ||
this.currentgamestate = currentgamestate; | ||
foreach (var flag in currentflags) | ||
{ | ||
if (flag.flag_type == FlagType.Single) | ||
{ | ||
foreach (var faction_id in flag.capture_positions.Keys) | ||
{ | ||
faction_scores.Add(faction_id, 0); | ||
IMyFaction faction = MyAPIGateway.Session.Factions.TryGetFactionById(faction_id); | ||
if (faction != null) | ||
{ | ||
ordered_faction_tags.Add(faction.Tag); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
if (!faction_scores.ContainsKey(flag.owning_faction.FactionId)) | ||
{ | ||
faction_scores.Add(flag.owning_faction.FactionId, 0); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void UpdateScore(long incoming_faction) | ||
{ | ||
if (faction_scores.ContainsKey(incoming_faction)) | ||
{ | ||
faction_scores[incoming_faction] += 1; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using ProtoBuf; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using static Klime.CTF.CTF; | ||
|
||
namespace Jnick_SCModRepository.StarCoreCTF.Data.Scripts.CTF | ||
{ | ||
[ProtoContract] | ||
public class PacketBase | ||
{ | ||
[ProtoMember(200)] | ||
public PacketOp packet_op; | ||
|
||
[ProtoMember(201)] | ||
public List<Flag> all_flags_packet = new List<Flag>(); | ||
|
||
[ProtoMember(202)] | ||
public GameState gamestate_packet; | ||
|
||
public PacketBase() | ||
{ | ||
|
||
} | ||
} | ||
} |
Oops, something went wrong.