Skip to content

Commit

Permalink
适配mower支持
Browse files Browse the repository at this point in the history
  • Loading branch information
Funny-ppt committed Mar 29, 2024
1 parent 5399fc2 commit 03e0fb3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
10 changes: 0 additions & 10 deletions InfrastSim/TimeDriven/Simulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,14 +438,4 @@ public Simulator Clone() {
using var doc = JsonDocument.Parse(ms);
return new Simulator(doc.RootElement);
}

/// <summary>
/// 该方法仅供测试使用:
/// 如果没有干员访问这两个属性,在Resolve中就不会产生,但Update中制造站和贸易站始终会访问这两个值,
/// 进而导致出现默认的值,使得序列化、反序列化结果不一致(尽管没有任何影响)
/// </summary>
public void EnsurePropExists() {
var p1 = GlobalManufacturingEfficiency;
var p2 = GlobalTradingEfficiency;
}
}
37 changes: 22 additions & 15 deletions InfrastSim/TimeDriven/WebHelper/MowerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public static JsonNode RewriteSimulator(Simulator simu, JsonNode node) {
}
node["global_properties"] = globalProperties;
var facilities = new JsonObject {
["controlCenter"] = RewriteFacility(node["control-center"]!),
["office"] = RewriteFacility(node["office"]!),
["recpetionRoom"] = RewriteFacility(node["reception"]!),
["training"] = RewriteFacility(node["training"]!),
["crafting"] = RewriteFacility(node["crafting"]!)
["controlCenter"] = RewriteFacility(node["control-center"]!, simu),
["office"] = RewriteFacility(node["office"]!, simu),
["recpetionRoom"] = RewriteFacility(node["reception"]!, simu),
["training"] = RewriteFacility(node["training"]!, simu),
["crafting"] = RewriteFacility(node["crafting"]!, simu)
};
for (int i = 0; i < 4; i++) {
var fac = node["dormitories"]![i];
Expand All @@ -40,7 +40,7 @@ public static JsonNode RewriteSimulator(Simulator simu, JsonNode node) {
newfac = RewriteManufacturing(fac, simu);
break;
case "Power":
newfac = RewritePower(fac);
newfac = RewritePower(fac, simu);
break;
default:
break;
Expand All @@ -51,7 +51,7 @@ public static JsonNode RewriteSimulator(Simulator simu, JsonNode node) {
node["facilities"] = facilities;
var operators = new JsonArray();
foreach (var op in node["operators"]!.AsArray()) {
operators.Add(RewriteOperator(op));
operators.Add(RewriteOperator(op, simu));
}
node["operators-mower"] = operators;
return node;
Expand All @@ -74,15 +74,22 @@ public static JsonObject RewriteOrder(JsonNode order, double timeRemain = 0) {
newOrder["time_remain"] = (long)timeRemain;
return newOrder;
}
public static JsonObject? RewriteOperator(JsonNode? op) {
public static JsonObject? RewriteOperator(JsonNode? op, Simulator simu) {
if (op == null) return null;

var name = op["name"]!.ToString();
var simuOp = simu.GetOperator(name);

var newop = new JsonObject {
["name"] = op["name"]!.DeepClone(),
["morale"] = op["mood"]!.DeepClone(),
["efficiency"] = op["efficiency"]!.DeepClone()
["is_working"] = simuOp.Facility != null && simuOp.Facility is not Dormitory
};

if (simuOp.Facility != null) {
newop["facility-index"] = Array.IndexOf(simu.Facilities, simuOp.Facility);
}
if (op["mood"]!.GetValue<double>() > 1e-9) {
newop["time"] = op["working-time-seconds"]!.DeepClone();
} else {
Expand All @@ -99,10 +106,10 @@ public static JsonObject RewriteOrder(JsonNode order, double timeRemain = 0) {

return newop;
}
public static JsonObject RewriteFacility(JsonNode fac) {
public static JsonObject RewriteFacility(JsonNode fac, Simulator simu) {
var ops = new JsonArray();
foreach (var op in fac["operators"]!.AsArray()) {
ops.Add(RewriteOperator(op!));
ops.Add(RewriteOperator(op!, simu));
}

var newfac = new JsonObject {
Expand All @@ -115,13 +122,13 @@ public static JsonObject RewriteFacility(JsonNode fac) {
return newfac;
}
public static JsonObject RewriteDormitory(JsonNode fac, Simulator simu, int id) {
var newfac = RewriteFacility(fac);
var newfac = RewriteFacility(fac, simu);
newfac["name"] = $"宿舍{id + 1}";
newfac["vip"] = simu.GetVipName(id);
return newfac;
}
public static JsonObject RewriteManufacturing(JsonNode fac, Simulator simu) {
var newfac = RewriteFacility(fac);
var newfac = RewriteFacility(fac, simu);

newfac["name"] = "制造站";
newfac["base_capacity"] = fac["base-capacity"]!.DeepClone();
Expand All @@ -144,7 +151,7 @@ public static JsonObject RewriteTrading(JsonNode fac, Simulator simu) {
var cur_order = fac["current-order"];
if (cur_order != null) orders.Add(RewriteOrder(cur_order, fac["remains"]!.GetValue<double>()));

var newfac = RewriteFacility(fac);
var newfac = RewriteFacility(fac, simu);
newfac["name"] = "贸易站";
newfac["base_order_limit"] = fac["base-capacity"]!.DeepClone();
newfac["order_limit"] = fac["capacity"]!.DeepClone();
Expand All @@ -159,10 +166,10 @@ public static JsonObject RewriteTrading(JsonNode fac, Simulator simu) {

return newfac;
}
public static JsonObject RewritePower(JsonNode fac) {
public static JsonObject RewritePower(JsonNode fac, Simulator simu) {
var ops = new JsonArray();
foreach (var op in fac["operators"]!.AsArray()) {
ops.Add(RewriteOperator(op!));
ops.Add(RewriteOperator(op!, simu));
}
var base_eff = fac["base-efficiency"]!.GetValue<double>() - 1;
var newfac = new JsonObject {
Expand Down
9 changes: 8 additions & 1 deletion InfrastSimTest/SimulatorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ public static class SimulatorService {
private static int SimuId = 0;
private static ConcurrentDictionary<int, Simulator> Simus = new();

static void EnsurePropExists(Simulator simu) {
/// 如果没有干员访问这两个属性,在Resolve中就不会产生,但Update中制造站和贸易站始终会访问这两个值,
/// 进而导致出现默认的值,使得序列化、反序列化结果不一致(尽管没有任何影响)
var p1 = simu.GlobalManufacturingEfficiency;
var p2 = simu.GlobalTradingEfficiency;
}

static Simulator GetSimulator(int id) {
if (!Simus.TryGetValue(id, out var simulator)) {
throw new KeyNotFoundException();
Expand Down Expand Up @@ -45,7 +52,7 @@ public static bool Destory(int id) {
public static string GetData(int id, bool detailed = true) {
var simu = GetSimulator(id);

simu.EnsurePropExists();
EnsurePropExists(simu);
simu.Resolve();
using var ms = new MemoryStream();
using var writer = new Utf8JsonWriter(ms);
Expand Down
1 change: 1 addition & 0 deletions InfrastSimTest/SimulatorTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using InfrastSim.TimeDriven;
using InfrastSim.TimeDriven.Enumerate;
using InfrastSim.Wasm;
using System.Text;
Expand Down

0 comments on commit 03e0fb3

Please sign in to comment.