using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace DataServer.Data.Entities; /// /// Stores unique custom socket names for autocomplete in the tile editor. /// All names are stored lowercase for consistency. /// Examples: "field-wall", "cave-entrance", "water-edge" /// [Table("socket_names")] public class SocketName { [Key] public int Id { get; set; } /// /// The socket name (lowercase, unique). /// Used for Custom socket type matching: Custom:field-wall matches Custom:field-wall /// [Required, MaxLength(64)] public string Name { get; set; } = ""; /// Optional description of what this socket represents [MaxLength(256)] public string? Description { get; set; } /// Usage count (for sorting by popularity) public int UsageCount { get; set; } = 0; public DateTime CreatedAt { get; set; } = DateTime.UtcNow; } /// /// Socket configuration for a single edge. /// public class EdgeSocket { /// Socket type: Open, Closed, or Custom [System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.JsonStringEnumConverter))] public SocketType Type { get; set; } = SocketType.Open; /// Custom socket name (only used when Type == Custom) public string? CustomName { get; set; } } /// /// Complete socket configuration for a tile template. /// For 1x1 tiles: 4 sockets (N, E, S, W) /// For larger tiles: multiple sockets per edge based on dimensions /// /// Example 3x2 template: /// - North edge: 3 sockets (one per column) /// - South edge: 3 sockets (one per column) /// - East edge: 2 sockets (one per row) /// - West edge: 2 sockets (one per row) /// /// JSON format: /// { /// "north": [ /// { "type": "Open", "customName": null }, /// { "type": "Custom", "customName": "field-wall" }, /// { "type": "Open", "customName": null } /// ], /// "east": [ /// { "type": "Closed", "customName": null }, /// { "type": "Closed", "customName": null } /// ], /// "south": [...], /// "west": [...] /// } /// public class SocketConfiguration { /// North edge sockets (left to right) public List North { get; set; } = new(); /// East edge sockets (top to bottom) public List East { get; set; } = new(); /// South edge sockets (left to right) public List South { get; set; } = new(); /// West edge sockets (top to bottom) public List West { get; set; } = new(); /// /// Create a default configuration for a template of given dimensions. /// All sockets default to Open. /// public static SocketConfiguration CreateDefault(int width, int height, SocketType defaultType = SocketType.Open) { var config = new SocketConfiguration(); // North and South have 'width' sockets for (int i = 0; i < width; i++) { config.North.Add(new EdgeSocket { Type = defaultType }); config.South.Add(new EdgeSocket { Type = defaultType }); } // East and West have 'height' sockets for (int i = 0; i < height; i++) { config.East.Add(new EdgeSocket { Type = defaultType }); config.West.Add(new EdgeSocket { Type = defaultType }); } return config; } /// /// Resize the socket configuration when template dimensions change. /// Preserves existing sockets where possible. /// public void Resize(int newWidth, int newHeight, SocketType defaultType = SocketType.Open) { // Adjust North/South (horizontal edges) while (North.Count < newWidth) North.Add(new EdgeSocket { Type = defaultType }); while (North.Count > newWidth) North.RemoveAt(North.Count - 1); while (South.Count < newWidth) South.Add(new EdgeSocket { Type = defaultType }); while (South.Count > newWidth) South.RemoveAt(South.Count - 1); // Adjust East/West (vertical edges) while (East.Count < newHeight) East.Add(new EdgeSocket { Type = defaultType }); while (East.Count > newHeight) East.RemoveAt(East.Count - 1); while (West.Count < newHeight) West.Add(new EdgeSocket { Type = defaultType }); while (West.Count > newHeight) West.RemoveAt(West.Count - 1); } /// /// Check if a specific socket on this template can connect to another socket. /// Rules: Open↔Open, Closed↔Closed, Custom:name↔Custom:name (same name) /// public static bool CanConnect(EdgeSocket a, EdgeSocket b) { if (a.Type != b.Type) return false; if (a.Type == SocketType.Custom) { // Custom sockets must have matching names (case-insensitive) return string.Equals(a.CustomName, b.CustomName, StringComparison.OrdinalIgnoreCase); } // Open↔Open or Closed↔Closed if (a.Type == SocketType.Closed && b.Type == SocketType.Closed) { return false; } return true; } }