using System;
using System.Collections.Concurrent;
namespace codelanguage
{
///
/// Manages per-client variables and output routing for the Craft++ runtime.
/// Mirrors BYOND's client variable store and output channel system.
///
public static class ClientVariablesService
{
private static readonly ConcurrentDictionary _variables = new();
/// Callback for sending output to the active client (mob/client << msg).
public static Action? OutputCallback { get; set; }
/// Callback invoked when a client connects (client.Login equivalent).
public static Action? LoginCallback { get; set; }
/// Callback invoked when a client disconnects (client.Logout equivalent).
public static Action? LogoutCallback { get; set; }
public static void SetVariable(string key, CraftValue value)
{
_variables[key] = value;
}
public static CraftValue? GetVariable(string key)
{
_variables.TryGetValue(key, out var value);
return value;
}
public static void DeleteVariable(string key)
{
_variables.TryRemove(key, out _);
}
public static IReadOnlyDictionary GetAllVariables()
{
return _variables;
}
///
/// Sends output to the active client's output stream.
/// Routes through OutputCallback if set, otherwise is a no-op.
///
public static void SendOutput(string message)
{
OutputCallback?.Invoke(message);
}
///
/// Triggers the Login callback — called when a client attaches to a mob.
/// Mirrors BYOND's /client/proc/Login().
///
public static void TriggerLogin(string clientKey)
{
LoginCallback?.Invoke(clientKey);
}
///
/// Triggers the Logout callback — called when a client disconnects.
/// Mirrors BYOND's /client/proc/Logout().
///
public static void TriggerLogout(string clientKey)
{
LogoutCallback?.Invoke(clientKey);
}
/// Clears all stored variables (called on world reset).
public static void Reset()
{
_variables.Clear();
}
}
}