From 829bf729e5651d97f4045a2279e61f91a0e9b08b Mon Sep 17 00:00:00 2001 From: nxiaoxiao <3247747442@qq.com> Date: Sun, 18 Aug 2024 21:39:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=BB=BA=E5=88=86=E7=BB=84=E5=9B=BE?= =?UTF-8?q?=E5=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common/LayerData.cs | 16 +++++ Common/Utils.cs | 29 +++++++++ GlobalArea.cs | 93 +++++++++++++++++++++++++++ Instructs/CreateGroupLayerInstruct.cs | 56 ++++++++++++++++ 4 files changed, 194 insertions(+) create mode 100644 Instructs/CreateGroupLayerInstruct.cs diff --git a/Common/LayerData.cs b/Common/LayerData.cs index c144190..781f081 100644 --- a/Common/LayerData.cs +++ b/Common/LayerData.cs @@ -13,6 +13,13 @@ namespace OMS.NET.Common public JsonNode? Members { get; set; } public JsonNode? Structure { get; set; } public int Phase { get; set; } = 1; + public string? Name + { + get + { + return Structure?.AsArray().ElementAt(0)?.GetValue(); + } + } public LayerData(long layerId, string type) { @@ -130,6 +137,15 @@ namespace OMS.NET.Common UpdateToDb(); } } + + public void AddGroupLayer(long layerId) + { + if (Type == "order") + { + Members?.AsArray().Add(layerId); + UpdateToDb(); + } + } public MapLayer ConvertToMapLayer() { return new MapLayer() diff --git a/Common/Utils.cs b/Common/Utils.cs index 4614e28..71915ce 100644 --- a/Common/Utils.cs +++ b/Common/Utils.cs @@ -104,5 +104,34 @@ namespace OMS.NET.Common } details = newDetails; } + + /// + /// 生成 100000 到 999999 之间的随机整数 + /// + public static int GetRandomNumber6() + { + Random random = new(); + int minValue = 100000; + int maxValue = 999999; + return random.Next(minValue, maxValue + 1); + } + + /// + /// 创建模板id + /// + public static string CreateTemplateId() + { + // 定义有效字符集 + const string validChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + Random random = new(); + int length = random.Next(8, 15); + string result = ""; + for (int i = 0; i < length; i++) + { + int byteValue = random.Next(0, 256); + result += validChars[byteValue % validChars.Length]; + } + return result; + } } } \ No newline at end of file diff --git a/GlobalArea.cs b/GlobalArea.cs index 956c898..d1ffccc 100644 --- a/GlobalArea.cs +++ b/GlobalArea.cs @@ -3,6 +3,7 @@ using System.Security.Cryptography; using System.Text; using System.Text.Json; using System.Text.Json.Nodes; +using K4os.Compression.LZ4.Internal; using OMS.NET.Common; using OMS.NET.DbClass; using OMS.NET.Instructs; @@ -427,7 +428,99 @@ namespace OMS.NET } } + public static LayerData CreateGroupLayer(string creator) + { + lock (_LayerDataListLock) + { + //int max = _LayerDataList.Count; + LayerData layer = new(-1, "group") + { + Members = new JsonObject(), + TemplateId = CreateGroupLayerTemplateId() + }; + layer.Members.AsObject()["0"] = 0; + string time = GetCurrentTime(); + layer.Structure = new JsonArray(); + layer.Structure.AsArray().Add(CreateGroupLayerName()); + layer.Structure.AsArray().Add(new + { + template = new + { + id = layer.TemplateId, + name = "template", + creator = creator, + modify = time, + locked = false, + explain = "none", + typeRule = new + { + point = true, + line = true, + area = true, + curve = true + }, + detailsRule = new List + { + new { + set = false, + name = "name", + @default = "default", + type = "text" + } + }, + colorRule = new + { + basis = "", + type = "", + condition = new List() + }, + widthRule = new + { + basis = "", + type = "", + condition = new List() + } + } + }); + MapLayer mapLayer = layer.ConvertToMapLayer(); + MapLayer.Add(mapLayer); + if (mapLayer.Id != -1) + { + LayerData order = GetOrderLayerData(); + order.AddGroupLayer(mapLayer.Id); + layer.HasChange = true; + layer.LayerId = mapLayer.Id; + _LayerDataList.Add(layer); + } + return layer; + } + } + private static string CreateGroupLayerName() + { + lock (_LayerDataListLock) + { + string name = "layer " + Util.GetRandomNumber6(); + while (_LayerDataList.Any(x => x.Name == name)) + { + name = "layer " + Util.GetRandomNumber6(); + } + return name; + } + } + + private static string CreateGroupLayerTemplateId() + { + lock (_LayerDataListLock) + { + string tmpId = Util.CreateTemplateId(); + while (_LayerDataList.Any(x => x.TemplateId == tmpId)) + { + tmpId = Util.CreateTemplateId(); + } + return tmpId; + } + } #endregion diff --git a/Instructs/CreateGroupLayerInstruct.cs b/Instructs/CreateGroupLayerInstruct.cs new file mode 100644 index 0000000..91a4e6b --- /dev/null +++ b/Instructs/CreateGroupLayerInstruct.cs @@ -0,0 +1,56 @@ +using System.Text.Json; +using OMS.NET.Common; +using OMS.NET.DbClass; + +namespace OMS.NET.Instructs +{ + public class CreateGroupLayerInstruct : Instruct + { + public CreateGroupLayerInstruct() + { + Type = "broadcast"; + Class = "createGroupLayer"; + } + + public override Task Handler(string wsid) + { + return Task.Run(() => + { + if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查不通过则直接退出 + if (Data?.GetType() != typeof(JsonElement)) return;//Data 非空和JsonElement类型检查 + try + { + string conveyor = GlobalArea.GetLoginEmailByID(wsid); + AccountData accountData = GlobalArea.GetLoginAccountData(conveyor)!; + string creator = $"{accountData.UserName}({conveyor})"; + LayerData newGroupLayer = GlobalArea.CreateGroupLayer(creator); + string time = GlobalArea.GetCurrentTime(); + ResponseOrBroadcastInstructs.Add(new Instruct() + { + IsBroadcast = true, + Type = "broadcast", + Class = "createGroupLayer", + Conveyor = conveyor, + Time = time, + Data = newGroupLayer + }); + ResponseOrBroadcastInstructs.Add(new Instruct() + { + IsBroadcast = true, + Type = "broadcast", + Class = "updateLayerOrder", + Conveyor = conveyor, + Time = time, + Data = new{ + member = GlobalArea.GetOrderLayerData().Members + } + }); + } + catch (Exception ex) + { + GlobalArea.Log.Warn($"处理{Class}广播指令出错:" + ex.Message); + } + }); + } + } +} \ No newline at end of file