Files
systemrevivalserver/data/entities/SocketName.cs
T
2026-01-09 14:48:44 -05:00

151 lines
5.2 KiB
C#

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace DataServer.Data.Entities;
/// <summary>
/// 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"
/// </summary>
[Table("socket_names")]
public class SocketName
{
[Key]
public int Id { get; set; }
/// <summary>
/// The socket name (lowercase, unique).
/// Used for Custom socket type matching: Custom:field-wall matches Custom:field-wall
/// </summary>
[Required, MaxLength(64)]
public string Name { get; set; } = "";
/// <summary>Optional description of what this socket represents</summary>
[MaxLength(256)]
public string? Description { get; set; }
/// <summary>Usage count (for sorting by popularity)</summary>
public int UsageCount { get; set; } = 0;
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
}
/// <summary>
/// Socket configuration for a single edge.
/// </summary>
public class EdgeSocket
{
/// <summary>Socket type: Open, Closed, or Custom</summary>
[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.JsonStringEnumConverter))]
public SocketType Type { get; set; } = SocketType.Open;
/// <summary>Custom socket name (only used when Type == Custom)</summary>
public string? CustomName { get; set; }
}
/// <summary>
/// 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": [...]
/// }
/// </summary>
public class SocketConfiguration
{
/// <summary>North edge sockets (left to right)</summary>
public List<EdgeSocket> North { get; set; } = new();
/// <summary>East edge sockets (top to bottom)</summary>
public List<EdgeSocket> East { get; set; } = new();
/// <summary>South edge sockets (left to right)</summary>
public List<EdgeSocket> South { get; set; } = new();
/// <summary>West edge sockets (top to bottom)</summary>
public List<EdgeSocket> West { get; set; } = new();
/// <summary>
/// Create a default configuration for a template of given dimensions.
/// All sockets default to Open.
/// </summary>
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;
}
/// <summary>
/// Resize the socket configuration when template dimensions change.
/// Preserves existing sockets where possible.
/// </summary>
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);
}
/// <summary>
/// Check if a specific socket on this template can connect to another socket.
/// Rules: Open↔Open, Closed↔Closed, Custom:name↔Custom:name (same name)
/// </summary>
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
return true;
}
}