218 lines
7.3 KiB
C#
218 lines
7.3 KiB
C#
using DeluxeBackend.Models;
|
|
using DeluxeBackend.Services;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Newtonsoft.Json.Linq;
|
|
using System.Text.Json;
|
|
using static DeluxeBackend.Enums;
|
|
|
|
namespace DeluxeBackend.Controllers.Api
|
|
{
|
|
[Route("api/avatar")]
|
|
[ApiController]
|
|
public class AvatarController : ControllerBase
|
|
{
|
|
private readonly IJwtService jwt;
|
|
private readonly ILiteDbService db;
|
|
|
|
public AvatarController(IJwtService _jwt, ILiteDbService _db)
|
|
{
|
|
jwt = _jwt;
|
|
db = _db;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("v1/defaultunlocked")]
|
|
public async Task<IActionResult> Defaultunlocked()
|
|
{
|
|
return Ok(ServerConfig.avatarItems);
|
|
}
|
|
[HttpGet("v1/defaultbaseavataritems")]
|
|
public async Task<IActionResult> Defaultbaseavataritems()
|
|
{
|
|
return Ok(new List<object>());
|
|
}
|
|
|
|
[HttpGet("v4/items")]
|
|
[Authorize(Roles = "gameClient")]
|
|
public async Task<IActionResult> Items()
|
|
{
|
|
return Ok(ServerConfig.avatarItems);
|
|
}
|
|
|
|
[HttpGet("v2")]
|
|
[Authorize(Roles = "gameClient")]
|
|
public async Task<IActionResult> MyAv()
|
|
{
|
|
Account? account = await jwt.GetLogin(User);
|
|
|
|
if (account == null)
|
|
{
|
|
return NotFound("User not found in database");
|
|
}
|
|
|
|
AccountAvatar? accountAvatar = db.Avatar.FindOne(x => x.Account!.Id == account.Id);
|
|
|
|
|
|
return Ok(new
|
|
{
|
|
OutfitSelections = accountAvatar?.OutfitSelections ?? "",
|
|
OutfitSelectionsV2 = accountAvatar?.OutfitSelectionsV2 ?? "",
|
|
FaceFeatures = accountAvatar?.FaceFeatures ?? "",
|
|
SkinColor = accountAvatar?.SkinColor ?? "",
|
|
HairColor = accountAvatar?.HairColor ?? "",
|
|
CustomAvatarItems = new List<object>()
|
|
});
|
|
}
|
|
[HttpGet("v2/{accId}")]
|
|
[Authorize(Roles = "gameClient")]
|
|
public async Task<IActionResult> Av(long accId)
|
|
{
|
|
Account? account = db.Accounts.FindById(accId);
|
|
|
|
if (account == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
AccountAvatar? accountAvatar = db.Avatar.FindOne(x => x.Account!.Id == account.Id);
|
|
|
|
|
|
return Ok(new
|
|
{
|
|
OutfitSelections = accountAvatar?.OutfitSelections ?? "",
|
|
OutfitSelectionsV2 = accountAvatar?.OutfitSelectionsV2 ?? "",
|
|
FaceFeatures = accountAvatar?.FaceFeatures ?? "",
|
|
SkinColor = accountAvatar?.SkinColor ?? "",
|
|
HairColor = accountAvatar?.HairColor ?? "",
|
|
CustomAvatarItems = new List<object>()
|
|
});
|
|
}
|
|
|
|
public class AvatarSetRequest
|
|
{
|
|
public required string OutfitSelections { get; set; }
|
|
public required string OutfitSelectionsV2 { get; set; }
|
|
public required string FaceFeatures { get; set; }
|
|
public required string SkinColor { get; set; }
|
|
public required string HairColor { get; set; }
|
|
}
|
|
[HttpPost("v2/set")]
|
|
[Authorize(Roles = "gameClient")]
|
|
public async Task<IActionResult> Set([FromBody] AvatarSetRequest request)
|
|
{
|
|
if (request == null) return BadRequest();
|
|
|
|
Account? account = await jwt.GetLogin(User);
|
|
|
|
if (account == null)
|
|
{
|
|
return NotFound("User not found in database");
|
|
}
|
|
|
|
AccountAvatar? accountAvatar = db.Avatar.FindOne(x => x.Account!.Id == account.Id);
|
|
if (accountAvatar == null)
|
|
{
|
|
accountAvatar = new AccountAvatar() { Account = account };
|
|
}
|
|
|
|
accountAvatar.OutfitSelections = request.OutfitSelections;
|
|
accountAvatar.OutfitSelectionsV2 = request.OutfitSelectionsV2;
|
|
accountAvatar.FaceFeatures = request.FaceFeatures;
|
|
accountAvatar.HairColor = request.HairColor;
|
|
accountAvatar.SkinColor = request.SkinColor;
|
|
|
|
db.Avatar.Upsert(accountAvatar);
|
|
return Ok(new { Success = true });
|
|
}
|
|
[HttpGet("v2/gifts")]
|
|
[Authorize(Roles = "gameClient")]
|
|
public async Task<IActionResult> Gifts()
|
|
{
|
|
return Ok(new List<object>());
|
|
}
|
|
|
|
private Dictionary<string, object> SavedAvToDict(AvatarSaved av)
|
|
{
|
|
return new Dictionary<string, object> {
|
|
["CustomAvatarItems"] = new List<object>(),
|
|
["FaceFeatures"] = av.FaceFeatures,
|
|
["HairColor"] = av.HairColor,
|
|
["Name"] = av.Name,
|
|
["OutfitSelections"] = av.OutfitSelections,
|
|
["OutfitSelectionsV2"] = av.OutfitSelectionsV2,
|
|
["PreviewImageName"] = av.PreviewImageName,
|
|
["SkinColor"] = av.SkinColor,
|
|
["Slot"] = av.Slot
|
|
};
|
|
}
|
|
|
|
[HttpGet("v3/saved")]
|
|
[Authorize(Roles = "gameClient")]
|
|
public async Task<IActionResult> Saved()
|
|
{
|
|
Account? account = await jwt.GetLogin(User);
|
|
|
|
if (account == null)
|
|
{
|
|
return NotFound("User not found in database");
|
|
}
|
|
return Ok(db.AvatarSaveds.Find(x => x.Account.Id == account.Id).Select(d => SavedAvToDict(d)).ToList());
|
|
}
|
|
|
|
public class AvatarSavedSetRequest
|
|
{
|
|
public required string OutfitSelections { get; set; }
|
|
public required string OutfitSelectionsV2 { get; set; }
|
|
public required string FaceFeatures { get; set; }
|
|
public required string SkinColor { get; set; }
|
|
public required string HairColor { get; set; }
|
|
public string? Name { get; set; } = null;
|
|
public int Slot { get; set; }
|
|
public required string PreviewImageName { get; set; }
|
|
}
|
|
|
|
[HttpPost("v4/saved/set")]
|
|
[Authorize(Roles = "gameClient")]
|
|
public async Task<IActionResult> SavedSet([FromBody] AvatarSavedSetRequest request)
|
|
{
|
|
Account? account = await jwt.GetLogin(User);
|
|
|
|
if (account == null)
|
|
{
|
|
return NotFound("User not found in database");
|
|
}
|
|
|
|
|
|
SavedImage? image = db.SavedImages.Include(x => x.Player).FindOne(x => x.ImageName == request.PreviewImageName);
|
|
if (image == null) { return NotFound("image not found"); }
|
|
if (image.SavedImageType != SavedImageType.OutfitThumbnail)
|
|
{
|
|
return BadRequest("image not for outfut");
|
|
}
|
|
|
|
|
|
AvatarSaved avatarSaved = new()
|
|
{
|
|
Account = account,
|
|
Slot = request.Slot,
|
|
PreviewImageName = request.PreviewImageName,
|
|
Name = request.Name ?? "",
|
|
OutfitSelections = request.OutfitSelections,
|
|
OutfitSelectionsV2 = request.OutfitSelectionsV2,
|
|
SkinColor = request.SkinColor,
|
|
HairColor = request.HairColor,
|
|
FaceFeatures = request.FaceFeatures,
|
|
};
|
|
db.AvatarSaveds.Insert(avatarSaved);
|
|
|
|
|
|
return Ok(new { Success = true, Value = SavedAvToDict(avatarSaved)});
|
|
}
|
|
}
|
|
}
|