using Fantasy;
using Fantasy.Async;

namespace NB.Map;

public static class MapHelper
{
    public static async FTask Offline(Scene scene, long accountId, long gateRouteId)
    {
        // 在缓存中检查该账号是否存在
        var chatUnitManageComponent = scene.GetComponent<MapUnitManageComponent>();
        if (chatUnitManageComponent == null) return;
        var mapUnit = chatUnitManageComponent.Get(accountId);
        if (mapUnit == null) return;

        var roomManageComponent = scene.GetComponent<RoomManageComponent>();

        if (mapUnit.RoomId > 0)
        {
            var room = roomManageComponent.Get(mapUnit.RoomId);
            if (room != null)
            {
                await room.Exit(accountId);
                roomManageComponent.Check(mapUnit.RoomId);
            }
        }

        await chatUnitManageComponent.Offline(scene, accountId, gateRouteId);
    }
}