//
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
//
namespace MUnique.OpenMU.Persistence;
using System.Collections;
using Microsoft.Extensions.Logging;
using MUnique.OpenMU.AttributeSystem;
using MUnique.OpenMU.DataModel.Configuration;
using MUnique.OpenMU.DataModel.Entities;
using MUnique.OpenMU.Interfaces;
///
/// Implementation of a .
///
public sealed class AccountDataSource : DataSourceBase
{
private static readonly IReadOnlyDictionary> Enumerables = new Dictionary>
{
{ typeof(Account), a => Enumerable.Repeat(a, 1) },
{ typeof(Character), a => a.Characters },
{ typeof(ItemStorage), a => a.Characters.Select(c => c.Inventory).Concat(Enumerable.Repeat(a.Vault, 1)).Where(i => i is not null) },
{ typeof(Item), a => a.Characters.SelectMany(c => c.Inventory?.Items ?? Enumerable.Empty- ()).Concat(a.Vault?.Items ?? Enumerable.Empty
- ()) },
{ typeof(StatAttribute), a => a.Characters.SelectMany(c => c.Attributes) },
{ typeof(LetterHeader), a => a.Characters.SelectMany(c => c.Letters) },
{ typeof(SkillEntry), a => a.Characters.SelectMany(c => c.LearnedSkills) },
{ typeof(CharacterQuestState), a => a.Characters.SelectMany(c => c.QuestStates) },
{ typeof(QuestMonsterKillRequirementState), a => a.Characters.SelectMany(c => c.QuestStates).SelectMany(q => q.RequirementStates) },
}.AsReadOnly();
private readonly IDataSource _gameConfigurationSource;
///
/// Initializes a new instance of the class.
///
/// The logger.
/// The persistence context provider.
/// The game configuration source.
public AccountDataSource(
ILogger> logger,
IPersistenceContextProvider persistenceContextProvider,
IDataSource gameConfigurationSource)
: base(logger, persistenceContextProvider)
{
this._gameConfigurationSource = gameConfigurationSource;
}
///
protected override IReadOnlyDictionary> TypeToEnumerables => Enumerables;
///
protected override async ValueTask CreateNewContextAsync()
{
var gameConfiguration = await this._gameConfigurationSource.GetOwnerAsync(Guid.Empty).ConfigureAwait(false);
return this.ContextProvider.CreateNewPlayerContext(gameConfiguration);
}
}