using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Sog; namespace Account { public static class DeviceIdLimitSvc { /// /// 是否被限制登录(同设备id相同时间内登录帐号数量限制) /// /// /// /// /// public static bool CheckDeviceIdLimited(int accountType,string accountId, string deviceId) { int countLimit = AccountServerUtils.GetServerConfig().deviceIdFBAccountLimit; int timeLimit = AccountServerUtils.GetServerConfig().deviceIdFBAccountLimitTime; //配置了关闭限制功能 if (countLimit <= 0 || timeLimit <= 0) { return false; } //游客不做限制 if(accountType == (int)AccountType.AccountType_Guest) { return false; } if(string.IsNullOrEmpty(deviceId)) { TraceLog.Error("DeviceIdLimitSvc.CheckDeviceIdLimited id {0} invalid deviceId", accountId); return true; } long now = AccountServerUtils.GetTimeSecond(); var deviceIdMap = AccountServerUtils.GetAccountServerData().m_devicdIdMap; List list; if (deviceIdMap.ContainsKey(deviceId) == false) { list = new List(); DevicdIDFBAccountInfo info = new DevicdIDFBAccountInfo(); info.AccountID = accountId; info.LastLoginTime = now; list.Add(info); deviceIdMap.Add(deviceId, list); } else { list = deviceIdMap[deviceId]; } foreach(var info in list) { //旧有的account,允许登录 if(info.AccountID == accountId) { info.LastLoginTime = now; TraceLog.Trace("DeviceIdLimitSvc.CheckDeviceIdLimited id {0} already in list,can login", accountId); return false; } } //删除过期的帐号记录 for(int i = list.Count - 1; i >= 0; i--) { var info = list[i]; if (now - info.LastLoginTime >= timeLimit) { TraceLog.Trace("DeviceIdLimitSvc.CheckDeviceIdLimited delete timeout accountId {0}", info.AccountID); list.RemoveAt(i); } } //允许登录,添加帐号信息 if (list.Count < countLimit) { DevicdIDFBAccountInfo info = new DevicdIDFBAccountInfo(); info.AccountID = accountId; info.LastLoginTime = now; list.Add(info); TraceLog.Trace("DeviceIdLimitSvc.CheckDeviceIdLimited id {0} add to list,can login", accountId); return false; } TraceLog.Error("DeviceIdLimitSvc.CheckDeviceIdLimited id {0} be limited by deviceid {1}", accountId,deviceId); return true; } } }