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

@@ -0,0 +1,109 @@
using Jaina;
using Photon.Core.Redis;
using Photon.Core.Timer;
namespace Application.Web;
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

@@ -0,0 +1,10 @@
namespace Application.Web;
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

@@ -186,17 +186,22 @@ public class TradeController : ControllerBase
if (info.code == nameof(GameEnum.PropCode.Equ))
{
unit_user_equ equInfo = JsonConvert.DeserializeObject<unit_user_equ>(info.content);
if (await _weightService.CheckUserWeight(userId, (int)equInfo.weight)==false)
{
return PoAction.Message("您的负重不足!");
}
var myAcc = await _accService.GetUserAccInfo(userId, nameof(AccEnum.AccType.copper));
if (myAcc < info.price)
{
return PoAction.Message("您的铜贝不足!");
}
if (await _accService.UpdateUserCopper(userId, 0, (long)info.price, $"交易支付:{info.name}"))
{
if (await _tradeService.UpdateTradeCount(info.tradeId, 0, 1, true))
{
unit_user_equ equInfo = JsonConvert.DeserializeObject<unit_user_equ>(info.content);
equInfo.userId = userId;
await _equService.AddUserEqu(equInfo, "交易获得");
long GetCopper = Convert.ToInt64(info.price * 0.9M);
@@ -220,6 +225,12 @@ public class TradeController : ControllerBase
}
else if (info.code == nameof(GameEnum.PropCode.Goods))
{
var goodsInfo = await _goodsService.GetGoodsInfo(Convert.ToInt32(info.propId));
int needWeight = (int)goodsInfo.weight * count;
if (await _weightService.CheckUserWeight(userId, needWeight)==false)
{
return PoAction.Message("您的负重不足!");
}
var myAcc = await _accService.GetUserAccInfo(userId, nameof(AccEnum.AccType.copper));
var needCopper = Convert.ToInt64(info.price) * count;
if (myAcc < needCopper)

View File

@@ -0,0 +1,24 @@
using Photon.Core.Timer;
using Quartz;
namespace Application.Web;
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

@@ -0,0 +1,69 @@
using Photon.Core.Timer;
using Quartz;
using Quartz.Spi;
namespace Application.Web;
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

@@ -0,0 +1,57 @@
using Photon.Core.Timer;
using Quartz;
namespace Application.Web;
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));
}
}