Skip to content

Commit

Permalink
feat: add sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu1537 committed Dec 10, 2024
1 parent 253fec7 commit ea6bf62
Show file tree
Hide file tree
Showing 22 changed files with 700 additions and 27 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -405,4 +405,7 @@ Plugins/
Canvas/
Zones/

!WFDS.Server/Pages/Canvas/
!WFDS.Server/Pages/Canvas/

data.db
data.db-*
5 changes: 1 addition & 4 deletions WFDS.Common/Steam/SessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ private bool TryCreateSession(CSteamID steamId)
var session = new Session(steamId);
if (_sessions.TryAdd(steamId.m_SteamID, session))
{
GameEventBus.Publish(new PlayerJoinedEvent(steamId));
return true;
}

Expand Down Expand Up @@ -250,10 +251,6 @@ private void BroadcastP2PPacket(CSteamID lobbyId, NetChannel channel, object dat
}
}

/* ************************************************************************ */
/* Steamworks Callbacks */
/* ************************************************************************ */

#region Steamworks Callbacks

private void OnLobbyChatUpdate(LobbyChatUpdate_t param)
Expand Down
18 changes: 18 additions & 0 deletions WFDS.Database/DataDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.EntityFrameworkCore;
using WFDS.Database.DbSet;

namespace WFDS.Database;

public class DataDbContext(DbContextOptions<DataDbContext> options) : DbContext(options)
{
public DbSet<Player> Players { get; set; }
public DbSet<BannedPlayer> BannedPlayers { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Player>().ToTable("players");
modelBuilder.Entity<BannedPlayer>().ToTable("banned_players");

base.OnModelCreating(modelBuilder);
}
}
15 changes: 15 additions & 0 deletions WFDS.Database/DataDbContextFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

namespace WFDS.Database;

public class DataDbContextFactory : IDesignTimeDbContextFactory<DataDbContext>
{
public DataDbContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<DataDbContext>();
optionsBuilder.UseSqlite("Data Source=./data.db;Cache=Shared");

return new DataDbContext(optionsBuilder.Options);
}
}
23 changes: 23 additions & 0 deletions WFDS.Database/DbSet/BannedPlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;

namespace WFDS.Database.DbSet;

[Index(nameof(SteamId), IsUnique = true)]
public class BannedPlayer
{
[Key]
[Column("id")]
public long Id { get; init; }

[Column("steam_id")]
public ulong SteamId { get; init; }

[MaxLength(32)]
[Column("display_name")]
public string DisplayName { get; set; } = string.Empty;

[Column("banned_at")]
public DateTime BannedAt { get; set; } = DateTime.UtcNow;
}
25 changes: 25 additions & 0 deletions WFDS.Database/DbSet/Player.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;

namespace WFDS.Database.DbSet;

[Index(nameof(SteamId), IsUnique = true)]
public class Player
{
[Key]
[Column("id")]
public long Id { get; init; }
[Column("steam_id")]
public ulong SteamId { get; init; }

[MaxLength(32)]
[Column("display_name")]
public string DisplayName { get; set; } = string.Empty;
[Column("last_joined_at")]
public DateTime LastJoinedAt { get; set; } = DateTime.UtcNow;
[Column("created_at")]
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
[Column("updated_at")]
public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
}
81 changes: 81 additions & 0 deletions WFDS.Database/Migrations/20241210075353_Initial.Designer.cs

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

69 changes: 69 additions & 0 deletions WFDS.Database/Migrations/20241210075353_Initial.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace WFDS.Database.Migrations
{
/// <inheritdoc />
public partial class Initial : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "banned_players",
columns: table => new
{
Id = table.Column<long>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
SteamId = table.Column<ulong>(type: "INTEGER", nullable: false),
DisplayName = table.Column<string>(type: "TEXT", maxLength: 32, nullable: false),
CreatedAt = table.Column<DateTime>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_banned_players", x => x.Id);
});

migrationBuilder.CreateTable(
name: "players",
columns: table => new
{
Id = table.Column<long>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
SteamId = table.Column<ulong>(type: "INTEGER", nullable: false),
DisplayName = table.Column<string>(type: "TEXT", maxLength: 32, nullable: false),
LastJoinedAt = table.Column<DateTime>(type: "TEXT", nullable: false),
CreatedAt = table.Column<DateTime>(type: "TEXT", nullable: false),
UpdatedAt = table.Column<DateTime>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_players", x => x.Id);
});

migrationBuilder.CreateIndex(
name: "IX_banned_players_SteamId",
table: "banned_players",
column: "SteamId",
unique: true);

migrationBuilder.CreateIndex(
name: "IX_players_SteamId",
table: "players",
column: "SteamId",
unique: true);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "banned_players");

migrationBuilder.DropTable(
name: "players");
}
}
}

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

Loading

0 comments on commit ea6bf62

Please sign in to comment.