Skip to content

Commit

Permalink
Small bug fixes and new logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcaver committed Mar 7, 2024
1 parent 7d446fe commit 55d676d
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 9 deletions.
26 changes: 26 additions & 0 deletions ProjectEarthServerAPI/Controllers/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,32 @@ public async Task<IActionResult> PutItemInHotbar()

return Content(JsonConvert.SerializeObject(returnHotbar.Item2));
}

[ApiVersion("1.1")]
[HttpPost]
[Route("1/api/v{version:apiVersion}/inventory/survival/{ItemID}/consume")]
public async Task<IActionResult> ConsumeItem(Guid ItemID) //TODO: actual healing logic ._.
{
var stream = new StreamReader(Request.Body);
var body = await stream.ReadToEndAsync();

string authtoken = User.FindFirstValue(ClaimTypes.NameIdentifier);
Log.Information($"{authtoken} is trying to consume item {ItemID}");
InventoryUtils.RemoveItemFromInv(authtoken, ItemID);
InventoryUtils.AddItemToInv(authtoken, ItemID, 2);
ProfileUtils.AddHealthToPlayer(authtoken, 20);
var streamVersion = GenericUtils.GetNextStreamVersion();
var resp = new UpdateResponse
{
updates = new Updates
{
characterProfile = streamVersion,
inventory = streamVersion
}
};

return Content(JsonConvert.SerializeObject(resp), "application/json");
}
}

[Authorize]
Expand Down
2 changes: 1 addition & 1 deletion ProjectEarthServerAPI/Models/Features/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class ItemInfo
public float? blockDamage { get; set; }
public float? health { get; set; }
public BlockMetadata blockMetadata { get; set; }
public ItemMetadata ItemMetadata { get; set; }
public ItemMetadata itemMetadata { get; set; }
public BoostMetadata? boostMetadata { get; set; }
public JournalMetadata journalMetadata { get; set; }
public AudioMetadata? audioMetadata { get; set; }
Expand Down
7 changes: 5 additions & 2 deletions ProjectEarthServerAPI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using ProjectEarthServerAPI.Models.Player;
using Serilog;
using Uma.Uuid;
using Serilog.Events;

namespace ProjectEarthServerAPI
{
Expand All @@ -23,7 +24,9 @@ public static void Main(string[] args)
.WriteTo.Console()
.WriteTo.File("logs/debug.txt", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true, fileSizeLimitBytes: 8338607, outputTemplate: "{Timestamp:HH:mm:ss.fff} [{Level:u3}] {Message:lj}{NewLine}{Exception}")
.MinimumLevel.Debug()
.CreateLogger();
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
.CreateLogger();

Log.Logger = log;

Expand All @@ -46,7 +49,7 @@ public static void Main(string[] args)

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
//.UseSerilog()
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
Expand Down
19 changes: 18 additions & 1 deletion ProjectEarthServerAPI/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Microsoft.AspNetCore.Authentication;
using ProjectEarthServerAPI.Authentication;
using Serilog;
using Serilog.Events;

namespace ProjectEarthServerAPI
{
Expand Down Expand Up @@ -62,7 +63,23 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
//app.UseDeveloperExceptionPage();
}

//app.UseSerilogRequestLogging();
app.UseSerilogRequestLogging(options =>
{
// Customize the message template
options.MessageTemplate = "{RemoteIpAddress} {RequestMethod} {RequestScheme}://{RequestHost}{RequestPath}{RequestQuery} responded {StatusCode} in {Elapsed:0.0000} ms";

// Emit debug-level events instead of the defaults
options.GetLevel = (httpContext, elapsed, ex) => LogEventLevel.Debug;

// Attach additional properties to the request completion event
options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
{
diagnosticContext.Set("RequestHost", httpContext.Request.Host.Value);
diagnosticContext.Set("RequestScheme", httpContext.Request.Scheme);
diagnosticContext.Set("RemoteIpAddress", httpContext.Connection.RemoteIpAddress);
diagnosticContext.Set("RequestQuery", httpContext.Request.QueryString);
};
});

app.UseETagger();
//app.UseHttpsRedirection();
Expand Down
2 changes: 1 addition & 1 deletion ProjectEarthServerAPI/Util/BuildplateUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static BuildplateShareResponse GetBuildplateById(BuildplateRequest buildp
{
BuildplateData buildplate = ReadBuildplate(buildplateReq.buildplateId);

return new BuildplateShareResponse {result = new BuildplateShareResponse.BuildplateShareInfo {buildplateData = buildplate, playerId = null}};
return new BuildplateShareResponse {result = new BuildplateShareResponse.BuildplateShareInfo {buildplateData = buildplate, playerId = buildplateReq.playerId}};
}

public static void UpdateBuildplateAndList(BuildplateShareResponse data, string playerId)
Expand Down
22 changes: 19 additions & 3 deletions ProjectEarthServerAPI/Util/MultiplayerUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ public static async Task AuthenticateServer(WebSocket webSocketRequest)
ServerInformation info = null;
string challenge = null;

while (!result.CloseStatus.HasValue)
while (!result.CloseStatus.HasValue && webSocketRequest.State == WebSocketState.Open)
{
info ??= JsonConvert.DeserializeObject<ServerInformation>(Encoding.UTF8.GetString(messageBuffer));

Expand Down Expand Up @@ -728,11 +728,27 @@ await webSocketRequest.SendAsync(
break;

case ServerAuthInformation.Authed:
while (true) { }

try
{
result = await webSocketRequest.ReceiveAsync(new ArraySegment<byte>(messageBuffer), CancellationToken.None);
}
catch
{
continue;
}
break;
}
}

if (info != null)
Log.Information($"Server {info.serverId} has disconnected from the api");

if (info != null && ApiKeyList.ContainsKey(info.serverId))
ApiKeyList.Remove(info.serverId);
if (info != null && ServerInfoList.ContainsKey(info.serverId))
ServerInfoList.Remove(info.serverId);
if (info != null && ServerSocketList.ContainsKey(info.serverId))
ServerSocketList.Remove(info.serverId);
}

private static bool VerifyChallenge(string challenge, string challengeResponse, ServerInformation info)
Expand Down
8 changes: 8 additions & 0 deletions ProjectEarthServerAPI/Util/ProfileUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public static void AddExperienceToPlayer(string playerId, int experiencePoints)
WriteProfile(playerId, playerProfile);
}

public static void AddHealthToPlayer(string playerId, int healthPoints)
{
var playerProfile = ReadProfile(playerId);
playerProfile.healthPercentage += healthPoints;

WriteProfile(playerId, playerProfile);
}

private static void RewardLevelupRewards(string playerId, int level)
{
var rewards = ProfileResponse.levels[level.ToString()].rewards;
Expand Down
1 change: 1 addition & 0 deletions ProjectEarthServerAPI/Util/RewardUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class RewardUtils
public static Updates RedeemRewards(string playerId, Rewards rewards, EventLocation location)
{
Updates updates = new Updates();
if (rewards == null) return updates;
uint nextStreamId = GenericUtils.GetNextStreamVersion();
foreach (var buildplate in rewards.Buildplates)
{
Expand Down
2 changes: 1 addition & 1 deletion ProjectEarthServerAPI/data
Submodule data updated 30 files
+1 −1 challenges/0b346237-79a4-4f24-a45a-e2e6284e3e56.json
+1 −1 challenges/0d4c99ce-b485-4890-be69-caab88400df3.json
+1 −1 challenges/152a875e-2389-4245-8e1f-08f1915d6c7a.json
+1 −1 challenges/1ccbe15e-3a72-4d8b-ad0b-00a23ac72eb1.json
+1 −1 challenges/242b6706-8112-4302-819b-bde98fd574f6.json
+1 −1 challenges/26f62499-5996-477a-8530-22f852b1ccad.json
+1 −1 challenges/29ebe650-072f-4f70-996f-4ffdda93ed1f.json
+1 −1 challenges/2bb26b41-e841-4815-ade5-a0d93ac51258.json
+1 −1 challenges/2ebbf878-5c69-4f55-8858-83bd53c57381.json
+1 −1 challenges/3183cf3d-19e5-4bf5-b8a8-3085f4e650d3.json
+1 −1 challenges/3d82b9c1-f4e0-4a20-b87e-9a11734bcb6a.json
+1 −1 challenges/63e5d65e-1152-4adc-97e7-2818854a867a.json
+1 −1 challenges/68337eb8-c1c8-4f57-876a-d262ca7a22f4.json
+1 −1 challenges/703c24f0-aedb-4fc1-bce8-6b06229b2006.json
+1 −1 challenges/70961d0f-a762-4846-a79c-576d685263f4.json
+1 −1 challenges/761c6754-ad1f-4cbf-a116-8a1e1dd611b9.json
+1 −1 challenges/8e052786-0495-4eb9-80bf-e1b36b9d89ef.json
+1 −1 challenges/8ff3a6c3-0a1a-4343-8672-7f559cc8918d.json
+1 −1 challenges/96593763-a8a6-41f8-986a-8d6df0470c57.json
+1 −1 challenges/a1394f77-20ba-44c2-b46c-e8d7933f2e51.json
+1 −1 challenges/ab2e0734-62b1-4383-a239-d1b4d4a93dc4.json
+1 −1 challenges/abd271f8-22cd-42ff-b985-cd5e642ea25a.json
+1 −1 challenges/cc456b52-1586-4e75-b7e9-aa811f609567.json
+1 −1 challenges/d434c853-7fab-4ac7-b64b-80fb6dd9ddd9.json
+1 −1 challenges/d8d2abd5-f318-4c34-a257-57b9691a2774.json
+1 −1 challenges/dbdb4592-4211-4ffe-a76c-8a3a2213c93c.json
+1 −1 challenges/e81d6ce8-622c-461a-a2d5-bb1f8ac36c62.json
+1 −1 challenges/edf9f1dc-f73f-4275-a3e5-e1d9d861d158.json
+1 −1 challenges/f0532069-a70a-4a01-8611-f770bb46d9cd.json
+1 −1 challenges/fb13127e-d19d-48bd-a977-f830eecff180.json

0 comments on commit 55d676d

Please sign in to comment.