150 lines
4.3 KiB
C#
150 lines
4.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Concurrent;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
|
|
using NLog.Filters;
|
|
|
|
|
|
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 = System.String;
|
|
using OWNER_GUID = System.String;
|
|
using USER_GUID = System.String;
|
|
using CHARACTER_GUID = System.String;
|
|
using ITEM_GUID = System.String;
|
|
|
|
|
|
|
|
namespace ServerBase;
|
|
|
|
public abstract partial class UserManagerBase<TPrimaryKey, TSubKey, TEntity>
|
|
where TPrimaryKey : notnull
|
|
where TSubKey : notnull
|
|
where TEntity : EntityBase
|
|
{
|
|
private MultiIndexDictionary<TPrimaryKey, TSubKey, TEntity> m_users = new();
|
|
|
|
public UserManagerBase()
|
|
{
|
|
}
|
|
|
|
public Task<Result> onInit() => Task.FromResult(new Result());
|
|
|
|
public Result tryAdd(TPrimaryKey primaryKey, TEntity entityUser)
|
|
{
|
|
var result = new Result();
|
|
var err_msg = string.Empty;
|
|
|
|
if (false == m_users.tryAdd(primaryKey, entityUser))
|
|
{
|
|
err_msg = $"Failed to tryAdd() !!!, Already registered : Key:{primaryKey} - {entityUser.toBasicString()}";
|
|
Log.getLogger().error(err_msg);
|
|
result.setFail(ServerErrorCode.UserDuplicatedLogin, err_msg);
|
|
return result;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public Result trySubKeyBindToPrimaryKey(TSubKey subKey, TPrimaryKey primaryKey, TEntity entityUser)
|
|
{
|
|
var result = new Result();
|
|
var err_msg = string.Empty;
|
|
|
|
if(false == m_users.trySubKeyBindToPrimaryKey(subKey, primaryKey))
|
|
{
|
|
err_msg = $"Failed to trySubKeyBindToPrimaryKey() !!! : SubKey:{subKey}, PrimaryKey:{primaryKey} - {entityUser.toBasicString()}";
|
|
result.setFail(ServerErrorCode.UserSubKeyBindToFailed, err_msg);
|
|
Log.getLogger().error(result.toBasicString());
|
|
|
|
return result;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public Result tryRemoveByPrimaryKey(TPrimaryKey primaryKey, out TEntity? entityUser)
|
|
{
|
|
var result = new Result();
|
|
var err_msg = string.Empty;
|
|
|
|
if (false == m_users.tryRemoveByPrimaryKey(primaryKey, out entityUser))
|
|
{
|
|
err_msg = $"Failed to tryRemoveByPrimaryKey() !!!, TEntity not found : PrimaryKey:{primaryKey}";
|
|
Log.getLogger().debug(err_msg);
|
|
result.setFail(ServerErrorCode.UserNotLogin, err_msg);
|
|
return result;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public Result tryRemoveBySubKey(TSubKey subKey, TEntity entityUser)
|
|
{
|
|
var result = new Result();
|
|
var err_msg = string.Empty;
|
|
|
|
if (false == m_users.tryRemoveBySubKey(subKey, out _))
|
|
{
|
|
err_msg = $"Failed to tryRemoveBySubKey() !!!, TEntity not found : SubKey;{subKey} - {entityUser.toBasicString()}";
|
|
Log.getLogger().error(err_msg);
|
|
result.setFail(ServerErrorCode.UserNotLogin, err_msg);
|
|
return result;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public Result tryReplaceSubKey(TSubKey newSubKey, TPrimaryKey targetPrimaryKey, TEntity entityUser)
|
|
{
|
|
var result = new Result();
|
|
var err_msg = string.Empty;
|
|
|
|
if (false == m_users.tryReplaceSubKey(newSubKey, targetPrimaryKey))
|
|
{
|
|
err_msg = $"Failed to tryReplaceSubKey() !!!, TEntity not found : NewSubKey;{newSubKey}, TargetPrimaryKey:{targetPrimaryKey} - {entityUser.toBasicString()}";
|
|
Log.getLogger().error(err_msg);
|
|
result.setFail(ServerErrorCode.UserSubKeyReplaceFailed, err_msg);
|
|
return result;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public bool tryGetUserByPrimaryKey(TPrimaryKey primaryKey, [MaybeNullWhen(false)] out TEntity foundUser)
|
|
{
|
|
foundUser = null;
|
|
|
|
if (false == m_users.tryGetValue(primaryKey, out var found_user))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
foundUser = found_user;
|
|
return true;
|
|
}
|
|
|
|
public bool tryGetUserBySubKey(TSubKey subKey, out TEntity? foundUser)
|
|
{
|
|
foundUser = null;
|
|
|
|
if (false == m_users.tryGetValue(subKey, out var found_user))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
foundUser = found_user;
|
|
return true;
|
|
}
|
|
}
|