Changeset 487 for TFP-WebServer/MarkersMod/src
- Timestamp:
- Jun 17, 2024, 5:25:43 PM (6 months ago)
- Location:
- TFP-WebServer/MarkersMod/src
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
TFP-WebServer/MarkersMod/src/Markers.cs
r459 r487 15 15 "https://upload.wikimedia.org/wikipedia/commons/thumb/1/11/Blue_question_mark_icon.svg/1200px-Blue_question_mark_icon.svg.png"; 16 16 17 private readonly Dictionary<string, (int, int, string)> markers = new Dictionary<string, (int, int, string)> ();17 private readonly Dictionary<string, MarkerData> markers = new Dictionary<string, MarkerData> (); 18 18 19 19 public Markers () { … … 24 24 int y = random.RandomRange (-1000, 1001); 25 25 26 markers.Add (WebUtils.GenerateGuid (), (x, y, null)); 26 string guid = WebUtils.GenerateGuid (); 27 markers.Add (guid, new MarkerData(guid, new Vector2i (x, y), "RandomMarker " + i, null)); 27 28 } 28 29 … … 33 34 private static readonly byte[] jsonKeyX = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("x"); 34 35 private static readonly byte[] jsonKeyY = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("y"); 36 private static readonly byte[] jsonKeyName = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("name"); 35 37 private static readonly byte[] jsonKeyIcon = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("icon"); 36 38 … … 44 46 45 47 bool first = true; 46 foreach (( string markerId, (int, int, string) properties) in markers) {48 foreach ((_, MarkerData iMarker) in markers) { 47 49 if (!first) { 48 50 writer.WriteValueSeparator (); … … 51 53 first = false; 52 54 53 writeMarkerJson (ref writer, markerId, properties);55 writeMarkerJson (ref writer, iMarker); 54 56 } 55 57 … … 59 61 } 60 62 61 if (!markers.TryGetValue (id, out (int, int, string) properties2)) {63 if (!markers.TryGetValue (id, out MarkerData singleMarker)) { 62 64 writer.WriteRaw (WebUtils.JsonEmptyData); 63 65 SendEnvelopedResult (_context, ref writer, HttpStatusCode.NotFound); … … 68 70 writer.WriteBeginArray (); 69 71 70 writeMarkerJson (ref writer, id, properties2);72 writeMarkerJson (ref writer, singleMarker); 71 73 72 74 writer.WriteEndArray (); … … 75 77 } 76 78 77 private void writeMarkerJson (ref JsonWriter _writer, string _markerId, (int, int, string) _properties) {79 private void writeMarkerJson (ref JsonWriter _writer, MarkerData _marker) { 78 80 _writer.WriteRaw (jsonKeyId); 79 _writer.WriteString (_marker Id);81 _writer.WriteString (_marker.Id); 80 82 _writer.WriteRaw (jsonKeyX); 81 (int x, int y, string icon) = _properties; 82 _writer.WriteInt32 (x); 83 _writer.WriteInt32 (_marker.Position.x); 83 84 _writer.WriteRaw (jsonKeyY); 84 _writer.WriteInt32 (y); 85 _writer.WriteInt32 (_marker.Position.y); 86 _writer.WriteRaw (jsonKeyName); 87 _writer.WriteString (_marker.Name); 85 88 _writer.WriteRaw (jsonKeyIcon); 86 _writer.WriteString ( icon ?? defaultIcon);89 _writer.WriteString (_marker.Icon ?? defaultIcon); 87 90 _writer.WriteEndObject (); 88 91 } … … 99 102 } 100 103 104 JsonCommons.TryGetJsonField (_jsonInput, "name", out string name); 105 if (string.IsNullOrEmpty (name)) { 106 name = null; 107 } 108 101 109 JsonCommons.TryGetJsonField (_jsonInput, "icon", out string icon); 102 110 if (string.IsNullOrEmpty (icon)) { … … 105 113 106 114 string newId = WebUtils.GenerateGuid (); 107 markers.Add (newId, (x, y, icon));115 markers.Add (newId, new MarkerData(newId, new Vector2i (x, y), name, icon)); 108 116 109 117 PrepareEnvelopedResult (out JsonWriter writer); … … 123 131 } 124 132 125 bool keepIcon = !_jsonInput.TryGetValue ("icon", out _); 126 JsonCommons.TryGetJsonField (_jsonInput, "icon", out string icon); 127 if (string.IsNullOrEmpty (icon)) { 128 icon = null; 129 } 133 bool keepName = !JsonCommons.TryGetJsonField (_jsonInput, "name", out string name); 134 135 bool keepIcon = !JsonCommons.TryGetJsonField (_jsonInput, "icon", out string icon); 130 136 131 137 string id = _context.RequestPath; 132 138 133 if (!markers.TryGetValue (id, out (int, int, string) properties)) {139 if (!markers.TryGetValue (id, out MarkerData oldMarker)) { 134 140 SendEmptyResponse (_context, HttpStatusCode.NotFound, _jsonInputData, "ID_NOT_FOUND"); 135 141 return; 136 142 } 137 143 144 if (keepName) { 145 name = oldMarker.Name; 146 } 147 138 148 if (keepIcon) { 139 icon = properties.Item3;149 icon = oldMarker.Icon; 140 150 } 141 markers [id] = (x, y, icon); 151 152 MarkerData updatedMarker = new MarkerData(id, new Vector2i (x, y), name, icon); 153 markers [id] = updatedMarker; 142 154 143 155 PrepareEnvelopedResult (out JsonWriter writer); 144 writer.WriteRaw (jsonKeyId); 145 writer.WriteString (id); 146 writer.WriteRaw (jsonKeyX); 147 writer.WriteInt32 (x); 148 writer.WriteRaw (jsonKeyY); 149 writer.WriteInt32 (y); 150 writer.WriteRaw (jsonKeyIcon); 151 writer.WriteString (icon); 152 writer.WriteEndObject (); 156 writeMarkerJson (ref writer, updatedMarker); 153 157 SendEnvelopedResult (_context, ref writer); 154 158 } -
TFP-WebServer/MarkersMod/src/Markers.openapi.yaml
r460 r487 24 24 examples: 25 25 - 321 26 name: 27 type: 28 - string 29 - 'null' 30 examples: 31 - My House 32 description: Name shown with the marker 26 33 icon: 27 34 type: … … 36 43 - x 37 44 - y 45 - name 46 - icon 38 47 39 48 MarkersList: … … 58 67 examples: 59 68 - 842 69 name: 70 type: 71 - string 72 - 'null' 73 examples: 74 - My House 60 75 icon: 61 type: string 76 type: 77 - string 78 - 'null' 62 79 format: uuid 63 80 examples:
Note:
See TracChangeset
for help on using the changeset viewer.