Store job/shop values and vehicle switch states for late joiners (2.9.2)
WorldVars and VehicleControls were only relayed; players joining later only got them with the next save pull. They are now stored in world.json (cleared with a new save) and appended to the world snapshot (old clients ignore it). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -29,6 +29,10 @@ public sealed class WorldState
|
||||
public Dictionary<string, string> Doors { get; set; } = new();
|
||||
/// <summary>Fahrzeugzustand (Tank, Öl, Schrauben ...): Schlüssel -> Base64 [byte typ, wert].</summary>
|
||||
public Dictionary<int, string> Vars { get; set; } = new();
|
||||
/// <summary>Jobs/Laden (WorldVars): Schlüssel -> Base64 [byte typ, wert] - nur int/bool.</summary>
|
||||
public Dictionary<int, string> JobVars { get; set; } = new();
|
||||
/// <summary>Schalter an Fahrzeugen (Licht, Zündung, Motor ...): FSM-Schlüssel -> aktiver Zustand.</summary>
|
||||
public Dictionary<int, string> Controls { get; set; } = new();
|
||||
/// <summary>Von Admins gespawnte Items (SpawnItem-Nachrichten, Base64), damit später Beitretende sie auch haben.</summary>
|
||||
public List<string> Spawns { get; set; } = new();
|
||||
public const int MaxSpawns = 2000;
|
||||
@@ -76,6 +80,8 @@ public sealed class WorldState
|
||||
MoneyDelta = 0f;
|
||||
Doors.Clear();
|
||||
Vars.Clear();
|
||||
JobVars.Clear();
|
||||
Controls.Clear();
|
||||
Appeared.Clear();
|
||||
Destroyed.Clear();
|
||||
Dirty = true;
|
||||
@@ -93,6 +99,8 @@ public sealed class WorldState
|
||||
MoneyDelta = 0f;
|
||||
Doors.Clear();
|
||||
Vars.Clear();
|
||||
JobVars.Clear();
|
||||
Controls.Clear();
|
||||
Dirty = true;
|
||||
}
|
||||
|
||||
@@ -183,6 +191,47 @@ public sealed class WorldState
|
||||
Dirty = true;
|
||||
}
|
||||
|
||||
/// <summary>WorldVars (Jobs/Laden): int n, je [int key, byte typ (2 int / 3 bool), wert].</summary>
|
||||
public void ApplyJobVars(byte[] payload)
|
||||
{
|
||||
ChangesSinceSave++;
|
||||
using var r = new BinaryReader(new MemoryStream(payload));
|
||||
int count = r.ReadInt32();
|
||||
if (count < 0 || count > 10000) return;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
int key = r.ReadInt32();
|
||||
byte type = r.ReadByte();
|
||||
if (type != 2 && type != 3) return;
|
||||
byte[] value = r.ReadBytes(type == 3 ? 1 : 4);
|
||||
var all = new byte[value.Length + 1];
|
||||
all[0] = type;
|
||||
value.CopyTo(all, 1);
|
||||
if (JobVars.Count >= MaxVars && !JobVars.ContainsKey(key)) continue;
|
||||
JobVars[key] = Convert.ToBase64String(all);
|
||||
}
|
||||
Dirty = true;
|
||||
}
|
||||
|
||||
/// <summary>VehicleControls: int n, je [int key, string zustand].</summary>
|
||||
public void ApplyControls(byte[] payload)
|
||||
{
|
||||
using var r = new BinaryReader(new MemoryStream(payload));
|
||||
int count = r.ReadInt32();
|
||||
if (count < 0 || count > 10000) return;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
int key = r.ReadInt32();
|
||||
string state = r.ReadString();
|
||||
if (state.Length > 100) continue;
|
||||
if (Controls.Count >= MaxControls && !Controls.ContainsKey(key)) continue;
|
||||
Controls[key] = state;
|
||||
}
|
||||
Dirty = true;
|
||||
}
|
||||
|
||||
public const int MaxControls = 5000;
|
||||
|
||||
// Obergrenzen, damit ein fehlerhafter oder manipulierter Client world.json nicht endlos aufbläht
|
||||
public const int MaxEvents = 50_000;
|
||||
public const int MaxVars = 50_000;
|
||||
@@ -330,6 +379,18 @@ public sealed class WorldState
|
||||
w.Write(needs.Count);
|
||||
foreach (var kv in needs) { w.Write(kv.Key); w.Write(kv.Value); }
|
||||
}
|
||||
|
||||
// Jobs/Laden: int n, je [int key, byte typ, wert]
|
||||
w.Write(JobVars.Count);
|
||||
foreach (var kv in JobVars)
|
||||
{
|
||||
w.Write(kv.Key);
|
||||
w.Write(Convert.FromBase64String(kv.Value));
|
||||
}
|
||||
|
||||
// Schalter (Licht, Zündung, Motor ...): int n, je [int key, string zustand]
|
||||
w.Write(Controls.Count);
|
||||
foreach (var kv in Controls) { w.Write(kv.Key); w.Write(kv.Value); }
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user