From 3264956cdf00950472c7db10d1a7afa9dacc1c66 Mon Sep 17 00:00:00 2001 From: nxiaoxiao <3247747442@qq.com> Date: Sun, 18 Aug 2024 14:46:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E6=9B=B4=E6=96=B0=E5=85=83?= =?UTF-8?q?=E7=B4=A0=E6=8C=87=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Instructs/UpdateElementInstuct.cs | 91 +++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 Instructs/UpdateElementInstuct.cs diff --git a/Instructs/UpdateElementInstuct.cs b/Instructs/UpdateElementInstuct.cs new file mode 100644 index 0000000..dffb88d --- /dev/null +++ b/Instructs/UpdateElementInstuct.cs @@ -0,0 +1,91 @@ +using System.Text.Json; +using OMS.NET.Common; +using OMS.NET.DbClass; + +namespace OMS.NET.Instructs +{ + public class UpdateElementInstuct : Instruct + { + public UpdateElementInstuct() + { + Type = "broadcast"; + Class = "updateElement"; + } + + public override Task Handler(string wsid) + { + return Task.Run(() => + { + if (!GlobalArea.LoginCheckByID(wsid)) return;//登录检查不通过则直接退出 + if (Data?.GetType() != typeof(JsonElement)) return;//Data 非空和JsonElement类型检查 + string conveyor = GlobalArea.GetLoginEmailByID(wsid); + try + { + long id = Data.GetProperty("id").GetInt64(); + string updateId = Data.GetProperty("updateId").GetString(); + if (Data.TryGetProperty("changes", out JsonElement changes)) + { + MapData mapData = MapData.Get(id) ?? throw new Exception($"数据库中未能找到id为{id}的元素"); + string color = changes.GetProperty("color").GetString()!; + if (changes.TryGetProperty("width", out JsonElement widthElement)) + { + int width = widthElement.GetInt32(); + mapData.Width = width; + } + if (changes.TryGetProperty("details", out JsonElement detailsElement)) + { + string details = detailsElement.GetString()!; + mapData.Details = Util.JsonToBase64(details); + } + if (changes.TryGetProperty("custom", out JsonElement customElement)) + { + string custom = customElement.GetString()!; + mapData.Custom = Util.JsonToBase64(custom); + } + if (MapData.Update(mapData) == -1) throw new Exception("数据库修改失败"); + ResponseOrBroadcastInstructs.Add(new Instruct() + { + IsResponse = true, + Type = "send_correct", + Class = "updateElement", + Conveyor = conveyor, + Time = GlobalArea.GetCurrentTime(), + Data = new + { + vid = updateId, + rid = id + } + }); + ResponseOrBroadcastInstructs.Add(new Instruct() + { + IsBroadcast = true, + Type = "broadcast", + Class = "updateElement", + Conveyor = conveyor, + Time = GlobalArea.GetCurrentTime(), + Data = mapData + }); + } + + } + catch (Exception ex) + { + GlobalArea.Log.Warn($"处理{Class}广播指令出错:" + ex.Message); + ResponseOrBroadcastInstructs.Add(new Instruct() + { + IsResponse = true, + Type = "send_error", + Class = "updateElement", + Conveyor = conveyor, + Time = GlobalArea.GetCurrentTime(), + Data = new + { + source = Data, + message = ex.Message + } + }); + } + }); + } + } +} \ No newline at end of file