Skip to content

Commit

Permalink
Add full custom modded items support
Browse files Browse the repository at this point in the history
Fix blazor reload garbage
  • Loading branch information
danbopes committed Aug 7, 2023
1 parent 273cc3e commit 6ef9901
Show file tree
Hide file tree
Showing 25 changed files with 516 additions and 116 deletions.
2 changes: 1 addition & 1 deletion BrotatoServer/Controllers/RunsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public async Task<IActionResult> PostRun([FromBody] RunInformation runInfo)
{
var user = HttpContext.GetUser();

var run = await _runRepository.AddRunAsync(user.SteamId, runInfo);
var run = await _runRepository.AddRunAsync(user.SteamId, runInfo, user.CustomData);

if (user.TwitchUsername is not null)
{
Expand Down
18 changes: 17 additions & 1 deletion BrotatoServer/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using Microsoft.EntityFrameworkCore;
using System.Buffers;
using BrotatoServer.Models.DB;
using BrotatoServer.Models.JSON;
using Microsoft.AspNetCore.Authorization;
using Newtonsoft.Json;

namespace BrotatoServer.Controllers;

Expand All @@ -15,10 +17,12 @@ namespace BrotatoServer.Controllers;
public class UsersController : ControllerBase
{
private readonly BrotatoServerContext _db;
private readonly IUserRepository _userRepository;

public UsersController(BrotatoServerContext db)
public UsersController(BrotatoServerContext db, IUserRepository userRepository)
{
_db = db;
_userRepository = userRepository;
}

[HttpPost]
Expand Down Expand Up @@ -54,6 +58,18 @@ public async Task<IActionResult> GetApiKeyForUser([FromRoute] ulong steamId)
return Ok(user.ApiKey);
}

[HttpPost]
[Route("custom_data")]
[Authorize(AuthenticationSchemes = "ApiKey")]
public async Task<IActionResult> CustomData([FromBody] CustomData customData)
{
var user = HttpContext.GetUser();

await _userRepository.UpdateCustomDataAsync(user.SteamId, JsonConvert.SerializeObject(customData));

return Ok();
}

[HttpGet]
[Route("test")]
[Authorize(AuthenticationSchemes = "Twitch")]
Expand Down
2 changes: 1 addition & 1 deletion BrotatoServer/Data/IRunRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public interface IRunRepository
{
IAsyncEnumerable<FullRun> GetAllRunsAsync(string twitchUsername);
Task<FullRun?> GetRunAsync(Guid id);
Task<Run> AddRunAsync(ulong userId, RunInformation runInfo);
Task<Run> AddRunAsync(ulong userId, RunInformation runInfo, string? customData);
Task<bool> UpdateCurrentRunAsync(RunInformation runInfo);
Task<bool> DeleteCurrentRunAsync();
Task<bool> DeleteRunAsync(Guid id);
Expand Down
2 changes: 2 additions & 0 deletions BrotatoServer/Data/IUserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public interface IUserRepository
IAsyncEnumerable<string> GetAllChatUsersAsync(CancellationToken ct = default);
Task EnsureUserAsync(User user);
Task<User?> GetUserAsync(ulong steamId);
Task<User?> GetUserByTwitchUsername(string twitchUsername);
Task UpdateUserAsync(User user);
Task SaveSettingsAsync(UserSettings userSettings);
Task UpdateCustomDataAsync(ulong userId, string customData);
}
5 changes: 3 additions & 2 deletions BrotatoServer/Data/RunRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public async IAsyncEnumerable<FullRun> GetAllRunsAsync(string twitchUsername)
return run is null ? null : _mapper.Map<FullRun>(run);
}

public async Task<Run> AddRunAsync(ulong userId, RunInformation runInfo)
public async Task<Run> AddRunAsync(ulong userId, RunInformation runInfo, string? customData)
{
var run = new Run
{
Expand All @@ -55,7 +55,8 @@ public async Task<Run> AddRunAsync(ulong userId, RunInformation runInfo)
Won = runInfo.RunData.Won,
Date = DateTimeOffset.FromUnixTimeSeconds(runInfo.Created),
CurrentRotation = true,
RunInformation = JsonConvert.SerializeObject(runInfo)
RunInformation = JsonConvert.SerializeObject(runInfo),
CustomData = customData
};

_context.Run.Add(run);
Expand Down
17 changes: 17 additions & 0 deletions BrotatoServer/Data/UserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ public async Task EnsureUserAsync(User user)
.FirstOrDefaultAsync(user => user.SteamId == steamId);
}

public async Task<User?> GetUserByTwitchUsername(string twitchUsername)
{
return await _db
.Users
.Include(user => user.Settings)
.AsNoTracking()
.FirstOrDefaultAsync(user => user.TwitchUsername == twitchUsername);
}

public async Task UpdateUserAsync(User user)
{
_db.Entry(user).State = EntityState.Modified;
Expand All @@ -84,4 +93,12 @@ public async Task SaveSettingsAsync(UserSettings userSettings)

_db.Entry(userSettings).State = EntityState.Detached;
}

public async Task UpdateCustomDataAsync(ulong userId, string customData)
{
await _db.Users
.Where(user => user.SteamId == userId)
.ExecuteUpdateAsync(setters =>
setters.SetProperty(r => r.CustomData, customData));
}
}
21 changes: 15 additions & 6 deletions BrotatoServer/Hubs/RunsHub.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Collections.Concurrent;
using BrotatoServer.Data;
using BrotatoServer.Models.JSON;
using BrotatoServer.Services;
using BrotatoServer.Utilities;
using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json;

namespace BrotatoServer.Hubs;

Expand Down Expand Up @@ -34,29 +34,38 @@ public async Task UpdateRunAsync(string twitchUsername, RunData? newRun)
public interface IRunHub
{
Task RunUpdate(RunData? newRun);
Task CustomDataUpdate(CustomData? newCustomData);
}

public class RunsHub : Hub<IRunHub>
{
private readonly CurrentRunProvider _currentRunProvider;
private readonly IUserRepository _userRepo;

public RunsHub(CurrentRunProvider currentRunProvider)
public RunsHub(CurrentRunProvider currentRunProvider, IUserRepository userRepo)
{
_currentRunProvider = currentRunProvider;
_userRepo = userRepo;
}

public override async Task OnConnectedAsync()
{
var twitchUsername = Context.GetHttpContext()?.Request.Query["twitchUsername"];
var twitchUsername = Context.GetHttpContext()?.Request.Query["twitchUsername"].FirstOrDefault();

if (string.IsNullOrEmpty(twitchUsername))
throw new InvalidOperationException("No twitchUsername was provided.");

await Groups.AddToGroupAsync(Context.ConnectionId, twitchUsername!);
await Groups.AddToGroupAsync(Context.ConnectionId, twitchUsername);

_currentRunProvider.Current.TryGetValue(twitchUsername, out var currentRun);

var user = await _userRepo.GetUserByTwitchUsername(twitchUsername);
if (user?.CustomData is not null)
await Clients.Caller.CustomDataUpdate(JsonConvert.DeserializeObject<CustomData>(user.CustomData));

_currentRunProvider.Current.TryGetValue(twitchUsername!, out var currentRun);

await Clients.Caller.RunUpdate(currentRun);

await base.OnConnectedAsync();
}
}
147 changes: 147 additions & 0 deletions BrotatoServer/Migrations/20230807212228_CustomData.Designer.cs

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

38 changes: 38 additions & 0 deletions BrotatoServer/Migrations/20230807212228_CustomData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace BrotatoServer.Migrations
{
/// <inheritdoc />
public partial class CustomData : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "CustomData",
table: "Users",
type: "TEXT",
nullable: true);

migrationBuilder.AddColumn<string>(
name: "CustomData",
table: "Run",
type: "TEXT",
nullable: true);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "CustomData",
table: "Users");

migrationBuilder.DropColumn(
name: "CustomData",
table: "Run");
}
}
}
6 changes: 6 additions & 0 deletions BrotatoServer/Migrations/BrotatoServerContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<bool>("CurrentRotation")
.HasColumnType("INTEGER");
b.Property<string>("CustomData")
.HasColumnType("TEXT");
b.Property<long>("Date")
.HasColumnType("INTEGER");
Expand Down Expand Up @@ -57,6 +60,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<Guid>("ApiKey")
.HasColumnType("TEXT");
b.Property<string>("CustomData")
.HasColumnType("TEXT");
b.Property<bool>("JoinedChat")
.HasColumnType("INTEGER");
Expand Down
1 change: 1 addition & 0 deletions BrotatoServer/Models/DB/FullRun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public class FullRun
public DateTimeOffset Date { get; set; }
public bool CurrentRotation { get; set; }
public RunData RunData { get; set; }
public CustomData? CustomData { get; set; }
}
1 change: 1 addition & 0 deletions BrotatoServer/Models/DB/Run.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class Run
public bool CurrentRotation { get; set; }
public bool Won { get; set; }
public string RunInformation { get; set; }
public string? CustomData { get; set; }
public virtual User? User { get; set; }

public string? TwitchClip { get; set; }
Expand Down
Loading

0 comments on commit 6ef9901

Please sign in to comment.