Skip to content

Commit

Permalink
PickBanStore
Browse files Browse the repository at this point in the history
  • Loading branch information
ipax77 committed Jun 8, 2024
1 parent e8f6737 commit 827d3a6
Show file tree
Hide file tree
Showing 8 changed files with 2,484 additions and 4 deletions.
2,369 changes: 2,369 additions & 0 deletions src/MysqlMigrations/Migrations/20240608183114_DsPickBans.Designer.cs

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions src/MysqlMigrations/Migrations/20240608183114_DsPickBans.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace MysqlMigrations.Migrations
{
/// <inheritdoc />
public partial class DsPickBans : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "DsPickBans",
columns: table => new
{
DsPickBanId = table.Column<int>(type: "int", nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
PickBanMode = table.Column<int>(type: "int", nullable: false),
Time = table.Column<DateTime>(type: "datetime(0)", precision: 0, nullable: false),
Bans = table.Column<string>(type: "longtext", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
Picks = table.Column<string>(type: "longtext", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4")
},
constraints: table =>
{
table.PrimaryKey("PK_DsPickBans", x => x.DsPickBanId);
})
.Annotation("MySql:CharSet", "utf8mb4");
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "DsPickBans");
}
}
}
30 changes: 29 additions & 1 deletion src/MysqlMigrations/Migrations/ReplayContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.5")
.HasAnnotation("ProductVersion", "8.0.6")
.HasAnnotation("Relational:MaxIdentifierLength", 64);

MySqlModelBuilderExtensions.AutoIncrementColumns(modelBuilder);
Expand Down Expand Up @@ -586,6 +586,34 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.ToTable("DsAbilities");
});

modelBuilder.Entity("dsstats.db8.DsPickBan", b =>
{
b.Property<int>("DsPickBanId")
.ValueGeneratedOnAdd()
.HasColumnType("int");

MySqlPropertyBuilderExtensions.UseMySqlIdentityColumn(b.Property<int>("DsPickBanId"));

b.Property<string>("Bans")
.IsRequired()
.HasColumnType("longtext");

b.Property<int>("PickBanMode")
.HasColumnType("int");

b.Property<string>("Picks")
.IsRequired()
.HasColumnType("longtext");

b.Property<DateTime>("Time")
.HasPrecision(0)
.HasColumnType("datetime(0)");

b.HasKey("DsPickBanId");

b.ToTable("DsPickBans");
});

modelBuilder.Entity("dsstats.db8.DsUnit", b =>
{
b.Property<int>("DsUnitId")
Expand Down
14 changes: 14 additions & 0 deletions src/dsstats.db8/PickBan.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using dsstats.shared;
using Microsoft.EntityFrameworkCore;

namespace dsstats.db8;

public class DsPickBan
{
public int DsPickBanId { get; set; }
public PickBanMode PickBanMode { get; set; }
[Precision(0)]
public DateTime Time { get; set; }
public List<Commander> Bans { get; set; } = [];
public List<Commander> Picks { get; set; } = [];
}
1 change: 1 addition & 0 deletions src/dsstats.db8/ReplayContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class ReplayContext : DbContext
public virtual DbSet<FaqVote> FaqVotes { get; set; } = null!;
public virtual DbSet<IhSession> IhSessions { get; set; } = null!;
public virtual DbSet<IhSessionPlayer> IhSessionPlayers { get; set; } = null!;
public virtual DbSet<DsPickBan> DsPickBans { get; set; } = null!;
public int Week(DateTime date) => throw new InvalidOperationException($"{nameof(Week)} cannot be called client side.");
public int Strftime(string arg, DateTime date) => throw new InvalidOperationException($"{nameof(Strftime)} cannot be called client side.");

Expand Down
9 changes: 8 additions & 1 deletion src/dsstats.pickban/PickBanHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ public async Task SetBan(PickBan pickBan)
return;
}
await Clients.Group(guid.ToString()).SendAsync("Bans", resultDto.Bans);

if (resultDto.TotalPicks == 0 && resultDto.Bans.Count == resultDto.TotalBans)
{
await pickBanRepository.SavePickBan(resultDto);
}
}
}

Expand All @@ -69,6 +72,10 @@ public async Task SetPick(PickBan pickBan)
return;
}
await Clients.Group(guid.ToString()).SendAsync("Picks", resultDto.Picks);
if (resultDto.Picks.Count == resultDto.TotalPicks)
{
await pickBanRepository.SavePickBan(resultDto);
}
}
}

Expand Down
22 changes: 20 additions & 2 deletions src/dsstats.pickban/PickBanService.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using dsstats.shared;
using dsstats.db8;
using dsstats.shared;
using Microsoft.Extensions.DependencyInjection;

namespace dsstats.pickban;

public class PickBanRepository
public class PickBanRepository(IServiceScopeFactory scopeFactory)
{
private Dictionary<Guid, PickBanState> pickbanStates = [];

Expand Down Expand Up @@ -63,4 +65,20 @@ public int SetVisitor(Guid guid, bool joined)
}
return null;
}

public async Task SavePickBan(PickBanStateDto pickBan)
{
using var scope = scopeFactory.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ReplayContext>();

DsPickBan dsPickBan = new()
{
PickBanMode = pickBan.PickBanMode,
Time = DateTime.UtcNow,
Bans = pickBan.Bans.Select(s => s.Commander).ToList(),
Picks = pickBan.Picks.Select(s => s.Commander).ToList()
};
context.DsPickBans.Add(dsPickBan);
await context.SaveChangesAsync();
}
}
1 change: 1 addition & 0 deletions src/dsstats.pickban/dsstats.pickban.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\dsstats.db8\dsstats.db8.csproj" />
<ProjectReference Include="..\dsstats.shared\dsstats.shared.csproj" />
</ItemGroup>

Expand Down

0 comments on commit 827d3a6

Please sign in to comment.