Implement UserData and TitleData
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user