Files
2025-05-01 07:20:41 +09:00

471 lines
19 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using ServerCore; using ServerBase;
using SESSION_ID = System.Int32;
using WORLD_ID = System.UInt32;
using META_ID = System.UInt32;
using ENTITY_GUID = System.String;
using ACCOUNT_ID_STRING = System.String;
using ACCOUNT_ID = System.String;
using OWNER_GUID = System.String;
using USER_GUID = System.String;
using CHARACTER_GUID = System.String;
using ITEM_GUID = System.String;
using System.Runtime.InteropServices;
using Amazon.S3.Model;
using Amazon.Runtime.Telemetry;
namespace ServerCommon;
public class AccountAttribute : EntityAttributeBase, ICopyEntityAttributeFromMeta, ICopyEntityAttributeFromCache, ICopyEntityAttributeFromDoc, IMergeWithEntityAttribute
{
[JsonProperty]
public AccountType AccountType { get; set; } = AccountType.None;
[JsonProperty]
public PlatformType PlatformType { get; set; } = PlatformType.None;
[JsonProperty]
public ACCOUNT_ID_STRING AccountIdString { get; set; } = string.Empty;
[JsonProperty]
public ACCOUNT_ID AccountId { get; set; } = string.Empty;
[JsonProperty]
public string UserId { get; set; } = string.Empty;
[JsonProperty]
public USER_GUID UserGuid { get; set; } = string.Empty;
public UInt64 AccessToken { get; set; } = 0;
public string SsoAccountAuthJWT { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public LanguageType LanguageType { get; set; } = LanguageType.None;
public AccountCreationType AccountCreationType { get; set; } = AccountCreationType.Normal;
public META_ID AccountCreationMetaId { get; set; } = 0;
public AuthAdminLevelType AuthAdminLevelType { get; set; } = AuthAdminLevelType.None;
public DateTime LoginDateTime { get; set; } = DateTimeHelper.MaxTime;
public DateTime LogoutDateTime { get; set; } = DateTimeHelper.MaxTime;
public NetworkAddress ToConnectGameServerAddress { get; set; } = new();
public string ToConnectGameServerName { get; set; } = string.Empty;
public string OtpForServerConnect { get; set; } = string.Empty;
public AccountAttribute(EntityBase owner)
: base(owner, owner)
{
ArgumentNullReferenceCheckHelper.throwIfNull(owner, () => $"owner is null !! ");
}
public USER_GUID newUserGuid()
{
UserGuid = System.Guid.NewGuid().ToString("N");
return UserGuid;
}
public override void onClear()
{
AccountType = AccountType.None;
PlatformType = PlatformType.None;
AccountIdString = string.Empty;
AccountId = string.Empty;
AccessToken = 0;
Password = string.Empty;
UserId = string.Empty;
UserGuid = string.Empty;
LanguageType = LanguageType.None;
AccountCreationType = AccountCreationType.None;
AuthAdminLevelType = AuthAdminLevelType.None;
LoginDateTime = DateTimeHelper.MaxTime;
LogoutDateTime = DateTimeHelper.MaxTime;
ToConnectGameServerAddress.reset();
OtpForServerConnect = string.Empty;
SsoAccountAuthJWT = string.Empty;
getAttributeState().reset();
}
public override EntityAttributeBase onCloned()
{
var owner = getOwner() as UserBase;
NullReferenceCheckHelper.throwIfNull(owner, () => $"owner is null !!!");
var cloned = new AccountAttribute(owner);
cloned.AccountType = AccountType;
cloned.PlatformType = PlatformType;
cloned.AccountIdString = AccountIdString;
cloned.AccountId = AccountId;
cloned.AccessToken = AccessToken;
cloned.Password = Password;
cloned.UserId = UserId;
cloned.UserGuid = UserGuid;
cloned.LanguageType = LanguageType;
cloned.AuthAdminLevelType = AuthAdminLevelType;
cloned.AccountCreationType = AccountCreationType;
cloned.LoginDateTime = LoginDateTime;
cloned.LogoutDateTime = LogoutDateTime;
cloned.ToConnectGameServerAddress.IP = ToConnectGameServerAddress.IP;
cloned.ToConnectGameServerAddress.Port = ToConnectGameServerAddress.Port;
cloned.OtpForServerConnect = OtpForServerConnect;
cloned.SsoAccountAuthJWT = SsoAccountAuthJWT;
return cloned;
}
public override IEntityAttributeTransactor onNewEntityAttributeTransactor()
{
return new AccountAttributeTransactor(getOwner());
}
public override async Task<(Result, DynamoDbDocBase?)> toDocBase(bool isForQuery = true)
{
var result = new Result();
var owner = getOwner();
//=====================================================================================
// Attribute => Doc
//=====================================================================================
var try_pending_doc = getTryPendingDocBase() as AccountBaseDoc;
if (null == try_pending_doc)
{
var to_copy_doc = new AccountBaseDoc(AccountId);
var origin_doc = getOriginDocBase<AccountAttribute>();
if (null != origin_doc)
{
to_copy_doc.copyTimestampsFromOriginDocBase(origin_doc);
}
try_pending_doc = to_copy_doc;
setTryPendingDocBase(try_pending_doc);
}
var account_base_attrib = try_pending_doc.getAttrib<AccountBaseAttrib>();
NullReferenceCheckHelper.throwIfNull(account_base_attrib, () => $"account_base_attrib is null !!!");
account_base_attrib.AccountId = AccountId;
account_base_attrib.UserGuid = UserGuid;
account_base_attrib.Password = Password;
account_base_attrib.LanguageType = LanguageType;
account_base_attrib.AuthAdminLevelType = AuthAdminLevelType;
account_base_attrib.AccountCreationType = AccountCreationType;
account_base_attrib.AccountCreationMetaId = AccountCreationMetaId;
account_base_attrib.LoginDateTime = LoginDateTime;
account_base_attrib.LogoutDateTime = LogoutDateTime;
account_base_attrib.AccessToken = AccessToken;
account_base_attrib.SsoAccountAuthJWT = SsoAccountAuthJWT;
if(false == isForQuery)
{
return (result, try_pending_doc);
}
//=====================================================================================
// Doc QueryType 반영
//=====================================================================================
(result, var to_query_doc) = await applyDoc4Query(try_pending_doc);
if(result.isFail())
{
return (result, null);
}
NullReferenceCheckHelper.throwIfNull(to_query_doc, () => $"to_query_doc is null !!!");
return (result, to_query_doc);
}
public Result onMerge(EntityAttributeBase otherEntityAttribute)
{
var owner = getOwner();
NullReferenceCheckHelper.throwIfNull(owner, () => $"owner is null !!!");
var result = new Result();
var err_msg = string.Empty;
if (null == otherEntityAttribute)
{
err_msg = $"Invalid Param !!!, otherEntityAttribute is null";
result.setFail(ServerErrorCode.FunctionParamNull, err_msg);
Log.getLogger().error(result.toBasicString());
return result;
}
//=====================================================================================
// OtherAttribute => Attribute
//=====================================================================================
var other_account_attribute = otherEntityAttribute as AccountAttribute;
if (null == other_account_attribute)
{
err_msg = $"Failed to cast AccountAttribute !!!, other_account_attribute is null";
result.setFail(ServerErrorCode.ClassTypeCastIsNull, err_msg);
Log.getLogger().error(result.toBasicString());
return result;
}
AccountId = other_account_attribute.AccountId;
UserId = other_account_attribute.UserId;
UserGuid = other_account_attribute.UserGuid;
LanguageType = other_account_attribute.LanguageType;
Password = other_account_attribute.Password;
AuthAdminLevelType = other_account_attribute.AuthAdminLevelType;
AccountCreationType = other_account_attribute.AccountCreationType;
AccountCreationMetaId = other_account_attribute.AccountCreationMetaId;
LoginDateTime = other_account_attribute.LoginDateTime;
LogoutDateTime = other_account_attribute.LogoutDateTime;
ToConnectGameServerAddress.IP = other_account_attribute.ToConnectGameServerAddress.IP;
ToConnectGameServerAddress.Port = other_account_attribute.ToConnectGameServerAddress.Port;
OtpForServerConnect = other_account_attribute.OtpForServerConnect;
AccessToken = other_account_attribute.AccessToken;
SsoAccountAuthJWT = other_account_attribute.SsoAccountAuthJWT;
//=====================================================================================
// Attribute Try Pending Doc => Origin Doc
//=====================================================================================
var try_pending_doc = other_account_attribute.getTryPendingDocBase() as AccountBaseDoc;
if (null != try_pending_doc)
{
other_account_attribute.resetTryPendingDocBase();
syncOriginDocBaseWithNewDoc<AccountAttribute>(try_pending_doc);
}
var origin_doc_base = getOriginDocBase<AccountAttribute>();
if (null == origin_doc_base)
{
// DB 에 저장되어 있지 않는 경우 OriginDoc은 null 이다 !!!
return result;
}
var account_base_attrib = origin_doc_base.getAttrib<AccountBaseAttrib>();
NullReferenceCheckHelper.throwIfNull(account_base_attrib, () => $"account_base_attrib is null !!! - {owner.toBasicString()}");
account_base_attrib.AccountId = AccountId;
account_base_attrib.UserGuid = UserGuid;
account_base_attrib.LanguageType = LanguageType;
account_base_attrib.Password = Password;
account_base_attrib.AuthAdminLevelType = AuthAdminLevelType;
account_base_attrib.AccountCreationType = AccountCreationType;
account_base_attrib.AccountCreationMetaId = AccountCreationMetaId;
account_base_attrib.LoginDateTime = LoginDateTime;
account_base_attrib.LogoutDateTime = LogoutDateTime;
account_base_attrib.AccessToken = AccessToken;
account_base_attrib.SsoAccountAuthJWT = SsoAccountAuthJWT;
return result;
}
public bool copyEntityAttributeFromMeta(MetaAssets.IMetaData customMeta)
{
var owner = getOwner();
NullReferenceCheckHelper.throwIfNull(owner, () => $"owner is null !!!");
ArgumentNullReferenceCheckHelper.throwIfNull(customMeta, () => $"customMeta is null !!! - {owner.toBasicString()}");
var err_msg = string.Empty;
bool is_success = false;
var test_user_create_data = customMeta as MetaAssets.TestUserCreateMetaData;
if (null != test_user_create_data)
{
is_success = copyEntityAttributeFromTestUserCreateData(test_user_create_data);
}
var user_create_data = customMeta as MetaAssets.UserCreateMetaData;
if (null != user_create_data)
{
is_success = copyEntityAttributeFromUserCreateData(user_create_data);
}
if (false == is_success)
{
err_msg = $"Failed to copyEntityAttributeFromMeta() !!! : {customMeta.getTypeName()} - {owner.toBasicString()}";
Log.getLogger().error(err_msg);
return false;
}
return true;
}
private bool copyEntityAttributeFromTestUserCreateData(MetaAssets.TestUserCreateMetaData customMeta)
{
var err_msg = string.Empty;
var to_cast_string = typeof(MetaAssets.TestUserCreateMetaData).Name;
if (null == customMeta)
{
err_msg = $"Failed to copyEntityAttributeFromTestUserCreateData() !!!, customMeta is null : {to_cast_string}";
Log.getLogger().warn(err_msg);
return false;
}
Password = customMeta.Password;
LanguageType = customMeta.Language;
AccountCreationType = AccountCreationType.Test;
AccountCreationMetaId = (UInt32)customMeta.MetaId;
return true;
}
private bool copyEntityAttributeFromUserCreateData(MetaAssets.UserCreateMetaData customMeta)
{
var err_msg = string.Empty;
var to_cast_string = typeof(MetaAssets.UserCreateMetaData).Name;
if (null == customMeta)
{
err_msg = $"Failed to copyEntityAttributeFromUserCreateData() !!!, customMeta is null : {to_cast_string}";
Log.getLogger().warn(err_msg);
return false;
}
AccountCreationType = AccountCreationType.Normal;
AccountCreationMetaId = (UInt32)customMeta.MetaId;
return true;
}
public bool copyEntityAttributeFromCache(CacheBase? cacheBase)
{
ArgumentNullReferenceCheckHelper.throwIfNull(cacheBase, () => $"cacheBase is null !!!");
var err_msg = string.Empty;
var to_cast_string = typeof(LoginCache).Name;
var login_cache = cacheBase as LoginCache;
if(null == login_cache)
{
err_msg = $"Failed to copyEntityAttributeFromCache() !!!, login_cache is null :{to_cast_string}";
Log.getLogger().error(err_msg);
return false;
}
//=====================================================================================
// Cache => Attribute
//=====================================================================================
AccountId = login_cache.AccountId;
UserGuid = login_cache.UserGuid;
LanguageType = login_cache.LanguageType;
LoginDateTime = login_cache.LoginDateTime;
return true;
}
public bool copyEntityAttributeFromDoc(DynamoDbDocBase? docBase)
{
ArgumentNullReferenceCheckHelper.throwIfNull(docBase, () => $"docBase is null !!!");
var err_msg = string.Empty;
var to_cast_string = typeof(AccountBaseDoc).Name;
var account_base_doc = docBase as AccountBaseDoc;
if (null == account_base_doc)
{
err_msg = $"Failed to copyEntityAttributeFromDoc() !!!, account_base_doc is null :{to_cast_string}";
Log.getLogger().error(err_msg);
return false;
}
//=====================================================================================
// New Doc => Origin Doc
//=====================================================================================
syncOriginDocBaseWithNewDoc<AccountAttribute>(account_base_doc);
//=====================================================================================
// Doc => Attribute
//=====================================================================================
var account_base_attrib = account_base_doc.getAttrib<AccountBaseAttrib>();
NullReferenceCheckHelper.throwIfNull(account_base_attrib, () => $"account_base_attrib is null !!!");
AccountId = account_base_attrib.AccountId;
Password = account_base_attrib.Password;
UserGuid = account_base_attrib.UserGuid;
LanguageType = account_base_attrib.LanguageType;
AuthAdminLevelType = account_base_attrib.AuthAdminLevelType;
AccountCreationType = account_base_attrib.AccountCreationType;
AccountCreationMetaId = account_base_attrib.AccountCreationMetaId;
LoginDateTime = account_base_attrib.LoginDateTime;
AccessToken = account_base_attrib.AccessToken;
SsoAccountAuthJWT = account_base_attrib.SsoAccountAuthJWT;
return true;
}
public override string toBasicString()
{
return $"{base.toBasicString()}, UserGuid:{UserGuid}, AccountId:{AccountId}, UserId:{UserId}";
}
}
public class AccountAttributeTransactor : EntityAttributeTransactorBase<AccountAttribute>, ICopyEntityAttributeTransactorFromEntityAttribute
{
public AccountAttributeTransactor(EntityBase owner)
: base(owner)
{
}
public bool copyEntityAttributeTransactorFromEntityAttribute(EntityAttributeBase entityAttributeBase)
{
ArgumentNullReferenceCheckHelper.throwIfNull(entityAttributeBase, () => $"item is null !!! - {toBasicString()}");
var err_msg = string.Empty;
var to_cast_string = typeof(AccountAttribute).Name;
var copy_from_account_attribute = entityAttributeBase as AccountAttribute;
if (null == copy_from_account_attribute)
{
err_msg = $"Failed to copyEntityAttributeTransactorFromEntityAttribute() !!!, copy_from_account_attribute is null :{to_cast_string}";
Log.getLogger().error(err_msg);
return false;
}
var copy_to_account_attribute = getClonedEntityAttribute() as AccountAttribute;
if (null == copy_to_account_attribute)
{
err_msg = $"Failed to copyEntityAttributeTransactorFromEntityAttribute() !!!, copy_to_account_attribute is null :{to_cast_string}";
Log.getLogger().error(err_msg);
return false;
}
copy_to_account_attribute.AccountType = copy_from_account_attribute.AccountType;
copy_to_account_attribute.AccountId = copy_from_account_attribute.AccountId;
copy_to_account_attribute.AccessToken = copy_from_account_attribute.AccessToken;
copy_to_account_attribute.UserId = copy_from_account_attribute.UserId;
copy_to_account_attribute.UserGuid = copy_from_account_attribute.UserGuid;
copy_to_account_attribute.LanguageType = copy_from_account_attribute.LanguageType;
copy_to_account_attribute.AccountCreationType = copy_from_account_attribute.AccountCreationType;
copy_to_account_attribute.AccountCreationMetaId = copy_from_account_attribute.AccountCreationMetaId;
copy_to_account_attribute.AuthAdminLevelType = copy_from_account_attribute.AuthAdminLevelType;
copy_to_account_attribute.LoginDateTime = copy_from_account_attribute.LoginDateTime;
copy_to_account_attribute.LogoutDateTime = copy_from_account_attribute.LogoutDateTime;
copy_to_account_attribute.ToConnectGameServerAddress.IP = copy_from_account_attribute.ToConnectGameServerAddress.IP;
copy_to_account_attribute.ToConnectGameServerAddress.Port = copy_from_account_attribute.ToConnectGameServerAddress.Port;
copy_to_account_attribute.OtpForServerConnect = copy_from_account_attribute.OtpForServerConnect;
copy_to_account_attribute.SsoAccountAuthJWT = copy_from_account_attribute.SsoAccountAuthJWT;
return true;
}
}