feat: subscription field in worlds table

This commit is contained in:
CyberL1 2024-03-17 16:10:58 +01:00
parent cf62e42743
commit 540e43b5f5
9 changed files with 517 additions and 56 deletions

View File

@ -18,18 +18,15 @@ namespace Minecraft_Realms_Emulator.Controllers
[HttpGet("{id}")]
public async Task<ActionResult<Subscription>> Get(int id)
{
var world = await _context.Worlds.FindAsync(id);
var subscriptions = await _context.Subscriptions.ToListAsync();
var world = await _context.Worlds.Include(w => w.Subscription).FirstOrDefaultAsync(w => w.Id == id);
if (world == null) return NotFound("Subscription not found");
var subscription = subscriptions.Find(s => s.World.RemoteSubscriptionId == world.RemoteSubscriptionId);
if (world?.Subscription == null) return NotFound("Subscription not found");
var sub = new Subscription
{
StartDate = ((DateTimeOffset) subscription.StartDate).ToUnixTimeMilliseconds(),
DaysLeft = ((DateTimeOffset) subscription.StartDate.AddDays(30) - DateTime.Today).Days,
SubscriptionType = subscription.SubscriptionType
StartDate = ((DateTimeOffset)world.Subscription.StartDate).ToUnixTimeMilliseconds(),
DaysLeft = ((DateTimeOffset)world.Subscription.StartDate.AddDays(30) - DateTime.Today).Days,
SubscriptionType = world.Subscription.SubscriptionType
};
return Ok(sub);

View File

@ -2,6 +2,8 @@
using Microsoft.EntityFrameworkCore;
using Minecraft_Realms_Emulator.Data;
using Minecraft_Realms_Emulator.Entities;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
namespace Minecraft_Realms_Emulator.Controllers
{
@ -24,22 +26,20 @@ namespace Minecraft_Realms_Emulator.Controllers
string playerUUID = cookie.Split(";")[0].Split(":")[2];
string playerName = cookie.Split(";")[1].Split("=")[1];
var ownedWorlds = await _context.Worlds.Where(w => w.OwnerUUID == playerUUID).ToListAsync();
var memberWorlds = await _context.Players.Where(p => p.Uuid == playerUUID && p.Accepted).Select(p => p.World).ToListAsync();
var ownedWorlds = await _context.Worlds.Where(w => w.OwnerUUID == playerUUID).Include(w => w.Subscription).ToListAsync();
var memberWorlds = await _context.Players.Where(p => p.Uuid == playerUUID && p.Accepted).Include(p => p.World.Subscription).Select(p => p.World).ToListAsync();
List<WorldResponse> allWorlds = [];
if (ownedWorlds.ToArray().Length == 0)
{
var world = new World
{
RemoteSubscriptionId = Guid.NewGuid().ToString(),
Owner = playerName,
OwnerUUID = playerUUID,
Name = null,
Motd = null,
State = State.UNINITIALIZED.ToString(),
DaysLeft = 30,
Expired = false,
ExpiredTrial = false,
WorldType = WorldType.NORMAL.ToString(),
MaxPlayers = 10,
MinigameId = null,
@ -55,10 +55,61 @@ namespace Minecraft_Realms_Emulator.Controllers
_context.SaveChanges();
}
List<World> allWorlds = [];
foreach (var world in ownedWorlds)
{
WorldResponse response = new()
{
Id = world.Id,
Owner = world.Owner,
OwnerUUID = world.OwnerUUID,
Name = world.Name,
Motd = world.Motd,
State = world.State,
WorldType = world.WorldType,
MaxPlayers = world.MaxPlayers,
MinigameId = world.MinigameId,
MinigameName = world.MinigameName,
MinigameImage = world.MinigameImage,
ActiveSlot = world.ActiveSlot,
Member = world.Member,
Players = world.Players
};
allWorlds.AddRange(ownedWorlds);
allWorlds.AddRange(memberWorlds);
if (world.Subscription != null)
{
response.DaysLeft = ((DateTimeOffset)world.Subscription.StartDate.AddDays(30) - DateTime.Today).Days;
response.Expired = ((DateTimeOffset)world.Subscription.StartDate.AddDays(30) - DateTime.Today).Days < 0;
response.ExpiredTrial = false;
}
allWorlds.Add(response);
}
foreach (var world in memberWorlds)
{
WorldResponse response = new()
{
Id = world.Id,
Owner = world.Owner,
OwnerUUID = world.OwnerUUID,
Name = world.Name,
Motd = world.Motd,
State = world.State,
WorldType = world.WorldType,
MaxPlayers = world.MaxPlayers,
MinigameId = world.MinigameId,
MinigameName = world.MinigameName,
MinigameImage = world.MinigameImage,
ActiveSlot = world.ActiveSlot,
Member = world.Member,
Players = world.Players,
DaysLeft = 0,
Expired = ((DateTimeOffset)world.Subscription.StartDate.AddDays(30) - DateTime.Today).Days < 0,
ExpiredTrial = false
};
allWorlds.Add(response);
}
ServersArray servers = new()
{
@ -69,13 +120,34 @@ namespace Minecraft_Realms_Emulator.Controllers
}
[HttpGet("{id}")]
public async Task<ActionResult<World>> GetWorldById(int id)
public async Task<ActionResult<WorldResponse>> GetWorldById(int id)
{
var world = await _context.Worlds.Include(w => w.Players).FirstOrDefaultAsync(w => w.Id == id);
var world = await _context.Worlds.Include(w => w.Players).Include(w => w.Subscription).FirstOrDefaultAsync(w => w.Id == id);
if (world == null) return NotFound("World not found");
if (world?.Subscription == null) return NotFound("World not found");
return world;
WorldResponse response = new()
{
Id = world.Id,
Owner = world.Owner,
OwnerUUID = world.OwnerUUID,
Name = world.Name,
Motd = world.Motd,
State = world.State,
WorldType = world.WorldType,
MaxPlayers = world.MaxPlayers,
MinigameId = world.MinigameId,
MinigameName = world.MinigameName,
MinigameImage = world.MinigameImage,
ActiveSlot = world.ActiveSlot,
Member = world.Member,
Players = world.Players,
DaysLeft = ((DateTimeOffset)world.Subscription.StartDate.AddDays(30) - DateTime.Today).Days,
Expired = ((DateTimeOffset)world.Subscription.StartDate.AddDays(30) - DateTime.Today).Days < 0,
ExpiredTrial = false
};
return response;
}
[HttpPost("{id}/initialize")]
@ -88,17 +160,17 @@ namespace Minecraft_Realms_Emulator.Controllers
if (world == null) return NotFound("World not found");
if (world.State != State.UNINITIALIZED.ToString()) return NotFound("World already initialized");
world.Name = body.Name;
world.Motd = body.Description;
world.State = State.OPEN.ToString();
var subscription = new Subscription
{
World = world,
StartDate = DateTime.UtcNow,
SubscriptionType = SubscriptionType.NORMAL.ToString()
};
world.Name = body.Name;
world.Motd = body.Description;
world.State = State.OPEN.ToString();
world.Subscription = subscription;
var connection = new Connection
{
World = world,
@ -143,7 +215,7 @@ namespace Minecraft_Realms_Emulator.Controllers
{
var worlds = await _context.Worlds.ToListAsync();
var world = worlds.Find(w => w.Id == id);
var world = worlds.FirstOrDefault(w => w.Id == id);
if (world == null) return NotFound("World not found");

View File

@ -2,6 +2,6 @@
{
public class ServersArray
{
public List<World>? Servers { get; set; }
public List<WorldResponse>? Servers { get; set; }
}
}

View File

@ -3,7 +3,8 @@
public class Subscription
{
public int Id { get; set; }
public World World { get; set; }
public int WorldId { get; set; }
public World World { get; set; } = null!;
public DateTime StartDate { get; set; } = DateTime.Now;
public string SubscriptionType { get; set; } = "NORMAL";
}

View File

@ -5,15 +5,12 @@ namespace Minecraft_Realms_Emulator.Entities
public class World
{
public int Id { get; set; }
public string RemoteSubscriptionId { get; set; } = Guid.NewGuid().ToString();
public Subscription? Subscription { get; set; }
public string? Owner { get; set; }
public string? OwnerUUID { get; set; }
public string? Name { get; set; }
public string? Motd { get; set; }
public string State { get; set; } = "OPEN";
public int DaysLeft { get; set; } = 30;
public bool Expired { get; set; } = false;
public bool ExpiredTrial { get; set; } = false;
public string WorldType { get; set; } = "NORMAL";
public List<Player> Players { get; set; } = [];
public int MaxPlayers { get; set; } = 10;

View File

@ -0,0 +1,313 @@
// <auto-generated />
using System;
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Minecraft_Realms_Emulator.Data;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace Minecraft_Realms_Emulator.Migrations
{
[DbContext(typeof(DataContext))]
[Migration("20240317120329_Worlds_Subscription_field")]
partial class Worlds_Subscription_field
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.1")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Backup", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("BackupId")
.IsRequired()
.HasColumnType("text");
b.Property<long>("LastModifiedDate")
.HasColumnType("bigint");
b.Property<JsonDocument>("Metadata")
.IsRequired()
.HasColumnType("jsonb");
b.Property<int>("Size")
.HasColumnType("integer");
b.Property<int>("WorldId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("WorldId");
b.ToTable("Backups");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Configuration", b =>
{
b.Property<string>("Key")
.HasColumnType("text");
b.Property<string>("Value")
.IsRequired()
.HasColumnType("text");
b.HasKey("Key");
b.ToTable("Configuration");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Connection", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("Address")
.IsRequired()
.HasColumnType("text");
b.Property<bool>("PendingUpdate")
.HasColumnType("boolean");
b.Property<int>("WorldId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("WorldId");
b.ToTable("Connections");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Invite", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<DateTime>("Date")
.HasColumnType("timestamp with time zone");
b.Property<string>("InvitationId")
.IsRequired()
.HasColumnType("text");
b.Property<string>("RecipeintUUID")
.IsRequired()
.HasColumnType("text");
b.Property<int>("WorldId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("WorldId");
b.ToTable("Invites");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Player", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<bool>("Accepted")
.HasColumnType("boolean");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");
b.Property<bool>("Online")
.HasColumnType("boolean");
b.Property<bool>("Operator")
.HasColumnType("boolean");
b.Property<string>("Permission")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Uuid")
.IsRequired()
.HasColumnType("text");
b.Property<int>("WorldId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("WorldId");
b.ToTable("Players");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Subscription", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<DateTime>("StartDate")
.HasColumnType("timestamp with time zone");
b.Property<string>("SubscriptionType")
.IsRequired()
.HasColumnType("text");
b.Property<int>("WorldId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("WorldId")
.IsUnique();
b.ToTable("Subscriptions");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.World", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int>("ActiveSlot")
.HasColumnType("integer");
b.Property<int>("MaxPlayers")
.HasColumnType("integer");
b.Property<bool>("Member")
.HasColumnType("boolean");
b.Property<int?>("MinigameId")
.HasColumnType("integer");
b.Property<string>("MinigameImage")
.HasColumnType("text");
b.Property<string>("MinigameName")
.HasColumnType("text");
b.Property<string>("Motd")
.HasColumnType("text");
b.Property<string>("Name")
.HasColumnType("text");
b.Property<string>("Owner")
.HasColumnType("text");
b.Property<string>("OwnerUUID")
.HasColumnType("text");
b.Property<JsonDocument[]>("Slots")
.IsRequired()
.HasColumnType("jsonb[]");
b.Property<string>("State")
.IsRequired()
.HasColumnType("text");
b.Property<string>("WorldType")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("Worlds");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Backup", b =>
{
b.HasOne("Minecraft_Realms_Emulator.Entities.World", "World")
.WithMany()
.HasForeignKey("WorldId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("World");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Connection", b =>
{
b.HasOne("Minecraft_Realms_Emulator.Entities.World", "World")
.WithMany()
.HasForeignKey("WorldId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("World");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Invite", b =>
{
b.HasOne("Minecraft_Realms_Emulator.Entities.World", "World")
.WithMany()
.HasForeignKey("WorldId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("World");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Player", b =>
{
b.HasOne("Minecraft_Realms_Emulator.Entities.World", "World")
.WithMany("Players")
.HasForeignKey("WorldId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("World");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Subscription", b =>
{
b.HasOne("Minecraft_Realms_Emulator.Entities.World", "World")
.WithOne("Subscription")
.HasForeignKey("Minecraft_Realms_Emulator.Entities.Subscription", "WorldId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("World");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.World", b =>
{
b.Navigation("Players");
b.Navigation("Subscription");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,81 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Minecraft_Realms_Emulator.Migrations
{
/// <inheritdoc />
public partial class Worlds_Subscription_field : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex(
name: "IX_Subscriptions_WorldId",
table: "Subscriptions");
migrationBuilder.DropColumn(
name: "DaysLeft",
table: "Worlds");
migrationBuilder.DropColumn(
name: "Expired",
table: "Worlds");
migrationBuilder.DropColumn(
name: "ExpiredTrial",
table: "Worlds");
migrationBuilder.DropColumn(
name: "RemoteSubscriptionId",
table: "Worlds");
migrationBuilder.CreateIndex(
name: "IX_Subscriptions_WorldId",
table: "Subscriptions",
column: "WorldId",
unique: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex(
name: "IX_Subscriptions_WorldId",
table: "Subscriptions");
migrationBuilder.AddColumn<int>(
name: "DaysLeft",
table: "Worlds",
type: "integer",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<bool>(
name: "Expired",
table: "Worlds",
type: "boolean",
nullable: false,
defaultValue: false);
migrationBuilder.AddColumn<bool>(
name: "ExpiredTrial",
table: "Worlds",
type: "boolean",
nullable: false,
defaultValue: false);
migrationBuilder.AddColumn<string>(
name: "RemoteSubscriptionId",
table: "Worlds",
type: "text",
nullable: false,
defaultValue: "");
migrationBuilder.CreateIndex(
name: "IX_Subscriptions_WorldId",
table: "Subscriptions",
column: "WorldId");
}
}
}

View File

@ -52,7 +52,7 @@ namespace Minecraft_Realms_Emulator.Migrations
b.HasIndex("WorldId");
b.ToTable("Backups");
b.ToTable("Backups", (string)null);
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Configuration", b =>
@ -66,7 +66,7 @@ namespace Minecraft_Realms_Emulator.Migrations
b.HasKey("Key");
b.ToTable("Configuration");
b.ToTable("Configuration", (string)null);
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Connection", b =>
@ -91,7 +91,7 @@ namespace Minecraft_Realms_Emulator.Migrations
b.HasIndex("WorldId");
b.ToTable("Connections");
b.ToTable("Connections", (string)null);
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Invite", b =>
@ -120,7 +120,7 @@ namespace Minecraft_Realms_Emulator.Migrations
b.HasIndex("WorldId");
b.ToTable("Invites");
b.ToTable("Invites", (string)null);
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Player", b =>
@ -159,7 +159,7 @@ namespace Minecraft_Realms_Emulator.Migrations
b.HasIndex("WorldId");
b.ToTable("Players");
b.ToTable("Players", (string)null);
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Subscription", b =>
@ -182,9 +182,10 @@ namespace Minecraft_Realms_Emulator.Migrations
b.HasKey("Id");
b.HasIndex("WorldId");
b.HasIndex("WorldId")
.IsUnique();
b.ToTable("Subscriptions");
b.ToTable("Subscriptions", (string)null);
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.World", b =>
@ -198,15 +199,6 @@ namespace Minecraft_Realms_Emulator.Migrations
b.Property<int>("ActiveSlot")
.HasColumnType("integer");
b.Property<int>("DaysLeft")
.HasColumnType("integer");
b.Property<bool>("Expired")
.HasColumnType("boolean");
b.Property<bool>("ExpiredTrial")
.HasColumnType("boolean");
b.Property<int>("MaxPlayers")
.HasColumnType("integer");
@ -234,10 +226,6 @@ namespace Minecraft_Realms_Emulator.Migrations
b.Property<string>("OwnerUUID")
.HasColumnType("text");
b.Property<string>("RemoteSubscriptionId")
.IsRequired()
.HasColumnType("text");
b.Property<JsonDocument[]>("Slots")
.IsRequired()
.HasColumnType("jsonb[]");
@ -252,7 +240,7 @@ namespace Minecraft_Realms_Emulator.Migrations
b.HasKey("Id");
b.ToTable("Worlds");
b.ToTable("Worlds", (string)null);
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Backup", b =>
@ -302,8 +290,8 @@ namespace Minecraft_Realms_Emulator.Migrations
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Subscription", b =>
{
b.HasOne("Minecraft_Realms_Emulator.Entities.World", "World")
.WithMany()
.HasForeignKey("WorldId")
.WithOne("Subscription")
.HasForeignKey("Minecraft_Realms_Emulator.Entities.Subscription", "WorldId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
@ -313,6 +301,8 @@ namespace Minecraft_Realms_Emulator.Migrations
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.World", b =>
{
b.Navigation("Players");
b.Navigation("Subscription");
});
#pragma warning restore 612, 618
}

View File

@ -0,0 +1,10 @@
namespace Minecraft_Realms_Emulator.Entities
{
public class WorldResponse : World
{
public string RemoteSubscriptionId { get; set; } = Guid.NewGuid().ToString();
public int DaysLeft { get; set; } = 30;
public bool Expired { get; set; } = false;
public bool ExpiredTrial { get; set; } = false;
}
}