183 lines
6.5 KiB
C#
183 lines
6.5 KiB
C#
using Microsoft.VisualBasic.CompilerServices;
|
|
|
|
namespace Application.Domain;
|
|
|
|
public class GameMonsterService(ISqlSugarClient DbClient, IRedisCache redis) : IGameMonsterService, ITransient
|
|
{
|
|
#region 基础资源库
|
|
|
|
public async Task<game_monster> GetMonsterInfo(string monsterId)
|
|
{
|
|
string key = string.Format(BaseCache.BaseCacheKeys, "MonsterData", "MonsterInfo");
|
|
if (await redis.HExistsHashAsync(key, monsterId))
|
|
{
|
|
return await redis.GetHashAsync<game_monster>(key, monsterId);
|
|
}
|
|
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<game_monster>();
|
|
var data = await db.Queryable<game_monster>().Where(it => it.monsterId == monsterId).SingleAsync();
|
|
await redis.AddHashAsync(key, monsterId, data);
|
|
return data;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 地图怪物
|
|
|
|
public async Task<List<game_monster_map>> GetMonsterDataByMap(string mapId)
|
|
{
|
|
string key = string.Format(BaseCache.BaseCacheKeys, "MonsterData", "MonsterOnMap");
|
|
if (await redis.HExistsHashAsync(key, mapId))
|
|
{
|
|
return await redis.GetHashAsync<List<game_monster_map>>(key, mapId);
|
|
}
|
|
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<game_monster_map>();
|
|
var data = await db.Queryable<game_monster_map>().Where(it => it.mapId == mapId).ToListAsync();
|
|
await redis.AddHashAsync(key, mapId, data);
|
|
return data;
|
|
}
|
|
|
|
public async Task<bool> CreateMonsterToMap(string userId, string mapId, string areaCode, string monsterId,
|
|
int count = 1, int time = 0, string par = "")
|
|
{
|
|
bool result = false;
|
|
var monsterInfo = await GetMonsterInfo(monsterId);
|
|
if (monsterInfo != null)
|
|
{
|
|
time = time <= 0 ? 7 * 24 * 60 : time;
|
|
long onTime = TimeExtend.GetTimeStampSeconds;
|
|
long endTime = onTime + time * 60;
|
|
List<unit_user_monster> data = new List<unit_user_monster>();
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
data.Add(new unit_user_monster()
|
|
{
|
|
umId = StringAssist.NewGuid,
|
|
userId = userId,
|
|
areaCode = areaCode,
|
|
mapId = mapId,
|
|
monsterId = monsterId,
|
|
monsterName = monsterInfo.name,
|
|
code = monsterInfo.code,
|
|
par = par,
|
|
addTime = onTime,
|
|
endTime = endTime
|
|
});
|
|
}
|
|
|
|
if (data.Count > 0)
|
|
{
|
|
var createDb = DbClient.AsTenant().GetConnectionWithAttr<unit_user_monster>();
|
|
await createDb.Insertable(data).ExecuteCommandAsync();
|
|
}
|
|
|
|
result = true;
|
|
}
|
|
|
|
if (result)
|
|
{
|
|
await ClearCreateMonster(mapId);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
private async Task<List<unit_user_monster>> GetCreateMonsterByMapId(string mapId)
|
|
{
|
|
string key = string.Format(BaseCache.BaseCacheKeys, "MonsterData", "CreateMonsterOnMap");
|
|
if (await redis.HExistsHashAsync(key, mapId))
|
|
{
|
|
return await redis.GetHashAsync<List<unit_user_monster>>(key, mapId);
|
|
}
|
|
|
|
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_monster>();
|
|
var data = await db.Queryable<unit_user_monster>().Where(it => it.mapId == mapId).ToListAsync();
|
|
await redis.AddHashAsync(key, mapId, data);
|
|
return data;
|
|
}
|
|
|
|
private async Task ClearCreateMonster(string mapId)
|
|
{
|
|
string key = string.Format(BaseCache.BaseCacheKeys, "MonsterData", "CreateMonsterOnMap");
|
|
await redis.DelHashAsync(key, mapId);
|
|
}
|
|
|
|
public async Task<List<MapMonsterModel>> GetMapMonster(string userId, string mapId, string teamId)
|
|
{
|
|
List<MapMonsterModel> result = new List<MapMonsterModel>();
|
|
var mapMonster = await GetMonsterDataByMap(mapId);
|
|
if (mapMonster.Count > 0)
|
|
{
|
|
int random = 0; //此处需要判断用户状态中的几率
|
|
int onTime = TimeAssist.GetHourNum;
|
|
foreach (var item in mapMonster)
|
|
{
|
|
if (item.sTime < onTime && item.eTime > onTime)
|
|
{
|
|
if (RandomAssist.CheakRandom((int)item.chance + random))
|
|
{
|
|
for (int i = 0; i < item.addCount; i++)
|
|
{
|
|
MapMonsterModel temp = new MapMonsterModel()
|
|
{
|
|
monterId = item.mmId,
|
|
name = item.monsterName,
|
|
type = 0
|
|
};
|
|
result.Add(temp);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//此处追加个人生成怪物
|
|
List<unit_user_monster> createData = new List<unit_user_monster>();
|
|
var createMonster = await GetCreateMonsterByMapId(mapId);
|
|
if (createMonster.Count > 0)
|
|
{
|
|
long onTime = TimeExtend.GetTimeStampSeconds;
|
|
createMonster = createMonster.FindAll(it => it.endTime > onTime);
|
|
|
|
createData.AddRange(createMonster.FindAll(it =>
|
|
it.userId == userId || it.areaCode == nameof(MonsterEnum.AreaCode.Public)));
|
|
if (!string.IsNullOrEmpty(teamId) && teamId != "0")
|
|
{
|
|
var teamService = App.GetService<IGameTeamService>();
|
|
var teamMember = await teamService.GetTeamMember(teamId);
|
|
teamMember = teamMember.FindAll(it => it.userId != userId);
|
|
var onCreate = createMonster.FindAll(it =>
|
|
teamMember.Any(team =>
|
|
team.userId == it.userId && it.areaCode == nameof(MonsterEnum.AreaCode.Team)));
|
|
foreach (var item in onCreate)
|
|
{
|
|
if (createData.Any(it => it.umId == item.umId))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
createData.Add(item);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (createData.Count > 0)
|
|
{
|
|
foreach (var item in createData)
|
|
{
|
|
MapMonsterModel temp = new MapMonsterModel()
|
|
{
|
|
monterId = item.umId,
|
|
name = item.monsterName,
|
|
type = 1
|
|
};
|
|
result.Add(temp);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
#endregion
|
|
} |