Skip to content

Commit

Permalink
[增加]1. 增加ActorID的生成接口
Browse files Browse the repository at this point in the history
  • Loading branch information
AlianBlank committed Aug 4, 2024
1 parent b9dbbb0 commit 8ff6e9d
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions GameFrameX.Core/Utility/ActorIdGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ public static long GetActorId(ActorType type, int serverId = 0)
/// <summary>
/// 根据ActorType类型和服务器id获取ActorId
/// </summary>
/// <param name="actorType"></param>
/// <param name="actorType">ActorType</param>
/// <param name="serverId">服务器ID</param>
/// <returns></returns>
private static long GetGlobalActorId(ActorType actorType, int serverId)
public static long GetGlobalActorId(ActorType actorType, int serverId)
{
if (serverId <= 0)
{
Expand All @@ -148,8 +148,19 @@ private static long GetGlobalActorId(ActorType actorType, int serverId)
}


private static long GetMultiActorId(ActorType type, int serverId)
/// <summary>
/// 根据ActorType类型和服务器id获取ActorId
/// </summary>
/// <param name="actorType">ActorType</param>
/// <param name="serverId">服务器ID</param>
/// <returns></returns>
public static long GetMultiActorId(ActorType actorType, int serverId)
{
if (serverId <= 0)
{
throw new ArgumentOutOfRangeException(nameof(serverId), "serverId is less than 0");
}

long second = (long)(DateTime.UtcNow - IdGenerator.UtcTimeStart).TotalSeconds;
lock (LockObj)
{
Expand All @@ -170,12 +181,31 @@ private static long GetMultiActorId(ActorType type, int serverId)
}

var actorId = (long)serverId << GlobalConst.ServerIdOrModuleIdMask; // serverId-14位, 支持1000~9999
actorId |= (long)type << GlobalConst.ActorTypeMask; // 多actor类型-7位, 支持0~127
actorId |= (long)actorType << GlobalConst.ActorTypeMask; // 多actor类型-7位, 支持0~127
actorId |= _genSecond << GlobalConst.TimestampMask; // 时间戳-30位, 支持34年
actorId |= _incrNum; // 自增-12位, 每秒4096个
return actorId;
}


/// <summary>
/// 生成账号的唯一ID
/// </summary>
/// <returns></returns>
public static long GetAccountUniqueId()
{
return GetUniqueId(IdModule.Account);
}

/// <summary>
/// 生成玩家的唯一ID
/// </summary>
/// <returns></returns>
public static long GetPlayerUniqueId()
{
return GetUniqueId(IdModule.Player);
}

/// <summary>
/// 根据模块获取唯一ID
/// </summary>
Expand Down

0 comments on commit 8ff6e9d

Please sign in to comment.