CTT/Server/Hotfix/Game/System/User/GateUserComponentSystem.cs

203 lines
7.9 KiB
C#
Executable File

using ET;
using ET.EventType;
using System;
using System.Collections.Generic;
using System.Text;
namespace ET
{
public class GateUserComponentAwakeSystem : AwakeSystem<GateUserComponent>
{
public override void Awake(GateUserComponent self)
{
self.Awake();
}
}
public static class GateUserComponentSystem
{
/// <summary>
/// 查询缓存或数据库
/// </summary>
/// <param name="self"></param>
/// <param name="id"></param>
/// <returns></returns>
public static async ETTask<User> Query(this GateUserComponent self, long id)
{
User player = self.Get(id);
if (player == null)
player = await self.QueryUserByCache(id);
if (player == null)
{
Log.Error($"[ERROR] user == null where id = {id}");
return null;
}
return player;
}
private static async ETTask<User> QueryUserByCache(this GateUserComponent self,long id)
{
long mapInstanceId = StartSceneConfigCategory.Instance.GetBySceneName(self.DomainZone(), SceneType.UserCache.ToString()).SceneId;
U2M_GetComponent getComponent= (U2M_GetComponent) await MessageHelper.CallActor(mapInstanceId, new M2U_GetComponent() { UserId = id, type = nameof (User) });
if (getComponent.component == null) return null;
User user = getComponent.component as User;
return user;
}
#region 登录 注册
//用户登陆
public static async ETTask<(Account, string)> Login(this GateUserComponent self, string ip, string dataStr, LoginType loginType)
{
int forbiddenCount = await AccountHelper.GetFirbiddenIpCount(ip);
if (forbiddenCount > 0)
{
return (null, "您的帐号已被停封,请联系客服");
}
Account accountInfo = null;
switch (loginType)
{
case LoginType.Editor:
case LoginType.Tourist:
case LoginType.Voucher://检验凭证
accountInfo = await self.VoucherLogin(dataStr);
break;
default:
Log.Error("不存在的登陆方式:" + loginType);
break;
}
//更新最后登录时间
if (accountInfo != null)
{
accountInfo.LastLoginTime = TimeHelper.ClientNow();
accountInfo.LastIp = ip;
accountInfo.HistoryIpList ??= new List<string>();
if (!accountInfo.HistoryIpList.Contains(ip))
{
accountInfo.HistoryIpList.Add(ip);
}
await DBComponent.Instance.Save(accountInfo);
}
if (accountInfo == null)
return (null, "帐号或者密码错误");
return (accountInfo, string.Empty);
}
//用户注册
public static async ETTask<(Account, string)> Register(this GateUserComponent self, string ip, string dataStr,string identifyStr)
{
int forbiddenCount = await AccountHelper.GetFirbiddenIpCount(ip);
if (forbiddenCount > 0)
{
return (null, "您的帐号已被停封,请联系客服");
}
if (!AccountHelper.CanRegister(identifyStr))
{
return (null, "今日注册数达到上限");
}
string[] userIdPassword = dataStr.Split('|');
if (userIdPassword.Length != 2)
{
return (null, "帐号或者密码错误");
}
Account accountInfo = null;
List<Account> accountInfos = await DBComponent.Instance.Query<Account>(t => t.Username == userIdPassword[0]);
if (accountInfos.Count == 0)
{
accountInfo = await AccountFactory.EditRegisterCreatUser(self.DomainScene(), userIdPassword[0], userIdPassword[1]);
accountInfos.Add(accountInfo);
accountInfo.LastLoginTime = TimeHelper.ClientNow();
accountInfo.CreateIp = identifyStr;
accountInfo.LastIp = ip;
accountInfo.HistoryIpList ??= new List<string>();
if (!accountInfo.HistoryIpList.Contains(ip))
{
accountInfo.HistoryIpList.Add(ip);
}
await DBComponent.Instance.Save(accountInfo);
}
if (accountInfo == null)
return (null, "帐号已被注册");
return (accountInfo, string.Empty);
}
//凭证登陆 就是 userId'|'密码
public static async ETTask<Account> VoucherLogin(this GateUserComponent self, string userIdAndPassword)
{
string[] userIdPassword = userIdAndPassword.Split('|');
if (userIdPassword.Length != 2)
{
return null;
}
try
{
string queryUserId = userIdPassword[0];
List<Account> accountInfos = await DBComponent.Instance.Query<Account>(AccountInfo =>
AccountInfo.Username == queryUserId && AccountInfo.Pwd == userIdPassword[1]);
if (accountInfos.Count > 0)
{
return accountInfos[0];
}
}
catch (Exception e)
{
Log.Error(e);
}
return null;
}
#endregion
public static async ETTask<User> Save(this GateUserComponent self, User player)
{
long mapInstanceId = StartSceneConfigCategory.Instance.GetBySceneName(self.DomainZone(), SceneType.UserCache.ToString()).SceneId;
MessageHelper.SendActor(mapInstanceId, new M2U_WriteComponent() { Id = player.Id, type = nameof (User), component =player });
return player;
}
/// <summary>
/// 玩家上线事件
/// </summary>
/// <param name="gateUserComponent"></param>
/// <param name="userId"></param>
/// <param name="sessionActorId"></param>
/// <returns></returns>
public static async ETTask<User> OnLine(this GateUserComponent self, Session session, long userId, string platform)
{
User user = await self.Query(userId);
if (user == null)
{
return null;
}
Log.Info($"************\n【上线】【{platform}】玩家Id=【{user.Id}】 Name=【{user.NickName}】上线了\n***********");
OnLineComponent.Instance.OnLine(userId, session);
long mapInstanceId = StartSceneConfigCategory.Instance.GetBySceneName(session.DomainZone(), "Map").SceneId;
MessageHelper.SendActor(mapInstanceId, new G2M_UserOnLine() { Id = user.Id});
//记录玩家信息
self.idPlayers[user.Id]=user;
return user;
}
/// <summary>
/// 玩家下线
/// </summary>
/// <param name="self"></param>
/// <param name="playerId"></param>
public static async ETTask OffLine(this GateUserComponent self, User user, Session session)
{
await ETTask.CompletedTask;
if (user != null)
{
Log.Info($"************\n【离线】玩家Id=【{user.Id}】 Name=【{user.NickName}】下线了\n***********");
OnLineComponent.Instance.OffLine(user.Id);
long mapInstanceId = StartSceneConfigCategory.Instance.GetBySceneName(session.DomainZone(), "Map").SceneId;
MessageHelper.SendActor(mapInstanceId, new G2M_UserOffLine { Id =user.Id});
bool ret = self.Remove(user.Id);
if (!ret)
Log.Error($"玩家Id=【{user.Id}】 Name=【{user.NickName}】不在GateUserComponent中");
}
}
}
}