Skip to content

Commit

Permalink
Add total playtime to !playtime, and added !playstats
Browse files Browse the repository at this point in the history
  • Loading branch information
matte-ek committed May 3, 2023
1 parent 22aea59 commit 8940914
Show file tree
Hide file tree
Showing 11 changed files with 349 additions and 12 deletions.
19 changes: 19 additions & 0 deletions BanchoMultiplayerBot.Database/BanchoMultiplayerBot.Database.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.5" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions BanchoMultiplayerBot.Database/BotDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BanchoMultiplayerBot.Database.Models;
using Microsoft.EntityFrameworkCore;

namespace BanchoMultiplayerBot.Database
{
public class BotDbContext : DbContext
{
public DbSet<User> Users { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseSqlite($"Data Source=bot.db");
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace BanchoMultiplayerBot.Database.Migrations
{
/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
Id = table.Column<Guid>(type: "TEXT", nullable: false),
UserId = table.Column<int>(type: "INTEGER", nullable: true),
Name = table.Column<string>(type: "TEXT", nullable: false),
Playtime = table.Column<int>(type: "INTEGER", nullable: false),
MatchesPlayed = table.Column<int>(type: "INTEGER", nullable: false),
NumberOneResults = table.Column<int>(type: "INTEGER", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.Id);
});
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Users");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// <auto-generated />
using System;
using BanchoMultiplayerBot.Database;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

#nullable disable

namespace BanchoMultiplayerBot.Database.Migrations
{
[DbContext(typeof(BotDbContext))]
partial class BotDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "7.0.5");

modelBuilder.Entity("BanchoMultiplayerBot.Database.Models.User", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");

b.Property<int>("MatchesPlayed")
.HasColumnType("INTEGER");

b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");

b.Property<int>("NumberOneResults")
.HasColumnType("INTEGER");

b.Property<int>("Playtime")
.HasColumnType("INTEGER");

b.Property<int?>("UserId")
.HasColumnType("INTEGER");

b.HasKey("Id");

b.ToTable("Users");
});
#pragma warning restore 612, 618
}
}
}
37 changes: 37 additions & 0 deletions BanchoMultiplayerBot.Database/Models/User.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BanchoMultiplayerBot.Database.Models
{
public class User
{

public Guid Id { get; set; }

/// <summary>
/// osu! user id
/// </summary>
public int? UserId { get; set; }

/// <summary>
/// osu! username
/// </summary>
public string Name { get; set; } = string.Empty;

/// <summary>
/// Total playtime in seconds
/// </summary>
public int Playtime { get; set; }

public int MatchesPlayed { get; set; }

/// <summary>
/// Number of times the user has gotten #1 in a match
/// </summary>
public int NumberOneResults { get; set; }

}
}
64 changes: 64 additions & 0 deletions BanchoMultiplayerBot.Database/Repositories/UserRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BanchoMultiplayerBot.Database.Models;
using Microsoft.EntityFrameworkCore;

namespace BanchoMultiplayerBot.Database.Repositories
{
public class UserRepository : IDisposable
{
private readonly BotDbContext _botDbContext;
private bool _disposed;

public UserRepository()
{
_botDbContext = new BotDbContext();
}

public async Task<User?> FindUser(string username)
{
return await _botDbContext.Users.Where(x => x.Name == username).FirstOrDefaultAsync();
}

public async Task<User> CreateUser(string username)
{
var user = new User()
{
Id = Guid.NewGuid(),
Name = username
};

await _botDbContext.AddAsync(user);
await _botDbContext.SaveChangesAsync();

return user;
}

public async Task Save()
{
await _botDbContext.SaveChangesAsync();
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_botDbContext.Dispose();
}
}

_disposed = true;
}
}
}
8 changes: 7 additions & 1 deletion BanchoMultiplayerBot.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BanchoMultiplayerBot.Host.C
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BanchoMultiplayerBot.Host.Web", "BanchoMultiplayerBot.Host.Web\BanchoMultiplayerBot.Host.Web.csproj", "{A152675A-71B1-4F4A-ACB2-79248EF39E78}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BanchoMultiplayerBot.Status", "BanchoMultiplayerBot.Status\BanchoMultiplayerBot.Status.csproj", "{42E250EB-2555-4B04-B91F-731A38145174}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BanchoMultiplayerBot.Status", "BanchoMultiplayerBot.Status\BanchoMultiplayerBot.Status.csproj", "{42E250EB-2555-4B04-B91F-731A38145174}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BanchoMultiplayerBot.Database", "BanchoMultiplayerBot.Database\BanchoMultiplayerBot.Database.csproj", "{8CE6D749-46FE-4DFB-B3EB-2D06F6492FA6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -33,6 +35,10 @@ Global
{42E250EB-2555-4B04-B91F-731A38145174}.Debug|Any CPU.Build.0 = Debug|Any CPU
{42E250EB-2555-4B04-B91F-731A38145174}.Release|Any CPU.ActiveCfg = Release|Any CPU
{42E250EB-2555-4B04-B91F-731A38145174}.Release|Any CPU.Build.0 = Release|Any CPU
{8CE6D749-46FE-4DFB-B3EB-2D06F6492FA6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8CE6D749-46FE-4DFB-B3EB-2D06F6492FA6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8CE6D749-46FE-4DFB-B3EB-2D06F6492FA6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8CE6D749-46FE-4DFB-B3EB-2D06F6492FA6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
4 changes: 4 additions & 0 deletions BanchoMultiplayerBot/BanchoMultiplayerBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<PackageReference Include="Serilog" Version="2.12.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BanchoMultiplayerBot.Database\BanchoMultiplayerBot.Database.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="BanchoSharp">
<HintPath>..\..\forks\BanchoSharp\BanchoSharp\bin\Release\net6.0\BanchoSharp.dll</HintPath>
Expand Down
Loading

0 comments on commit 8940914

Please sign in to comment.