using Microsoft.EntityFrameworkCore; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Text.Json.Serialization; using RadicadeServer.DB; using RadicadeServer.RecNet.Enums.Inventions; namespace RadicadeServer.RecNet.Inventions { public class InventionDB { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long InventionId { get; set; } public string ReplicationId { get; set; } = ""; public long CreatorPlayerId { get; set; } = 1; public string Name { get; set; } = ""; public string Description { get; set; } = ""; public string ImageName { get; set; } = DBConfig.DefaultRoomImage; public int CurrentVersionNumber { get; set; } = 0; public DateTime ModifiedAt { get; set; } = DateTime.Now; public DateTime CreatedAt { get; set; } = DateTime.Now; public int NumPlayersHaveUsedInRoom { get; set; } = 0; public int NumDownloads { get; set; } = 0; public int CheerCount { get; set; } = 0; public InventionRole CreatorPermission { get; set; } = InventionRole.Unlimited; public InventionRole GeneralPermission { get; set; } = InventionRole.Unassigned; public bool IsAGInvention { get; set; } = false; public bool IsCertifiedInvention { get; set; } = false; public bool IsPublished { get; set; } = false; public bool AllowTrial { get; set; } = false; public bool HideFromPlayer { get; set; } = false; public int? Price { get; set; } = 0; public string BlobName { get; set; } = ""; public int InstantiationCost { get; set; } = 0; public int LightsCost { get; set; } = 0; public int AICost { get; set; } = 0; public int ChipsCost { get; set; } = 0; public int CloudVariablesCost { get; set; } = 0; public long RoomOrigin { get; set; } = -1; public string Tags { get; set; } = string.Empty; public Invention ToInvention() { return new Invention { InventionId = InventionId, CreationRoomId = RoomOrigin >= 0 ? RoomOrigin : 0, ReplicationId = ReplicationId, CreatorPlayerId = CreatorPlayerId, Name = Name, Description = Description, ImageName = ImageName, CurrentVersionNumber = CurrentVersionNumber, ModifiedAt = ModifiedAt, CreatedAt = CreatedAt, NumPlayersHaveUsedInRoom = NumPlayersHaveUsedInRoom, NumDownloads = NumDownloads, CheerCount = CheerCount, CreatorPermission = CreatorPermission, GeneralPermission = GeneralPermission, IsAGInvention = IsAGInvention, IsCertifiedInvention = IsCertifiedInvention, IsPublished = IsPublished, AllowTrial = AllowTrial, HideFromPlayer = HideFromPlayer, Price = Price is > 0 ? Price : null }; } public InventionVersion ToVersion() { return new InventionVersion { InventionId = InventionId, ReplicationId = ReplicationId, BlobName = BlobName, InstantiationCost = InstantiationCost, LightsCost = LightsCost, ChipsCost = ChipsCost, CloudVariablesCost = CloudVariablesCost, VersionNumber = CurrentVersionNumber, }; } public async Task ToFullInvention(RecDBContext ctx) { var full = new FullInvention(); full.Status = InventionStatus.Success; full.Invention = ToInvention(); full.InventionVersion = await ctx.GetInventionVersion(CurrentVersionNumber, InventionId); return full; } public async Task ToFullInvention(RecDBContext ctx, int version) { var full = new FullInvention(); full.Status = InventionStatus.Success; full.Invention = ToInvention(); full.InventionVersion = await ctx.GetInventionVersion(version, InventionId); return full; } } public static class InventionGetter { public static async Task GetLatestInventionVersion(this RecDBContext ctx, long inventionId) { var ver = await ctx.InventionVersions.LastOrDefaultAsync(x => x.InventionId == inventionId); return ver; } public static async Task GetInventionVersion(this RecDBContext ctx, int version, long inventionId) { var ver = await ctx.InventionVersions.FirstOrDefaultAsync(x => x.InventionId == inventionId && x.VersionNumber == version); return ver; } } }