Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "staticdata"]
path = staticdata
url = https://github.com/Earth-Restored/Solace.StaticData
url = https://github.com/LNLenost/ViennaDotNet.StaticData.git
351 changes: 334 additions & 17 deletions src/Solace.ApiServer/Controllers/EarthApi/BoostsController.cs

Large diffs are not rendered by default.

360 changes: 322 additions & 38 deletions src/Solace.ApiServer/Controllers/EarthApi/BuildplatesController.cs

Large diffs are not rendered by default.

381 changes: 367 additions & 14 deletions src/Solace.ApiServer/Controllers/EarthApi/CatalogController.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Solace.ApiServer.Controllers.EarthApi;

[ApiVersion("1.1")]
[Route("cdn/tile/16/{_}/{tilePos1}_{tilePos2}_16.png")]
[ResponseCache(Duration = 11200)]
[ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)]
internal sealed class CdnTileController : SolaceControllerBase
{
private readonly EarthDbContext _earthDb;
Expand All @@ -27,14 +27,14 @@ public CdnTileController(EarthDbContext earthDb, EventBusClient eventBus, Object
[HttpGet]
public async Task<Results<EmptyHttpResult, NotFound>> GetTile(int _, int tilePos1, int tilePos2, CancellationToken cancellationToken) // _ used because we dont care :|
{
if (!await TileUtils.TryWriteTile(tilePos1, tilePos2, Response.Body, _earthDb, _eventBus, _objectStore, cancellationToken))
Response.Headers.ContentType = "image/png";
if (!await TileUtils.TryWriteTile(tilePos1, tilePos2, Response.Body, cancellationToken))
{
return TypedResults.NotFound();
}

var cd = new System.Net.Mime.ContentDisposition { FileName = $"{tilePos1}_{tilePos2}_16.png", Inline = true };
Response.Headers.Append("Content-Disposition", cd.ToString());
Response.Headers.ContentType = "application/octet-stream";

return TypedResults.Empty;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Solace.DB;
using Solace.ApiServer.Utils;
using Solace.Common.Utils;
using System.Security.Claims;
using ApiRewards = Solace.ApiServer.Types.Common.Rewards;
using RedeemRewards = Solace.ApiServer.Utils.Rewards;

namespace Solace.ApiServer.Controllers.EarthApi;
Expand All @@ -15,17 +18,51 @@ internal sealed class ChallengeActionsController : SolaceControllerBase
{
[HttpPost("{challengeId}/modifyState")]
[HttpPut("{challengeId}/modifyState")]
public ContentHttpResult ModifyState(string challengeId)
public async Task<Results<ContentHttpResult, BadRequest>> ModifyState(string challengeId, CancellationToken cancellationToken)
{
string? playerId = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (string.IsNullOrWhiteSpace(playerId))
{
return TypedResults.BadRequest();
}

long now = HttpContext.GetTimestamp();
var updates = new EarthApiResponse.UpdatesResponse();
ApiRewards? apiRewards = ChallengesController.GetSeasonChallengeRewards(challengeId);
RedeemRewards rewards = ToRedeemRewards(apiRewards);

EarthDB.Results results = await new EarthDB.Query(true)
.Get("challenges", playerId, typeof(ChallengeProgressVersion))
.Then(queryResults =>
{
ChallengeProgressVersion progress = queryResults.Get<ChallengeProgressVersion>("challenges");
progress.EnsureDate(now);
progress.ClaimedChallengeIds ??= [];

bool firstClaim = progress.ClaimedChallengeIds.Add(challengeId);
progress.ActiveSeasonId = ChallengesController.ActiveSeasonId;
progress.ActiveSeasonChallengeId = ChallengesController.SelectActiveSeasonChallengeId(progress, progress.ActiveSeasonChallengeId);
progress.UpdatedAt = now;

var query = new EarthDB.Query(true)
.Update("challenges", playerId, progress);

if (firstClaim && apiRewards is not null)
{
query.Then(rewards.ToRedeemQuery(playerId, now, Program.staticData), false);
}

return query;
})
.ExecuteAsync(Program.DB, cancellationToken);

var updates = new EarthApiResponse.UpdatesResponse(results);
updates.Map["challenges"] = (int)(now / 1000);

return EarthJson(new Dictionary<string, object?>
{
["challengeId"] = challengeId,
["state"] = "Claimed",
["rewards"] = new RedeemRewards().ToApiResponse(),
["rewards"] = apiRewards ?? rewards.ToApiResponse(),
["updates"] = new Dictionary<string, object>()
}, updates);
}
Expand All @@ -48,10 +85,31 @@ public ContentHttpResult ResetChallenges()

[HttpPost("continuous/{id}/remove")]
[HttpDelete("continuous/{id}/remove")]
public ContentHttpResult RemoveContinuousChallenge(string id)
public async Task<Results<ContentHttpResult, BadRequest>> RemoveContinuousChallenge(string id, CancellationToken cancellationToken)
{
string? playerId = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (string.IsNullOrWhiteSpace(playerId))
{
return TypedResults.BadRequest();
}

long now = HttpContext.GetTimestamp();
var updates = new EarthApiResponse.UpdatesResponse();
EarthDB.Results results = await new EarthDB.Query(true)
.Get("challenges", playerId, typeof(ChallengeProgressVersion))
.Then(queryResults =>
{
ChallengeProgressVersion progress = queryResults.Get<ChallengeProgressVersion>("challenges");
progress.EnsureDate(now);
progress.RemovedContinuousChallengeIds ??= [];
progress.RemovedContinuousChallengeIds.Add(id);
progress.UpdatedAt = now;

return new EarthDB.Query(true)
.Update("challenges", playerId, progress);
})
.ExecuteAsync(Program.DB, cancellationToken);

var updates = new EarthApiResponse.UpdatesResponse(results);
updates.Map["challenges"] = (int)(now / 1000);

return EarthJson(new Dictionary<string, object?>
Expand All @@ -60,4 +118,40 @@ public ContentHttpResult RemoveContinuousChallenge(string id)
["updates"] = new Dictionary<string, object>()
}, updates);
}

private static RedeemRewards ToRedeemRewards(ApiRewards? rewards)
{
var result = new RedeemRewards();
if (rewards is null)
{
return result;
}

if (rewards.Rubies is > 0)
{
result.AddRubies(rewards.Rubies.Value);
}

if (rewards.ExperiencePoints is > 0)
{
result.AddExperiencePoints(rewards.ExperiencePoints.Value);
}

foreach (var item in rewards.Inventory)
{
result.AddItem(item.Id, item.Amount);
}

foreach (string buildplateId in rewards.Buildplates)
{
result.AddBuildplate(buildplateId);
}

foreach (var challenge in rewards.Challenges)
{
result.AddChallenge(challenge.Id);
}

return result;
}
}
Loading