using OWML.Common; using QSB.Messaging; using QSB.TransformSync; using QSB.Utility; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; namespace QSB { public static class PlayerRegistry { public const int NetworkObjectCount = 4; public static uint LocalPlayerId => PlayerTransformSync.LocalInstance.netId.Value; public static PlayerInfo LocalPlayer => GetPlayer(LocalPlayerId); public static List PlayerList { get; } = new List(); public static List PlayerSyncObjects { get; } = new List(); public static PlayerInfo GetPlayer(uint id) { var player = PlayerList.FirstOrDefault(x => x.NetId == id); if (player != null) { return player; } var stacktrace = new StackTrace(); DebugLog.ToConsole($"Creating player with id {id}, called from {stacktrace.GetFrame(1).GetMethod().DeclaringType}.{stacktrace.GetFrame(1).GetMethod().Name}", MessageType.Info); player = new PlayerInfo(id); PlayerList.Add(player); return player; } public static void RemovePlayer(uint id) { var stacktrace = new StackTrace(); DebugLog.ToConsole($"Removing player with id {id}, called from {stacktrace.GetFrame(1).GetMethod().DeclaringType.Name}.{stacktrace.GetFrame(1).GetMethod().Name}", MessageType.Info); PlayerList.Remove(GetPlayer(id)); } public static bool PlayerExists(uint id) { return PlayerList.Any(x => x.NetId == id); } public static void HandleFullStateMessage(PlayerStateMessage message) { var player = GetPlayer(message.AboutId); player.Name = message.PlayerName; player.IsReady = message.PlayerReady; player.State = message.PlayerState; DebugLog.ToConsole($"Updating state of player {player.NetId} to : {Environment.NewLine}" + $"{DebugLog.GenerateTable(Enum.GetNames(typeof(State)).ToList(), FlagsHelper.FlagsToListSet(player.State))}"); if (LocalPlayer.IsReady) { player.UpdateStateObjects(); } } public static IEnumerable GetSyncObjects() where T : PlayerSyncObject { return PlayerSyncObjects.OfType().Where(x => x != null); } public static T GetSyncObject(uint id) where T : PlayerSyncObject { return GetSyncObjects().FirstOrDefault(x => x != null && x.NetId == id); } public static PlayerSyncObject GetSyncObject(uint id) { return PlayerSyncObjects.FirstOrDefault(x => x != null && x.NetId == id); } public static bool IsBelongingToLocalPlayer(uint id) { return id == LocalPlayerId || GetSyncObject(id)?.PlayerId == LocalPlayerId; } public static List GetPlayerNetIds(PlayerInfo player) { return Enumerable.Range((int)player.NetId, NetworkObjectCount).Select(x => (uint)x).ToList(); } } }