You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

101 lines
3.1 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Sog
{
public static class NameUtils
{
/// <summary>
/// nameserver工作线程数量,这个尽量不要修改,10个线程是比较合理的配置了,不允许服务器运行过程中动态修改,很多逻辑依赖这个
/// </summary>
public static int DBServerWorkThreadCount = 10;
public static string CalcNameKey(string hashName)
{
return hashName;
}
//注意:这个算法不允许修改!!,改了会导致运营的项目的悲剧
// 自己实现字符串的hash算法,不用C#缺省的string.HashCode(),方便其他程序和工具计算结果一致
//这个算法来自Brian Kernighan 和 Dennis Ritchie的 The C Programming Language。
//这是一个很简单的哈希算法,使用了一系列奇怪的数字,形式如31,3131,31...31,看上去和DJB算法很相似。(这个就是Java的字符串哈希函数)
//c#内部用的应该和这个类似
public static int HashStringForIndex(string str)
{
const int seed = 131;
int hash = 0;
for (int i = 0; i < str.Length; i++)
{
hash = hash * seed + str[i];
}
//保证正数
return hash & 0x7FFFFFFF;
/*
* 这个是 ELF hash 算法, 这个冲突其实挺多,测试效果看来没有上面的好
uint h = 0;
uint g = 0;
for (int i = 0; i < value.Length; i++)
{
h = (h << 4) + value[i];
g = h & 0xF0000000;
if (g != 0)
{
h = h ^ (g >> 24);
h = h ^ g;
}
}
return h;
*/
}
/// <summary>
/// 0 ~ totalCount-1
/// 计算account帐号应该由那个account服务器处理,多accountserver的时候使用
/// </summary>
/// <param name="accountType"></param>
/// <param name="accountID"></param>
/// <param name="totalCount"></param>
/// <returns></returns>
public static int CalcNameIndexByTotalCount(string hashName, int totalCount)
{
string strHash = CalcNameKey(hashName);
int index = HashStringForIndex(strHash) % totalCount;
return index;
}
//算法方便眼睛看,同时考虑到客户端 lua number不溢出
/*
public static long GetGameIdFromUserId(long userId)
{
return (userId % GAMEID_USERID_MOD);
}
public static int GetRealmIdFromUserId(long userId)
{
return (int)(userId / GAMEID_USERID_MOD);
}
public static long GetUserIdFromGameIdReamId(long gameId, int realmId)
{
long userId = realmId;
userId = userId * GAMEID_USERID_MOD;
userId += gameId;
return userId;
}
public static long GetGameIDUserIdMod()
{
return GAMEID_USERID_MOD;
}*/
}
}