mirror of
https://github.com/JogmansonReal/Radicade.git
synced 2026-10-25 23:18:56 -07:00
84 lines
2.2 KiB
C#
84 lines
2.2 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using RadicadeServer.DB;
|
|
using RadicadeServer.Extensions;
|
|
using RadicadeServer.RecNet.Accounts;
|
|
using RadicadeServer.RecNet.Avatar;
|
|
|
|
namespace RadicadeServer.API.Avatars.V2
|
|
{
|
|
[Route("api/equipment/v2")]
|
|
[Route("econ/api/equipment/v2")]
|
|
public class EquipmentV2 : Controller
|
|
{
|
|
RecDBContext db;
|
|
public EquipmentV2(RecDBContext context)
|
|
{
|
|
this.db = context;
|
|
}
|
|
|
|
[HttpGet("getUnlocked"), Authorize]
|
|
public List<Equipment> GetUnlockedEquipment()
|
|
{
|
|
var userId = User.GetUserId();
|
|
|
|
var items = db.Unlocks.FirstOrDefault(x => x.AccountId == userId);
|
|
var defaultEquipment = TryReadEquipment(LockerAccess.HasFullInventory(db, userId)
|
|
? EquipmentSkinCatalog.MergeInto(DBConfig.DefaultEquipmentItems, DBConfig.DefaultEquipmentSkinItems)
|
|
: DBConfig.DefaultEquipmentUnlocks);
|
|
var playerEquipment = GetPlayerEquipment(items);
|
|
|
|
return MergeEquipment(defaultEquipment, playerEquipment);
|
|
}
|
|
|
|
private static List<Equipment> TryReadEquipment(string json)
|
|
{
|
|
try
|
|
{
|
|
return (json.FromJson<List<Equipment>>() ?? new List<Equipment>())
|
|
.Where(x => !string.IsNullOrWhiteSpace(x.PrefabName))
|
|
.Select(NormalizeEquipment)
|
|
.ToList();
|
|
}
|
|
catch
|
|
{
|
|
return new List<Equipment>();
|
|
}
|
|
}
|
|
|
|
private static List<Equipment> GetPlayerEquipment(PlayerUnlocks? items)
|
|
{
|
|
try
|
|
{
|
|
return items?.GetEquipment() ?? new List<Equipment>();
|
|
}
|
|
catch
|
|
{
|
|
return new List<Equipment>();
|
|
}
|
|
}
|
|
|
|
private static List<Equipment> MergeEquipment(IEnumerable<Equipment> defaults, IEnumerable<Equipment> playerEquipment)
|
|
{
|
|
var merged = new Dictionary<string, Equipment>();
|
|
|
|
foreach (var item in defaults.Concat(playerEquipment).Select(NormalizeEquipment))
|
|
{
|
|
if (string.IsNullOrWhiteSpace(item.PrefabName)) continue;
|
|
merged[$"{item.PrefabName}\u001f{item.ModificationGuid}"] = item;
|
|
}
|
|
|
|
return merged.Values.ToList();
|
|
}
|
|
|
|
private static Equipment NormalizeEquipment(Equipment item)
|
|
{
|
|
item.PrefabName ??= "";
|
|
item.ModificationGuid ??= "";
|
|
item.FriendlyName = string.IsNullOrWhiteSpace(item.FriendlyName) ? item.PrefabName : item.FriendlyName;
|
|
item.Tooltip ??= "";
|
|
return item;
|
|
}
|
|
}
|
|
}
|