Changeset 390
- Timestamp:
- Aug 7, 2022, 1:12:28 AM (2 years ago)
- Location:
- binary-improvements2
- Files:
-
- 8 added
- 2 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/MapRendering/WebAndMapRendering.csproj
r387 r390 91 91 <Compile Include="Web\API\GetHostileLocation.cs" /> 92 92 <Compile Include="Web\API\GetWebMods.cs" /> 93 <Compile Include="Web\API\Markers.cs" />94 93 <Compile Include="Web\API\Null.cs" /> 95 94 <Compile Include="Web\Handlers\RewriteHandler.cs" /> -
binary-improvements2/MarkersMod/Markers.cs
r389 r390 1 using System;2 1 using System.Collections.Generic; 3 2 using System.Net; 4 3 using AllocsFixes.JSON; 4 using AllocsFixes.NetConnections.Servers.Web; 5 using AllocsFixes.NetConnections.Servers.Web.API; 5 6 6 namespace AllocsFixes.NetConnections.Servers.Web.API{7 namespace Examples { 7 8 class Markers : AbsRestApi { 8 private readonly Dictionary<string, (string, string)> markers = new Dictionary<string, (string, string)> (); 9 private const int NumRandomMarkers = 5; 10 11 private readonly Dictionary<string, (int, int)> markers = new Dictionary<string, (int, int)> (); 9 12 10 13 private static readonly JSONArray emptyResult = new JSONArray (); 11 14 12 15 public Markers () { 13 markers.Add (WebUtils.GenerateGuid (), ("539", "498")); 14 markers.Add (WebUtils.GenerateGuid (), ("-18", "524")); 15 markers.Add (WebUtils.GenerateGuid (), ("29", "-162")); 16 markers.Add (WebUtils.GenerateGuid (), ("458", "-257")); 16 GameRandom random = GameRandomManager.Instance.CreateGameRandom (); 17 18 for (int i = 0; i < NumRandomMarkers; i++) { 19 int lat = random.RandomRange (-1000, 1001); 20 int lng = random.RandomRange (-1000, 1001); 21 22 markers.Add (WebUtils.GenerateGuid (), (lat, lng)); 23 } 17 24 } 18 25 … … 23 30 JSONArray result = new JSONArray (); 24 31 25 foreach (KeyValuePair<string, ( string, string)> kvp in markers) {32 foreach (KeyValuePair<string, (int, int)> kvp in markers) { 26 33 JSONObject marker = new JSONObject (); 27 34 marker.Add ("id", new JSONString (kvp.Key)); 28 marker.Add ("lat", new JSON String(kvp.Value.Item1));29 marker.Add ("lng", new JSON String(kvp.Value.Item2));35 marker.Add ("lat", new JSONNumber (kvp.Value.Item1)); 36 marker.Add ("lng", new JSONNumber (kvp.Value.Item2)); 30 37 result.Add (marker); 31 38 } … … 35 42 } 36 43 37 if (!markers.TryGetValue (id, out ( string, string) location)) {44 if (!markers.TryGetValue (id, out (int, int) location)) { 38 45 SendEnvelopedResult (_context, emptyResult, HttpStatusCode.NotFound); 39 46 return; … … 44 51 JSONObject marker = new JSONObject (); 45 52 marker.Add ("id", new JSONString (id)); 46 marker.Add ("lat", new JSON String(location.Item1));47 marker.Add ("lng", new JSON String(location.Item2));53 marker.Add ("lat", new JSONNumber (location.Item1)); 54 marker.Add ("lng", new JSONNumber (location.Item2)); 48 55 result.Add (marker); 49 56 SendEnvelopedResult (_context, result); … … 53 60 protected override void HandleRestPost (RequestContext _context, JSONNode _jsonBody) { 54 61 if (!(_jsonBody is JSONObject bodyObject)) { 55 62 SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, _jsonBody, "BODY_NOT_OBJECT"); 63 return; 56 64 } 57 65 58 throw new NotImplementedException (); 66 if (!TryGetJsonField (bodyObject, "lat", out int lat)) { 67 SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, _jsonBody, "NO_OR_INVALID_LAT"); 68 return; 69 } 70 71 if (!TryGetJsonField (bodyObject, "lng", out int lng)) { 72 SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, _jsonBody, "NO_OR_INVALID_LNG"); 73 return; 74 } 75 76 string newId = WebUtils.GenerateGuid (); 77 markers.Add (newId, (lat, lng)); 78 79 JSONString result = new JSONString (newId); 80 SendEnvelopedResult (_context, result); 59 81 } 60 82 61 83 protected override void HandleRestPut (RequestContext _context, JSONNode _jsonBody) { 62 throw new NotImplementedException (); 84 if (!(_jsonBody is JSONObject bodyObject)) { 85 SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, _jsonBody, "BODY_NOT_OBJECT"); 86 return; 87 } 88 89 if (!TryGetJsonField (bodyObject, "lat", out int lat)) { 90 SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, _jsonBody, "NO_OR_INVALID_LAT"); 91 return; 92 } 93 94 if (!TryGetJsonField (bodyObject, "lng", out int lng)) { 95 SendEnvelopedResult (_context, null, HttpStatusCode.BadRequest, _jsonBody, "NO_OR_INVALID_LNG"); 96 return; 97 } 98 99 string id = _context.RequestPath; 100 101 if (!markers.TryGetValue (id, out (int, int) location)) { 102 SendEnvelopedResult (_context, null, HttpStatusCode.NotFound, _jsonBody, "ID_NOT_FOUND"); 103 return; 104 } 105 106 markers [id] = (lat, lng); 107 108 JSONObject result = new JSONObject (); 109 result.Add ("id", new JSONString (id)); 110 result.Add ("lat", new JSONNumber (lat)); 111 result.Add ("lng", new JSONNumber (lng)); 112 SendEnvelopedResult (_context, result); 63 113 } 64 114 65 115 protected override void HandleRestDelete (RequestContext _context) { 66 throw new NotImplementedException (); 116 string id = _context.RequestPath; 117 118 SendEnvelopedResult (_context, null, markers.Remove (id) ? HttpStatusCode.NoContent : HttpStatusCode.NotFound); 67 119 } 68 120 } -
binary-improvements2/server-fixes.sln
r382 r390 9 9 EndProject 10 10 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpaceWizards.HttpListener", "SpaceWizards.HttpListener\SpaceWizards.HttpListener.csproj", "{E273D042-57F9-4E2E-8268-5053527E5287}" 11 EndProject 12 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MarkersMod", "MarkersMod\MarkersMod.csproj", "{2A008E16-6EB8-4B85-A175-3CB89D9FF4AE}" 11 13 EndProject 12 14 Global … … 42 44 {E273D042-57F9-4E2E-8268-5053527E5287}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 43 45 {E273D042-57F9-4E2E-8268-5053527E5287}.Debug|Any CPU.Build.0 = Debug|Any CPU 46 {2A008E16-6EB8-4B85-A175-3CB89D9FF4AE}.Release|Any CPU.ActiveCfg = Release|Any CPU 47 {2A008E16-6EB8-4B85-A175-3CB89D9FF4AE}.Release|Any CPU.Build.0 = Release|Any CPU 48 {2A008E16-6EB8-4B85-A175-3CB89D9FF4AE}.Release_Version|Any CPU.ActiveCfg = Release|Any CPU 49 {2A008E16-6EB8-4B85-A175-3CB89D9FF4AE}.Release_Version|Any CPU.Build.0 = Release|Any CPU 50 {2A008E16-6EB8-4B85-A175-3CB89D9FF4AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 51 {2A008E16-6EB8-4B85-A175-3CB89D9FF4AE}.Debug|Any CPU.Build.0 = Debug|Any CPU 52 {2A008E16-6EB8-4B85-A175-3CB89D9FF4AE}.Release_Profiler|Any CPU.ActiveCfg = Release_Profiler|Any CPU 53 {2A008E16-6EB8-4B85-A175-3CB89D9FF4AE}.Release_Profiler|Any CPU.Build.0 = Release_Profiler|Any CPU 44 54 EndGlobalSection 45 55 GlobalSection(MonoDevelopProperties) = preSolution
Note:
See TracChangeset
for help on using the changeset viewer.