369 lines
12 KiB
C#
369 lines
12 KiB
C#
using Photon.Core.Assist;
|
|
|
|
namespace Application.Domain;
|
|
|
|
public class UnitUserService(ISqlSugarClient DbClient, IRedisCache redis) : IUnitUserService, ITransient
|
|
{
|
|
|
|
#region 用户信息
|
|
|
|
public async Task<List<unit_user>> GetUserDataByAccId(string accId)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user>();
|
|
return await db.Queryable<unit_user>().Where(it => it.accId == accId).ToListAsync();
|
|
}
|
|
|
|
public async Task<unit_user> GetUserInfoByUserId(string userId)
|
|
{
|
|
string key = string.Format(UserCache.BaseCacheKeys, "UserInfo", "UserId");
|
|
var data = await redis.GetHashAsync<unit_user>(key, userId);
|
|
if (data == null)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user>();
|
|
data = await db.Queryable<unit_user>().Where(it => it.userId == userId).SingleAsync();
|
|
if (data != null)
|
|
{
|
|
await redis.AddHashAsync(key, userId, data);
|
|
}
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
public async Task<unit_user> GetUserInfoByUserNo(string userNo)
|
|
{
|
|
string key = string.Format(UserCache.BaseCacheKeys, "UserInfo", "UserNo");
|
|
var data = await redis.GetHashAsync<unit_user>(key, userNo);
|
|
if (data == null)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user>();
|
|
data = await db.Queryable<unit_user>().Where(it => it.userNo == userNo).SingleAsync();
|
|
if (data != null)
|
|
{
|
|
await redis.AddHashAsync(key, userNo, data);
|
|
}
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
public async Task<unit_user> GetUserInfoByToken(string token)
|
|
{
|
|
string key = string.Format(UserCache.BaseCacheKeys, "UserInfo", "token");
|
|
var data = await redis.GetHashAsync<unit_user>(key, token);
|
|
if (data == null)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user>();
|
|
data = await db.Queryable<unit_user>().Where(it => it.token == token).SingleAsync();
|
|
if (data != null)
|
|
{
|
|
await redis.AddHashAsync(key, token, data);
|
|
}
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
private async Task ClearUserInfo(int type, string id)
|
|
{
|
|
unit_user result = new unit_user();
|
|
switch (type)
|
|
{
|
|
case 0:
|
|
result = await GetUserInfoByUserId(id);
|
|
break;
|
|
|
|
case 1:
|
|
result = await GetUserInfoByToken(id);
|
|
break;
|
|
}
|
|
|
|
await redis.DelHashAsync(string.Format(UserCache.BaseCacheKeys, "UserInfo", "UserId"), result.userId);
|
|
await redis.DelHashAsync(string.Format(UserCache.BaseCacheKeys, "UserInfo", "UserNo"), result.userNo);
|
|
await redis.DelHashAsync(string.Format(UserCache.BaseCacheKeys, "UserInfo", "Sid"), result.token);
|
|
}
|
|
|
|
public async Task<bool> CheckUserNickIsAt(string nick)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user>();
|
|
return await db.Queryable<unit_user>().Where(it => it.nick.Contains(nick)).AnyAsync();
|
|
}
|
|
|
|
public async Task<bool> UpdateUserNick(string userId, string nick)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user>();
|
|
bool result = await db.Updateable<unit_user>().SetColumns(it => it.nick == nick)
|
|
.Where(it => it.userId == userId).ExecuteCommandAsync() > 0;
|
|
if (result)
|
|
{
|
|
await ClearUserInfo(0, userId);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
public async Task<bool> UpdateUserSex(string userId, string sex)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user>();
|
|
bool result = await db.Updateable<unit_user>().SetColumns(it => it.sex == sex)
|
|
.Where(it => it.userId == userId).ExecuteCommandAsync() > 0;
|
|
if (result)
|
|
{
|
|
await ClearUserInfo(0, userId);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
public async Task<bool> UpdateUserSign(string userId, string sign)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user>();
|
|
bool result = await db.Updateable<unit_user>().SetColumns(it => it.sign == sign)
|
|
.Where(it => it.userId == userId).ExecuteCommandAsync() > 0;
|
|
if (result)
|
|
{
|
|
await ClearUserInfo(0, userId);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 用户注册
|
|
|
|
public async Task<unit_user> Register(int areaId, string accId)
|
|
{
|
|
unit_user result = new unit_user();
|
|
try
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user>();
|
|
await DbClient.AsTenant().BeginTranAsync();
|
|
string userId = StringAssist.NewGuid;
|
|
result.userId = userId;
|
|
result.areaId = areaId;
|
|
result.accId = accId;
|
|
result.nick = "四海虾米";
|
|
result.headImg = "";
|
|
int no = await GetUserNo();
|
|
result.userNo = no.ToString();
|
|
string token = await GetToken();
|
|
result.token = token;
|
|
result.sign = "这个小家伙儿很懒,什么也没留下.";
|
|
result.status = 1;
|
|
result.regOk = 0;
|
|
result.isSystem = 0;
|
|
result.addTime = DateTime.Now;
|
|
bool isok = db.Insertable(result).ExecuteCommand() > 0;
|
|
if (!isok)
|
|
{
|
|
result = null;
|
|
}
|
|
|
|
await DbClient.AsTenant().CommitTranAsync();
|
|
}
|
|
catch
|
|
{
|
|
result = null;
|
|
await DbClient.AsTenant().RollbackTranAsync();
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<bool> RegisterUserInfo(string userId, string nick, string sex)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user>();
|
|
bool result = await db.Updateable<unit_user>().SetColumns(it => it.nick == nick)
|
|
.SetColumns(it => it.sex == sex)
|
|
.SetColumns(it => it.regOk == 1)
|
|
.Where(it => it.userId == userId).ExecuteCommandAsync() > 0;
|
|
if (result)
|
|
{
|
|
await ClearUserInfo(0, userId); //清理个人信息表缓存
|
|
|
|
//注册账户
|
|
unit_user_acc acc = new unit_user_acc();
|
|
acc.userId = userId;
|
|
acc.cowry = 0;
|
|
acc.gold = 0;
|
|
acc.teach = 0;
|
|
acc.renown = 0;
|
|
db.Insertable(acc).AddQueue();
|
|
//注册游戏货币
|
|
unit_user_copper copper = new unit_user_copper();
|
|
copper.userId = userId;
|
|
copper.copper = 0;
|
|
db.Insertable(copper).AddQueue();
|
|
|
|
//注册个人基础属性
|
|
unit_user_attr userAttr = GameTool.GetAttrData(1);
|
|
userAttr.userId = userId;
|
|
db.Insertable(userAttr).AddQueue();
|
|
|
|
//注册配置
|
|
unit_user_config config = new unit_user_config();
|
|
config.userId = userId;
|
|
config.friendRole = 1;
|
|
config.chatRole = 0;
|
|
config.autoBag = -1;
|
|
config.autoDrug = -1;
|
|
db.Insertable(config).AddQueue();
|
|
|
|
//注册个人血量
|
|
unit_user_blood blood = new unit_user_blood();
|
|
blood.userId = userId;
|
|
blood.blood = userAttr.upBlood;
|
|
db.Insertable(blood).AddQueue();
|
|
//注册士气
|
|
unit_user_morale morale = new unit_user_morale();
|
|
morale.userId = userId;
|
|
morale.morale = userAttr.upMorale;
|
|
db.Insertable(morale).AddQueue();
|
|
|
|
//注册等级经验
|
|
unit_user_exp exp = new unit_user_exp();
|
|
exp.userId = userId;
|
|
exp.exp = 0;
|
|
exp.upExp = GameTool.GetUserUpExp(1);
|
|
db.Insertable(exp).AddQueue();
|
|
|
|
//注册负重
|
|
unit_user_weight weight = new unit_user_weight();
|
|
weight.userId = userId;
|
|
weight.onWeight = 0;
|
|
weight.maxWeight = 100;
|
|
db.Insertable(weight).AddQueue();
|
|
//注册活力
|
|
unit_user_vigour vitality = new unit_user_vigour();
|
|
vitality.userId = userId;
|
|
vitality.vitality = GameConfig.GameBaseVigour;
|
|
vitality.upVitality = GameConfig.GameBaseVigour;
|
|
vitality.upTime = TimeAssist.GetDateTimeYMDString(0);
|
|
db.Insertable(vitality).AddQueue();
|
|
|
|
//注册在线
|
|
unit_user_online online = new unit_user_online();
|
|
online.userId = userId;
|
|
online.ip = "";
|
|
online.mapId = "16_27";
|
|
online.upTime = TimeAssist.GetTimeStampNum;
|
|
db.Insertable(online).AddQueue();
|
|
|
|
await db.SaveQueuesAsync(false);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
private async Task<int> GetUserNo()
|
|
{
|
|
int No = 0;
|
|
bool ok = true;
|
|
while (ok)
|
|
{
|
|
No = RandomAssist.GetFormatedNumeric(11012585, 97521695);
|
|
if (await GetUserInfoByUserNo(No.ToString()) == null)
|
|
{
|
|
ok = false;
|
|
}
|
|
}
|
|
|
|
return No;
|
|
}
|
|
|
|
private async Task<string> GetToken()
|
|
{
|
|
string token = string.Empty;
|
|
bool ok = true;
|
|
while (ok)
|
|
{
|
|
token = StringAssist.RandomString(32);
|
|
if (await GetUserInfoByToken(token) == null)
|
|
{
|
|
ok = false;
|
|
}
|
|
}
|
|
|
|
return token;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 其他
|
|
|
|
public async Task<int> GetOnlineCount()
|
|
{
|
|
long time = TimeExtend.GetTimeStampBySeconds(DateTime.Now.AddMinutes(0 - GameConfig.OnLineTime));
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_online>();
|
|
return await db.Queryable<unit_user_online>().Where(it => it.upTime > time).CountAsync();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 权限配置
|
|
|
|
public async Task<unit_user_config> GetUserConfigInfo(string userId)
|
|
{
|
|
string key = string.Format(UserCache.BaseCacheKeys, "UserInfo", "Config");
|
|
var data = await redis.GetHashAsync<unit_user_config>(key, userId);
|
|
if (data == null)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_config>();
|
|
data = await db.Queryable<unit_user_config>().Where(it => it.userId == userId).SingleAsync();
|
|
if (data != null)
|
|
{
|
|
await redis.AddHashAsync(key, userId, data);
|
|
}
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
public async Task<int> GetUserConfigInfo(string userId, string code)
|
|
{
|
|
int result = 0;
|
|
var config = await GetUserConfigInfo(userId);
|
|
switch (code)
|
|
{
|
|
case "FriendRole":
|
|
result = (int)config.friendRole;
|
|
break;
|
|
case "ChatRole":
|
|
result = (int)config.chatRole;
|
|
break;
|
|
case "AutoBag":
|
|
result = (int)config.autoBag;
|
|
break;
|
|
case "AutoDrug":
|
|
result = (int)config.autoDrug;
|
|
break;
|
|
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private async Task ClearUserConfig(string userId)
|
|
{
|
|
string key = string.Format(UserCache.BaseCacheKeys, "UserInfo", "Config");
|
|
await redis.DelHashAsync(key, userId);
|
|
}
|
|
|
|
public async Task<bool> UpdateUserConfigInfo(string userId, string code, int state)
|
|
{
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_config>();
|
|
bool result = await db.Updateable<unit_user_config>()
|
|
.SetColumnsIF(code == "FriendRole", it => it.friendRole == state)
|
|
.SetColumnsIF(code == "ChatRole", it => it.chatRole == state)
|
|
.SetColumnsIF(code == "AutoBag", it => it.autoBag == state)
|
|
.SetColumnsIF(code == "AutoDrug", it => it.autoDrug == state)
|
|
.Where(it => it.userId == userId).ExecuteCommandAsync() > 0;
|
|
if (result)
|
|
{
|
|
await ClearUserConfig(userId);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
#endregion
|
|
} |