Added RaidTemplates and TimerService to create weekly raids

This commit is contained in:
t.ruspekhofer 2022-03-12 21:16:45 +01:00
parent c215ed058f
commit ab6602710d
13 changed files with 270 additions and 53 deletions

View file

@ -106,6 +106,31 @@ namespace Lieb.Data
context.Raids.Add(raid);
context.SaveChanges();
DateTime templateStartTime = DateTime.UtcNow.AddDays(-7).AddMinutes(-117);
RaidTemplate template = new RaidTemplate()
{
Title = "Testraid",
Description = "This is a test raid\nwith multiple lines?",
Guild = "LIEB",
Organizer = "Sarah",
RaidType = RaidType.RandomWithBoons,
StartTime = templateStartTime,
EndTime = templateStartTime.AddHours(2),
FreeForAllTime = templateStartTime.AddHours(-2),
VoiceChat = "ts.lieb.games",
Frequency = 7,
CreateDaysBefore = 7,
TimeZone = "Europe/Vienna",
Roles = new[] { new PlannedRaidRole(){
Description = "WupWup",
Name = "Ups",
Spots = 10
}
}
};
context.RaidTemplates.Add(template);
context.SaveChanges();
var signUps = new RaidSignUp[]
{
new RaidSignUp{GuildWars2AccountId = linaith.GuildWars2AccountId, LiebUserId = users[0].LiebUserId, PlannedRaidRoleId = ele.PlannedRaidRoleId, RaidId = raid.RaidId, SignUpType = SignUpType.SignedUp },

View file

@ -21,6 +21,7 @@ namespace Lieb.Data
public DbSet<GuildWars2Build> GuildWars2Builds { get; set; }
public DbSet<PlannedRaidRole> PlannedRaidRoles { get; set; }
public DbSet<Raid> Raids { get; set; }
public DbSet<RaidTemplate> RaidTemplates { get; set; }
public DbSet<RaidReminder> RaidReminders { get; set; }
public DbSet<RaidSignUp> RaidSignUps { get; set; }
public DbSet<SignUpHistory> SignUpHistories { get; set; }
@ -36,6 +37,7 @@ namespace Lieb.Data
modelBuilder.Entity<GuildWars2Build>().ToTable("GuildWars2Build");
modelBuilder.Entity<PlannedRaidRole>().ToTable("PlannedRaidRole");
modelBuilder.Entity<Raid>().ToTable("Raid");
modelBuilder.Entity<RaidTemplate>().ToTable("RaidTemplate");
modelBuilder.Entity<RaidReminder>().ToTable("RaidReminder");
modelBuilder.Entity<RaidSignUp>().ToTable("RaidSignUp");
modelBuilder.Entity<SignUpHistory>().ToTable("SignUpHistory");

View file

@ -67,12 +67,10 @@ namespace Lieb.Data
raidToChange.Description = raid.Description;
raidToChange.StartTimeUTC = raid.StartTimeUTC;
raidToChange.EndTimeUTC = raid.EndTimeUTC;
raidToChange.TimeZone = raid.TimeZone;
raidToChange.Organizer = raid.Organizer;
raidToChange.Guild = raid.Guild;
raidToChange.VoiceChat = raid.VoiceChat;
raidToChange.RaidType = raid.RaidType;
raidToChange.Frequency = raid.Frequency;
raidToChange.RequiredRole = raid.RequiredRole;
raidToChange.FreeForAllTimeUTC = raid.FreeForAllTimeUTC;
raidToChange.DiscordMessageId = raid.DiscordMessageId;
@ -337,5 +335,22 @@ namespace Lieb.Data
return true;
}
public void SendReminders()
{
using var context = _contextFactory.CreateDbContext();
List<Raid> raids = context.Raids
.Include(r => r.Reminders)
.Where(raid => raid.Reminders.Where(reminder => !reminder.Sent && raid.StartTimeUTC.AddHours(-reminder.HoursBeforeRaid) < DateTime.UtcNow).Any())
.ToList();
foreach(Raid raid in raids)
{
foreach(RaidReminder reminder in raid.Reminders.Where(reminder => !reminder.Sent && raid.StartTimeUTC.AddHours(-reminder.HoursBeforeRaid) < DateTime.UtcNow))
{
//TODO send reminders -> this is a Discord Problem...
}
}
}
}
}

View file

@ -0,0 +1,49 @@
using Lieb.Models.GuildWars2.Raid;
using Microsoft.EntityFrameworkCore;
namespace Lieb.Data
{
public class RaidTemplateService
{
private readonly IDbContextFactory<LiebContext> _contextFactory;
public RaidTemplateService(IDbContextFactory<LiebContext> contextFactory)
{
_contextFactory = contextFactory;
}
public List<RaidTemplate> GetTemplates()
{
using var context = _contextFactory.CreateDbContext();
return context.RaidTemplates
.Include(r => r.Roles)
.Include(r => r.Reminders)
.ToList();
}
public async Task CreateNewRaid(int raidTempalteId)
{
using var context = _contextFactory.CreateDbContext();
RaidTemplate? template = await context.RaidTemplates
.Include(r => r.Roles)
.Include(r => r.Reminders)
.FirstOrDefaultAsync(t => t.RaidTemplateId == raidTempalteId);
if(template == null)
{
return;
}
Raid raid = new Raid(template);
context.Raids.Add(raid);
MoveTemplate(template);
await context.SaveChangesAsync();
}
private void MoveTemplate(RaidTemplate template)
{
template.StartTime = template.StartTime.AddDays(template.Frequency);
template.EndTime = template.EndTime.AddDays(template.Frequency);
template.FreeForAllTime = template.FreeForAllTime.AddDays(template.Frequency);
}
}
}

60
Lieb/Data/TimerService.cs Normal file
View file

@ -0,0 +1,60 @@
using Lieb.Models.GuildWars2.Raid;
namespace Lieb.Data
{
public class TimerService : IHostedService, IDisposable
{
private Timer _timer = null!;
private IServiceProvider _services;
public TimerService(IServiceProvider services)
{
_services = services;
}
public Task StartAsync(CancellationToken stoppingToken)
{
_timer = new Timer(CheckRaids, null, TimeSpan.Zero,
TimeSpan.FromMinutes(1));
return Task.CompletedTask;
}
private void CheckRaids(object? state)
{
using (var scope = _services.CreateScope())
{
var raidTemplateService =
scope.ServiceProvider
.GetRequiredService<RaidTemplateService>();
foreach(RaidTemplate template in raidTemplateService.GetTemplates())
{
TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById(template.TimeZone);
DateTime UTCStartTime = TimeZoneInfo.ConvertTimeToUtc(template.StartTime, timeZone);
if(UTCStartTime.AddDays(-template.CreateDaysBefore).AddHours(1) < DateTime.UtcNow)
{
raidTemplateService.CreateNewRaid(template.RaidTemplateId).Wait();
}
}
var raidService =
scope.ServiceProvider
.GetRequiredService<RaidService>();
raidService.SendReminders();
}
}
public Task StopAsync(CancellationToken stoppingToken)
{
_timer?.Change(Timeout.Infinite, 0);
return Task.CompletedTask;
}
public void Dispose()
{
_timer?.Dispose();
}
}
}