Allow linking servers to games

This commit is contained in:
Pat Hartl 2023-08-14 21:28:18 -05:00
parent 93c269af6f
commit 4c86ebfc3e
8 changed files with 1479 additions and 2 deletions

View file

@ -92,6 +92,12 @@ namespace LANCommander.Data
.WithOne(gs => gs.Game)
.IsRequired(true)
.OnDelete(DeleteBehavior.NoAction);
builder.Entity<Server>()
.HasOne(s => s.Game)
.WithMany(g => g.Servers)
.IsRequired(false)
.OnDelete(DeleteBehavior.NoAction);
}
public DbSet<Game>? Games { get; set; }

View file

@ -31,6 +31,7 @@ namespace LANCommander.Data.Models
public virtual ICollection<Script>? Scripts { get; set; }
public virtual ICollection<GameSave>? GameSaves { get; set; }
public virtual ICollection<SavePath>? SavePaths { get; set; }
public virtual ICollection<Server>? Servers { get; set; }
public string? ValidKeyRegex { get; set; }
public virtual ICollection<Key>? Keys { get; set; }

View file

@ -1,4 +1,7 @@
namespace LANCommander.Data.Models
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;
namespace LANCommander.Data.Models
{
public class Server : BaseModel
{
@ -13,5 +16,11 @@
public bool UseShellExecute { get; set; }
public bool Autostart { get; set; }
public int AutostartDelay { get; set; }
public Guid? GameId { get; set; }
[JsonIgnore]
[ForeignKey(nameof(GameId))]
[InverseProperty("Servers")]
public virtual Game? Game { get; set; }
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,67 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace LANCommander.Migrations
{
/// <inheritdoc />
public partial class LinkGameServers : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<Guid>(
name: "GameId",
table: "Servers",
type: "TEXT",
nullable: true);
migrationBuilder.AlterColumn<DateTime>(
name: "ApprovedOn",
table: "AspNetUsers",
type: "TEXT",
nullable: true,
oldClrType: typeof(DateTime),
oldType: "TEXT");
migrationBuilder.CreateIndex(
name: "IX_Servers_GameId",
table: "Servers",
column: "GameId");
migrationBuilder.AddForeignKey(
name: "FK_Servers_Games_GameId",
table: "Servers",
column: "GameId",
principalTable: "Games",
principalColumn: "Id");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Servers_Games_GameId",
table: "Servers");
migrationBuilder.DropIndex(
name: "IX_Servers_GameId",
table: "Servers");
migrationBuilder.DropColumn(
name: "GameId",
table: "Servers");
migrationBuilder.AlterColumn<DateTime>(
name: "ApprovedOn",
table: "AspNetUsers",
type: "TEXT",
nullable: false,
defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified),
oldClrType: typeof(DateTime),
oldType: "TEXT",
oldNullable: true);
}
}
}

View file

@ -640,6 +640,9 @@ namespace LANCommander.Migrations
b.Property<DateTime>("CreatedOn")
.HasColumnType("TEXT");
b.Property<Guid?>("GameId")
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
@ -673,6 +676,8 @@ namespace LANCommander.Migrations
b.HasIndex("CreatedById");
b.HasIndex("GameId");
b.HasIndex("UpdatedById");
b.ToTable("Servers");
@ -721,7 +726,7 @@ namespace LANCommander.Migrations
b.Property<bool>("Approved")
.HasColumnType("INTEGER");
b.Property<DateTime>("ApprovedOn")
b.Property<DateTime?>("ApprovedOn")
.HasColumnType("TEXT");
b.Property<string>("ConcurrencyStamp")
@ -1215,12 +1220,19 @@ namespace LANCommander.Migrations
.WithMany()
.HasForeignKey("CreatedById");
b.HasOne("LANCommander.Data.Models.Game", "Game")
.WithMany("Servers")
.HasForeignKey("GameId")
.OnDelete(DeleteBehavior.NoAction);
b.HasOne("LANCommander.Data.Models.User", "UpdatedBy")
.WithMany()
.HasForeignKey("UpdatedById");
b.Navigation("CreatedBy");
b.Navigation("Game");
b.Navigation("UpdatedBy");
});
@ -1310,6 +1322,8 @@ namespace LANCommander.Migrations
b.Navigation("SavePaths");
b.Navigation("Scripts");
b.Navigation("Servers");
});
modelBuilder.Entity("LANCommander.Data.Models.User", b =>

View file

@ -2,6 +2,7 @@
@page "/Servers/{id:guid}/Edit/{panel}"
@page "/Servers/Add"
@using LANCommander.Pages.Servers.Components
@inject GameService GameService
@inject ServerService ServerService
@inject ServerProcessService ServerProcessService
@inject IMessageService MessageService
@ -78,6 +79,22 @@
<FormItem Label="Name">
<Input @bind-Value="@context.Name" />
</FormItem>
<FormItem Label="Game">
<Select TItem="Game"
TItemValue="Guid"
DataSource="@Games.OrderBy(g => String.IsNullOrWhiteSpace(g.SortTitle) ? g.Title : g.SortTitle)"
@bind-Value="@GameId"
LabelName="Title"
ValueName="Id"
Placeholder="Select a Game"
DefaultActiveFirstOption="false"
EnableSearch>
<ItemTemplate Context="game">
<Image Src="@GetIcon(game)" Height="32" Width="32" Preview="false"></Image>
@game.Title
</ItemTemplate>
</Select>
</FormItem>
<FormItem Label="Path">
<Input @bind-Value="@context.Path" />
</FormItem>
@ -146,9 +163,12 @@
[Parameter] public Guid Id { get; set; }
[Parameter] public string Panel { get; set; }
IEnumerable<Game> Games = new List<Game>();
Server Server;
ServerProcessStatus Status;
Timer Timer;
Guid GameId;
protected override async Task OnInitializedAsync()
{
@ -159,6 +179,11 @@
if (String.IsNullOrWhiteSpace(Server.WorkingDirectory))
Server.WorkingDirectory = "C:\\";
if (Server.GameId.HasValue)
GameId = Server.GameId.Value;
Games = GameService.Get().ToList();
}
protected override void OnAfterRender(bool firstRender)
@ -195,6 +220,8 @@
{
try
{
Server.GameId = GameId;
if (Server.Id != Guid.Empty)
{
Server = await ServerService.Update(Server);
@ -215,4 +242,9 @@
await MessageService.Error("Could not save!");
}
}
private string GetIcon(Game game)
{
return $"/api/Games/{game?.Id}/Icon.png";
}
}

View file

@ -13,6 +13,10 @@
<Table TItem="Server" DataSource="@Servers" Loading="@Loading">
<PropertyColumn Property="s => s.Name" Sortable />
<PropertyColumn Property="s => s.Game">
<Image Src="@GetIcon(context.Game)" Height="32" Width="32" Preview="false"></Image>
@context.Game?.Title
</PropertyColumn>
<PropertyColumn Property="s => s.CreatedOn" Format="MM/dd/yyyy hh:mm tt" Sortable />
<PropertyColumn Property="s => s.CreatedBy" Sortable>
@context.CreatedBy?.UserName
@ -143,4 +147,9 @@
Loading = false;
}
private string GetIcon(Game game)
{
return $"/api/Games/{game?.Id}/Icon.png";
}
}