forked from steveman0/FreightCartMod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFreightInterfaceContainer.cs
More file actions
77 lines (69 loc) · 2.96 KB
/
FreightInterfaceContainer.cs
File metadata and controls
77 lines (69 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using FortressCraft.Community.Utilities;
public class FreightInterfaceContainer
{
public FreightCartStation Station;
public FreightSystemInterface Interface;
public List<FreightRegistry> Registries;
public object offerlock = new object();
public object requestlock = new object();
public FreightInterfaceContainer(FreightCartStation station, FreightSystemInterface inter)
{
this.Station = station;
this.Interface = inter;
this.Registries = new List<FreightRegistry>();
}
public void UpdateRegistries()
{
if (string.IsNullOrEmpty(Station.NetworkID))
return;
List<FreightRegistry> registries = new List<FreightRegistry>();
lock (offerlock)
{
foreach (FreightListing entry in Interface.FreightOfferings)
{
if (entry.Item == null)
continue;
FreightRegistry reg = new FreightRegistry(Station.NetworkID, null, entry.Item, 0, entry.Quantity, FreightRegistry.RegistryType.InterfaceData);
reg.Surplus = entry.Quantity;
registries.Add(reg);
}
}
lock (requestlock)
{
foreach (FreightListing entry in Interface.FreightRequests)
{
if (entry.Item == null)
continue;
FreightRegistry reg = registries.Where(x => x.FreightItem.Compare(entry.Item)).FirstOrDefault();
if (reg != null)
{
reg.Deficit = entry.Quantity;
reg.LowStock = entry.Quantity;
}
else
{
reg = new FreightRegistry(Station.NetworkID, null, entry.Item, entry.Quantity, int.MaxValue, FreightRegistry.RegistryType.InterfaceData);
reg.Deficit = entry.Quantity;
registries.Add(reg);
}
}
}
foreach (FreightRegistry reg in registries)
{
//Debug.Log("FreightInterfaceContainer updating registry with reg item: " + reg.FreightItem.ToString() + " lowstock: " + reg.LowStock + "highstock: " + reg.HighStock + " deficit: " + reg.Deficit + " surplus: " + reg.Surplus);
}
// Check existing registries -> if the new update doesn't have the entry then zero it in the network to remove later
foreach (FreightRegistry reg in this.Registries)
{
if (!registries.Exists(x => x.FreightItem.Compare(reg.FreightItem)))
{
FreightCartManager.instance.ZeroInterfaceRegistry(Station.NetworkID, reg.FreightItem);
//Debug.Log("FreightInterfaceContainer had to zero an interface registry for " + reg.FreightItem.ToString());
}
}
this.Registries = registries;
}
}