Move world players to a separate table

This commit is contained in:
CyberL1 2024-02-21 19:02:52 +01:00
parent 200c71cf95
commit 0a9cb2d41c
9 changed files with 219 additions and 64 deletions

View File

@ -2,9 +2,8 @@
using Microsoft.EntityFrameworkCore;
using Minecraft_Realms_Emulator.Data;
using Minecraft_Realms_Emulator.Entities;
using Minecraft_Realms_Emulator.Migrations;
using Minecraft_Realms_Emulator.Responses;
using System;
using System.Text.Json;
namespace Minecraft_Realms_Emulator.Controllers
{
@ -53,9 +52,17 @@ namespace Minecraft_Realms_Emulator.Controllers
[HttpPut("accept/{id}")]
public ActionResult<bool> AcceptInvite(string id)
{
var invite = _context.Invites.FirstOrDefault(i => i.InvitationId == id);
string cookie = Request.Headers.Cookie;
string playerUUID = cookie.Split(";")[0].Split(":")[2];
var invite = _context.Invites.Include(i => i.World).FirstOrDefault(i => i.InvitationId == id);
if (invite == null) return NotFound("Invite not found");
var player = _context.Players.Where(p => p.World.Id == invite.World.Id).FirstOrDefault(p => p.Uuid == playerUUID);
player.Accepted = true;
_context.Invites.Remove(invite);
_context.SaveChanges();
@ -64,7 +71,7 @@ namespace Minecraft_Realms_Emulator.Controllers
}
[HttpPut("reject/{id}")]
public async Task<ActionResult<bool>> RejectInvite(string id)
public ActionResult<bool> RejectInvite(string id)
{
var invite = _context.Invites.Include(i => i.World).FirstOrDefault(i => i.InvitationId == id);
@ -75,10 +82,9 @@ namespace Minecraft_Realms_Emulator.Controllers
string cookie = Request.Headers.Cookie;
string playerUUID = cookie.Split(";")[0].Split(":")[2];
World world = await _context.Worlds.FindAsync(invite.World.Id);
var playerIndex = world.Players.FindIndex(p => p.RootElement.GetProperty("uuid").ToString() == playerUUID);
var player = _context.Players.Where(p => p.World.Id == invite.World.Id).FirstOrDefault(p => p.Uuid == playerUUID);
world.Players.RemoveAt(playerIndex);
_context.Players.Remove(player);
_context.SaveChanges();
@ -97,22 +103,28 @@ namespace Minecraft_Realms_Emulator.Controllers
if (world == null) return NotFound("World not found");
if (world.Players.Exists(p => p.RootElement.GetProperty("name").ToString() == body.Name)) return NotFound("Player already invited");
// Get player UUID
var playerInfo = await new HttpClient().GetFromJsonAsync<MinecraftPlayerInfo>($"https://api.mojang.com/users/profiles/minecraft/{body.Name}");
body.Uuid = playerInfo.Id;
JsonDocument player = JsonDocument.Parse(JsonSerializer.Serialize(body));
world.Players.Add(player);
var playerInDB = await _context.Players.Where(p => p.World.Id == wId).FirstOrDefaultAsync(p => p.Uuid == playerInfo.Id);
_context.Worlds.Update(world);
if (playerInDB?.Uuid == playerInfo.Id) return BadRequest("Player already invited");
Player player = new()
{
Name = body.Name,
Uuid = playerInfo.Id,
World = world
};
_context.Players.Add(player);
// world.Players.Add(player);
Invite invite = new()
{
InvitationId = Guid.NewGuid().ToString(),
World = world,
RecipeintUUID = body.Uuid,
RecipeintUUID = playerInfo.Id,
Date = DateTime.UtcNow,
};
@ -134,7 +146,7 @@ namespace Minecraft_Realms_Emulator.Controllers
var players = world.Players.ToList();
var playerIndex = world.Players.FindIndex(p => p.RootElement.GetProperty("uuid").ToString() == uuid);
var playerIndex = world.Players.FindIndex(p => p.Uuid == uuid);
world.Players.RemoveAt(playerIndex);

View File

@ -2,6 +2,7 @@
using Microsoft.EntityFrameworkCore;
using Minecraft_Realms_Emulator.Data;
using Minecraft_Realms_Emulator.Entities;
using System.Collections.Immutable;
namespace Minecraft_Realms_Emulator.Controllers
{
@ -24,9 +25,10 @@ namespace Minecraft_Realms_Emulator.Controllers
string playerUUID = cookie.Split(";")[0].Split(":")[2];
string playerName = cookie.Split(";")[1].Split("=")[1];
var worlds = await _context.Worlds.Where(w => w.OwnerUUID == playerUUID).ToListAsync();
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();
if (worlds.ToArray().Length == 0)
if (ownedWorlds.ToArray().Length == 0)
{
var world = new World
{
@ -40,7 +42,6 @@ namespace Minecraft_Realms_Emulator.Controllers
Expired = false,
ExpiredTrial = false,
WorldType = WorldType.NORMAL.ToString(),
Players = [],
MaxPlayers = 10,
MinigameId = null,
MinigameName = null,
@ -49,15 +50,20 @@ namespace Minecraft_Realms_Emulator.Controllers
Member = false
};
worlds.Add(world);
ownedWorlds.Add(world);
_context.Worlds.Add(world);
_context.SaveChanges();
}
List<World> allWorlds = [];
allWorlds.AddRange(ownedWorlds);
allWorlds.AddRange(memberWorlds);
ServersArray servers = new()
{
Servers = worlds
Servers = allWorlds
};
return Ok(servers);

View File

@ -10,5 +10,6 @@ namespace Minecraft_Realms_Emulator.Data
public DbSet<Connection> Connections { get; set; }
public DbSet<Backup> Backups { get; set; }
public DbSet<Invite> Invites { get; set; }
public DbSet<Player> Players { get; set; }
}
}

View File

@ -1,20 +1,14 @@
using System.Text.Json.Serialization;
namespace Minecraft_Realms_Emulator.Entities
namespace Minecraft_Realms_Emulator.Entities
{
public class Player
{
[JsonPropertyName("name")]
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
[JsonPropertyName("uuid")]
public string Uuid { get; set; } = string.Empty;
[JsonPropertyName("operator")]
public bool Operator { get; set; }
[JsonPropertyName("accepted")]
public bool Accepted { get; set; }
[JsonPropertyName("online")]
public bool Online { get; set; }
[JsonPropertyName("permission")]
public string Permission { get; set; } = "MEMBER";
public World? World { get; set; }
}
}

View File

@ -15,7 +15,7 @@ namespace Minecraft_Realms_Emulator.Entities
public bool Expired { get; set; } = false;
public bool ExpiredTrial { get; set; } = false;
public string WorldType { get; set; } = "NORMAL";
public List<JsonDocument> Players { get; set; } = [];
public List<Player> Players { get; set; } = [];
public int MaxPlayers { get; set; } = 10;
public string? MinigameName { get; set; }
public int? MinigameId { get; set; }

View File

@ -1,23 +0,0 @@
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Minecraft_Realms_Emulator.Migrations
{
/// <inheritdoc />
public partial class Worlds_Players : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql("ALTER TABLE \"Worlds\" ALTER COLUMN \"Players\" TYPE jsonb[] USING \"Players\"::jsonb[]");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql("ALTER TABLE \"Worlds\" ALTER COLUMN \"Players\" TYPE text[] USING \"Players\"::text[]");
}
}
}

View File

@ -13,8 +13,8 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace Minecraft_Realms_Emulator.Migrations
{
[DbContext(typeof(DataContext))]
[Migration("20240220110531_Worlds_Players")]
partial class Worlds_Players
[Migration("20240221131108_Players")]
partial class Players
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -112,6 +112,45 @@ namespace Minecraft_Realms_Emulator.Migrations
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")
@ -185,10 +224,6 @@ namespace Minecraft_Realms_Emulator.Migrations
b.Property<string>("OwnerUUID")
.HasColumnType("text");
b.Property<JsonDocument[]>("Players")
.IsRequired()
.HasColumnType("jsonb[]");
b.Property<string>("RemoteSubscriptionId")
.IsRequired()
.HasColumnType("text");
@ -243,6 +278,17 @@ namespace Minecraft_Realms_Emulator.Migrations
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")
@ -253,6 +299,11 @@ namespace Minecraft_Realms_Emulator.Migrations
b.Navigation("World");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.World", b =>
{
b.Navigation("Players");
});
#pragma warning restore 612, 618
}
}

View File

@ -0,0 +1,63 @@
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace Minecraft_Realms_Emulator.Migrations
{
/// <inheritdoc />
public partial class Players : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Players",
table: "Worlds");
migrationBuilder.CreateTable(
name: "Players",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Name = table.Column<string>(type: "text", nullable: false),
Uuid = table.Column<string>(type: "text", nullable: false),
Operator = table.Column<bool>(type: "boolean", nullable: false),
Accepted = table.Column<bool>(type: "boolean", nullable: false),
Online = table.Column<bool>(type: "boolean", nullable: false),
Permission = table.Column<string>(type: "text", nullable: false),
WorldId = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Players", x => x.Id);
table.ForeignKey(
name: "FK_Players_Worlds_WorldId",
column: x => x.WorldId,
principalTable: "Worlds",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_Players_WorldId",
table: "Players",
column: "WorldId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Players");
migrationBuilder.AddColumn<string[]>(
name: "Players",
table: "Worlds",
type: "text[]",
nullable: false,
defaultValue: new string[0]);
}
}
}

View File

@ -109,6 +109,45 @@ namespace Minecraft_Realms_Emulator.Migrations
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")
@ -182,10 +221,6 @@ namespace Minecraft_Realms_Emulator.Migrations
b.Property<string>("OwnerUUID")
.HasColumnType("text");
b.Property<JsonDocument[]>("Players")
.IsRequired()
.HasColumnType("jsonb[]");
b.Property<string>("RemoteSubscriptionId")
.IsRequired()
.HasColumnType("text");
@ -240,6 +275,17 @@ namespace Minecraft_Realms_Emulator.Migrations
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")
@ -250,6 +296,11 @@ namespace Minecraft_Realms_Emulator.Migrations
b.Navigation("World");
});
modelBuilder.Entity("Minecraft_Realms_Emulator.Entities.World", b =>
{
b.Navigation("Players");
});
#pragma warning restore 612, 618
}
}