Skip to content

Commit

Permalink
Merge pull request #11 from theiam79/dev
Browse files Browse the repository at this point in the history
Adds play file subscriptions
  • Loading branch information
theiam79 authored Aug 23, 2020
2 parents bab9fb0 + ac4740c commit 58c0b92
Show file tree
Hide file tree
Showing 15 changed files with 423 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/BGStats.Bot/BGStats.Bot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
<PackageReference Include="Discord.Addons.Hosting" Version="3.0.0" />
<PackageReference Include="Discord.Addons.Interactive" Version="2.0.0" />
<PackageReference Include="Discord.Net" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.7" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.6" />
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.6" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.9" />
Expand All @@ -30,6 +36,9 @@
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestSubscribers.db">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
32 changes: 32 additions & 0 deletions src/BGStats.Bot/Context/SubscriberContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using BGStats.Bot.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using System;
using System.Collections.Generic;
using System.Text;

namespace BGStats.Bot.Context
{
public class SubscriberContext : DbContext
{
public SubscriberContext(DbContextOptions<SubscriberContext> options) : base(options) { }

public DbSet<Subscriber> Subscribers { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Subscriber>()
.Property(p => p.SubscriberId)
.ValueGeneratedOnAdd();
}
}

public class SubscriberContextFactory : IDesignTimeDbContextFactory<SubscriberContext>
{
public SubscriberContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<SubscriberContext>().UseSqlite("Data Source = TestSubscribers.db");
return new SubscriberContext(optionsBuilder.Options);
}
}
}
39 changes: 39 additions & 0 deletions src/BGStats.Bot/Migrations/20200822044922_init.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions src/BGStats.Bot/Migrations/20200822044922_init.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.EntityFrameworkCore.Migrations;

namespace BGStats.Bot.Migrations
{
public partial class init : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Subscribers",
columns: table => new
{
SubscriberId = table.Column<int>(nullable: false)
.Annotation("Sqlite:Autoincrement", true),
DiscordId = table.Column<ulong>(nullable: false),
PlayerName = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Subscribers", x => x.SubscriberId);
});
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Subscribers");
}
}
}
39 changes: 39 additions & 0 deletions src/BGStats.Bot/Migrations/20200822050642_autoId.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions src/BGStats.Bot/Migrations/20200822050642_autoId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.EntityFrameworkCore.Migrations;

namespace BGStats.Bot.Migrations
{
public partial class autoId : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{

}

protected override void Down(MigrationBuilder migrationBuilder)
{

}
}
}
37 changes: 37 additions & 0 deletions src/BGStats.Bot/Migrations/SubscriberContextModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// <auto-generated />
using BGStats.Bot.Context;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

namespace BGStats.Bot.Migrations
{
[DbContext(typeof(SubscriberContext))]
partial class SubscriberContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "3.1.7");

modelBuilder.Entity("BGStats.Bot.Models.Subscriber", b =>
{
b.Property<int>("SubscriberId")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<ulong>("DiscordId")
.HasColumnType("INTEGER");
b.Property<string>("PlayerName")
.HasColumnType("TEXT");
b.HasKey("SubscriberId");
b.ToTable("Subscribers");
});
#pragma warning restore 612, 618
}
}
}
14 changes: 14 additions & 0 deletions src/BGStats.Bot/Models/Subscriber.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace BGStats.Bot.Models
{
public class Subscriber
{

public int SubscriberId { get; set; }
public ulong DiscordId { get; set; }
public string PlayerName { get; set; }
}
}
13 changes: 10 additions & 3 deletions src/BGStats.Bot/Modules/PlayFileModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Discord.Commands;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
Expand All @@ -17,12 +18,14 @@ public class PlayFileModule : ModuleBase<SocketCommandContext>
private readonly IHttpClientFactory _httpClientFactory;
private readonly PlayFormatService _playFormatService;
private readonly PostingService _postingService;
private readonly INotificationService _notificationService;

public PlayFileModule(IHttpClientFactory httpClientFactory, PlayFormatService playFormatService, PostingService postingService)
public PlayFileModule(IHttpClientFactory httpClientFactory, PlayFormatService playFormatService, PostingService postingService, INotificationService notificationService)
{
_httpClientFactory = httpClientFactory;
_playFormatService = playFormatService;
_postingService = postingService;
_notificationService = notificationService;
}

[Command("sharePlayFile")]
Expand All @@ -33,12 +36,16 @@ public async Task SharePlayFile()
var playFile = Context.Message.Attachments.FirstOrDefault(x => x.Filename.EndsWith(".bgsplay"));
var playFileContents = await _httpClientFactory.CreateClient().GetStreamAsync(playFile.Url);

var contentStream = new MemoryStream();
await playFileContents.CopyToAsync(contentStream);
contentStream.Seek(0, SeekOrigin.Begin);

var serializerOptions = new JsonSerializerOptions();
serializerOptions.Converters.Add(new AutoIntToBoolConverter());

var play = await JsonSerializer.DeserializeAsync<PlayFile>(playFileContents, serializerOptions);

var play = await JsonSerializer.DeserializeAsync<PlayFile>(contentStream, serializerOptions);
await _postingService.PostAsync(_playFormatService.FormatPlay(play));
await _notificationService.Notify(playFile.Filename, contentStream, play, Context.User.Id);
}
}

Expand Down
66 changes: 66 additions & 0 deletions src/BGStats.Bot/Modules/SubscribeModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using BGStats.Bot.Services;
using Discord.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BGStats.Bot.Modules
{
[Group("subscription")]
public class SubscribeModule : ModuleBase<SocketCommandContext>
{
private readonly INotificationService _notificationService;

public SubscribeModule(INotificationService notificationService)
{
_notificationService = notificationService;
}

[Command("add")]
[Summary("Subscribe to recieve any play files sent by others that include the given player name")]
public async Task Subscribe([Remainder] string playerName)
{
await _notificationService.Subscribe(Context.User.Id, playerName);
await ReplyAsync($"Successfully subscribed to plays that include {playerName}");
}

[Command("remove")]
[Summary("Remove one of your existing subscriptions")]
public async Task Unsubscribe([Remainder] string playerName)
{
await _notificationService.Unsubscribe(Context.User.Id, playerName);
await ReplyAsync($"Successfully unsubscribed from plays that include {playerName}");
}

[Command("removeAll")]
[Summary("Remove all of your existing subscriptions")]
public async Task UnsubscribeAll()
{
await _notificationService.Unsubscribe(Context.User.Id);
await ReplyAsync($"Successfully unsubscribed from all plays");
}

[Command("list")]
[Summary("List all of your current subscriptions")]
public async Task ListSubscriptions()
{
var subs = await _notificationService.GetSubscriptions(Context.User.Id);

var message = new StringBuilder().AppendLine($"Found {subs.Count} subscriptions for {Context.User.Username}").AppendJoin('\n', subs.Where(s => s.DiscordId == Context.User.Id).Select(s => s.PlayerName)).ToString();
var channel = await Context.User.GetOrCreateDMChannelAsync();
await channel.SendMessageAsync(message);
}

[Command("listAll")]
[RequireOwner]
public async Task ListAllSubscriptions()
{
var subs = await _notificationService.GetAllSubscriptions();
var message = new StringBuilder().AppendLine($"Found {subs.Count} total subscriptions").AppendJoin('\n', subs.Select(s => $"{Context.Client.GetUser(s.DiscordId).Username} - {s.PlayerName}")).ToString();
var channel = await Context.User.GetOrCreateDMChannelAsync();
await channel.SendMessageAsync(message);
}
}
}
Loading

0 comments on commit 58c0b92

Please sign in to comment.