This commit is contained in:
2026-07-15 23:04:46 +08:00
parent 4ef892e15e
commit c94b3ce200
28 changed files with 562 additions and 10 deletions

View File

@@ -1,10 +0,0 @@
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);
}

View File

@@ -26,6 +26,9 @@ public interface IMessageService
Task<List<unit_user_mail>> GetUserMailData(string userId, int page, int limit, RefAsync<int> total);
Task<unit_user_mail> GetMailInfo(string mailId);
Task<bool> SendMaill(string userId, string name, string sign, List<TowerGet> award, int days = 10);
Task<bool> SendMaill(List<string> userIds, string name, string sign, List<TowerGet> award,
int days = 10);
Task<bool> UpdateMaillInfo(unit_user_mail data);
#endregion

View File

@@ -31,7 +31,7 @@ public interface IUnitUserService
#region
Task<int> GetOnlineCount();
Task<List<unit_user_online>> GetOnlineUserByTime(long time);
#endregion

View File

@@ -1,107 +0,0 @@
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));
}
else if (data.code == nameof(GameEnum.JobCode.UpdateRank)) //更新排名
{
var _eventPublisher = App.GetService<IEventPublisher>();
await _eventPublisher.PublishAsync(new ChannelEventSource(BusEventsEnum.BusEventsName.HandleUpdateRank,
data));
}
else if (data.code == nameof(GameEnum.JobCode.UpdateOnlineTime)) //更新在线时间
{
var _eventPublisher = App.GetService<IEventPublisher>();
await _eventPublisher.PublishAsync(new ChannelEventSource(BusEventsEnum.BusEventsName.HandleOnlineTime,
data));
}
else if (data.code == nameof(GameEnum.JobCode.UpdateCreateMonster)) //更新创建得怪物
{
var _eventPublisher = App.GetService<IEventPublisher>();
await _eventPublisher.PublishAsync(new ChannelEventSource(BusEventsEnum.BusEventsName.HandleCreateMonster,
data));
}
else if (data.code == nameof(GameEnum.JobCode.UpdateFightData)) //更新战斗记录
{
var _eventPublisher = App.GetService<IEventPublisher>();
await _eventPublisher.PublishAsync(new ChannelEventSource(BusEventsEnum.BusEventsName.HandleFightData,
data));
}
else if (data.code == nameof(GameEnum.JobCode.UpdateExchangeData)) //更新兑换类记录
{
var _eventPublisher = App.GetService<IEventPublisher>();
await _eventPublisher.PublishAsync(new ChannelEventSource(BusEventsEnum.BusEventsName.HandleExchangeData,
data));
}
else if (data.code == nameof(GameEnum.JobCode.UpdateTaskLog)) //更新任务日志
{
var _eventPublisher = App.GetService<IEventPublisher>();
await _eventPublisher.PublishAsync(new ChannelEventSource(BusEventsEnum.BusEventsName.HandleTaskLog,
data));
}
else if (data.code == nameof(GameEnum.JobCode.UpdateTaskUser)) //更新角色任务
{
var _eventPublisher = App.GetService<IEventPublisher>();
await _eventPublisher.PublishAsync(new ChannelEventSource(BusEventsEnum.BusEventsName.HandelTaskUser,
data));
}
}
}

View File

@@ -194,7 +194,7 @@ public class MessageService(ISqlSugarClient DbClient, IRedisCache redis) : IMess
return count;
}
private async Task ClearMailCountCache(string userId)
private async Task ClearMailCountCache(params string[] userId)
{
string key = string.Format(UserCache.BaseCacheKeys, "Message", "MailCount");
await redis.DelHashAsync(key, userId);
@@ -240,6 +240,42 @@ public class MessageService(ISqlSugarClient DbClient, IRedisCache redis) : IMess
return result;
}
public async Task<bool> SendMaill(List<string> userIds, string name, string sign, List<TowerGet> award,
int days = 10)
{
List<unit_user_mail> data = new List<unit_user_mail>();
foreach (var userId in userIds)
{
unit_user_mail mail = new unit_user_mail();
mail.mailId = StringAssist.NewGuid;
mail.userId = userId;
mail.name = name;
mail.sign = sign;
mail.isRead = 0;
mail.isGet = award.Count > 0 ? 0 : 1;
mail.award = award;
mail.addTime = DateTime.Now;
mail.endTime = TimeExtend.GetTimeStampBySeconds(DateTime.Now.AddDays(days));
data.Add(mail);
}
if (data.Count > 0)
{
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_mail>();
bool result = await db.Insertable(data).ExecuteCommandAsync() > 0;
if (result)
{
await ClearMailCountCache(userIds.ToArray());
}
return result;
}
else
{
return false;
}
}
public async Task<bool> UpdateMaillInfo(unit_user_mail data)
{
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_mail>();

View File

@@ -322,6 +322,12 @@ public class UnitUserService(ISqlSugarClient DbClient, IRedisCache redis) : IUni
return await db.Queryable<unit_user_online>().Where(it => it.upTime > time).CountAsync();
}
public async Task<List<unit_user_online>> GetOnlineUserByTime(long time)
{
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_online>();
return await db.Queryable<unit_user_online>().Where(it => it.upTime > time).ToListAsync();
}
#endregion
#region

View File

@@ -1,24 +0,0 @@
using Photon.Core.Timer;
using Quartz;
namespace Application.Domain;
public class AutoJob: ITimerAutoJob
{
public async Task Execute(IJobExecutionContext context)
{
string jobId = context.JobDetail.Key.Name;
if (!string.IsNullOrEmpty(jobId))
{
var jobService = App.GetService<IGameAutoJobService>();
var jobData = await jobService.GetJobInfo(jobId);
if (jobData != null)
{
await jobService.HandleJob(jobData);
if (jobData.opType == nameof(GameEnum.JobType.single))
{
await jobService.StopJob(jobId);
}
}
}
}
}

View File

@@ -1,69 +0,0 @@
using Photon.Core.Timer;
using Quartz;
using Quartz.Spi;
namespace Application.Domain;
public class JobStart: ITimerAutoStart
{
private readonly ISchedulerFactory _schedulerFactory;
private readonly IJobFactory _jobFactory;
public JobStart(
ISchedulerFactory schedulerFactory,
IJobFactory jobFactory)
{
_schedulerFactory = schedulerFactory;
_jobFactory = jobFactory;
}
public IScheduler Scheduler { get; private set; }
public async Task StartAsync(CancellationToken cancellationToken)
{
Scheduler = await _schedulerFactory.GetScheduler(cancellationToken);
Scheduler.JobFactory = _jobFactory;
// 创建作业
var job = JobBuilder.Create<AutoJob>()
.WithIdentity("DayHandle", "Handle")
.Build();
// 创建触发器
var trigger = TriggerBuilder.Create()
.WithIdentity($"DayHandle_trigger", "Handle")
.WithCronSchedule("0 0 0 * * ? ")
.Build();
await Scheduler.ScheduleJob(job, trigger, cancellationToken);
await Scheduler.Start(cancellationToken);
Console.WriteLine("----航海时代V3自动程序已启动-----");
}
public async Task Begin()
{
Scheduler = await _schedulerFactory.GetScheduler();
Scheduler.JobFactory = _jobFactory;
// 创建作业
var job = JobBuilder.Create<AutoJob>()
.WithIdentity("111", "test")
.Build();
// 创建触发器
var trigger = TriggerBuilder.Create()
.WithIdentity($"111_trigger", "test")
.WithCronSchedule("0/1 * * * * ? ")
.Build();
await Scheduler.ScheduleJob(job, trigger);
await Scheduler.Start();
Console.WriteLine("----探玩驿站自动程序已启动-----");
}
public async Task StopAsync(CancellationToken cancellationToken)
{
await Scheduler?.Shutdown(cancellationToken);
}
}

View File

@@ -1,57 +0,0 @@
using Photon.Core.Timer;
using Quartz;
namespace Application.Domain;
public class TimerJobManager : ITimerJobManager
{
private readonly IScheduler _scheduler;
public TimerJobManager(IScheduler scheduler)
{
_scheduler = scheduler;
}
public async Task CreateAndStartTask(string jobId, string group, string cronExpression)
{
// 创建作业
var job = JobBuilder.Create<AutoJob>()
.WithIdentity(jobId, group)
.Build();
// 创建触发器
var trigger = TriggerBuilder.Create()
.WithIdentity($"{jobId}_trigger", group)
.WithCronSchedule(cronExpression)
.Build();
// 安排作业
await _scheduler.ScheduleJob(job, trigger);
// 启动调度器(如果尚未启动)
if (!_scheduler.IsStarted)
{
await _scheduler.Start();
}
}
public async Task PauseTask(string jobId)
{
await _scheduler.PauseJob(new JobKey(jobId, "default"));
}
public async Task ResumeTask(string jobId, string group)
{
await _scheduler.ResumeJob(new JobKey(jobId, group));
}
public async Task DeleteTask(string jobId, string group)
{
await _scheduler.DeleteJob(new JobKey(jobId, group));
}
public async Task DeleteTask(string jobId)
{
await _scheduler.DeleteJob(new JobKey(jobId));
}
}