feat(server): snapshot worlds

This commit is contained in:
CyberL1 2024-06-29 21:34:42 +02:00
parent 4b7eec2057
commit 54953fbd6c
10 changed files with 1262 additions and 7 deletions

View File

@ -18,5 +18,6 @@
public int ActiveSlot { get; set; } = 1; public int ActiveSlot { get; set; } = 1;
public List<Slot> Slots { get; set; } = []; public List<Slot> Slots { get; set; } = [];
public bool Member { get; set; } = false; public bool Member { get; set; } = false;
public World? ParentWorld { get; set; }
} }
} }

View File

@ -1,4 +1,5 @@
using Minecraft_Realms_Emulator.Attributes; using Microsoft.EntityFrameworkCore;
using Minecraft_Realms_Emulator.Attributes;
using Minecraft_Realms_Emulator.Data; using Minecraft_Realms_Emulator.Data;
using Minecraft_Realms_Emulator.Entities; using Minecraft_Realms_Emulator.Entities;
@ -20,9 +21,9 @@ namespace Minecraft_Realms_Emulator.Middlewares
} }
string playerUUID = httpContext.Request.Headers.Cookie.ToString().Split(";")[0].Split(":")[2]; string playerUUID = httpContext.Request.Headers.Cookie.ToString().Split(";")[0].Split(":")[2];
World world = db.Worlds.Find(int.Parse(httpContext.Request.RouteValues["wId"].ToString())); World world = db.Worlds.Include(w => w.ParentWorld).FirstOrDefault(w => w.Id == int.Parse(httpContext.Request.RouteValues["wId"].ToString()));
if (world != null && !attribute.IsRealmOwner(playerUUID, world.OwnerUUID)) if (world != null && !attribute.IsRealmOwner(playerUUID, world.ParentWorld == null ? world.OwnerUUID : world.ParentWorld.OwnerUUID))
{ {
httpContext.Response.StatusCode = 403; httpContext.Response.StatusCode = 403;
await httpContext.Response.WriteAsync("You don't own this world"); await httpContext.Response.WriteAsync("You don't own this world");

View File

@ -0,0 +1,500 @@
// <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("20240629161935_Snapshot_Worlds")]
partial class Snapshot_Worlds
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.5")
.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<object>("Value")
.IsRequired()
.HasColumnType("jsonb");
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.Notification", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<JsonDocument>("ButtonText")
.HasColumnType("jsonb");
b.Property<bool>("Dismissable")
.HasColumnType("boolean");
b.Property<string>("Image")
.HasColumnType("text");
b.Property<JsonDocument>("Message")
.IsRequired()
.HasColumnType("jsonb");
b.Property<string>("NotificationUuid")
.IsRequired()
.HasColumnType("text");
b.Property<JsonDocument>("Title")
.HasColumnType("jsonb");
b.Property<string>("Type")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Url")
.HasColumnType("text");
b.Property<JsonDocument>("UrlButton")
.HasColumnType("jsonb");
b.HasKey("Id");
b.ToTable("Notifications");
});
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.SeenNotification", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("NotificationUUID")
.IsRequired()
.HasColumnType("text");
b.Property<string>("PlayerUUID")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("SeenNotifications");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.Slot", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<bool>("CommandBlocks")
.HasColumnType("boolean");
b.Property<int>("Difficulty")
.HasColumnType("integer");
b.Property<bool>("ForceGameMode")
.HasColumnType("boolean");
b.Property<int>("GameMode")
.HasColumnType("integer");
b.Property<bool>("Pvp")
.HasColumnType("boolean");
b.Property<int>("SlotId")
.HasColumnType("integer");
b.Property<string>("SlotName")
.IsRequired()
.HasColumnType("text");
b.Property<bool>("SpawnAnimals")
.HasColumnType("boolean");
b.Property<bool>("SpawnMonsters")
.HasColumnType("boolean");
b.Property<bool>("SpawnNPCs")
.HasColumnType("boolean");
b.Property<int>("SpawnProtection")
.HasColumnType("integer");
b.Property<string>("Version")
.IsRequired()
.HasColumnType("text");
b.Property<int>("WorldId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("WorldId");
b.ToTable("Slots");
});
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.Template", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string>("Author")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Image")
.HasColumnType("text");
b.Property<string>("Link")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");
b.Property<string>("RecommendedPlayers")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Trailer")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Type")
.IsRequired()
.HasColumnType("text");
b.Property<string>("Version")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("Templates");
});
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<int?>("ParentWorldId")
.HasColumnType("integer");
b.Property<string>("State")
.IsRequired()
.HasColumnType("text");
b.Property<string>("WorldType")
.IsRequired()
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("ParentWorldId");
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.Slot", b =>
{
b.HasOne("Minecraft_Realms_Emulator.Entities.World", "World")
.WithMany("Slots")
.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.HasOne("Minecraft_Realms_Emulator.Entities.World", "ParentWorld")
.WithMany()
.HasForeignKey("ParentWorldId");
b.Navigation("ParentWorld");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.World", b =>
{
b.Navigation("Players");
b.Navigation("Slots");
b.Navigation("Subscription");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,48 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Minecraft_Realms_Emulator.Migrations
{
/// <inheritdoc />
public partial class Snapshot_Worlds : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "ParentWorldId",
table: "Worlds",
type: "integer",
nullable: true);
migrationBuilder.CreateIndex(
name: "IX_Worlds_ParentWorldId",
table: "Worlds",
column: "ParentWorldId");
migrationBuilder.AddForeignKey(
name: "FK_Worlds_Worlds_ParentWorldId",
table: "Worlds",
column: "ParentWorldId",
principalTable: "Worlds",
principalColumn: "Id");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Worlds_Worlds_ParentWorldId",
table: "Worlds");
migrationBuilder.DropIndex(
name: "IX_Worlds_ParentWorldId",
table: "Worlds");
migrationBuilder.DropColumn(
name: "ParentWorldId",
table: "Worlds");
}
}
}

View File

@ -390,6 +390,9 @@ namespace Minecraft_Realms_Emulator.Migrations
b.Property<string>("OwnerUUID") b.Property<string>("OwnerUUID")
.HasColumnType("text"); .HasColumnType("text");
b.Property<int?>("ParentWorldId")
.HasColumnType("integer");
b.Property<string>("State") b.Property<string>("State")
.IsRequired() .IsRequired()
.HasColumnType("text"); .HasColumnType("text");
@ -400,6 +403,8 @@ namespace Minecraft_Realms_Emulator.Migrations
b.HasKey("Id"); b.HasKey("Id");
b.HasIndex("ParentWorldId");
b.ToTable("Worlds"); b.ToTable("Worlds");
}); });
@ -469,6 +474,15 @@ namespace Minecraft_Realms_Emulator.Migrations
b.Navigation("World"); b.Navigation("World");
}); });
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.World", b =>
{
b.HasOne("Minecraft_Realms_Emulator.Entities.World", "ParentWorld")
.WithMany()
.HasForeignKey("ParentWorldId");
b.Navigation("ParentWorld");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.World", b => modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.World", b =>
{ {
b.Navigation("Players"); b.Navigation("Players");

View File

@ -22,7 +22,12 @@ namespace Minecraft_Realms_Emulator.Modes.External
[CheckRealmOwner] [CheckRealmOwner]
public async Task<ActionResult<SubscriptionResponse>> Get(int wId) public async Task<ActionResult<SubscriptionResponse>> Get(int wId)
{ {
var world = await _context.Worlds.Include(w => w.Subscription).FirstOrDefaultAsync(w => w.Id == wId); var world = await _context.Worlds.Include(w => w.Subscription).Include(w => w.ParentWorld.Subscription).FirstOrDefaultAsync(w => w.Id == wId);
if (world.ParentWorld != null)
{
world.Subscription = world.ParentWorld.Subscription;
}
if (world?.Subscription == null) return NotFound("Subscription not found"); if (world?.Subscription == null) return NotFound("Subscription not found");

View File

@ -139,6 +139,246 @@ namespace Minecraft_Realms_Emulator.Modes.External
return Ok(servers); return Ok(servers);
} }
[HttpGet("listUserWorldsOfType/any")]
public async Task<ActionResult<ServersResponse>> GetWorldsSnapshot()
{
string cookie = Request.Headers.Cookie;
string playerUUID = cookie.Split(";")[0].Split(":")[2];
string playerName = cookie.Split(";")[1].Split("=")[1];
string gameVersion = cookie.Split(";")[2].Split("=")[1];
var ownedWorlds = await _context.Worlds.Where(w => w.OwnerUUID == playerUUID || w.ParentWorld.OwnerUUID == playerUUID).Include(w => w.Subscription).Include(w => w.Slots).Include(w => w.ParentWorld).ToListAsync();
var memberWorlds = await _context.Players.Where(p => p.Uuid == playerUUID && p.Accepted).Include(p => p.World.Subscription).Include(p => p.World.Slots).Include(p => p.World.ParentWorld).Select(p => p.World).ToListAsync();
List<WorldResponse> allWorlds = [];
foreach (var world in ownedWorlds)
{
Slot activeSlot = world.Slots.Find(s => s.SlotId == world.ActiveSlot);
int versionsCompared = new MinecraftVersionParser.MinecraftVersion(gameVersion).CompareTo(new MinecraftVersionParser.MinecraftVersion(activeSlot?.Version ?? gameVersion));
string isCompatible = versionsCompared == 0 ? nameof(CompatibilityEnum.COMPATIBLE) : versionsCompared < 0 ? nameof(CompatibilityEnum.NEEDS_DOWNGRADE) : nameof(CompatibilityEnum.NEEDS_UPGRADE);
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,
ActiveVersion = activeSlot?.Version ?? gameVersion,
Compatibility = isCompatible
};
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;
}
if (world.ParentWorld == null)
{
response.ParentWorldId = -1;
}
if (world.ParentWorld != null)
{
response.Owner = world.ParentWorld.Owner;
response.OwnerUUID = world.ParentWorld.OwnerUUID;
response.ParentWorldId = world.ParentWorld.Id;
response.ParentWorldName = world.ParentWorld.Name;
}
allWorlds.Add(response);
}
foreach (var world in memberWorlds)
{
Slot activeSlot = world.Slots.Find(s => s.SlotId == world.ActiveSlot);
int versionsCompared = new MinecraftVersionParser.MinecraftVersion(gameVersion).CompareTo(new MinecraftVersionParser.MinecraftVersion(activeSlot.Version));
string isCompatible = versionsCompared == 0 ? nameof(CompatibilityEnum.COMPATIBLE) : versionsCompared < 0 ? nameof(CompatibilityEnum.NEEDS_DOWNGRADE) : nameof(CompatibilityEnum.NEEDS_UPGRADE);
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,
ActiveVersion = activeSlot.Version,
Compatibility = isCompatible
};
if (world.ParentWorld == null)
{
response.ParentWorldId = -1;
}
if (world.ParentWorld != null)
{
response.Owner = world.ParentWorld.Owner;
response.OwnerUUID = world.ParentWorld.OwnerUUID;
response.ParentWorldId = world.ParentWorld.Id;
response.ParentWorldName = world.ParentWorld.Name;
}
allWorlds.Add(response);
}
ServersResponse servers = new()
{
Servers = allWorlds
};
return Ok(servers);
}
[HttpGet("listPrereleaseEligibleWorlds")]
public async Task<ActionResult<ServersResponse>> GetPrereleaseWorlds()
{
string cookie = Request.Headers.Cookie;
string playerUUID = cookie.Split(";")[0].Split(":")[2];
string playerName = cookie.Split(";")[1].Split("=")[1];
string gameVersion = cookie.Split(";")[2].Split("=")[1];
var ownedWorlds = await _context.Worlds.Where(w => w.ParentWorld != null && w.ParentWorld.OwnerUUID == playerUUID).Include(w => w.Subscription).Include(w => w.Slots).Include(w => w.ParentWorld).ToListAsync();
var memberWorlds = await _context.Players.Where(p => p.World.ParentWorld != null && p.Uuid == playerUUID && p.Accepted).Include(p => p.World.Subscription).Include(p => p.World.Slots).Include(p => p.World.ParentWorld).Select(p => p.World).ToListAsync();
List<WorldResponse> allWorlds = [];
if (ownedWorlds.ToArray().Length == 0 && new ConfigHelper(_context).GetSetting(nameof(SettingsEnum.AutomaticRealmsCreation)).Value)
{
var parentWorld = _context.Worlds.FirstOrDefault(w => w.OwnerUUID == playerUUID && w.ParentWorld == null);
if (parentWorld != null && parentWorld.State != nameof(StateEnum.UNINITIALIZED))
{
var world = new World
{
Name = parentWorld.Name,
Motd = null,
State = nameof(StateEnum.UNINITIALIZED),
WorldType = nameof(WorldTypeEnum.NORMAL),
MaxPlayers = 10,
MinigameId = null,
MinigameName = null,
MinigameImage = null,
ActiveSlot = 1,
Member = false,
ParentWorld = parentWorld,
};
ownedWorlds.Add(world);
_context.Worlds.Add(world);
_context.SaveChanges();
}
}
foreach (var world in ownedWorlds)
{
Slot activeSlot = world.Slots.Find(s => s.SlotId == world.ActiveSlot);
int versionsCompared = new MinecraftVersionParser.MinecraftVersion(gameVersion).CompareTo(new MinecraftVersionParser.MinecraftVersion(activeSlot?.Version ?? gameVersion));
string isCompatible = versionsCompared == 0 ? nameof(CompatibilityEnum.COMPATIBLE) : versionsCompared < 0 ? nameof(CompatibilityEnum.NEEDS_DOWNGRADE) : nameof(CompatibilityEnum.NEEDS_UPGRADE);
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,
ActiveVersion = activeSlot?.Version ?? gameVersion,
Compatibility = isCompatible,
ParentWorldId = world.ParentWorld.Id,
ParentWorldName = world.ParentWorld.Name,
};
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)
{
Slot activeSlot = world.Slots.Find(s => s.SlotId == world.ActiveSlot);
int versionsCompared = new MinecraftVersionParser.MinecraftVersion(gameVersion).CompareTo(new MinecraftVersionParser.MinecraftVersion(activeSlot.Version));
string isCompatible = versionsCompared == 0 ? nameof(CompatibilityEnum.COMPATIBLE) : versionsCompared < 0 ? nameof(CompatibilityEnum.NEEDS_DOWNGRADE) : nameof(CompatibilityEnum.NEEDS_UPGRADE);
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,
ActiveVersion = activeSlot.Version,
Compatibility = isCompatible
};
allWorlds.Add(response);
}
ServersResponse servers = new()
{
Servers = allWorlds
};
return Ok(servers);
}
[HttpGet("{wId}")] [HttpGet("{wId}")]
[CheckForWorld] [CheckForWorld]
[CheckRealmOwner] [CheckRealmOwner]
@ -147,7 +387,7 @@ namespace Minecraft_Realms_Emulator.Modes.External
string cookie = Request.Headers.Cookie; string cookie = Request.Headers.Cookie;
string gameVersion = cookie.Split(";")[2].Split("=")[1]; string gameVersion = cookie.Split(";")[2].Split("=")[1];
var world = await _context.Worlds.Include(w => w.Players).Include(w => w.Subscription).Include(w => w.Slots).FirstOrDefaultAsync(w => w.Id == wId); var world = await _context.Worlds.Include(w => w.Players).Include(w => w.Subscription).Include(w => w.Slots).Include(w => w.ParentWorld.Subscription).FirstOrDefaultAsync(w => w.Id == wId);
Slot activeSlot = world.Slots.Find(s => s.SlotId == world.ActiveSlot); Slot activeSlot = world.Slots.Find(s => s.SlotId == world.ActiveSlot);
@ -181,6 +421,11 @@ namespace Minecraft_Realms_Emulator.Modes.External
var activeSlotOptions = JsonConvert.DeserializeObject<SlotOptionsResponse>(slots.Find(s => s.SlotId == activeSlot.SlotId).Options); var activeSlotOptions = JsonConvert.DeserializeObject<SlotOptionsResponse>(slots.Find(s => s.SlotId == activeSlot.SlotId).Options);
if (world.ParentWorld != null)
{
world.Subscription = world.ParentWorld.Subscription;
}
WorldResponse response = new() WorldResponse response = new()
{ {
Id = world.Id, Id = world.Id,
@ -279,6 +524,80 @@ namespace Minecraft_Realms_Emulator.Modes.External
return Ok(world); return Ok(world);
} }
[HttpPost("{wId}/createPrereleaseRealm")]
[CheckForWorld]
[CheckRealmOwner]
public async Task<ActionResult<World>> CreatePrereleaseRealms(int wId)
{
string cookie = Request.Headers.Cookie;
string gameVersion = cookie.Split(";")[2].Split("=")[1];
var worlds = await _context.Worlds.ToListAsync();
var world = worlds.Find(w => w.Id == wId);
if (world.ParentWorld.State == nameof(StateEnum.UNINITIALIZED))
{
ErrorResponse errorResponse = new()
{
ErrorCode = 401,
ErrorMsg = "You must initialize release world first",
};
return StatusCode(401, errorResponse);
}
if (world.State != nameof(StateEnum.UNINITIALIZED))
{
ErrorResponse errorResponse = new()
{
ErrorCode = 401,
ErrorMsg = "A prerealease realm is already created for this world",
};
return StatusCode(401, errorResponse);
}
world.Name = $"[PRE] {world.ParentWorld.Name}";
world.Motd = $"[PRE] {world.ParentWorld.Motd}";
world.State = nameof(StateEnum.OPEN);
var config = new ConfigHelper(_context);
var defaultServerAddress = config.GetSetting(nameof(SettingsEnum.DefaultServerAddress));
var connection = new Connection
{
World = world,
Address = defaultServerAddress.Value
};
Slot slot = new()
{
World = world,
SlotId = 1,
SlotName = "",
Version = gameVersion,
GameMode = 0,
Difficulty = 2,
SpawnProtection = 0,
ForceGameMode = false,
Pvp = true,
SpawnAnimals = true,
SpawnMonsters = true,
SpawnNPCs = true,
CommandBlocks = false
};
_context.Worlds.Update(world);
_context.Connections.Add(connection);
_context.Slots.Add(slot);
_context.SaveChanges();
return Ok(world);
}
[HttpPost("{wId}/reset")] [HttpPost("{wId}/reset")]
[CheckForWorld] [CheckForWorld]
[CheckRealmOwner] [CheckRealmOwner]
@ -464,6 +783,20 @@ namespace Minecraft_Realms_Emulator.Modes.External
{ {
var world = _context.Worlds.Find(wId); var world = _context.Worlds.Find(wId);
if (world.ParentWorld == null)
{
var snapshotWorld = _context.Worlds.FirstOrDefault(w => w.ParentWorld.Id == wId);
if (snapshotWorld != null)
{
_context.Worlds.Remove(snapshotWorld);
}
}
else
{
_context.Worlds.Remove(world.ParentWorld);
}
_context.Worlds.Remove(world); _context.Worlds.Remove(world);
_context.SaveChanges(); _context.SaveChanges();

View File

@ -22,7 +22,12 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
[CheckRealmOwner] [CheckRealmOwner]
public async Task<ActionResult<SubscriptionResponse>> Get(int wId) public async Task<ActionResult<SubscriptionResponse>> Get(int wId)
{ {
var world = await _context.Worlds.Include(w => w.Subscription).FirstOrDefaultAsync(w => w.Id == wId); var world = await _context.Worlds.Include(w => w.Subscription).Include(w => w.ParentWorld.Subscription).FirstOrDefaultAsync(w => w.Id == wId);
if (world.ParentWorld != null)
{
world.Subscription = world.ParentWorld.Subscription;
}
if (world?.Subscription == null) return NotFound("Subscription not found"); if (world?.Subscription == null) return NotFound("Subscription not found");

View File

@ -142,6 +142,246 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
return Ok(servers); return Ok(servers);
} }
[HttpGet("listUserWorldsOfType/any")]
public async Task<ActionResult<ServersResponse>> GetWorldsSnapshot()
{
string cookie = Request.Headers.Cookie;
string playerUUID = cookie.Split(";")[0].Split(":")[2];
string playerName = cookie.Split(";")[1].Split("=")[1];
string gameVersion = cookie.Split(";")[2].Split("=")[1];
var ownedWorlds = await _context.Worlds.Where(w => w.OwnerUUID == playerUUID || w.ParentWorld.OwnerUUID == playerUUID).Include(w => w.Subscription).Include(w => w.Slots).Include(w => w.ParentWorld).ToListAsync();
var memberWorlds = await _context.Players.Where(p => p.Uuid == playerUUID && p.Accepted).Include(p => p.World.Subscription).Include(p => p.World.Slots).Include(p => p.World.ParentWorld).Select(p => p.World).ToListAsync();
List<WorldResponse> allWorlds = [];
foreach (var world in ownedWorlds)
{
Slot activeSlot = world.Slots.Find(s => s.SlotId == world.ActiveSlot);
int versionsCompared = new MinecraftVersionParser.MinecraftVersion(gameVersion).CompareTo(new MinecraftVersionParser.MinecraftVersion(activeSlot?.Version ?? gameVersion));
string isCompatible = versionsCompared == 0 ? nameof(CompatibilityEnum.COMPATIBLE) : versionsCompared < 0 ? nameof(CompatibilityEnum.NEEDS_DOWNGRADE) : nameof(CompatibilityEnum.NEEDS_UPGRADE);
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,
ActiveVersion = activeSlot?.Version ?? gameVersion,
Compatibility = isCompatible
};
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;
}
if (world.ParentWorld == null)
{
response.ParentWorldId = -1;
}
if (world.ParentWorld != null)
{
response.Owner = world.ParentWorld.Owner;
response.OwnerUUID = world.ParentWorld.OwnerUUID;
response.ParentWorldId = world.ParentWorld.Id;
response.ParentWorldName = world.ParentWorld.Name;
}
allWorlds.Add(response);
}
foreach (var world in memberWorlds)
{
Slot activeSlot = world.Slots.Find(s => s.SlotId == world.ActiveSlot);
int versionsCompared = new MinecraftVersionParser.MinecraftVersion(gameVersion).CompareTo(new MinecraftVersionParser.MinecraftVersion(activeSlot.Version));
string isCompatible = versionsCompared == 0 ? nameof(CompatibilityEnum.COMPATIBLE) : versionsCompared < 0 ? nameof(CompatibilityEnum.NEEDS_DOWNGRADE) : nameof(CompatibilityEnum.NEEDS_UPGRADE);
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,
ActiveVersion = activeSlot.Version,
Compatibility = isCompatible
};
if (world.ParentWorld == null)
{
response.ParentWorldId = -1;
}
if (world.ParentWorld != null)
{
response.Owner = world.ParentWorld.Owner;
response.OwnerUUID = world.ParentWorld.OwnerUUID;
response.ParentWorldId = world.ParentWorld.Id;
response.ParentWorldName = world.ParentWorld.Name;
}
allWorlds.Add(response);
}
ServersResponse servers = new()
{
Servers = allWorlds
};
return Ok(servers);
}
[HttpGet("listPrereleaseEligibleWorlds")]
public async Task<ActionResult<ServersResponse>> GetPrereleaseWorlds()
{
string cookie = Request.Headers.Cookie;
string playerUUID = cookie.Split(";")[0].Split(":")[2];
string playerName = cookie.Split(";")[1].Split("=")[1];
string gameVersion = cookie.Split(";")[2].Split("=")[1];
var ownedWorlds = await _context.Worlds.Where(w => w.ParentWorld != null && w.ParentWorld.OwnerUUID == playerUUID).Include(w => w.Subscription).Include(w => w.Slots).Include(w => w.ParentWorld).ToListAsync();
var memberWorlds = await _context.Players.Where(p => p.World.ParentWorld != null && p.Uuid == playerUUID && p.Accepted).Include(p => p.World.Subscription).Include(p => p.World.Slots).Include(p => p.World.ParentWorld).Select(p => p.World).ToListAsync();
List<WorldResponse> allWorlds = [];
if (ownedWorlds.ToArray().Length == 0 && new ConfigHelper(_context).GetSetting(nameof(SettingsEnum.AutomaticRealmsCreation)).Value)
{
var parentWorld = _context.Worlds.FirstOrDefault(w => w.OwnerUUID == playerUUID && w.ParentWorld == null);
if (parentWorld != null && parentWorld.State != nameof(StateEnum.UNINITIALIZED))
{
var world = new World
{
Name = parentWorld.Name,
Motd = null,
State = nameof(StateEnum.UNINITIALIZED),
WorldType = nameof(WorldTypeEnum.NORMAL),
MaxPlayers = 10,
MinigameId = null,
MinigameName = null,
MinigameImage = null,
ActiveSlot = 1,
Member = false,
ParentWorld = parentWorld,
};
ownedWorlds.Add(world);
_context.Worlds.Add(world);
_context.SaveChanges();
}
}
foreach (var world in ownedWorlds)
{
Slot activeSlot = world.Slots.Find(s => s.SlotId == world.ActiveSlot);
int versionsCompared = new MinecraftVersionParser.MinecraftVersion(gameVersion).CompareTo(new MinecraftVersionParser.MinecraftVersion(activeSlot?.Version ?? gameVersion));
string isCompatible = versionsCompared == 0 ? nameof(CompatibilityEnum.COMPATIBLE) : versionsCompared < 0 ? nameof(CompatibilityEnum.NEEDS_DOWNGRADE) : nameof(CompatibilityEnum.NEEDS_UPGRADE);
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,
ActiveVersion = activeSlot?.Version ?? gameVersion,
Compatibility = isCompatible,
ParentWorldId = world.ParentWorld.Id,
ParentWorldName = world.ParentWorld.Name,
};
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)
{
Slot activeSlot = world.Slots.Find(s => s.SlotId == world.ActiveSlot);
int versionsCompared = new MinecraftVersionParser.MinecraftVersion(gameVersion).CompareTo(new MinecraftVersionParser.MinecraftVersion(activeSlot.Version));
string isCompatible = versionsCompared == 0 ? nameof(CompatibilityEnum.COMPATIBLE) : versionsCompared < 0 ? nameof(CompatibilityEnum.NEEDS_DOWNGRADE) : nameof(CompatibilityEnum.NEEDS_UPGRADE);
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,
ActiveVersion = activeSlot.Version,
Compatibility = isCompatible
};
allWorlds.Add(response);
}
ServersResponse servers = new()
{
Servers = allWorlds
};
return Ok(servers);
}
[HttpGet("{wId}")] [HttpGet("{wId}")]
[CheckForWorld] [CheckForWorld]
[CheckRealmOwner] [CheckRealmOwner]
@ -150,7 +390,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
string cookie = Request.Headers.Cookie; string cookie = Request.Headers.Cookie;
string gameVersion = cookie.Split(";")[2].Split("=")[1]; string gameVersion = cookie.Split(";")[2].Split("=")[1];
var world = await _context.Worlds.Include(w => w.Players).Include(w => w.Subscription).Include(w => w.Slots).FirstOrDefaultAsync(w => w.Id == wId); var world = await _context.Worlds.Include(w => w.Players).Include(w => w.Subscription).Include(w => w.Slots).Include(w => w.ParentWorld.Subscription).FirstOrDefaultAsync(w => w.Id == wId);
Slot activeSlot = world.Slots.Find(s => s.SlotId == world.ActiveSlot); Slot activeSlot = world.Slots.Find(s => s.SlotId == world.ActiveSlot);
@ -184,6 +424,11 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
var activeSlotOptions = JsonConvert.DeserializeObject<SlotOptionsResponse>(slots.Find(s => s.SlotId == activeSlot.SlotId).Options); var activeSlotOptions = JsonConvert.DeserializeObject<SlotOptionsResponse>(slots.Find(s => s.SlotId == activeSlot.SlotId).Options);
if (world.ParentWorld != null)
{
world.Subscription = world.ParentWorld.Subscription;
}
WorldResponse response = new() WorldResponse response = new()
{ {
Id = world.Id, Id = world.Id,
@ -294,6 +539,91 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
return Ok(world); return Ok(world);
} }
[HttpPost("{wId}/createPrereleaseRealm")]
[CheckForWorld]
[CheckRealmOwner]
public async Task<ActionResult<World>> CreatePrereleaseRealms(int wId)
{
string cookie = Request.Headers.Cookie;
string gameVersion = cookie.Split(";")[2].Split("=")[1];
var worlds = await _context.Worlds.ToListAsync();
var world = worlds.Find(w => w.Id == wId);
if (world.ParentWorld.State == nameof(StateEnum.UNINITIALIZED))
{
ErrorResponse errorResponse = new()
{
ErrorCode = 401,
ErrorMsg = "You must initialize release world first",
};
return StatusCode(401, errorResponse);
}
if (world.State != nameof(StateEnum.UNINITIALIZED))
{
ErrorResponse errorResponse = new()
{
ErrorCode = 401,
ErrorMsg = "A prerealease realm is already created for this world",
};
return StatusCode(401, errorResponse);
}
world.Name = $"[PRE] {world.ParentWorld.Name}";
world.Motd = $"[PRE] {world.ParentWorld.Motd}";
world.State = nameof(StateEnum.OPEN);
var config = new ConfigHelper(_context);
var defaultServerAddress = config.GetSetting(nameof(SettingsEnum.DefaultServerAddress));
static int FindFreeTcpPort()
{
TcpListener l = new(IPAddress.Loopback, 0);
l.Start();
int port = ((IPEndPoint)l.LocalEndpoint).Port;
l.Stop();
return port;
}
var port = FindFreeTcpPort();
var connection = new Connection
{
World = world,
Address = $"{defaultServerAddress.Value}:{port}"
};
Slot slot = new()
{
World = world,
SlotId = 1,
SlotName = "",
Version = gameVersion,
GameMode = 0,
Difficulty = 2,
SpawnProtection = 0,
ForceGameMode = false,
Pvp = true,
SpawnAnimals = true,
SpawnMonsters = true,
SpawnNPCs = true,
CommandBlocks = false
};
_context.Worlds.Update(world);
_context.Connections.Add(connection);
_context.Slots.Add(slot);
_context.SaveChanges();
return Ok(world);
}
[HttpPost("{wId}/reset")] [HttpPost("{wId}/reset")]
[CheckForWorld] [CheckForWorld]
[CheckRealmOwner] [CheckRealmOwner]
@ -522,6 +852,22 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers
{ {
var world = _context.Worlds.Find(wId); var world = _context.Worlds.Find(wId);
if (world.ParentWorld == null)
{
var snapshotWorld = _context.Worlds.FirstOrDefault(w => w.ParentWorld.Id == wId);
if (snapshotWorld != null)
{
new DockerHelper(snapshotWorld).DeleteServer();
_context.Worlds.Remove(snapshotWorld);
}
}
else
{
new DockerHelper(world.ParentWorld).DeleteServer();
_context.Worlds.Remove(world.ParentWorld);
}
new DockerHelper(world).DeleteServer(); new DockerHelper(world).DeleteServer();
_context.Worlds.Remove(world); _context.Worlds.Remove(world);

View File

@ -11,5 +11,7 @@ namespace Minecraft_Realms_Emulator.Entities
public string Compatibility { get; set; } = null!; public string Compatibility { get; set; } = null!;
public List<SlotResponse> Slots { get; set; } = null!; public List<SlotResponse> Slots { get; set; } = null!;
public string ActiveVersion { get; set; } = null!; public string ActiveVersion { get; set; } = null!;
public int? ParentWorldId { get; set; }
public string? ParentWorldName { get; set; }
} }
} }