Levels save into database

This commit is contained in:
jvyden 2021-10-06 20:20:01 -04:00
parent 15f974fba1
commit e554ee04c7
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
5 changed files with 94 additions and 4 deletions

36
DatabaseMigrations/2.sql Normal file
View file

@ -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;

View file

@ -1,8 +1,10 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Xml.Serialization; using System.Xml.Serialization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using ProjectLighthouse.Serialization; using ProjectLighthouse.Serialization;
using ProjectLighthouse.Types; using ProjectLighthouse.Types;
@ -11,17 +13,44 @@ namespace ProjectLighthouse.Controllers {
[Route("LITTLEBIGPLANETPS3_XML/")] [Route("LITTLEBIGPLANETPS3_XML/")]
[Produces("text/xml")] [Produces("text/xml")]
public class PublishController : ControllerBase { public class PublishController : ControllerBase {
/// <summary>
/// Endpoint the game uses to verify that the level is compatible (?)
/// </summary>
[HttpPost("startPublish")] [HttpPost("startPublish")]
public async Task<IActionResult> StartPublish() { public async Task<IActionResult> StartPublish() {
Slot slot = await this.GetSlotFromBody(); 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")); return this.Ok(LbpSerializer.TaggedStringElement("slot", "", "type", "user"));
} }
/// <summary>
/// Endpoint actually used to publish a level
/// </summary>
[HttpPost("publish")] [HttpPost("publish")]
public async Task<IActionResult> Publish() { public async Task<IActionResult> 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(); string bodyString = await new StreamReader(Request.Body).ReadToEndAsync();
return this.Ok(LbpSerializer.TaggedStringElement("slot", bodyString, "type", "user")); return this.Ok(bodyString);
} }
public async Task<Slot> GetSlotFromBody() { public async Task<Slot> GetSlotFromBody() {

View file

@ -33,5 +33,6 @@ namespace ProjectLighthouse {
public DbSet<User> Users { get; set; } public DbSet<User> Users { get; set; }
public DbSet<Location> Locations { get; set; } public DbSet<Location> Locations { get; set; }
public DbSet<Slot> Slots { get; set; }
} }
} }

View file

@ -25,7 +25,8 @@ namespace ProjectLighthouse.Types {
try { try {
return new Database().Database.CanConnect(); return new Database().Database.CanConnect();
} }
catch { catch(Exception e) {
Console.WriteLine(e);
return false; return false;
} }
} }

View file

@ -1,4 +1,6 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Xml.Serialization; using System.Xml.Serialization;
namespace ProjectLighthouse.Types { namespace ProjectLighthouse.Types {
@ -10,6 +12,11 @@ namespace ProjectLighthouse.Types {
[XmlAttribute("type")] [XmlAttribute("type")]
[NotMapped] [NotMapped]
public string Type { get; set; } public string Type { get; set; }
[Key]
[XmlIgnore]
public int SlotId { get; set; }
[XmlElement("name")] [XmlElement("name")]
public string Name { get; set; } public string Name { get; set; }
@ -26,8 +33,25 @@ namespace ProjectLighthouse.Types {
[XmlElement("resource")] [XmlElement("resource")]
public string Resource { get; set; } public string Resource { get; set; }
[XmlIgnore]
public int LocationId { get; set; }
[XmlIgnore]
public int CreatorId { get; set; }
private Location location;
/// <summary>
/// The location of the level on the creator's earth
/// </summary>
[XmlElement("location")] [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")] [XmlElement("initiallyLocked")]
public bool InitiallyLocked { get; set; } public bool InitiallyLocked { get; set; }
@ -55,6 +79,5 @@ namespace ProjectLighthouse.Types {
[XmlElement("moveRequired")] [XmlElement("moveRequired")]
public bool MoveRequired { get; set; } public bool MoveRequired { get; set; }
} }
} }