41 lines
3.5 KiB
C#
41 lines
3.5 KiB
C#
using System.Text.Json;
|
||
using System.Text.RegularExpressions;
|
||
using PlotLine.Models;
|
||
namespace PlotLine.Services;
|
||
|
||
// Geometry stays in the existing feature JSON; settlement features remain authoritative Marker anchors.
|
||
internal static class MapAreaGeometryValidation
|
||
{
|
||
internal static readonly string[] SettlementTypes=["Capital","City","Town","Village","Hamlet","Settlement"];
|
||
public static string? Validate(MapFeature feature,JsonElement geometry)
|
||
{
|
||
if(geometry.TryGetProperty("drawingOrder",out var order)&&(order.ValueKind!=JsonValueKind.Number||!order.TryGetDouble(out var rank)||!double.IsFinite(rank)||rank<0||rank>500000))return "Invalid drawing order.";
|
||
if(feature.GeometryClass=="Area" && geometry.TryGetProperty("appearance",out var areaStyle) && !ValidAppearance(areaStyle))return "Check area colours, opacity and border style.";
|
||
if(!geometry.TryGetProperty("boundary",out var boundary)||boundary.ValueKind==JsonValueKind.Null)return null;
|
||
if(feature.GeometryClass!="Marker"||!SettlementTypes.Contains(feature.Type))return "A settlement boundary must retain its settlement Marker anchor.";
|
||
if(boundary.ValueKind!=JsonValueKind.Object)return "Invalid settlement boundary.";
|
||
foreach(var field in new[]{"offsetX","offsetY","width","height","rotation"})
|
||
{
|
||
var limit=field.StartsWith("offset",StringComparison.Ordinal)?200000:100000;
|
||
if(!boundary.TryGetProperty(field,out var value)||value.ValueKind!=JsonValueKind.Number||!value.TryGetDouble(out var number)||!double.IsFinite(number)||Math.Abs(number)>limit||((field=="width"||field=="height")&&number<1))return "Check settlement boundary position, dimensions and rotation.";
|
||
}
|
||
if(!boundary.TryGetProperty("points",out var points)||points.ValueKind!=JsonValueKind.Array||points.GetArrayLength() is <3 or >64)return "A settlement boundary needs 3–64 control points.";
|
||
foreach(var point in points.EnumerateArray())
|
||
if(point.ValueKind!=JsonValueKind.Array||point.GetArrayLength()!=2||point.EnumerateArray().Any(v=>v.ValueKind!=JsonValueKind.Number||!v.TryGetDouble(out var n)||!double.IsFinite(n)||Math.Abs(n)>10))return "Invalid settlement boundary control point.";
|
||
if(boundary.TryGetProperty("coast",out var coast)&&(coast.ValueKind!=JsonValueKind.String||!new[]{"Gentle","Natural","Rugged","Cliffs","Fjords","Marsh/Delta"}.Contains(coast.GetString())))return "Choose a supported boundary smoothing style.";
|
||
if(boundary.TryGetProperty("appearance",out var style)&&!ValidAppearance(style))return "Check boundary colours, opacity and border style.";
|
||
return null;
|
||
}
|
||
private static bool ValidAppearance(JsonElement style)
|
||
{
|
||
if(style.ValueKind==JsonValueKind.Null)return true;
|
||
if(style.ValueKind!=JsonValueKind.Object)return false;
|
||
foreach(var key in new[]{"fillColor","borderColor"})
|
||
if(style.TryGetProperty(key,out var colour)&&(colour.ValueKind!=JsonValueKind.String||!Regex.IsMatch(colour.GetString()??"","^#[0-9a-fA-F]{6}$")))return false;
|
||
foreach(var (key,max) in new[]{("fillOpacity",1d),("borderWidth",30d)})
|
||
if(style.TryGetProperty(key,out var value)&&(value.ValueKind!=JsonValueKind.Number||!value.TryGetDouble(out var n)||!double.IsFinite(n)||n<0||n>max))return false;
|
||
if(style.TryGetProperty("borderStyle",out var border)&&(border.ValueKind!=JsonValueKind.String||!new[]{"Solid","Dashed","Dotted"}.Contains(border.GetString())))return false;
|
||
return true;
|
||
}
|
||
}
|