<commit_before>﻿using LunaClient.Base;
using System;
using System.Collections;
using System.Collections.Generic;
using UniLinq;
using UnityEngine;

namespace LunaClient.Systems.VesselFlightStateSys
{
    /// <summary>
    /// System that controls the flight state (user control inputs) for all the vessesl that are in flight
    /// </summary>
    public class VesselFlightStateSystem : MessageSystem<VesselFlightStateSystem, VesselFlightStateMessageSender, VesselFlightStateMessageHandler>
    {
        public Dictionary<Guid, FlightInputCallback> FlyByWireDictionary { get; } =
            new Dictionary<Guid, FlightInputCallback>();

        public Dictionary<Guid, FlightCtrlState> FlightStatesDictionary { get; } =
            new Dictionary<Guid, FlightCtrlState>();

        public bool FlightStateSystemReady
            => Enabled && FlightGlobals.ActiveVessel != null && Time.timeSinceLevelLoad > 1f &&
               FlightGlobals.ready && FlightGlobals.ActiveVessel.loaded &&
               FlightGlobals.ActiveVessel.state != Vessel.State.DEAD && !FlightGlobals.ActiveVessel.packed &&
               FlightGlobals.ActiveVessel.vesselType != VesselType.Flag;

        private const float DictionaryUpdateSInterval = 1.5f;
        private const float FlightStateSendSInterval = 0.1f;
        private const float FlightStateSendLowSInterval = 0.5f;

        public override void OnEnabled()
        {
            base.OnEnabled();
            Client.Singleton.StartCoroutine(SendFlightState());
            Client.Singleton.StartCoroutine(AddRemoveActiveVesselFromDictionary());
            Client.Singleton.StartCoroutine(RemovePackedVesselsFromDictionary());
            Client.Singleton.StartCoroutine(AddUnPackedVesselsToDictionary());
        }

        public override void OnDisabled()
        {
            base.OnDisabled();
            FlyByWireDictionary.Clear();
            FlightStatesDictionary.Clear();
        }

        private IEnumerator SendFlightState()
        {
            var seconds = new WaitForSeconds(VesselCommon.IsSomeoneSpectatingUs ? FlightStateSendSInterval : FlightStateSendLowSInterval);
            while (true)
            {
                if (!Enabled) break;

                if (FlightStateSystemReady)
                {
                    MessageSender.SendCurrentFlightState();
                }

                yield return seconds;
            }
        }

        private IEnumerator RemovePackedVesselsFromDictionary()
        {
            var seconds = new WaitForSeconds(DictionaryUpdateSInterval);
            while (true)
            {
                if (!Enabled) break;

                if (FlightStateSystemReady)
                {
                    var vesselsToRemove = FlightGlobals.Vessels.Where(v => v.packed)
                        .Where(v => FlyByWireDictionary.Keys.Contains(v.id))
                        .ToList();

                    foreach (var vesselToRemove in vesselsToRemove)
                    {
                        try
                        {
                            vesselToRemove.OnFlyByWire -= FlyByWireDictionary[vesselToRemove.id];<commit_msg>Send Flight state only when someone spectate us or other vessels are nearby<commit_after>﻿using LunaClient.Base;
using System;
using System.Collections;
using System.Collections.Generic;
using UniLinq;
using UnityEngine;

namespace LunaClient.Systems.VesselFlightStateSys
{
    /// <summary>
    /// System that controls the flight state (user control inputs) for all the vessesl that are in flight
    /// </summary>
    public class VesselFlightStateSystem : MessageSystem<VesselFlightStateSystem, VesselFlightStateMessageSender, VesselFlightStateMessageHandler>
    {
        public Dictionary<Guid, FlightInputCallback> FlyByWireDictionary { get; } =
            new Dictionary<Guid, FlightInputCallback>();

        public Dictionary<Guid, FlightCtrlState> FlightStatesDictionary { get; } =
            new Dictionary<Guid, FlightCtrlState>();

        public bool FlightStateSystemReady
            => Enabled && FlightGlobals.ActiveVessel != null && Time.timeSinceLevelLoad > 1f &&
               FlightGlobals.ready && FlightGlobals.ActiveVessel.loaded &&
               FlightGlobals.ActiveVessel.state != Vessel.State.DEAD && !FlightGlobals.ActiveVessel.packed &&
               FlightGlobals.ActiveVessel.vesselType != VesselType.Flag;

        private const float DictionaryUpdateSInterval = 1.5f;
        private const float FlightStateSendSInterval = 0.1f;
        private const float FlightStateSendLowSInterval = 0.5f;

        public override void OnEnabled()
        {
            base.OnEnabled();
            Client.Singleton.StartCoroutine(SendFlightState());
            Client.Singleton.StartCoroutine(AddRemoveActiveVesselFromDictionary());
            Client.Singleton.StartCoroutine(RemovePackedVesselsFromDictionary());
            Client.Singleton.StartCoroutine(AddUnPackedVesselsToDictionary());
        }

        public override void OnDisabled()
        {
            base.OnDisabled();
            FlyByWireDictionary.Clear();
            FlightStatesDictionary.Clear();
        }

        private IEnumerator SendFlightState()
        {
            var seconds = new WaitForSeconds(VesselCommon.IsSomeoneSpectatingUs ? FlightStateSendSInterval : FlightStateSendLowSInterval);
            while (true)
            {
                if (!Enabled) break;

                if (FlightStateSystemReady && VesselCommon.PlayerVesselsNearby())
                {
                    MessageSender.SendCurrentFlightState();
                }

                yield return seconds;
            }
        }

        private IEnumerator RemovePackedVesselsFromDictionary()
        {
            var seconds = new WaitForSeconds(DictionaryUpdateSInterval);
            while (true)
            {
                if (!Enabled) break;

                if (FlightStateSystemReady)
                {
                    var vesselsToRemove = FlightGlobals.Vessels.Where(v => v.packed)
                        .Where(v => FlyByWireDictionary.Keys.Contains(v.id))
                        .ToList();

                    foreach (var vesselToRemove in vesselsToRemove)
                    {
                        try
                        {
                            vesselToRemove.OnFlyByWire -= FlyByWireDictionary[vesselToRemove.id];
{'fraction_non_alphanumeric': 0.0765990639625585, 'fraction_numerical': 0.00218408736349454, 'mean_word_length': 2.124269005847953, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1371157', 'n_tokens_mistral': 1645, 'n_tokens_neox': 1552, 'n_words': 312}