source: TFP-WebServer/MarkersMod/src/Markers.cs@ 488

Last change on this file since 488 was 487, checked in by alloc, 5 months ago

1.1.0.1 Release for V 1.0

File size: 5.8 KB
RevLine 
[388]1using System.Collections.Generic;
2using System.Net;
[402]3using JetBrains.Annotations;
4using Utf8Json;
[391]5using Webserver;
[426]6using Webserver.Permissions;
[391]7using Webserver.WebAPI;
[388]8
[390]9namespace Examples {
[402]10 [UsedImplicitly]
[391]11 public class Markers : AbsRestApi {
12 private const int numRandomMarkers = 5;
[388]13
[430]14 private const string defaultIcon =
15 "https://upload.wikimedia.org/wikipedia/commons/thumb/1/11/Blue_question_mark_icon.svg/1200px-Blue_question_mark_icon.svg.png";
16
[487]17 private readonly Dictionary<string, MarkerData> markers = new Dictionary<string, MarkerData> ();
[390]18
[388]19 public Markers () {
[390]20 GameRandom random = GameRandomManager.Instance.CreateGameRandom ();
21
[391]22 for (int i = 0; i < numRandomMarkers; i++) {
[459]23 int x = random.RandomRange (-1000, 1001);
24 int y = random.RandomRange (-1000, 1001);
[390]25
[487]26 string guid = WebUtils.GenerateGuid ();
27 markers.Add (guid, new MarkerData(guid, new Vector2i (x, y), "RandomMarker " + i, null));
[390]28 }
[434]29
30 GameRandomManager.Instance.FreeGameRandom (random);
[388]31 }
32
[402]33 private static readonly byte[] jsonKeyId = JsonWriter.GetEncodedPropertyNameWithBeginObject ("id");
[459]34 private static readonly byte[] jsonKeyX = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("x");
35 private static readonly byte[] jsonKeyY = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("y");
[487]36 private static readonly byte[] jsonKeyName = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("name");
[430]37 private static readonly byte[] jsonKeyIcon = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("icon");
[402]38
[388]39 protected override void HandleRestGet (RequestContext _context) {
40 string id = _context.RequestPath;
41
[402]42 PrepareEnvelopedResult (out JsonWriter writer);
43
[388]44 if (string.IsNullOrEmpty (id)) {
[402]45 writer.WriteBeginArray ();
[388]46
[402]47 bool first = true;
[487]48 foreach ((_, MarkerData iMarker) in markers) {
[402]49 if (!first) {
50 writer.WriteValueSeparator ();
51 }
52
53 first = false;
54
[487]55 writeMarkerJson (ref writer, iMarker);
[388]56 }
[402]57
58 writer.WriteEndArray ();
59 SendEnvelopedResult (_context, ref writer);
[388]60 return;
61 }
62
[487]63 if (!markers.TryGetValue (id, out MarkerData singleMarker)) {
[434]64 writer.WriteRaw (WebUtils.JsonEmptyData);
[402]65 SendEnvelopedResult (_context, ref writer, HttpStatusCode.NotFound);
[388]66 return;
67 }
68
69 {
[402]70 writer.WriteBeginArray ();
71
[487]72 writeMarkerJson (ref writer, singleMarker);
[402]73
74 writer.WriteEndArray ();
75 SendEnvelopedResult (_context, ref writer);
[388]76 }
77 }
78
[487]79 private void writeMarkerJson (ref JsonWriter _writer, MarkerData _marker) {
[402]80 _writer.WriteRaw (jsonKeyId);
[487]81 _writer.WriteString (_marker.Id);
[459]82 _writer.WriteRaw (jsonKeyX);
[487]83 _writer.WriteInt32 (_marker.Position.x);
[459]84 _writer.WriteRaw (jsonKeyY);
[487]85 _writer.WriteInt32 (_marker.Position.y);
86 _writer.WriteRaw (jsonKeyName);
87 _writer.WriteString (_marker.Name);
[430]88 _writer.WriteRaw (jsonKeyIcon);
[487]89 _writer.WriteString (_marker.Icon ?? defaultIcon);
[402]90 _writer.WriteEndObject ();
91 }
[388]92
[402]93 protected override void HandleRestPost (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) {
[459]94 if (!JsonCommons.TryGetJsonField (_jsonInput, "x", out int x)) {
95 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_X");
[390]96 return;
97 }
98
[459]99 if (!JsonCommons.TryGetJsonField (_jsonInput, "y", out int y)) {
100 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_Y");
[390]101 return;
102 }
103
[487]104 JsonCommons.TryGetJsonField (_jsonInput, "name", out string name);
105 if (string.IsNullOrEmpty (name)) {
106 name = null;
107 }
108
[434]109 JsonCommons.TryGetJsonField (_jsonInput, "icon", out string icon);
[430]110 if (string.IsNullOrEmpty (icon)) {
111 icon = null;
112 }
113
[390]114 string newId = WebUtils.GenerateGuid ();
[487]115 markers.Add (newId, new MarkerData(newId, new Vector2i (x, y), name, icon));
[390]116
[402]117 PrepareEnvelopedResult (out JsonWriter writer);
118 writer.WriteString (newId);
119 SendEnvelopedResult (_context, ref writer, HttpStatusCode.Created);
[388]120 }
121
[402]122 protected override void HandleRestPut (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) {
[459]123 if (!JsonCommons.TryGetJsonField (_jsonInput, "x", out int x)) {
124 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_X");
[390]125 return;
126 }
127
[459]128 if (!JsonCommons.TryGetJsonField (_jsonInput, "y", out int y)) {
129 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_Y");
[390]130 return;
131 }
132
[487]133 bool keepName = !JsonCommons.TryGetJsonField (_jsonInput, "name", out string name);
[430]134
[487]135 bool keepIcon = !JsonCommons.TryGetJsonField (_jsonInput, "icon", out string icon);
136
[390]137 string id = _context.RequestPath;
138
[487]139 if (!markers.TryGetValue (id, out MarkerData oldMarker)) {
[434]140 SendEmptyResponse (_context, HttpStatusCode.NotFound, _jsonInputData, "ID_NOT_FOUND");
[390]141 return;
142 }
143
[487]144 if (keepName) {
145 name = oldMarker.Name;
146 }
147
[430]148 if (keepIcon) {
[487]149 icon = oldMarker.Icon;
[430]150 }
151
[487]152 MarkerData updatedMarker = new MarkerData(id, new Vector2i (x, y), name, icon);
153 markers [id] = updatedMarker;
154
[402]155 PrepareEnvelopedResult (out JsonWriter writer);
[487]156 writeMarkerJson (ref writer, updatedMarker);
[402]157 SendEnvelopedResult (_context, ref writer);
[388]158 }
159
160 protected override void HandleRestDelete (RequestContext _context) {
[390]161 string id = _context.RequestPath;
162
[434]163 SendEmptyResponse (_context, markers.Remove (id) ? HttpStatusCode.NoContent : HttpStatusCode.NotFound);
[388]164 }
[426]165
166 public override int[] DefaultMethodPermissionLevels () => new[] {
167 AdminWebModules.MethodLevelNotSupported,
168 AdminWebModules.PermissionLevelGuest,
169 AdminWebModules.PermissionLevelUser,
170 AdminWebModules.MethodLevelInheritGlobal,
171 AdminWebModules.MethodLevelInheritGlobal
172 };
[388]173 }
174}
Note: See TracBrowser for help on using the repository browser.