From 43e2b69d666cee86733c32161c376754b8954a58 Mon Sep 17 00:00:00 2001 From: Yury Date: Tue, 25 Mar 2025 18:52:55 +0200 Subject: [PATCH] Set objective counter before deployment --- .../Functions/EnterMatchmakingMatch.cs | 64 ++++++++++++++++++- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/src/Prospect.Server.Api/Services/CloudScript/Functions/EnterMatchmakingMatch.cs b/src/Prospect.Server.Api/Services/CloudScript/Functions/EnterMatchmakingMatch.cs index a0a2908..427736a 100644 --- a/src/Prospect.Server.Api/Services/CloudScript/Functions/EnterMatchmakingMatch.cs +++ b/src/Prospect.Server.Api/Services/CloudScript/Functions/EnterMatchmakingMatch.cs @@ -1,7 +1,10 @@ -using System.Text.Json.Serialization; +using System.Text.Json; +using System.Text.Json.Serialization; using Microsoft.AspNetCore.SignalR; using Prospect.Server.Api.Hubs; +using Prospect.Server.Api.Services.Auth.Extensions; using Prospect.Server.Api.Services.CloudScript.Models; +using Prospect.Server.Api.Services.UserData; namespace Prospect.Server.Api.Services.CloudScript.Functions; @@ -17,15 +20,72 @@ public class OnSquadMatchmakingSuccessMessage { [CloudScriptFunction("EnterMatchmakingMatch")] public class EnterMatchmakingMatchFunction : ICloudScriptFunction { + private readonly IHttpContextAccessor _httpContextAccessor; private readonly IHubContext _hubContext; + private readonly UserDataService _userDataService; + private readonly TitleDataService _titleDataService; - public EnterMatchmakingMatchFunction(IHubContext hubContext) + public EnterMatchmakingMatchFunction(IHubContext hubContext, IHttpContextAccessor httpContextAccessor, UserDataService userDataService, TitleDataService titleDataService) { + _httpContextAccessor = httpContextAccessor; _hubContext = hubContext; + _userDataService = userDataService; + _titleDataService = titleDataService; } public async Task ExecuteAsync(FYEnterMatchAzureFunction request) { + var context = _httpContextAccessor.HttpContext; + if (context == null) + { + throw new CloudScriptException("CloudScript was not called within a http request"); + } + + var userId = context.User.FindAuthUserId(); + var titleData = _titleDataService.Find(new List{"Contracts"}); + var contracts = JsonSerializer.Deserialize>(titleData["Contracts"]); + + var userData = await _userDataService.FindAsync( + userId, userId, + new List{"ContractsActive", "Inventory"} + ); + var inventory = JsonSerializer.Deserialize>(userData["Inventory"].Value); + var contractsActive = JsonSerializer.Deserialize(userData["ContractsActive"].Value); + // Compute delivery quest progress before deploy. + // This ensures that the quest progress will be shown on the planet. + // The station doesn't seem to care about delivery quests progress and calculates progress + // based on actual items in stash. And so does the "ClaimActiveContract" function. + foreach (var contractActive in contractsActive.Contracts) { + if (!contracts.TryGetValue(contractActive.ContractID, out var contract)) { + continue; + } + for (var i = 0; i < contract.Objectives.Length; i++) { + var objective = contract.Objectives[i]; + if (objective.Type != EYContractObjectiveType.OwnNumOfItem) { + continue; + } + int remaining = objective.MaxProgress; + foreach (var item in inventory) { + if (item.BaseItemId != objective.ItemToOwn) { + continue; + } + remaining -= item.Amount; + if (remaining <= 0) { + remaining = 0; + break; + } + } + contractActive.Progress[i] = objective.MaxProgress - remaining; + } + } + + await _userDataService.UpdateAsync( + userId, userId, + new Dictionary{ + ["ContractsActive"] = JsonSerializer.Serialize(contractsActive), + } + ); + await _hubContext.Clients.All.SendAsync("OnSquadMatchmakingSuccess", new OnSquadMatchmakingSuccessMessage { Success = true, SessionID = request.MapName, // TODO: Need to implement TryGetCompleteSquadInfo and pass squad info