1212121
This commit is contained in:
25
Service/Application.Web/Common/AppBackgroundService.cs
Normal file
25
Service/Application.Web/Common/AppBackgroundService.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
namespace Application.Web;
|
||||
|
||||
public class AppBackgroundService:BackgroundService
|
||||
{
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.WriteLine("应用启动完成,开始执行初始化后台任务");
|
||||
|
||||
// ========== 你要执行的启动方法写在这里 ==========
|
||||
var jobService = App.GetService<IGameAutoJobService>();
|
||||
await jobService.StartJob();
|
||||
// ==========================================
|
||||
Console.WriteLine("启动初始化任务执行完成");
|
||||
|
||||
// 如果只需要执行一次,直接退出;如果是常驻定时任务就写while循环
|
||||
return;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("启动后台任务执行异常");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,11 +23,13 @@ public class MapController : ControllerBase
|
||||
private readonly IGameTeamService _teamService;
|
||||
private readonly IGameMonsterService _monsterService;
|
||||
private readonly IGameFightService _fightService;
|
||||
private readonly IOnHookService _hookService;
|
||||
|
||||
public MapController(IUnitUserService userService, IGameMapService mapService, IGameChatService chatService,
|
||||
IUnitUserAttrService attrService, IMessageService messageService, IUnitUserWeight weightService,
|
||||
IUnitUserAccService accService, IGameSkillService skillService, IGameGoodsService goodsService,
|
||||
IGameTeamService teamService, IGameMonsterService monsterService, IGameFightService fightService)
|
||||
IGameTeamService teamService, IGameMonsterService monsterService, IGameFightService fightService,
|
||||
IOnHookService hookService)
|
||||
{
|
||||
_userService = userService;
|
||||
_mapService = mapService;
|
||||
@@ -41,6 +43,7 @@ public class MapController : ControllerBase
|
||||
_teamService = teamService;
|
||||
_monsterService = monsterService;
|
||||
_fightService = fightService;
|
||||
_hookService = hookService;
|
||||
}
|
||||
|
||||
#region 地图相关
|
||||
@@ -150,6 +153,10 @@ public class MapController : ControllerBase
|
||||
var mapGoods = await _fightService.GetMapFightGoods(mapInfo.mapId);
|
||||
mapGoods = mapGoods.FindAll(it => it.userId == "0" || it.userId == userId).Take(3).ToList();
|
||||
|
||||
int isHook = 0; //挂机处理
|
||||
var onHook = await _hookService.GetUserOnHook(userId);
|
||||
isHook = (int)onHook.status;
|
||||
|
||||
object ret = new
|
||||
{
|
||||
mapInfo,
|
||||
@@ -165,9 +172,11 @@ public class MapController : ControllerBase
|
||||
broadcast,
|
||||
monster = monsterData,
|
||||
fightId,
|
||||
gameTips
|
||||
gameTips,
|
||||
isHook
|
||||
};
|
||||
|
||||
|
||||
return PoAction.Ok(ret);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,10 +18,11 @@ public class FightController : ControllerBase
|
||||
private readonly IUnitUserAccService _accService;
|
||||
private readonly IGameGoodsService _goodsService;
|
||||
private readonly IUnitUserService _userService;
|
||||
private readonly IOnHookService _hookService;
|
||||
|
||||
public FightController(IGameFightService fightService, IGameMonsterService monsterService,
|
||||
IUnitUserAttrService attrService, IGameMapService mapService, IUnitUserAccService accService,
|
||||
IGameGoodsService goodsService, IUnitUserService userService)
|
||||
IGameGoodsService goodsService, IUnitUserService userService,IOnHookService hookService)
|
||||
{
|
||||
_fightService = fightService;
|
||||
_monsterService = monsterService;
|
||||
@@ -30,6 +31,7 @@ public class FightController : ControllerBase
|
||||
_accService = accService;
|
||||
_goodsService = goodsService;
|
||||
_userService = userService;
|
||||
_hookService = hookService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -99,6 +101,7 @@ public class FightController : ControllerBase
|
||||
copper, petExp, award, pars);
|
||||
if (result)
|
||||
{
|
||||
await _hookService.StopOnHook(userId);
|
||||
return PoAction.Ok(fightId);
|
||||
}
|
||||
else
|
||||
@@ -398,8 +401,6 @@ public class FightController : ControllerBase
|
||||
public async Task<IPoAction> GetFightMsg(string fightId)
|
||||
{
|
||||
string userId = StateHelper.userId;
|
||||
var myFight = await _fightService.GetUserFight(userId);
|
||||
|
||||
var fight = await _fightService.GetFightInfo(fightId);
|
||||
if (fight == null)
|
||||
{
|
||||
@@ -430,6 +431,7 @@ public class FightController : ControllerBase
|
||||
isWin = 2;
|
||||
}
|
||||
|
||||
int isCopy = 0;
|
||||
string otName = string.Empty;
|
||||
if (isWin == 0)
|
||||
{
|
||||
@@ -455,6 +457,7 @@ public class FightController : ControllerBase
|
||||
{
|
||||
var monsterInfo = await _monsterService.GetMonsterInfo(fight.mainId);
|
||||
otName = monsterInfo.name;
|
||||
isCopy = (int)monsterInfo.isCopy;
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -474,6 +477,104 @@ public class FightController : ControllerBase
|
||||
var myAttr = await _attrService.GetUserAttrModel(userId, fight.scene);
|
||||
|
||||
|
||||
return PoAction.Ok(new { isWin, otName, blood = myAttr.blood, upBlood = myAttr.upBlood, fight });
|
||||
return PoAction.Ok(new { isWin, otName, blood = myAttr.blood, upBlood = myAttr.upBlood, fight, isCopy });
|
||||
}
|
||||
|
||||
#region 挂机相关
|
||||
|
||||
/// <summary>
|
||||
/// 个人挂机信息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IPoAction> GetUserHook()
|
||||
{
|
||||
string userId = StateHelper.userId;
|
||||
var onHook = await _hookService.GetUserOnHook(userId);
|
||||
decimal expAdd = 0;
|
||||
decimal copperAdd = 0;
|
||||
if (onHook.status == 1)
|
||||
{
|
||||
var myAttr = await _attrService.GetUserAttrModel(userId, onHook.scene);
|
||||
expAdd = myAttr.addExp;
|
||||
copperAdd = myAttr.addGold;
|
||||
onHook.exp = Convert.ToInt32(onHook.exp * (1.0M + expAdd));
|
||||
onHook.copper = Convert.ToInt32(onHook.copper * (1.0M + copperAdd));
|
||||
}
|
||||
|
||||
return PoAction.Ok(new { data = onHook, expAdd, copperAdd });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 挂机
|
||||
/// </summary>
|
||||
/// <param name="fightId"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IPoAction> OnHook(string fightId)
|
||||
{
|
||||
string userId = StateHelper.userId;
|
||||
var fight = await _fightService.GetFightInfo(fightId);
|
||||
if (fight == null)
|
||||
{
|
||||
return PoAction.Message("战斗不存在!");
|
||||
}
|
||||
|
||||
if (fight.userId != userId)
|
||||
{
|
||||
return PoAction.Message("战斗不存在!");
|
||||
}
|
||||
|
||||
if (fight.code != nameof(GameEnum.FightCode.PVE))
|
||||
{
|
||||
return PoAction.Message("该战斗不可复刻挂机!");
|
||||
}
|
||||
|
||||
var monster = await _monsterService.GetMonsterInfo(fight.mainId);
|
||||
if (monster == null)
|
||||
{
|
||||
return PoAction.Message("该战斗不可复刻挂机!");
|
||||
}
|
||||
|
||||
if (monster.isCopy == 0)
|
||||
{
|
||||
return PoAction.Message("该战斗不可复刻挂机!");
|
||||
}
|
||||
|
||||
var myAttr = await _attrService.GetUserAttrModel(userId, fight.scene);
|
||||
int diffTime = Convert.ToInt32(fight.okTime - fight.addTime);
|
||||
decimal score = myAttr.score * 0.7M;
|
||||
bool result = await _hookService.StartOnHook(userId, fight.scene, (long)monster.copper, (long)monster.exp,
|
||||
monster.name,
|
||||
monster.award, score, diffTime, 6);
|
||||
if (result)
|
||||
{
|
||||
return PoAction.Ok("挂机成功!");
|
||||
}
|
||||
else
|
||||
{
|
||||
return PoAction.Message("挂机失败,请稍后尝试!");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 结束挂机
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IPoAction> StopOnHook()
|
||||
{
|
||||
string userId = StateHelper.userId;
|
||||
bool result = await _hookService.StopOnHook(userId);
|
||||
if (result)
|
||||
{
|
||||
return PoAction.Ok(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
return PoAction.Message("操作失败,请稍后尝试!");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -130,6 +130,21 @@ public class UserController : ControllerBase
|
||||
return PoAction.Ok(model);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取个人特性
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IPoAction> GetUserBaseAttrData()
|
||||
{
|
||||
string userId = StateHelper.userId;
|
||||
var model = await _attrService.GetUserAttrModel(userId);
|
||||
|
||||
var unitExp = await _attrService.GetUserStateDataTotal(userId, nameof(MapEnum.MapCode.DEF_MAP));
|
||||
var dupExp = await _attrService.GetUserStateDataTotal(userId, nameof(MapEnum.MapCode.Dup_Map));
|
||||
return PoAction.Ok(new { data = model, unitExp, dupExp });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取用户资料
|
||||
/// </summary>
|
||||
@@ -405,7 +420,7 @@ public class UserController : ControllerBase
|
||||
temp.id = StringAssist.NewGuid;
|
||||
temp.goodsId = goodsId;
|
||||
temp.name = myGoods.goodsName;
|
||||
temp.code =Convert.ToString(_drugConfig.cls);
|
||||
temp.code = Convert.ToString(_drugConfig.cls);
|
||||
temp.count = count;
|
||||
myDrug.drug.Add(temp);
|
||||
}
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
using Quartz;
|
||||
namespace Application.Web;
|
||||
|
||||
public class AutoJob: ITimerAutoJob
|
||||
{
|
||||
public async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
string jobId = context.JobDetail.Key.Name;
|
||||
if (jobId == "DayHandle")
|
||||
{
|
||||
|
||||
////处理日红包
|
||||
//await businessService.HandleRedPack();
|
||||
//Console.WriteLine($"{DateTime.Now.ToString("yyyy-MMMM-dd")}:红包奖金池已处理!");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("定时程序测试执行");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Photon.Core.Redis;
|
||||
using Photon.Core.Timer;
|
||||
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
@@ -45,14 +46,19 @@ builder.Services.InjectJwt(builder.Configuration, new OpenApiInfo
|
||||
}, groups);
|
||||
|
||||
#endregion
|
||||
|
||||
//定时功能
|
||||
builder.Services.InjectTimer(services =>
|
||||
//自动程序
|
||||
string autoConfig = builder.Configuration["AutoProgram"]!;
|
||||
if (autoConfig == "1")
|
||||
{
|
||||
services.AddTransient<ITimerJobManager, TimerJobManager>();//注册管理器
|
||||
services.AddSingleton<AutoJob>();
|
||||
services.AddHostedService<JobStart>();
|
||||
});
|
||||
//定时功能
|
||||
builder.Services.InjectTimer(services =>
|
||||
{
|
||||
services.AddTransient<ITimerJobManager, TimerJobManager>();//注册管理器
|
||||
services.AddSingleton<AutoJob>();
|
||||
});
|
||||
builder.Services.AddHostedService<AppBackgroundService>();
|
||||
}
|
||||
|
||||
|
||||
//日志
|
||||
builder.Logging.InjectLog();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"AutoProgram": 0,
|
||||
"AutoProgram": 1,
|
||||
"Redis": {
|
||||
"connection": "127.0.0.1:6379,password=,defaultdatabase=5"
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user