forked from Insprill/dv-multiplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40ffaa9
commit e37dd3e
Showing
14 changed files
with
863 additions
and
49 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
Multiplayer/Components/Networking/World/NetworkedStation.cs
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,65 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using DV.Logic.Job; | ||
using UnityEngine; | ||
|
||
namespace Multiplayer.Components.Networking.World; | ||
|
||
public class NetworkedStation : MonoBehaviour | ||
{ | ||
private List<Job> jobs; | ||
private StationController stationController; | ||
|
||
public void AddJob(Job job) | ||
{ | ||
jobs.Add(job); | ||
} | ||
|
||
|
||
private void Awake() | ||
{ | ||
Multiplayer.Log("NetworkedStation.Awake()"); | ||
|
||
stationController = GetComponent<StationController>(); | ||
StartCoroutine(WaitForLogicStation()); | ||
} | ||
|
||
private IEnumerator WaitForLogicStation() | ||
{ | ||
while (stationController.logicStation == null) | ||
yield return null; | ||
|
||
StationComponentLookup.Instance.RegisterStation(stationController); | ||
|
||
jobs = new List<Job>(stationController.logicStation.availableJobs); | ||
|
||
if (NetworkLifecycle.Instance.IsHost()) | ||
{ | ||
stationController.logicStation.JobAddedToStation += OnJobAddedToStation; | ||
NetworkLifecycle.Instance.OnTick += Server_OnTick; | ||
} | ||
|
||
Multiplayer.Log("NetworkedStation.Awake() done"); | ||
} | ||
|
||
private void OnJobAddedToStation() | ||
{ | ||
foreach (var job in stationController.logicStation.availableJobs) | ||
{ | ||
jobs.Add(job); | ||
} | ||
|
||
Multiplayer.Log("NetworkedStation.OnJobAddedToStation()"); | ||
} | ||
|
||
private void Server_OnTick(uint tick) | ||
{ | ||
if (jobs.Count == 0) | ||
return; | ||
|
||
NetworkLifecycle.Instance.Server.SendJobsToAll(jobs.ToArray(), stationController.logicStation.ID); | ||
jobs.Clear(); | ||
|
||
Multiplayer.Log("NetworkedStation.Server_OnTick()"); | ||
} | ||
} |
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,50 @@ | ||
using System.Collections.Generic; | ||
using DV.Logic.Job; | ||
using DV.Utils; | ||
using JetBrains.Annotations; | ||
using Multiplayer.Components.Networking.World; | ||
|
||
namespace Multiplayer.Components; | ||
|
||
public class StationComponentLookup : SingletonBehaviour<StationComponentLookup> | ||
{ | ||
private readonly Dictionary<Station, NetworkedStation> stationToNetworkedStationController = new(); | ||
private readonly Dictionary<string, NetworkedStation> stationIdToNetworkedStation = new(); | ||
private readonly Dictionary<string, StationController> stationIdToStationController = new(); | ||
|
||
public void RegisterStation(StationController stationController) | ||
{ | ||
var networkedStation = stationController.GetComponent<NetworkedStation>(); | ||
stationToNetworkedStationController[stationController.logicStation] = networkedStation; | ||
stationIdToNetworkedStation[stationController.logicStation.ID] = networkedStation; | ||
stationIdToStationController[stationController.logicStation.ID] = stationController; | ||
} | ||
|
||
public void UnregisterStation(StationController stationController) | ||
{ | ||
stationToNetworkedStationController.Remove(stationController.logicStation); | ||
stationIdToNetworkedStation.Remove(stationController.logicStation.ID); | ||
stationIdToStationController.Remove(stationController.logicStation.ID); | ||
} | ||
|
||
public bool NetworkedStationFromStation(Station station, out NetworkedStation networkedStation) | ||
{ | ||
return stationToNetworkedStationController.TryGetValue(station, out networkedStation); | ||
} | ||
|
||
public bool NetworkedStationFromId(string stationId, out NetworkedStation networkedStation) | ||
{ | ||
return stationIdToNetworkedStation.TryGetValue(stationId, out networkedStation); | ||
} | ||
|
||
public bool StationControllerFromId(string stationId, out StationController stationController) | ||
{ | ||
return stationIdToStationController.TryGetValue(stationId, out stationController); | ||
} | ||
|
||
[UsedImplicitly] | ||
public new static string AllowAutoCreate() | ||
{ | ||
return $"[{nameof(StationComponentLookup)}]"; | ||
} | ||
} |
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
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,138 @@ | ||
using System.Linq; | ||
using DV.Logic.Job; | ||
using LiteNetLib.Utils; | ||
using Newtonsoft.Json; | ||
|
||
namespace Multiplayer.Networking.Data; | ||
|
||
public class JobData | ||
{ | ||
public byte JobType { get; set; } | ||
public string ID { get; set; } | ||
public TaskBeforeDataData[] Tasks { get; set; } | ||
public StationsChainDataData ChainData { get; set; } | ||
public int RequiredLicenses { get; set; } | ||
public float StartTime { get; set; } | ||
public float FinishTime { get; set; } | ||
public float InitialWage { get; set; } | ||
public byte State { get; set; } | ||
public float TimeLimit { get; set; } | ||
|
||
public static JobData FromJob(Job job) | ||
{ | ||
return new JobData | ||
{ | ||
JobType = (byte)job.jobType, | ||
ID = job.ID, | ||
Tasks = job.tasks.Select(x => TaskBeforeDataData.FromTask(x)).ToArray(), | ||
ChainData = StationsChainDataData.FromStationData(job.chainData), | ||
RequiredLicenses = (int)job.requiredLicenses, | ||
StartTime = job.startTime, | ||
FinishTime = job.finishTime, | ||
InitialWage = job.initialWage, | ||
State = (byte)job.State, | ||
TimeLimit = job.TimeLimit | ||
}; | ||
} | ||
|
||
public static void Serialize(NetDataWriter writer, JobData data) | ||
{ | ||
writer.Put(data.JobType); | ||
writer.Put(data.ID); | ||
writer.Put((byte)data.Tasks.Length); | ||
foreach (var taskBeforeDataData in data.Tasks) | ||
TaskBeforeDataData.SerializeTask(taskBeforeDataData, writer); | ||
StationsChainDataData.Serialize(writer, data.ChainData); | ||
writer.Put(data.RequiredLicenses); | ||
writer.Put(data.StartTime); | ||
writer.Put(data.FinishTime); | ||
writer.Put(data.InitialWage); | ||
writer.Put(data.State); | ||
writer.Put(data.TimeLimit); | ||
Multiplayer.Log(JsonConvert.SerializeObject(data, Formatting.Indented)); | ||
} | ||
|
||
public static JobData Deserialize(NetDataReader reader) | ||
{ | ||
Multiplayer.Log("JobData.Deserialize()"); | ||
var jobType = reader.GetByte(); | ||
Multiplayer.Log("JobData.Deserialize() jobType: " + jobType); | ||
var id = reader.GetString(); | ||
Multiplayer.Log("JobData.Deserialize() id: " + id); | ||
var tasksLength = reader.GetByte(); | ||
Multiplayer.Log("JobData.Deserialize() tasksLength: " + tasksLength); | ||
var tasks = new TaskBeforeDataData[tasksLength]; | ||
for (int i = 0; i < tasksLength; i++) | ||
tasks[i] = TaskBeforeDataData.DeserializeTask(reader); | ||
Multiplayer.Log("JobData.Deserialize() tasks: " + JsonConvert.SerializeObject(tasks, Formatting.Indented)); | ||
var chainData = StationsChainDataData.Deserialize(reader); | ||
Multiplayer.Log("JobData.Deserialize() chainData: " + JsonConvert.SerializeObject(chainData, Formatting.Indented)); | ||
var requiredLicenses = reader.GetInt(); | ||
Multiplayer.Log("JobData.Deserialize() requiredLicenses: " + requiredLicenses); | ||
var startTime = reader.GetFloat(); | ||
Multiplayer.Log("JobData.Deserialize() startTime: " + startTime); | ||
var finishTime = reader.GetFloat(); | ||
Multiplayer.Log("JobData.Deserialize() finishTime: " + finishTime); | ||
var initialWage = reader.GetFloat(); | ||
Multiplayer.Log("JobData.Deserialize() initialWage: " + initialWage); | ||
var state = reader.GetByte(); | ||
Multiplayer.Log("JobData.Deserialize() state: " + state); | ||
var timeLimit = reader.GetFloat(); | ||
Multiplayer.Log(JsonConvert.SerializeObject(new JobData | ||
{ | ||
JobType = jobType, | ||
ID = id, | ||
Tasks = tasks, | ||
ChainData = chainData, | ||
RequiredLicenses = requiredLicenses, | ||
StartTime = startTime, | ||
FinishTime = finishTime, | ||
InitialWage = initialWage, | ||
State = state, | ||
TimeLimit = timeLimit | ||
}, Formatting.Indented)); | ||
return new JobData | ||
{ | ||
JobType = jobType, | ||
ID = id, | ||
Tasks = tasks, | ||
ChainData = chainData, | ||
RequiredLicenses = requiredLicenses, | ||
StartTime = startTime, | ||
FinishTime = finishTime, | ||
InitialWage = initialWage, | ||
State = state, | ||
TimeLimit = timeLimit | ||
}; | ||
} | ||
} | ||
|
||
public struct StationsChainDataData | ||
{ | ||
public string ChainOriginYardId { get; set; } | ||
public string ChainDestinationYardId { get; set; } | ||
|
||
public static StationsChainDataData FromStationData(StationsChainData data) | ||
{ | ||
return new StationsChainDataData | ||
{ | ||
ChainOriginYardId = data.chainOriginYardId, | ||
ChainDestinationYardId = data.chainDestinationYardId | ||
}; | ||
} | ||
|
||
public static void Serialize(NetDataWriter writer, StationsChainDataData data) | ||
{ | ||
writer.Put(data.ChainOriginYardId); | ||
writer.Put(data.ChainDestinationYardId); | ||
} | ||
|
||
public static StationsChainDataData Deserialize(NetDataReader reader) | ||
{ | ||
return new StationsChainDataData | ||
{ | ||
ChainOriginYardId = reader.GetString(), | ||
ChainDestinationYardId = reader.GetString() | ||
}; | ||
} | ||
} |
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
Oops, something went wrong.