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