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

Last change on this file since 466 was 459, checked in by alloc, 16 months ago

Updated to dashboard/marker files 0.8.0
Added initial OpenAPI support

File size: 5.6 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
17 private readonly Dictionary<string, (int, int, string)> markers = new Dictionary<string, (int, int, string)> ();
[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
[459]26 markers.Add (WebUtils.GenerateGuid (), (x, y, null));
[390]27 }
[434]28
29 GameRandomManager.Instance.FreeGameRandom (random);
[388]30 }
31
[402]32 private static readonly byte[] jsonKeyId = JsonWriter.GetEncodedPropertyNameWithBeginObject ("id");
[459]33 private static readonly byte[] jsonKeyX = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("x");
34 private static readonly byte[] jsonKeyY = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("y");
[430]35 private static readonly byte[] jsonKeyIcon = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("icon");
[402]36
[388]37 protected override void HandleRestGet (RequestContext _context) {
38 string id = _context.RequestPath;
39
[402]40 PrepareEnvelopedResult (out JsonWriter writer);
41
[388]42 if (string.IsNullOrEmpty (id)) {
[402]43 writer.WriteBeginArray ();
[388]44
[402]45 bool first = true;
[430]46 foreach ((string markerId, (int, int, string) properties) in markers) {
[402]47 if (!first) {
48 writer.WriteValueSeparator ();
49 }
50
51 first = false;
52
[430]53 writeMarkerJson (ref writer, markerId, properties);
[388]54 }
[402]55
56 writer.WriteEndArray ();
57 SendEnvelopedResult (_context, ref writer);
[388]58 return;
59 }
60
[430]61 if (!markers.TryGetValue (id, out (int, int, string) properties2)) {
[434]62 writer.WriteRaw (WebUtils.JsonEmptyData);
[402]63 SendEnvelopedResult (_context, ref writer, HttpStatusCode.NotFound);
[388]64 return;
65 }
66
67 {
[402]68 writer.WriteBeginArray ();
69
[430]70 writeMarkerJson (ref writer, id, properties2);
[402]71
72 writer.WriteEndArray ();
73 SendEnvelopedResult (_context, ref writer);
[388]74 }
75 }
76
[430]77 private void writeMarkerJson (ref JsonWriter _writer, string _markerId, (int, int, string) _properties) {
[402]78 _writer.WriteRaw (jsonKeyId);
79 _writer.WriteString (_markerId);
[459]80 _writer.WriteRaw (jsonKeyX);
81 (int x, int y, string icon) = _properties;
82 _writer.WriteInt32 (x);
83 _writer.WriteRaw (jsonKeyY);
84 _writer.WriteInt32 (y);
[430]85 _writer.WriteRaw (jsonKeyIcon);
[434]86 _writer.WriteString (icon ?? defaultIcon);
[402]87 _writer.WriteEndObject ();
88 }
[388]89
[402]90 protected override void HandleRestPost (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) {
[459]91 if (!JsonCommons.TryGetJsonField (_jsonInput, "x", out int x)) {
92 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_X");
[390]93 return;
94 }
95
[459]96 if (!JsonCommons.TryGetJsonField (_jsonInput, "y", out int y)) {
97 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_Y");
[390]98 return;
99 }
100
[434]101 JsonCommons.TryGetJsonField (_jsonInput, "icon", out string icon);
[430]102 if (string.IsNullOrEmpty (icon)) {
103 icon = null;
104 }
105
[390]106 string newId = WebUtils.GenerateGuid ();
[459]107 markers.Add (newId, (x, y, icon));
[390]108
[402]109 PrepareEnvelopedResult (out JsonWriter writer);
110 writer.WriteString (newId);
111 SendEnvelopedResult (_context, ref writer, HttpStatusCode.Created);
[388]112 }
113
[402]114 protected override void HandleRestPut (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) {
[459]115 if (!JsonCommons.TryGetJsonField (_jsonInput, "x", out int x)) {
116 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_X");
[390]117 return;
118 }
119
[459]120 if (!JsonCommons.TryGetJsonField (_jsonInput, "y", out int y)) {
121 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_Y");
[390]122 return;
123 }
124
[430]125 bool keepIcon = !_jsonInput.TryGetValue ("icon", out _);
[434]126 JsonCommons.TryGetJsonField (_jsonInput, "icon", out string icon);
[430]127 if (string.IsNullOrEmpty (icon)) {
128 icon = null;
129 }
130
[390]131 string id = _context.RequestPath;
132
[430]133 if (!markers.TryGetValue (id, out (int, int, string) properties)) {
[434]134 SendEmptyResponse (_context, HttpStatusCode.NotFound, _jsonInputData, "ID_NOT_FOUND");
[390]135 return;
136 }
137
[430]138 if (keepIcon) {
139 icon = properties.Item3;
140 }
[459]141 markers [id] = (x, y, icon);
[430]142
[402]143 PrepareEnvelopedResult (out JsonWriter writer);
144 writer.WriteRaw (jsonKeyId);
145 writer.WriteString (id);
[459]146 writer.WriteRaw (jsonKeyX);
147 writer.WriteInt32 (x);
148 writer.WriteRaw (jsonKeyY);
149 writer.WriteInt32 (y);
[430]150 writer.WriteRaw (jsonKeyIcon);
151 writer.WriteString (icon);
[402]152 writer.WriteEndObject ();
153 SendEnvelopedResult (_context, ref writer);
[388]154 }
155
156 protected override void HandleRestDelete (RequestContext _context) {
[390]157 string id = _context.RequestPath;
158
[434]159 SendEmptyResponse (_context, markers.Remove (id) ? HttpStatusCode.NoContent : HttpStatusCode.NotFound);
[388]160 }
[426]161
162 public override int[] DefaultMethodPermissionLevels () => new[] {
163 AdminWebModules.MethodLevelNotSupported,
164 AdminWebModules.PermissionLevelGuest,
165 AdminWebModules.PermissionLevelUser,
166 AdminWebModules.MethodLevelInheritGlobal,
167 AdminWebModules.MethodLevelInheritGlobal
168 };
[388]169 }
170}
Note: See TracBrowser for help on using the repository browser.