quantum-space-buddies/QSB/PlayerRegistry.cs

95 lines
3.5 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-26 19:50:30 +00:00
using System;
2020-08-21 13:04:13 +00:00
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Networking;
namespace QSB
{
public static class PlayerRegistry
{
public static uint LocalPlayerId => PlayerTransformSync.LocalInstance.GetComponent<NetworkIdentity>()?.netId.Value ?? 0;
2020-08-14 18:54:15 +00:00
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-26 19:50:30 +00:00
var player = PlayerList.FirstOrDefault(x => x.PlayerId == id);
2020-08-17 16:58:45 +00:00
if (player != null)
{
2020-08-17 16:58:45 +00:00
return player;
}
2020-08-22 19:21:13 +00:00
DebugLog.DebugWrite($"Creating player with id {id}", 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-22 19:21:13 +00:00
DebugLog.DebugWrite($"Removing player with id {id}", 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)
{
2020-08-26 19:50:30 +00:00
return PlayerList.Any(x => x.PlayerId == id);
2020-08-18 20:29:31 +00:00
}
2020-08-09 20:46:51 +00:00
public static void HandleFullStateMessage(PlayerStateMessage message)
{
2020-08-24 14:51:12 +00:00
DebugLog.DebugWrite($"Handle full state message for player {message.AboutId}");
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;
2020-08-24 14:51:12 +00:00
DebugLog.DebugWrite($"* Is ready? : {player.IsReady}");
2020-08-10 10:45:24 +00:00
player.State = message.PlayerState;
2020-08-24 14:51:12 +00:00
DebugLog.DebugWrite($"* Suit is on? : {FlagsHelper.IsSet(player.State, State.Suit)}");
2020-08-22 19:21:13 +00:00
//DebugLog.DebugWrite($"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
}
public static bool IsBelongingToLocalPlayer(uint id)
{
2020-08-23 13:46:43 +00:00
return id == LocalPlayerId ||
PlayerSyncObjects.Any(x => x != null && x.NetId == id && x.PlayerId == LocalPlayerId);
}
public static List<uint> GetPlayerNetIds(PlayerInfo player)
{
2020-08-26 19:50:30 +00:00
return Enumerable.Range((int)player.PlayerId, PlayerSyncObjects.DistinctBy(x => x.NetId).Count(x => x.PlayerId == player.PlayerId)).Select(x => (uint)x).ToList();
}
private static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
HashSet<TKey> seenKeys = new HashSet<TKey>();
foreach (TSource element in source)
{
if (seenKeys.Add(keySelector(element)))
{
yield return element;
}
}
}
}
}