Implement UserData and TitleData
This commit is contained in:
@@ -8,9 +8,11 @@ using Prospect.Server.Api.Config;
|
||||
using Prospect.Server.Api.Models.Client;
|
||||
using Prospect.Server.Api.Models.Client.Data;
|
||||
using Prospect.Server.Api.Services.Auth;
|
||||
using Prospect.Server.Api.Services.Auth.Extensions;
|
||||
using Prospect.Server.Api.Services.Auth.User;
|
||||
using Prospect.Server.Api.Services.Database;
|
||||
using Prospect.Server.Api.Services.Database.Models;
|
||||
using Prospect.Server.Api.Services.UserData;
|
||||
using Prospect.Server.Steam;
|
||||
|
||||
namespace Prospect.Server.Api.Controllers
|
||||
@@ -24,16 +26,22 @@ namespace Prospect.Server.Api.Controllers
|
||||
private readonly AuthTokenService _authTokenService;
|
||||
private readonly DbUserService _userService;
|
||||
private readonly DbEntityService _entityService;
|
||||
private readonly UserDataService _userDataService;
|
||||
private readonly TitleDataService _titleDataService;
|
||||
|
||||
public ClientController(IOptions<PlayFabSettings> settings,
|
||||
AuthTokenService authTokenService,
|
||||
DbUserService userService,
|
||||
DbEntityService entityService)
|
||||
DbEntityService entityService,
|
||||
UserDataService userDataService,
|
||||
TitleDataService titleDataService)
|
||||
{
|
||||
_settings = settings.Value;
|
||||
_authTokenService = authTokenService;
|
||||
_userService = userService;
|
||||
_entityService = entityService;
|
||||
_userDataService = userDataService;
|
||||
_titleDataService = titleDataService;
|
||||
}
|
||||
|
||||
[AllowAnonymous]
|
||||
@@ -49,10 +57,12 @@ namespace Prospect.Server.Api.Controllers
|
||||
var userSteamId = ticket.SteamId.ToString();
|
||||
|
||||
var user = await _userService.FindOrCreateAsync(PlayFabUserAuthType.Steam, userSteamId);
|
||||
var userTicket = _authTokenService.Generate(user);
|
||||
|
||||
var entity = await _entityService.FindOrCreateAsync(user.Id);
|
||||
var entityTicket = _authTokenService.Generate(entity);
|
||||
|
||||
var userTicket = _authTokenService.GenerateUser(entity);
|
||||
var entityTicket = _authTokenService.GenerateEntity(entity);
|
||||
|
||||
await _userDataService.InitAsync(user.Id);
|
||||
|
||||
return Ok(new ClientResponse<FServerLoginResult>
|
||||
{
|
||||
@@ -143,15 +153,18 @@ namespace Prospect.Server.Api.Controllers
|
||||
|
||||
[HttpPost("GetUserData")]
|
||||
[Produces("application/json")]
|
||||
public IActionResult GetUserData(FGetUserDataRequest request)
|
||||
public async Task<IActionResult> GetUserData(FGetUserDataRequest request)
|
||||
{
|
||||
var userId = User.FindAuthUserId();
|
||||
var userData = await _userDataService.FindAsync(userId, request.PlayFabId, request.Keys);
|
||||
|
||||
return Ok(new ClientResponse<FGetUserDataResult>
|
||||
{
|
||||
Code = 200,
|
||||
Status = "OK",
|
||||
Data = new FGetUserDataResult
|
||||
{
|
||||
Data = new Dictionary<string, FUserDataRecord>(),
|
||||
Data = userData,
|
||||
DataVersion = 0
|
||||
}
|
||||
});
|
||||
@@ -185,29 +198,29 @@ namespace Prospect.Server.Api.Controllers
|
||||
{
|
||||
Inventory = new List<FItemInstance>
|
||||
{
|
||||
new FItemInstance
|
||||
{
|
||||
ItemId = "Helmet_03",
|
||||
ItemInstanceId = "0102030405060708",
|
||||
ItemClass = "Helmet",
|
||||
PurchaseDate = DateTime.Now.AddDays(-1),
|
||||
CatalogVersion = "StaticItems",
|
||||
DisplayName = "Helmet",
|
||||
UnitPrice = 0,
|
||||
CustomData = new Dictionary<string, string>
|
||||
{
|
||||
["insurance"] = "None",
|
||||
["mods"] = "{\"m\":[]}",
|
||||
["vanity_misc_data"] = "{\"v\":\"\",\"s\":\"\",\"l\":3,\"a\":1,\"d\":300}"
|
||||
}
|
||||
}
|
||||
// new FItemInstance
|
||||
// {
|
||||
// ItemId = "Helmet_03",
|
||||
// ItemInstanceId = "0102030405060708",
|
||||
// ItemClass = "Helmet",
|
||||
// PurchaseDate = DateTime.Now.AddDays(-1),
|
||||
// CatalogVersion = "StaticItems",
|
||||
// DisplayName = "Helmet",
|
||||
// UnitPrice = 0,
|
||||
// CustomData = new Dictionary<string, string>
|
||||
// {
|
||||
// ["insurance"] = "None",
|
||||
// ["mods"] = "{\"m\":[]}",
|
||||
// ["vanity_misc_data"] = "{\"v\":\"\",\"s\":\"\",\"l\":3,\"a\":1,\"d\":300}"
|
||||
// }
|
||||
// }
|
||||
},
|
||||
VirtualCurrency = new Dictionary<string, int>
|
||||
{
|
||||
["AE"] = 0,
|
||||
["AS"] = 0,
|
||||
["AU"] = 0,
|
||||
["SC"] = 12345
|
||||
["SC"] = 30000
|
||||
},
|
||||
VirtualCurrencyRechargeTimes = new Dictionary<string, FVirtualCurrencyRechargeTime>()
|
||||
}
|
||||
@@ -224,7 +237,7 @@ namespace Prospect.Server.Api.Controllers
|
||||
Status = "OK",
|
||||
Data = new FGetTitleDataResult()
|
||||
{
|
||||
Data = new Dictionary<string, string>()
|
||||
Data = _titleDataService.Find(request.Keys)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ namespace Prospect.Server.Api.Models.Client.Data
|
||||
/// </summary>
|
||||
[JsonPropertyName("Permission")]
|
||||
[JsonConverter(typeof(JsonStringEnumConverter))]
|
||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
|
||||
public UserDataPermission Permission { get; set; }
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
{
|
||||
public static class AuthClaimTypes
|
||||
{
|
||||
public const string Id = "id";
|
||||
public const string UserId = "user_id";
|
||||
public const string EntityId = "entity_id";
|
||||
public const string Type = "type";
|
||||
}
|
||||
}
|
||||
@@ -38,20 +38,21 @@ namespace Prospect.Server.Api.Services.Auth
|
||||
return _tokenHandler.WriteToken(token);
|
||||
}
|
||||
|
||||
public string Generate(PlayFabUser user)
|
||||
public string GenerateUser(PlayFabEntity user)
|
||||
{
|
||||
return CreateToken(new[]
|
||||
{
|
||||
new Claim(AuthClaimTypes.Id, user.Id),
|
||||
new Claim(AuthClaimTypes.UserId, user.UserId),
|
||||
new Claim(AuthClaimTypes.EntityId, user.Id),
|
||||
new Claim(AuthClaimTypes.Type, AuthType.User),
|
||||
});
|
||||
}
|
||||
|
||||
public string Generate(PlayFabEntity entity)
|
||||
public string GenerateEntity(PlayFabEntity entity)
|
||||
{
|
||||
return CreateToken(new[]
|
||||
{
|
||||
new Claim(AuthClaimTypes.Id, entity.Id),
|
||||
new Claim(AuthClaimTypes.EntityId, entity.Id),
|
||||
new Claim(AuthClaimTypes.Type, AuthType.Entity),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -5,9 +5,14 @@ namespace Prospect.Server.Api.Services.Auth.Extensions
|
||||
{
|
||||
public static class ClaimsPrincipalExtensions
|
||||
{
|
||||
public static string FindAuthId(this ClaimsPrincipal principal)
|
||||
public static string FindAuthUserId(this ClaimsPrincipal principal)
|
||||
{
|
||||
return Find(principal, AuthClaimTypes.Id);
|
||||
return Find(principal, AuthClaimTypes.UserId);
|
||||
}
|
||||
|
||||
public static string FindAuthEntityId(this ClaimsPrincipal principal)
|
||||
{
|
||||
return Find(principal, AuthClaimTypes.EntityId);
|
||||
}
|
||||
|
||||
public static string FindAuthType(this ClaimsPrincipal principal)
|
||||
|
||||
@@ -8,13 +8,13 @@ namespace Prospect.Server.Api.Services.Database
|
||||
{
|
||||
public class DbEntityService : BaseDbService<PlayFabEntity>
|
||||
{
|
||||
public DbEntityService(IOptions<DatabaseSettings> settings) : base(settings, nameof(DbEntityService))
|
||||
public DbEntityService(IOptions<DatabaseSettings> settings) : base(settings, nameof(PlayFabEntity))
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<PlayFabEntity> FindAsync(string userId)
|
||||
{
|
||||
return await Collection.Find(user => user.UserId == userId).FirstOrDefaultAsync();
|
||||
return await Collection.Find(user => user.UserId == userId).SingleOrDefaultAsync();
|
||||
}
|
||||
|
||||
private async Task<PlayFabEntity> CreateAsync(string userId)
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Options;
|
||||
using MongoDB.Driver;
|
||||
using Prospect.Server.Api.Config;
|
||||
using Prospect.Server.Api.Services.Database.Models;
|
||||
|
||||
namespace Prospect.Server.Api.Services.Database
|
||||
{
|
||||
public class DbUserDataService : BaseDbService<PlayFabUserData>
|
||||
{
|
||||
public DbUserDataService(IOptions<DatabaseSettings> options) : base(options, nameof(PlayFabUserData))
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<bool> HasAsync(string playFabId, string key)
|
||||
{
|
||||
return await Collection.Find(data => data.PlayFabId == playFabId && data.Key == key).AnyAsync();
|
||||
}
|
||||
|
||||
public async Task<PlayFabUserData> FindAsync(string playFabId, string key)
|
||||
{
|
||||
return await Collection.Find(data => data.PlayFabId == playFabId && data.Key == key).SingleOrDefaultAsync();
|
||||
}
|
||||
|
||||
public async Task<PlayFabUserData> InsertAsync(string playFabId, string key, string value, bool isPublic)
|
||||
{
|
||||
var data = new PlayFabUserData
|
||||
{
|
||||
PlayFabId = playFabId,
|
||||
Key = key,
|
||||
Value = value,
|
||||
Public = isPublic,
|
||||
LastUpdated = DateTime.UtcNow
|
||||
};
|
||||
|
||||
await Collection.InsertOneAsync(data);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
public async Task<IAsyncCursor<PlayFabUserData>> AllAsync(string playFabId, bool publicOnly)
|
||||
{
|
||||
var query = publicOnly
|
||||
? Collection.Find(data => data.PlayFabId == playFabId && data.Public)
|
||||
: Collection.Find(data => data.PlayFabId == playFabId);
|
||||
|
||||
return await query.ToCursorAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
|
||||
namespace Prospect.Server.Api.Services.Database.Models
|
||||
{
|
||||
public class PlayFabUserData
|
||||
{
|
||||
[BsonId]
|
||||
[BsonRepresentation(BsonType.ObjectId)]
|
||||
public string Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// PlayFabUser.Id
|
||||
/// </summary>
|
||||
[BsonRequired]
|
||||
[BsonElement("PlayFabId")]
|
||||
public string PlayFabId { get; set; }
|
||||
|
||||
[BsonRequired]
|
||||
[BsonElement("Key")]
|
||||
public string Key { get; set; }
|
||||
|
||||
[BsonRequired]
|
||||
[BsonElement("Value")]
|
||||
public string Value { get; set; }
|
||||
|
||||
[BsonRequired]
|
||||
[BsonElement("Public")]
|
||||
public bool Public { get; set; }
|
||||
|
||||
[BsonRequired]
|
||||
[BsonElement("LastUpdated")]
|
||||
public DateTime LastUpdated { get; set; }
|
||||
}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,33 @@
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Prospect.Server.Api.Services.UserData
|
||||
{
|
||||
public class TitleDataService
|
||||
{
|
||||
public TitleDataService(ILogger<TitleDataService> logger)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public Dictionary<string, string> Find(List<string> keys)
|
||||
{
|
||||
if (keys != null && keys.Count > 0)
|
||||
{
|
||||
var result = new Dictionary<string, string>();
|
||||
|
||||
foreach (var key in keys)
|
||||
{
|
||||
if (TitleDataDefault.Data.TryGetValue(key, out var value))
|
||||
{
|
||||
result.Add(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
return TitleDataDefault.Data;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Prospect.Server.Api.Models.Client.Data;
|
||||
using Prospect.Server.Api.Services.Database;
|
||||
|
||||
namespace Prospect.Server.Api.Services.UserData
|
||||
{
|
||||
public class UserDataService
|
||||
{
|
||||
private readonly ILogger<UserDataService> _logger;
|
||||
private readonly DbUserDataService _dbUserDataService;
|
||||
|
||||
public UserDataService(ILogger<UserDataService> logger, DbUserDataService dbUserDataService)
|
||||
{
|
||||
_logger = logger;
|
||||
_dbUserDataService = dbUserDataService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize data for the given PlayFabId.
|
||||
/// </summary>
|
||||
public async Task InitAsync(string playFabId)
|
||||
{
|
||||
// TODO: Proper objects.
|
||||
var defaultData = new Dictionary<string, (bool isPublic, string value)>
|
||||
{
|
||||
["Generators__2021_09_09"] = (true, "[{\"generatorId\":\"playerquarters_gen_aurum\",\"lastClaimTime\":{\"seconds\":0}},{\"generatorId\":\"playerquarters_gen_kmarks\",\"lastClaimTime\":{\"seconds\":0}},{\"generatorId\":\"playerquarters_gen_crate\",\"lastClaimTime\":{\"seconds\":0}}]"),
|
||||
["InventoryInfo"] = (true, "{\"inventoryStashLimit\":75,\"inventoryBagLimit\":300,\"inventorySafeLimit\":5}"),
|
||||
["LOADOUT"] = (true, $"{{\"id\":\"\",\"userId\":\"{playFabId}\",\"kit\":\"\",\"shield\":\"\",\"helmet\":\"\",\"weaponOne\":\"\",\"weaponTwo\":\"\",\"bag\":null,\"bagItemsAsJsonStr\":\"\",\"safeItemsAsJsonStr\":\"\"}}"),
|
||||
["OnboardingProgression"] = (true, "{\"currentMissionID\":\"TalkToBadum\",\"progress\":0,\"showPopup\":true}"),
|
||||
["PickaxeUpgradeLevel"] = (true, "0"),
|
||||
["PlayerQuartersLevel"] = (true, "{\"level\":1,\"upgradeStartedTime\":{\"seconds\":0}}"),
|
||||
};
|
||||
|
||||
foreach (var (key, (isPublic, value)) in defaultData)
|
||||
{
|
||||
if (!await _dbUserDataService.HasAsync(playFabId, key))
|
||||
{
|
||||
await _dbUserDataService.InsertAsync(playFabId, key, value, isPublic);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="currentUserId">
|
||||
/// The authenticated user id.
|
||||
/// </param>
|
||||
/// <param name="requestUserId">
|
||||
/// The requested user id.
|
||||
/// </param>
|
||||
/// <param name="keys"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<Dictionary<string, FUserDataRecord>> FindAsync(
|
||||
string currentUserId,
|
||||
string requestUserId,
|
||||
List<string> keys)
|
||||
{
|
||||
if (string.IsNullOrEmpty(currentUserId))
|
||||
{
|
||||
throw new ArgumentNullException(nameof(currentUserId));
|
||||
}
|
||||
|
||||
if (requestUserId == null)
|
||||
{
|
||||
requestUserId = currentUserId;
|
||||
}
|
||||
|
||||
var other = currentUserId != requestUserId;
|
||||
var result = new Dictionary<string, FUserDataRecord>();
|
||||
|
||||
if (keys != null && keys.Count > 0)
|
||||
{
|
||||
foreach (var key in keys)
|
||||
{
|
||||
var data = await _dbUserDataService.FindAsync(requestUserId, key);
|
||||
if (data == null)
|
||||
{
|
||||
// TODO: Error?
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!data.Public && other)
|
||||
{
|
||||
// TODO: Error?
|
||||
continue;
|
||||
}
|
||||
|
||||
result.Add(data.Key, new FUserDataRecord
|
||||
{
|
||||
LastUpdated = data.LastUpdated,
|
||||
Permission = data.Public ? UserDataPermission.Public : UserDataPermission.Private,
|
||||
Value = data.Value
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var cursor = await _dbUserDataService.AllAsync(requestUserId, other);
|
||||
|
||||
while (await cursor.MoveNextAsync())
|
||||
{
|
||||
foreach (var data in cursor.Current)
|
||||
{
|
||||
result.Add(data.Key, new FUserDataRecord
|
||||
{
|
||||
LastUpdated = data.LastUpdated,
|
||||
Permission = data.Public ? UserDataPermission.Public : UserDataPermission.Private,
|
||||
Value = data.Value
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ using Prospect.Server.Api.Middleware;
|
||||
using Prospect.Server.Api.Services.Auth;
|
||||
using Prospect.Server.Api.Services.Auth.User;
|
||||
using Prospect.Server.Api.Services.Database;
|
||||
using Prospect.Server.Api.Services.UserData;
|
||||
using Serilog;
|
||||
|
||||
namespace Prospect.Server.Api
|
||||
@@ -28,13 +29,17 @@ namespace Prospect.Server.Api
|
||||
services.Configure<PlayFabSettings>(Configuration.GetSection(nameof(PlayFabSettings)));
|
||||
|
||||
services.AddSingleton<AuthTokenService>();
|
||||
services.AddSingleton<UserDataService>();
|
||||
services.AddSingleton<TitleDataService>();
|
||||
|
||||
services.AddSingleton<DbUserService>();
|
||||
services.AddSingleton<DbEntityService>();
|
||||
|
||||
services.AddSingleton<DbUserDataService>();
|
||||
|
||||
services.AddAuthentication(options =>
|
||||
services.AddAuthentication(_ =>
|
||||
{
|
||||
options.DefaultAuthenticateScheme = UserAuthenticationOptions.DefaultScheme;
|
||||
options.DefaultChallengeScheme = UserAuthenticationOptions.DefaultScheme;
|
||||
|
||||
})
|
||||
.AddUserAuthentication(_ => {})
|
||||
.AddEntityAuthentication(_ => {});
|
||||
|
||||
Reference in New Issue
Block a user