Doors/DoorState.cs

Session and config state for one roleplay door in HorizonRP. Holds staff-set config (title, price, buyable, allowed jobs) and runtime ownership (owner, co-owners, lock state), with helpers to check ownership/job access, release the door and convert to and from the backend DoorConfig.

namespace HorizonRP;

/// <summary>
/// L'état RP d'une porte. Deux moitiés :
/// - le réglage, fait par le staff et gardé en base (titre, prix, achetable,
///   métiers qui y ont accès) ;
/// - la propriété, qui ne dure que la session du propriétaire, comme sur DarkRP :
///   elle se libère à sa déconnexion.
/// </summary>
public sealed class DoorState
{
	public string Key { get; set; }
	public string Title { get; set; }
	public int Price { get; set; } = DoorSystem.DefaultPrice;
	public bool Buyable { get; set; } = true;
	public string Jobs { get; set; } = "";

	public ulong OwnerId { get; set; }
	public string OwnerName { get; set; }
	public List<ulong> CoOwners { get; set; } = new();
	public bool Locked { get; set; }

	public bool IsOwned => OwnerId != 0;

	public IEnumerable<string> JobList =>
		(Jobs ?? "").Split( ',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries );

	/// <summary>Une porte de métier (commissariat, hôpital…) ne s'achète pas.</summary>
	public bool IsJobDoor => JobList.Any();

	public bool CanBuy => Buyable && !IsJobDoor && !IsOwned;

	public string DisplayTitle => string.IsNullOrWhiteSpace( Title ) ? "Porte" : Title;

	/// <summary>Peut verrouiller et déverrouiller : propriétaire, copropriétaire, ou métier autorisé.</summary>
	public bool CanUse( RoleplayPlayer player )
	{
		if ( player is null )
			return false;

		return (IsOwned && (player.SteamId == OwnerId || CoOwners.Contains( player.SteamId )))
			|| JobList.Contains( player.JobId );
	}

	public void Release()
	{
		OwnerId = 0;
		OwnerName = null;
		CoOwners.Clear();
		Locked = false;
	}

	public Backend.DoorConfig ToConfig() => new()
	{
		Key = Key,
		Title = Title,
		Price = Price,
		Buyable = Buyable,
		Jobs = Jobs,
	};

	public void ApplyConfig( Backend.DoorConfig config )
	{
		Title = config.Title;
		Price = config.Price;
		Buyable = config.Buyable;
		Jobs = config.Jobs ?? "";
	}
}