1212121
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
namespace Application.Domain;
|
||||
|
||||
public interface IGameAutoJobService
|
||||
{
|
||||
Task<List<game_job>> GetJobData();
|
||||
Task<game_job> GetJobInfo(string jobId);
|
||||
Task StartJob();
|
||||
Task StopJob(string jobId);
|
||||
Task HandleJob(game_job data);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace Application.Domain;
|
||||
|
||||
public interface IOnHookService
|
||||
{
|
||||
Task<game_onhook> GetUserOnHook(string userId);
|
||||
|
||||
Task<bool> StartOnHook(string userId,string scene, long copper, long exp, string name, string award, decimal score,
|
||||
int time, int onHookTime);
|
||||
|
||||
Task<bool> StopOnHook(string userId);
|
||||
Task HandleHook();
|
||||
}
|
||||
@@ -57,7 +57,7 @@ public interface IUnitUserAttrService
|
||||
#region BUFF状态
|
||||
|
||||
Task<List<unit_user_state>> GetUserStateData(string userId, string scene = "Default");
|
||||
|
||||
Task<decimal> GetUserStateDataTotal(string userId, string scene = "Default");
|
||||
Task<bool> AddUserState(string userId, string groupId, string name, string scene, string tips,
|
||||
List<AttrItem> attr, int time, int count = 1);
|
||||
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
using Photon.Core.Timer;
|
||||
|
||||
namespace Application.Domain;
|
||||
|
||||
public class GameAutoJobService(ISqlSugarClient DbClient, IRedisCache redis,ITimerJobManager jobManager) : IGameAutoJobService, ITransient
|
||||
{
|
||||
public async Task<List<game_job>> GetJobData()
|
||||
{
|
||||
var db = DbClient.AsTenant().GetConnectionWithAttr<game_job>();
|
||||
return await db.Queryable<game_job>().Where(it => it.state == 1).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<game_job> GetJobInfo(string jobId)
|
||||
{
|
||||
string key = string.Format(BaseCache.BaseCacheKey,"JobData");
|
||||
if (await redis.HExistsHashAsync(key, jobId))
|
||||
{
|
||||
return await redis.GetHashAsync<game_job>(key, jobId);
|
||||
}
|
||||
var db = DbClient.AsTenant().GetConnectionWithAttr<game_job>();
|
||||
var data = await db.Queryable<game_job>().Where(it => it.jobId == jobId).SingleAsync();
|
||||
await redis.AddHashAsync(key, jobId, data);
|
||||
return data;
|
||||
}
|
||||
|
||||
public async Task StartJob()
|
||||
{
|
||||
Console.WriteLine(">>>开始执行定时任务>>>");
|
||||
var jobData = await GetJobData();
|
||||
if (jobData.Count > 0)
|
||||
{
|
||||
foreach (var item in jobData)
|
||||
{
|
||||
await jobManager.CreateAndStartTask(item.jobId, item.code, item.cron);
|
||||
Console.WriteLine($"-已启动--->{item.name}");
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("<<<定时任务初始化完成<<<");
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task StopJob(string jobId)
|
||||
{
|
||||
var db = DbClient.AsTenant().GetConnectionWithAttr<game_job>();
|
||||
bool result = await db.Updateable<game_job>().SetColumns(it => it.state == 0).Where(it => it.jobId == jobId)
|
||||
.ExecuteCommandAsync() > 0;
|
||||
if (result)
|
||||
{
|
||||
await jobManager.DeleteTask(jobId);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task HandleJob(game_job data)
|
||||
{
|
||||
if (data.code == nameof(GameEnum.JobCode.OnHook))//挂机
|
||||
{
|
||||
var _eventPublisher = App.GetService<IEventPublisher>();
|
||||
await _eventPublisher.PublishAsync(new ChannelEventSource(BusEventsEnum.BusEventsName.HandleOnHook,
|
||||
data));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Application.Domain;
|
||||
|
||||
public class OnHookService(ISqlSugarClient DbClient, IRedisCache redis) : IOnHookService, ITransient
|
||||
{
|
||||
public async Task<game_onhook> GetUserOnHook(string userId)
|
||||
{
|
||||
string key = string.Format(UserCache.BaseCacheKey, "OnHook");
|
||||
if (await redis.HExistsHashAsync(key, userId))
|
||||
{
|
||||
return await redis.GetHashAsync<game_onhook>(key, userId);
|
||||
}
|
||||
|
||||
var db = DbClient.AsTenant().GetConnectionWithAttr<game_onhook>();
|
||||
var data = await db.Queryable<game_onhook>().Where(it => it.userId == userId).SingleAsync();
|
||||
if (data == null)
|
||||
{
|
||||
data = new game_onhook();
|
||||
data.userId = userId;
|
||||
data.scene = "";
|
||||
data.copper = 0;
|
||||
data.exp = 0;
|
||||
data.status = 0;
|
||||
data.name = "";
|
||||
data.award = "";
|
||||
data.score = 0;
|
||||
data.time = 0;
|
||||
data.uptime = DateTime.Now;
|
||||
data.endTime = DateTime.Now;
|
||||
data.stopTime = TimeExtend.GetTimeStampSeconds;
|
||||
data.sumExp = 0;
|
||||
data.sumCopper = 0;
|
||||
await db.Insertable(data).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
await redis.AddHashAsync(key, userId, data);
|
||||
return data;
|
||||
}
|
||||
|
||||
private async Task ClearOnHookCache(string userId)
|
||||
{
|
||||
string key = string.Format(UserCache.BaseCacheKey, "OnHook");
|
||||
await redis.DelHashAsync(key, userId);
|
||||
}
|
||||
|
||||
public async Task<bool> StartOnHook(string userId, string scene, long copper, long exp, string name, string award,
|
||||
decimal score,
|
||||
int time, int onHookTime)
|
||||
{
|
||||
game_onhook data = new game_onhook();
|
||||
data.userId = userId;
|
||||
data.scene = scene;
|
||||
data.copper = copper;
|
||||
data.exp = exp;
|
||||
data.status = 1;
|
||||
data.name = name;
|
||||
data.award = award;
|
||||
data.score = score;
|
||||
data.time = time;
|
||||
data.uptime = DateTime.Now;
|
||||
DateTime end = DateTime.Now.AddHours(onHookTime);
|
||||
data.endTime = end;
|
||||
data.stopTime = TimeExtend.GetTimeStampBySeconds(end);
|
||||
data.sumExp = 0;
|
||||
data.sumCopper = 0;
|
||||
var db = DbClient.AsTenant().GetConnectionWithAttr<game_onhook>();
|
||||
bool result = await db.Saveable(data).ExecuteCommandAsync() > 0;
|
||||
if (result)
|
||||
{
|
||||
await ClearOnHookCache(userId);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<bool> StopOnHook(string userId)
|
||||
{
|
||||
bool result = true;
|
||||
var onHook = await GetUserOnHook(userId);
|
||||
if (onHook.status == 1)
|
||||
{
|
||||
var db = DbClient.AsTenant().GetConnectionWithAttr<game_onhook>();
|
||||
result = await db.Updateable<game_onhook>().SetColumns(it => it.status == 0)
|
||||
.Where(it => it.userId == userId).ExecuteCommandAsync() > 0;
|
||||
if (result)
|
||||
{
|
||||
await ClearOnHookCache(userId);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private async Task<bool> UpdateOnHook(string userId, long exp, long copper)
|
||||
{
|
||||
var db = DbClient.AsTenant().GetConnectionWithAttr<game_onhook>();
|
||||
var result = await db.Updateable<game_onhook>()
|
||||
.SetColumnsIF(exp > 0, it => it.sumExp == it.sumExp + exp)
|
||||
.SetColumnsIF(copper > 0, it => it.sumCopper == it.sumCopper + copper)
|
||||
.SetColumns(it => it.uptime == DateTime.Now)
|
||||
.Where(it => it.userId == userId).ExecuteCommandAsync() > 0;
|
||||
if (result)
|
||||
{
|
||||
await ClearOnHookCache(userId);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task HandleHook()
|
||||
{
|
||||
long onTime = TimeExtend.GetTimeStampSeconds;
|
||||
var accService = App.GetService<IUnitUserAccService>();
|
||||
var attrService = App.GetService<IUnitUserAttrService>();
|
||||
var db = DbClient.AsTenant().GetConnectionWithAttr<game_onhook>();
|
||||
var data = await db.Queryable<game_onhook>().Where(it => it.status == 1).ToListAsync();
|
||||
foreach (var hook in data)
|
||||
{
|
||||
if (hook.stopTime < onTime)
|
||||
{
|
||||
await StopOnHook(hook.userId);
|
||||
continue;
|
||||
}
|
||||
|
||||
var userAttr = await attrService.GetUserAttrModel(hook.userId, hook.scene);
|
||||
if (userAttr.score < hook.score)
|
||||
{
|
||||
await StopOnHook(hook.userId);
|
||||
continue;
|
||||
}
|
||||
|
||||
int allTime = TimeAssist.TimeDiffSeconds((DateTime)hook.uptime, DateTime.Now) * 1000;
|
||||
int opCount = Convert.ToInt32(allTime / hook.time);
|
||||
long exp = Convert.ToInt64(hook.exp * (1.0m + userAttr.addExp) * opCount);
|
||||
long copper = Convert.ToInt64(hook.copper * (1.0m + userAttr.addGold) * opCount);
|
||||
List<TowerGet> award = new List<TowerGet>();
|
||||
if (!string.IsNullOrEmpty(hook.award))
|
||||
{
|
||||
var awardModel = JsonConvert.DeserializeObject<MonsterAwardModel>(hook.award);
|
||||
if (awardModel.type == nameof(MonsterEnum.AwardCode.Default))
|
||||
{
|
||||
var getAward = GameBus.GetRandomGoods(awardModel.award, opCount, userAttr.burst);
|
||||
award.AddRange(getAward);
|
||||
}
|
||||
}
|
||||
|
||||
if (await UpdateOnHook(hook.userId, exp, copper))
|
||||
{
|
||||
if (exp > 0)
|
||||
{
|
||||
await attrService.UpdateUserExp(hook.userId, exp);
|
||||
}
|
||||
|
||||
if (copper > 0)
|
||||
{
|
||||
await accService.UpdateUserCopper(hook.userId, 1, copper, "挂机获得");
|
||||
}
|
||||
|
||||
if (award.Count > 0)
|
||||
{
|
||||
await GameBus.UpdateBag(hook.userId, 1, award, "挂机获得");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -121,17 +121,17 @@ public class GameChatService : IGameChatService, ITransient
|
||||
chat.sign = sign;
|
||||
chat.state = 1;
|
||||
chat.addTime = DateTime.Now;
|
||||
chat.sort = TimeExtend.GetTimeStampSeconds;
|
||||
chat.sort = TimeExtend.GetTimeStampMilliseconds;
|
||||
chat.delTime = TimeExtend.GetTimeStampBySeconds(DateTime.Now.AddDays(3));
|
||||
chat.opUser = "";
|
||||
switch (code)
|
||||
{
|
||||
case "Region":
|
||||
chat.sort += 300; //五分钟
|
||||
chat.sort += 300 * 1000; //五分钟
|
||||
break;
|
||||
|
||||
case "Dress":
|
||||
chat.sort += 600; //10分钟
|
||||
chat.sort += 600 * 1000; //10分钟
|
||||
break;
|
||||
case "System":
|
||||
chat.delTime = TimeExtend.GetTimeStampBySeconds(DateTime.Now.AddHours(6));
|
||||
|
||||
@@ -102,8 +102,8 @@ public class GameFightService(ISqlSugarClient DbClient, IRedisCache redis) : IGa
|
||||
string scene, string mapId,
|
||||
string userId, long exp = 0, long copper = 0, long petExp = 0, string award = "", string pars = "")
|
||||
{
|
||||
long addTime = TimeExtend.GetTimeStampSeconds;
|
||||
long endTime = addTime + 24 * 60 * 60;
|
||||
long addTime = TimeExtend.GetTimeStampMilliseconds;
|
||||
long endTime = TimeExtend.GetTimeStampSeconds + 24 * 60 * 60;
|
||||
|
||||
List<game_fight_data> fights = new List<game_fight_data>();
|
||||
|
||||
@@ -170,7 +170,7 @@ public class GameFightService(ISqlSugarClient DbClient, IRedisCache redis) : IGa
|
||||
public async Task<bool> SetFightWin(game_fight_data fightData, string winUser,bool setDefMap=true)
|
||||
{
|
||||
bool result = false;
|
||||
long okTime = TimeExtend.GetTimeStampSeconds;
|
||||
long okTime = TimeExtend.GetTimeStampMilliseconds;
|
||||
var db = DbClient.AsTenant().GetConnectionWithAttr<game_fight_data>();
|
||||
if (fightData.code == nameof(GameEnum.FightCode.PVE))
|
||||
{
|
||||
|
||||
@@ -243,6 +243,8 @@ public class GameMapService(ISqlSugarClient DbClient, IRedisCache redis) : IGame
|
||||
else
|
||||
{
|
||||
//更新挂机,结束挂机
|
||||
var hookService = App.GetService<IOnHookService>();
|
||||
await hookService.StopOnHook(userId);
|
||||
}
|
||||
|
||||
if (mapInfo == null)
|
||||
|
||||
@@ -649,6 +649,17 @@ public class UnitUserAttrService(ISqlSugarClient DbClient, IRedisCache redis) :
|
||||
await redis.DelHashAsync(key, userId);
|
||||
}
|
||||
|
||||
public async Task<decimal> GetUserStateDataTotal(string userId, string scene = "Default")
|
||||
{
|
||||
var data = await GetUserStateData(userId, scene);
|
||||
List<AttrItem> result = new List<AttrItem>();
|
||||
foreach (var item in data)
|
||||
{
|
||||
result.AddRange(item.attr);
|
||||
}
|
||||
return GameAttrTool.GetAttrItemValue(nameof(GameEnum.AttrCode.AddExp), result, 0);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 游戏资源储备
|
||||
|
||||
Reference in New Issue
Block a user