From e554ee04c74e8417862788e82c66aa31bf674b7d Mon Sep 17 00:00:00 2001 From: jvyden Date: Wed, 6 Oct 2021 20:20:01 -0400 Subject: [PATCH] Levels save into database --- DatabaseMigrations/2.sql | 36 +++++++++++++++++++ .../Controllers/PublishController.cs | 31 +++++++++++++++- ProjectLighthouse/Database.cs | 1 + ProjectLighthouse/Types/ServerSettings.cs | 3 +- ProjectLighthouse/Types/Slot.cs | 27 ++++++++++++-- 5 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 DatabaseMigrations/2.sql diff --git a/DatabaseMigrations/2.sql b/DatabaseMigrations/2.sql new file mode 100644 index 00000000..6fbeb4c3 --- /dev/null +++ b/DatabaseMigrations/2.sql @@ -0,0 +1,36 @@ +create table Slots +( + SlotId int, + CreatorId int not null, + Name text not null, + Description text not null, + IconHash text not null, + RootLevel text not null, + Resource text not null, + LocationId int not null, + InitiallyLocked bool default false not null, + SubLevel bool default false null, + Lbp1Only bool default false not null, + Shareable int default 0 not null, + AuthorLabels text not null, + BackgroundHash text not null, + MinimumPlayers int default 1 not null, + MaximumPlayers int default 4 not null, + MoveRequired bool default false null +); + +create unique index Slots_SlotId_uindex + on Slots (SlotId); + +alter table Slots + add constraint Slots_pk + primary key (SlotId); + +alter table Slots + modify SlotId int auto_increment; + +alter table Slots + alter column CreatorId set default -1; + +alter table Slots + modify CreatorId int not null after LocationId; diff --git a/ProjectLighthouse/Controllers/PublishController.cs b/ProjectLighthouse/Controllers/PublishController.cs index 6536f45b..9e637321 100644 --- a/ProjectLighthouse/Controllers/PublishController.cs +++ b/ProjectLighthouse/Controllers/PublishController.cs @@ -1,8 +1,10 @@ using System; using System.IO; +using System.Linq; using System.Threading.Tasks; using System.Xml.Serialization; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; using ProjectLighthouse.Serialization; using ProjectLighthouse.Types; @@ -11,17 +13,44 @@ namespace ProjectLighthouse.Controllers { [Route("LITTLEBIGPLANETPS3_XML/")] [Produces("text/xml")] public class PublishController : ControllerBase { + /// + /// Endpoint the game uses to verify that the level is compatible (?) + /// [HttpPost("startPublish")] public async Task StartPublish() { Slot slot = await this.GetSlotFromBody(); + if(slot == null) return this.BadRequest(); // if the level cant be parsed then it obviously cant be uploaded + return this.Ok(LbpSerializer.TaggedStringElement("slot", "", "type", "user")); } + /// + /// Endpoint actually used to publish a level + /// [HttpPost("publish")] public async Task Publish() { + await using Database database = new(); + + User user = await database.Users.FirstOrDefaultAsync(u => u.Username == "jvyden"); + Slot slot = await this.GetSlotFromBody(); + + //TODO: parse location in body + Location l = new() { + X = 0, + Y = 0 + }; + database.Locations.Add(l); + await database.SaveChangesAsync(); + slot.LocationId = l.Id; + slot.CreatorId = user.UserId; + + database.Slots.Add(slot); + await database.SaveChangesAsync(); + + Request.Body.Position = 0; string bodyString = await new StreamReader(Request.Body).ReadToEndAsync(); - return this.Ok(LbpSerializer.TaggedStringElement("slot", bodyString, "type", "user")); + return this.Ok(bodyString); } public async Task GetSlotFromBody() { diff --git a/ProjectLighthouse/Database.cs b/ProjectLighthouse/Database.cs index bdab87bc..0fc6bb3b 100644 --- a/ProjectLighthouse/Database.cs +++ b/ProjectLighthouse/Database.cs @@ -33,5 +33,6 @@ namespace ProjectLighthouse { public DbSet Users { get; set; } public DbSet Locations { get; set; } + public DbSet Slots { get; set; } } } \ No newline at end of file diff --git a/ProjectLighthouse/Types/ServerSettings.cs b/ProjectLighthouse/Types/ServerSettings.cs index 0f5de5fa..2699ab0c 100644 --- a/ProjectLighthouse/Types/ServerSettings.cs +++ b/ProjectLighthouse/Types/ServerSettings.cs @@ -25,7 +25,8 @@ namespace ProjectLighthouse.Types { try { return new Database().Database.CanConnect(); } - catch { + catch(Exception e) { + Console.WriteLine(e); return false; } } diff --git a/ProjectLighthouse/Types/Slot.cs b/ProjectLighthouse/Types/Slot.cs index c6358407..f21ed725 100644 --- a/ProjectLighthouse/Types/Slot.cs +++ b/ProjectLighthouse/Types/Slot.cs @@ -1,4 +1,6 @@ +using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; using System.Xml.Serialization; namespace ProjectLighthouse.Types { @@ -10,6 +12,11 @@ namespace ProjectLighthouse.Types { [XmlAttribute("type")] [NotMapped] public string Type { get; set; } + + [Key] + [XmlIgnore] + public int SlotId { get; set; } + [XmlElement("name")] public string Name { get; set; } @@ -26,8 +33,25 @@ namespace ProjectLighthouse.Types { [XmlElement("resource")] public string Resource { get; set; } + [XmlIgnore] + public int LocationId { get; set; } + + [XmlIgnore] + public int CreatorId { get; set; } + + private Location location; + + /// + /// The location of the level on the creator's earth + /// [XmlElement("location")] - public Location Location { get; set; } + public Location Location { + get { + if(location != null) return this.location; + + return location = new Database().Locations.First(l => l.Id == LocationId); + } + } [XmlElement("initiallyLocked")] public bool InitiallyLocked { get; set; } @@ -55,6 +79,5 @@ namespace ProjectLighthouse.Types { [XmlElement("moveRequired")] public bool MoveRequired { get; set; } - } } \ No newline at end of file