quantum-space-buddies/QSB/PlayerRegistry.cs

89 lines
3.2 KiB
C#
Raw Normal View History

2020-08-22 17:08:25 +00:00
using OWML.Common;
using QSB.Messaging;
2020-08-21 13:04:13 +00:00
using QSB.TransformSync;
using QSB.Utility;
2020-08-21 13:04:13 +00:00
using System;
using System.Collections.Generic;
2020-08-19 20:28:04 +00:00
using System.Diagnostics;
2020-08-21 13:04:13 +00:00
using System.Linq;
namespace QSB
{
public static class PlayerRegistry
{
public const int NetworkObjectCount = 4;
2020-08-14 18:54:15 +00:00
public static uint LocalPlayerId => PlayerTransformSync.LocalInstance.netId.Value;
public static PlayerInfo LocalPlayer => GetPlayer(LocalPlayerId);
public static List<PlayerInfo> PlayerList { get; } = new List<PlayerInfo>();
2020-08-05 19:59:50 +00:00
public static List<PlayerSyncObject> PlayerSyncObjects { get; } = new List<PlayerSyncObject>();
2020-08-17 16:58:45 +00:00
public static PlayerInfo GetPlayer(uint id)
{
2020-08-17 16:58:45 +00:00
var player = PlayerList.FirstOrDefault(x => x.NetId == id);
if (player != null)
{
2020-08-17 16:58:45 +00:00
return player;
}
2020-08-19 20:28:04 +00:00
var stacktrace = new StackTrace();
2020-08-22 17:08:25 +00:00
DebugLog.ToConsole($"Creating player with id {id}, called from {stacktrace.GetFrame(1).GetMethod().DeclaringType}.{stacktrace.GetFrame(1).GetMethod().Name}", MessageType.Info);
2020-08-17 16:58:45 +00:00
player = new PlayerInfo(id);
2020-07-28 13:59:24 +00:00
PlayerList.Add(player);
return player;
}
public static void RemovePlayer(uint id)
{
2020-08-19 20:28:04 +00:00
var stacktrace = new StackTrace();
2020-08-22 17:08:25 +00:00
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));
2020-07-29 21:04:50 +00:00
}
2020-08-18 20:29:31 +00:00
public static bool PlayerExists(uint id)
{
return PlayerList.Any(x => x.NetId == id);
}
2020-08-09 20:46:51 +00:00
public static void HandleFullStateMessage(PlayerStateMessage message)
{
2020-08-17 17:25:28 +00:00
var player = GetPlayer(message.AboutId);
player.Name = message.PlayerName;
2020-08-10 10:45:24 +00:00
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))}");
2020-08-13 17:25:12 +00:00
if (LocalPlayer.IsReady)
2020-08-09 20:46:51 +00:00
{
2020-08-10 10:45:24 +00:00
player.UpdateStateObjects();
}
}
public static IEnumerable<T> GetSyncObjects<T>() where T : PlayerSyncObject
{
2020-08-23 10:53:11 +00:00
return PlayerSyncObjects.OfType<T>().Where(x => x != null);
}
public static T GetSyncObject<T>(uint id) where T : PlayerSyncObject
2020-08-14 18:54:15 +00:00
{
2020-08-23 10:53:11 +00:00
return GetSyncObjects<T>().FirstOrDefault(x => x != null && x.NetId == id);
2020-08-14 18:54:15 +00:00
}
2020-08-23 10:56:41 +00:00
public static PlayerSyncObject GetSyncObject(uint id)
{
return PlayerSyncObjects.FirstOrDefault(x => x != null && x.NetId == id);
}
public static bool IsBelongingToLocalPlayer(uint id)
{
2020-08-23 10:56:41 +00:00
return id == LocalPlayerId || GetSyncObject(id)?.PlayerId == LocalPlayerId;
}
public static List<uint> GetPlayerNetIds(PlayerInfo player)
{
return Enumerable.Range((int)player.NetId, NetworkObjectCount).Select(x => (uint)x).ToList();
}
}
}