quantum-space-buddies/QSB/PlayerRegistry.cs

121 lines
4.4 KiB
C#
Raw Normal View History

2020-08-22 18:08:25 +01:00
using OWML.Common;
using QSB.Messaging;
2020-08-21 14:04:13 +01:00
using QSB.TransformSync;
using QSB.Utility;
2020-08-21 14:04:13 +01:00
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Networking;
namespace QSB
{
public static class PlayerRegistry
{
2020-09-04 20:09:25 +01:00
public static uint LocalPlayerId => PlayerTransformSync.LocalInstance.GetComponent<NetworkIdentity>()?.netId.Value ?? uint.MaxValue;
2020-08-14 20:54:15 +02:00
public static PlayerInfo LocalPlayer => GetPlayer(LocalPlayerId);
public static List<PlayerInfo> PlayerList { get; } = new List<PlayerInfo>();
2020-08-05 21:59:50 +02:00
public static List<PlayerSyncObject> PlayerSyncObjects { get; } = new List<PlayerSyncObject>();
2020-09-04 20:09:25 +01:00
public static PlayerInfo GetPlayer(uint id)
{
if (id == uint.MaxValue || id == 0U)
{
return default;
}
2020-08-26 20:50:30 +01:00
var player = PlayerList.FirstOrDefault(x => x.PlayerId == id);
2020-08-17 18:58:45 +02:00
if (player != null)
{
2020-08-17 18:58:45 +02:00
return player;
}
2020-09-03 17:09:38 +01:00
DebugLog.DebugWrite($"Creating player id {id}", MessageType.Info);
2020-08-17 18:58:45 +02:00
player = new PlayerInfo(id);
2020-07-28 15:59:24 +02:00
PlayerList.Add(player);
return player;
}
2020-09-04 20:09:25 +01:00
public static void RemovePlayer(uint id)
{
2020-09-04 20:09:25 +01:00
DebugLog.DebugWrite($"Removing player {GetPlayer(id).Name} id {id}", MessageType.Info);
PlayerList.Remove(GetPlayer(id));
2020-07-29 22:04:50 +01:00
}
2020-09-04 20:09:25 +01:00
public static bool PlayerExists(uint id)
2020-08-18 21:29:31 +01:00
{
2020-09-07 19:57:43 +02:00
return id != uint.MaxValue && PlayerList.Any(x => x.PlayerId == id);
2020-08-18 21:29:31 +01:00
}
2020-08-09 21:46:51 +01:00
public static void HandleFullStateMessage(PlayerStateMessage message)
{
2020-08-17 19:25:28 +02:00
var player = GetPlayer(message.AboutId);
player.Name = message.PlayerName;
2020-08-10 11:45:24 +01:00
player.IsReady = message.PlayerReady;
player.State = message.PlayerState;
2020-08-13 19:25:12 +02:00
if (LocalPlayer.IsReady)
2020-08-09 21:46:51 +01:00
{
2020-08-10 11:45:24 +01:00
player.UpdateStateObjects();
}
}
public static IEnumerable<T> GetSyncObjects<T>() where T : PlayerSyncObject
{
2020-08-23 12:53:11 +02:00
return PlayerSyncObjects.OfType<T>().Where(x => x != null);
}
2020-09-04 20:09:25 +01:00
public static T GetSyncObject<T>(uint id) where T : PlayerSyncObject
2020-08-14 20:54:15 +02:00
{
2020-09-04 20:36:25 +01:00
return GetSyncObjects<T>().FirstOrDefault(x => x != null && x.AttachedNetId == id);
2020-08-14 20:54:15 +02:00
}
2020-09-04 20:09:25 +01:00
public static bool IsBelongingToLocalPlayer(uint id)
{
2020-09-04 20:36:25 +01:00
return id == LocalPlayerId ||
PlayerSyncObjects.Any(x => x != null && x.AttachedNetId == id && x.isLocalPlayer);
2020-09-02 11:17:04 +01:00
}
2020-09-04 20:09:25 +01:00
public static uint GetPlayerOfObject(this PlayerSyncObject syncObject)
2020-09-02 11:17:04 +01:00
{
2020-09-29 21:34:46 +01:00
if (PlayerList.Count == 0)
{
DebugLog.ToConsole($"Error - No players found when getting player from object. (Attached NetID : {syncObject.AttachedNetId})", MessageType.Error);
return uint.MaxValue;
}
2020-09-02 11:17:04 +01:00
var playerIds = PlayerList.Select(x => x.PlayerId).ToList();
2020-09-04 20:52:06 +01:00
var lowerBound = playerIds.Where(x => x <= syncObject.AttachedNetId).ToList().Max();
if (PlayerList.Count != PlayerSyncObjects.Count(x => x.GetType() == syncObject.GetType()) && lowerBound == playerIds.Max())
2020-09-02 11:17:04 +01:00
{
2020-09-04 20:09:25 +01:00
if (syncObject.PreviousPlayerId != uint.MaxValue)
2020-09-04 18:54:53 +01:00
{
return syncObject.PreviousPlayerId;
}
2020-09-04 20:52:06 +01:00
if (syncObject.GetType() == typeof(PlayerTransformSync) && syncObject.AttachedNetId != 0U)
2020-09-02 11:17:04 +01:00
{
2020-09-04 20:52:06 +01:00
return GetPlayer(syncObject.AttachedNetId).PlayerId;
2020-09-02 11:17:04 +01:00
}
2020-09-04 20:09:25 +01:00
syncObject.PreviousPlayerId = uint.MaxValue;
return uint.MaxValue;
2020-09-02 11:17:04 +01:00
}
2020-09-04 18:54:53 +01:00
syncObject.PreviousPlayerId = lowerBound;
2020-09-02 11:17:04 +01:00
return lowerBound;
}
2020-09-04 20:09:25 +01:00
public static List<uint> GetPlayerNetIds(PlayerInfo player)
{
2020-09-29 21:34:46 +01:00
if (PlayerSyncObjects.Count == 0)
{
return default;
}
int count = 0;
PlayerSyncObjects.RemoveAll(x => x == null);
PlayerSyncObjects.RemoveAll(x => x.GetComponent<NetworkIdentity>() == null);
foreach (var item in PlayerSyncObjects.DistinctBy(x => x.AttachedNetId))
{
if (item.PlayerId == player.PlayerId)
{
count++;
}
}
2020-09-07 19:57:43 +02:00
return Enumerable.Range((int)player.PlayerId, count).Select(x => (uint)x).ToList();
}
}
}