<commit_before>﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using LiteNetLib;
using LiteNetLib.Utils;
using UnityEngine.Profiling;
using System.Threading.Tasks;

namespace LiteNetLibManager
{
    [RequireComponent(typeof(LiteNetLibAssets))]
    public class LiteNetLibGameManager : LiteNetLibManager
    {
        public class GameMsgTypes
        {
            public const ushort EnterGame = 0;
            public const ushort ClientReady = 1;
            public const ushort ClientNotReady = 2;
            public const ushort CallFunction = 3;
            public const ushort ServerSpawnSceneObject = 4;
            public const ushort ServerSpawnObject = 5;
            public const ushort ServerDestroyObject = 6;
            public const ushort UpdateSyncField = 7;
            public const ushort InitialSyncField = 8;
            public const ushort OperateSyncList = 9;
            public const ushort ServerTime = 10;
            public const ushort ServerSyncBehaviour = 11;
            public const ushort ServerError = 12;
            public const ushort ServerSceneChange = 13;
            public const ushort ClientSendTransform = 14;
            public const ushort ServerSetObjectOwner = 15;
            public const ushort Ping = 16;
            public const ushort Highest = 16;
        }

        public class DestroyObjectReasons
        {
            public const byte RequestedToDestroy = 0;
            public const byte RemovedFromSubscribing = 1;
            public const byte Highest = 1;
        }

        public float updateServerTimeDuration = 5f;
        public float pingDuration = 1f;
        public bool doNotEnterGameOnConnect;
        public bool doNotDestroyOnSceneChanges;

        protected readonly Dictionary<long, LiteNetLibPlayer> Players = new Dictionary<long, LiteNetLibPlayer>();

        private float tempUpdateTime;
        private float lastSendServerTime;
        private float lastSendPingTime;
        private string serverSceneName;
        private AsyncOperation loadSceneAsyncOperation;
        private bool isPinging;
        private long pingTime;

        public long ClientConnectionId { get; protected set; }
        
        public long Rtt { get; private set; }
        public long Timestamp { get { return System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); } }
        public long ServerUnixTimeOffset { get; protected set; }
        public long ServerUnixTime
        {
            get
            {
                if (IsServer)
                    return Timestamp;
                return Timestamp + ServerUnixTimeOffset;
            }
        }

        public string ServerSceneName
        {
            get
            {
                if (IsServer)
                    return serverSceneName;
                return string.Empty;
            }
        }

        public LiteNetLibAssets Assets { get; private set; }

        protected override void Awake()
        {
            base.Awake();
            Assets = GetComponent<LiteNetLibAssets>();
            serverSceneName = string.Empty;
            if (doNotDestroyOnSceneChanges)
                DontDestroyOnLoad(gameObject);
        }

        protected override void LateUpdate()
        {
            if (loadSceneAsyncOperation == null)
            {
                tempUpdateTime = Time.unscaledTime;
                // Update Spawned Objects
                Profiler.BeginSample("LiteNetLibGameManager - Update Spawned Objects");
                foreach (LiteNetLibIdentity spawnedObject in Assets.SpawnedObjects.Values)
                {
                    if (spawnedObject == null)
                        continue;
                    spawnedObject.NetworkUpdate(tempUpdateTime);
                }
                Profiler.EndSample();

                if (IsServer)
                {
                    // Send server time from server
                    if (tempUpdateTime - lastSendServerTime > updateServerTimeDuration)
                    {
                        SendServerTime();
                        lastSendServerTime = tempUpdateTime;
                    }
                }

                if (IsClientConnected)
                {
                    if (tempUpdateTime - lastSendPingTime > pingDuration)
                    {
                        SendClientPing();
                        lastSendPingTime = tempUpdateTime;
                    }
                }
            }
            base.LateUpdate();
        }

        public virtual uint PacketVersion()
        {
            return 1;
        }

        public bool TryGetPlayer(long connectionId, out LiteNetLibPlayer player)
        {
            return Players.TryGetValue(connectionId, out player);
        }<commit_msg>Will update last send ping time when it's not pinging.<commit_after>﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using LiteNetLib;
using LiteNetLib.Utils;
using UnityEngine.Profiling;
using System.Threading.Tasks;

namespace LiteNetLibManager
{
    [RequireComponent(typeof(LiteNetLibAssets))]
    public class LiteNetLibGameManager : LiteNetLibManager
    {
        public class GameMsgTypes
        {
            public const ushort EnterGame = 0;
            public const ushort ClientReady = 1;
            public const ushort ClientNotReady = 2;
            public const ushort CallFunction = 3;
            public const ushort ServerSpawnSceneObject = 4;
            public const ushort ServerSpawnObject = 5;
            public const ushort ServerDestroyObject = 6;
            public const ushort UpdateSyncField = 7;
            public const ushort InitialSyncField = 8;
            public const ushort OperateSyncList = 9;
            public const ushort ServerTime = 10;
            public const ushort ServerSyncBehaviour = 11;
            public const ushort ServerError = 12;
            public const ushort ServerSceneChange = 13;
            public const ushort ClientSendTransform = 14;
            public const ushort ServerSetObjectOwner = 15;
            public const ushort Ping = 16;
            public const ushort Highest = 16;
        }

        public class DestroyObjectReasons
        {
            public const byte RequestedToDestroy = 0;
            public const byte RemovedFromSubscribing = 1;
            public const byte Highest = 1;
        }

        public float updateServerTimeDuration = 5f;
        public float pingDuration = 1f;
        public bool doNotEnterGameOnConnect;
        public bool doNotDestroyOnSceneChanges;

        protected readonly Dictionary<long, LiteNetLibPlayer> Players = new Dictionary<long, LiteNetLibPlayer>();

        private float tempUpdateTime;
        private float lastSendServerTime;
        private float lastSendPingTime;
        private string serverSceneName;
        private AsyncOperation loadSceneAsyncOperation;
        private bool isPinging;
        private long pingTime;

        public long ClientConnectionId { get; protected set; }
        
        public long Rtt { get; private set; }
        public long Timestamp { get { return System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); } }
        public long ServerUnixTimeOffset { get; protected set; }
        public long ServerUnixTime
        {
            get
            {
                if (IsServer)
                    return Timestamp;
                return Timestamp + ServerUnixTimeOffset;
            }
        }

        public string ServerSceneName
        {
            get
            {
                if (IsServer)
                    return serverSceneName;
                return string.Empty;
            }
        }

        public LiteNetLibAssets Assets { get; private set; }

        protected override void Awake()
        {
            base.Awake();
            Assets = GetComponent<LiteNetLibAssets>();
            serverSceneName = string.Empty;
            if (doNotDestroyOnSceneChanges)
                DontDestroyOnLoad(gameObject);
        }

        protected override void LateUpdate()
        {
            if (loadSceneAsyncOperation == null)
            {
                tempUpdateTime = Time.unscaledTime;
                // Update Spawned Objects
                Profiler.BeginSample("LiteNetLibGameManager - Update Spawned Objects");
                foreach (LiteNetLibIdentity spawnedObject in Assets.SpawnedObjects.Values)
                {
                    if (spawnedObject == null)
                        continue;
                    spawnedObject.NetworkUpdate(tempUpdateTime);
                }
                Profiler.EndSample();

                if (IsServer)
                {
                    // Send server time from server
                    if (tempUpdateTime - lastSendServerTime > updateServerTimeDuration)
                    {
                        SendServerTime();
                        lastSendServerTime = tempUpdateTime;
                    }
                }

                if (IsClientConnected)
                {
                    if (tempUpdateTime - lastSendPingTime > pingDuration && !isPinging)
                    {
                        SendClientPing();
                        lastSendPingTime = tempUpdateTime;
                    }
                }
            }
            base.LateUpdate();
        }

        public virtual uint PacketVersion()
        {
            return 1;
        }

        public bool TryGetPlayer(long connectionId, out LiteNetLibPlayer player)
        {
            return Players.TryGetValue(connectionId, out player);
        }
{'fraction_non_alphanumeric': 0.0532715095311695, 'fraction_numerical': 0.006594538897475528, 'mean_word_length': 1.6042393345854575, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '1200480', 'n_tokens_mistral': 2409, 'n_tokens_neox': 2257, 'n_words': 626}