Skip to content

Commit

Permalink
Job syncing almost works
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaoticWagon committed Sep 18, 2023
1 parent 40ffaa9 commit e37dd3e
Show file tree
Hide file tree
Showing 14 changed files with 863 additions and 49 deletions.
65 changes: 65 additions & 0 deletions Multiplayer/Components/Networking/World/NetworkedStation.cs
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()");
}
}
50 changes: 50 additions & 0 deletions Multiplayer/Components/StationComponentLookup.cs
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)}]";
}
}
2 changes: 2 additions & 0 deletions Multiplayer/Multiplayer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@

<!-- Copy to game install folder -->
<Exec Condition="Exists('$(DvInstallDir)') And '$(OS)' == 'Windows_NT'" Command="powershell -executionpolicy bypass -Command &quot;(../package.ps1 -NoArchive -OutputDirectory '$(DvInstallDir)\Mods')&quot;" />
<Exec Condition="Exists('$(DvInstallDir)') And '$(OS)' == 'Windows_NT'" Command="powershell -executionpolicy bypass -Command &quot;(../package.ps1 -NoArchive -OutputDirectory 'D:\SteamLibrary\steamapps\common\Derail Valley 2\Mods')&quot;" />
<Exec Condition="Exists('$(DvInstallDir)') And '$(OS)' == 'Windows_NT'" Command="powershell -executionpolicy bypass -Command &quot;(../openScript.ps1)&quot;" />
<Exec Condition="Exists('$(DvInstallDir)') And '$(OS)' != 'Windows_NT'" Command="pwsh -Command &quot;(../package.ps1 -NoArchive -OutputDirectory '$(DvInstallDir)/Mods')&quot;" />
</Target>
</Project>
138 changes: 138 additions & 0 deletions Multiplayer/Networking/Data/JobData.cs
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()
};
}
}
2 changes: 1 addition & 1 deletion Multiplayer/Networking/Data/ModInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public readonly struct ModInfo
public readonly string Id;
public readonly string Version;

private ModInfo(string id, string version)
public ModInfo(string id, string version)
{
Id = id;
Version = version;
Expand Down
Loading

0 comments on commit e37dd3e

Please sign in to comment.