From 9183c566a685e18433898a38ff1af3fa24184030 Mon Sep 17 00:00:00 2001 From: CyberL1 Date: Fri, 2 Feb 2024 18:53:52 +0100 Subject: [PATCH] feat: GET /worlds, GET /worlds/{id} --- .../Controllers/Mco/AvailableController.cs | 3 +- .../Mco/Client/CompatibleController.cs | 3 +- .../Controllers/TrialController.cs | 3 +- .../Controllers/WorldsController.cs | 42 ++++++++ Minecraft-Realms-Emulator/Data/DataContext.cs | 10 ++ .../Entities/ServersArray.cs | 7 ++ Minecraft-Realms-Emulator/Entities/State.cs | 10 ++ Minecraft-Realms-Emulator/Entities/World.cs | 25 +++++ .../Entities/WorldType.cs | 10 ++ .../20240202175209_Initial.Designer.cs | 101 ++++++++++++++++++ .../Migrations/20240202175209_Initial.cs | 52 +++++++++ .../Migrations/DataContextModelSnapshot.cs | 98 +++++++++++++++++ .../Minecraft-Realms-Emulator.csproj | 8 +- .../Minecraft-Realms-Emulator.http | 6 ++ Minecraft-Realms-Emulator/Program.cs | 8 ++ Minecraft-Realms-Emulator/appsettings.json | 17 +-- 16 files changed, 389 insertions(+), 14 deletions(-) create mode 100644 Minecraft-Realms-Emulator/Controllers/WorldsController.cs create mode 100644 Minecraft-Realms-Emulator/Data/DataContext.cs create mode 100644 Minecraft-Realms-Emulator/Entities/ServersArray.cs create mode 100644 Minecraft-Realms-Emulator/Entities/State.cs create mode 100644 Minecraft-Realms-Emulator/Entities/World.cs create mode 100644 Minecraft-Realms-Emulator/Entities/WorldType.cs create mode 100644 Minecraft-Realms-Emulator/Migrations/20240202175209_Initial.Designer.cs create mode 100644 Minecraft-Realms-Emulator/Migrations/20240202175209_Initial.cs create mode 100644 Minecraft-Realms-Emulator/Migrations/DataContextModelSnapshot.cs diff --git a/Minecraft-Realms-Emulator/Controllers/Mco/AvailableController.cs b/Minecraft-Realms-Emulator/Controllers/Mco/AvailableController.cs index 21e08a9..801886e 100644 --- a/Minecraft-Realms-Emulator/Controllers/Mco/AvailableController.cs +++ b/Minecraft-Realms-Emulator/Controllers/Mco/AvailableController.cs @@ -1,5 +1,4 @@ -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc; namespace Minecraft_Realms_Emulator.Controllers.Mco { diff --git a/Minecraft-Realms-Emulator/Controllers/Mco/Client/CompatibleController.cs b/Minecraft-Realms-Emulator/Controllers/Mco/Client/CompatibleController.cs index 751947d..a13e046 100644 --- a/Minecraft-Realms-Emulator/Controllers/Mco/Client/CompatibleController.cs +++ b/Minecraft-Realms-Emulator/Controllers/Mco/Client/CompatibleController.cs @@ -1,5 +1,4 @@ -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc; namespace Minecraft_Realms_Emulator.Controllers.Mco.Client { diff --git a/Minecraft-Realms-Emulator/Controllers/TrialController.cs b/Minecraft-Realms-Emulator/Controllers/TrialController.cs index 8af037c..314a9a2 100644 --- a/Minecraft-Realms-Emulator/Controllers/TrialController.cs +++ b/Minecraft-Realms-Emulator/Controllers/TrialController.cs @@ -1,5 +1,4 @@ -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc; namespace Minecraft_Realms_Emulator.Controllers { diff --git a/Minecraft-Realms-Emulator/Controllers/WorldsController.cs b/Minecraft-Realms-Emulator/Controllers/WorldsController.cs new file mode 100644 index 0000000..ab9671f --- /dev/null +++ b/Minecraft-Realms-Emulator/Controllers/WorldsController.cs @@ -0,0 +1,42 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using Minecraft_Realms_Emulator.Data; +using Minecraft_Realms_Emulator.Entities; + +namespace Minecraft_Realms_Emulator.Controllers +{ + [Route("[controller]")] + [ApiController] + public class WorldsController : ControllerBase + { + private readonly DataContext _context; + + public WorldsController(DataContext context) + { + _context = context; + } + + [HttpGet] + public async Task> GetWorlds() + { + var worlds = await _context.Worlds.ToListAsync(); + + ServersArray servers = new() + { + Servers = worlds + }; + + return Ok(servers); + } + + [HttpGet("{id}")] + public async Task> GetWorldById(int id) + { + var world = await _context.Worlds.FindAsync(id); + + if (world == null) return NotFound("World not found"); + + return world; + } + } +} diff --git a/Minecraft-Realms-Emulator/Data/DataContext.cs b/Minecraft-Realms-Emulator/Data/DataContext.cs new file mode 100644 index 0000000..0710246 --- /dev/null +++ b/Minecraft-Realms-Emulator/Data/DataContext.cs @@ -0,0 +1,10 @@ +using Microsoft.EntityFrameworkCore; +using Minecraft_Realms_Emulator.Entities; + +namespace Minecraft_Realms_Emulator.Data +{ + public class DataContext(DbContextOptions options) : DbContext(options) + { + public DbSet Worlds { get; set; } + } +} diff --git a/Minecraft-Realms-Emulator/Entities/ServersArray.cs b/Minecraft-Realms-Emulator/Entities/ServersArray.cs new file mode 100644 index 0000000..471e056 --- /dev/null +++ b/Minecraft-Realms-Emulator/Entities/ServersArray.cs @@ -0,0 +1,7 @@ +namespace Minecraft_Realms_Emulator.Entities +{ + public class ServersArray + { + public List? Servers { get; set; } + } +} diff --git a/Minecraft-Realms-Emulator/Entities/State.cs b/Minecraft-Realms-Emulator/Entities/State.cs new file mode 100644 index 0000000..824e7ea --- /dev/null +++ b/Minecraft-Realms-Emulator/Entities/State.cs @@ -0,0 +1,10 @@ +namespace Minecraft_Realms_Emulator.Entities +{ + public enum State + { + OPEN, + CLOSED, + ADMIN_LOCK, + UNINITIALIZED + } +} diff --git a/Minecraft-Realms-Emulator/Entities/World.cs b/Minecraft-Realms-Emulator/Entities/World.cs new file mode 100644 index 0000000..b96b39a --- /dev/null +++ b/Minecraft-Realms-Emulator/Entities/World.cs @@ -0,0 +1,25 @@ +namespace Minecraft_Realms_Emulator.Entities +{ + public class World + { + public int Id { get; set; } + public string? RemoteSubscriptionId { 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 string[] Players { get; set; } = []; + public int MaxPlayers { get; set; } = 10; + public string? MinigameName { get; set; } + public int? MinigameId { get; set; } + public string? MinigameImage { get; set; } + public int ActiveSlot { get; set; } = 1; + public string[] Slots { get; set; } = []; + public bool Member { get; set; } = false; + } +} \ No newline at end of file diff --git a/Minecraft-Realms-Emulator/Entities/WorldType.cs b/Minecraft-Realms-Emulator/Entities/WorldType.cs new file mode 100644 index 0000000..02601e2 --- /dev/null +++ b/Minecraft-Realms-Emulator/Entities/WorldType.cs @@ -0,0 +1,10 @@ +namespace Minecraft_Realms_Emulator.Entities +{ + public enum WorldType + { + NORMAL, + SUPERFLAT, + LARGEBIOMES, + AMPLIFIED + } +} diff --git a/Minecraft-Realms-Emulator/Migrations/20240202175209_Initial.Designer.cs b/Minecraft-Realms-Emulator/Migrations/20240202175209_Initial.Designer.cs new file mode 100644 index 0000000..734c86b --- /dev/null +++ b/Minecraft-Realms-Emulator/Migrations/20240202175209_Initial.Designer.cs @@ -0,0 +1,101 @@ +// +using System; +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("20240202175209_Initial")] + partial class Initial + { + /// + 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.World", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ActiveSlot") + .HasColumnType("integer"); + + b.Property("DaysLeft") + .HasColumnType("integer"); + + b.Property("Expired") + .HasColumnType("boolean"); + + b.Property("ExpiredTrial") + .HasColumnType("boolean"); + + b.Property("MaxPlayers") + .HasColumnType("integer"); + + b.Property("Member") + .HasColumnType("boolean"); + + b.Property("MinigameId") + .HasColumnType("integer"); + + b.Property("MinigameImage") + .HasColumnType("text"); + + b.Property("MinigameName") + .HasColumnType("text"); + + b.Property("Motd") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("Owner") + .HasColumnType("text"); + + b.Property("OwnerUUID") + .HasColumnType("text"); + + b.Property("Players") + .IsRequired() + .HasColumnType("text[]"); + + b.Property("RemoteSubscriptionId") + .HasColumnType("text"); + + b.Property("Slots") + .IsRequired() + .HasColumnType("text[]"); + + b.Property("State") + .IsRequired() + .HasColumnType("text"); + + b.Property("WorldType") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Worlds"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Minecraft-Realms-Emulator/Migrations/20240202175209_Initial.cs b/Minecraft-Realms-Emulator/Migrations/20240202175209_Initial.cs new file mode 100644 index 0000000..c293d3b --- /dev/null +++ b/Minecraft-Realms-Emulator/Migrations/20240202175209_Initial.cs @@ -0,0 +1,52 @@ +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace Minecraft_Realms_Emulator.Migrations +{ + /// + public partial class Initial : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Worlds", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + RemoteSubscriptionId = table.Column(type: "text", nullable: true), + Owner = table.Column(type: "text", nullable: true), + OwnerUUID = table.Column(type: "text", nullable: true), + Name = table.Column(type: "text", nullable: true), + Motd = table.Column(type: "text", nullable: true), + State = table.Column(type: "text", nullable: false), + DaysLeft = table.Column(type: "integer", nullable: false), + Expired = table.Column(type: "boolean", nullable: false), + ExpiredTrial = table.Column(type: "boolean", nullable: false), + WorldType = table.Column(type: "text", nullable: false), + Players = table.Column(type: "text[]", nullable: false), + MaxPlayers = table.Column(type: "integer", nullable: false), + MinigameName = table.Column(type: "text", nullable: true), + MinigameId = table.Column(type: "integer", nullable: true), + MinigameImage = table.Column(type: "text", nullable: true), + ActiveSlot = table.Column(type: "integer", nullable: false), + Slots = table.Column(type: "text[]", nullable: false), + Member = table.Column(type: "boolean", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Worlds", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Worlds"); + } + } +} diff --git a/Minecraft-Realms-Emulator/Migrations/DataContextModelSnapshot.cs b/Minecraft-Realms-Emulator/Migrations/DataContextModelSnapshot.cs new file mode 100644 index 0000000..c88b29e --- /dev/null +++ b/Minecraft-Realms-Emulator/Migrations/DataContextModelSnapshot.cs @@ -0,0 +1,98 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +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))] + partial class DataContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(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.World", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ActiveSlot") + .HasColumnType("integer"); + + b.Property("DaysLeft") + .HasColumnType("integer"); + + b.Property("Expired") + .HasColumnType("boolean"); + + b.Property("ExpiredTrial") + .HasColumnType("boolean"); + + b.Property("MaxPlayers") + .HasColumnType("integer"); + + b.Property("Member") + .HasColumnType("boolean"); + + b.Property("MinigameId") + .HasColumnType("integer"); + + b.Property("MinigameImage") + .HasColumnType("text"); + + b.Property("MinigameName") + .HasColumnType("text"); + + b.Property("Motd") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("Owner") + .HasColumnType("text"); + + b.Property("OwnerUUID") + .HasColumnType("text"); + + b.Property("Players") + .IsRequired() + .HasColumnType("text[]"); + + b.Property("RemoteSubscriptionId") + .HasColumnType("text"); + + b.Property("Slots") + .IsRequired() + .HasColumnType("text[]"); + + b.Property("State") + .IsRequired() + .HasColumnType("text"); + + b.Property("WorldType") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Worlds"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Minecraft-Realms-Emulator/Minecraft-Realms-Emulator.csproj b/Minecraft-Realms-Emulator/Minecraft-Realms-Emulator.csproj index 95b1282..44c0150 100644 --- a/Minecraft-Realms-Emulator/Minecraft-Realms-Emulator.csproj +++ b/Minecraft-Realms-Emulator/Minecraft-Realms-Emulator.csproj @@ -9,7 +9,13 @@ - + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/Minecraft-Realms-Emulator/Minecraft-Realms-Emulator.http b/Minecraft-Realms-Emulator/Minecraft-Realms-Emulator.http index 6374d25..308cded 100644 --- a/Minecraft-Realms-Emulator/Minecraft-Realms-Emulator.http +++ b/Minecraft-Realms-Emulator/Minecraft-Realms-Emulator.http @@ -6,5 +6,11 @@ Accept: application/json ### GET {{Minecraft_Realms_Emulator_HostAddress}}/mco/client/compatible +Accept: application/json + +### + +GET {{Minecraft_Realms_Emulator_HostAddress}}/worlds +Accept: application/json ### \ No newline at end of file diff --git a/Minecraft-Realms-Emulator/Program.cs b/Minecraft-Realms-Emulator/Program.cs index df2434c..a42223f 100644 --- a/Minecraft-Realms-Emulator/Program.cs +++ b/Minecraft-Realms-Emulator/Program.cs @@ -1,3 +1,6 @@ +using Microsoft.EntityFrameworkCore; +using Minecraft_Realms_Emulator.Data; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. @@ -7,6 +10,11 @@ builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); +builder.Services.AddDbContext(options => +{ + options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")); +}); + var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/Minecraft-Realms-Emulator/appsettings.json b/Minecraft-Realms-Emulator/appsettings.json index 10f68b8..bac46d0 100644 --- a/Minecraft-Realms-Emulator/appsettings.json +++ b/Minecraft-Realms-Emulator/appsettings.json @@ -1,9 +1,12 @@ { - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, - "AllowedHosts": "*" + "ConnectionStrings": { + "DefaultConnection": "User Id=postgres.aquqykciubzfolgrftwa;Password=8ypKY2pSWNRrlhCt;Server=aws-0-us-west-1.pooler.supabase.com;Port=5432;Database=postgres" + }, + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" }