Files
RAGECOOP-V/Client/Scripts/PlayerList.cs

170 lines
5.9 KiB
C#
Raw Normal View History

2022-07-20 17:50:01 +08:00
using GTA;
2022-08-08 17:03:41 +08:00
using GTA.Math;
2021-07-07 13:36:25 +02:00
using GTA.Native;
2022-09-06 21:46:35 +08:00
using Lidgren.Network;
2022-10-08 23:49:48 +08:00
using RageCoop.Client.Scripting;
2022-07-20 17:50:01 +08:00
using RageCoop.Core;
using System.Collections.Generic;
2022-08-11 15:38:19 +08:00
using System.Linq;
2022-09-06 21:46:35 +08:00
using System.Net;
2021-07-07 13:36:25 +02:00
2022-05-22 15:55:26 +08:00
namespace RageCoop.Client
2021-07-07 13:36:25 +02:00
{
internal static class PlayerList
2021-07-07 13:36:25 +02:00
{
private const float LEFT_POSITION = 0.122f;
private const float RIGHT_POSITION = 0.9f;
private static readonly Scaleform _mainScaleform = new Scaleform("mp_mm_card_freemode");
private static ulong _lastUpdate = Util.GetTickCount64();
public static ulong Pressed { get; set; }
public static bool LeftAlign = true;
2022-08-08 17:03:41 +08:00
public static Dictionary<int, Player> Players = new Dictionary<int, Player> { };
public static void Tick()
2021-08-13 15:20:26 +02:00
{
if (!Networking.IsOnServer)
2021-08-13 15:20:26 +02:00
{
2021-08-17 15:04:50 +02:00
return;
2021-08-13 15:20:26 +02:00
}
if ((Util.GetTickCount64() - _lastUpdate) >= 1000)
2021-08-13 15:20:26 +02:00
{
2022-08-08 17:03:41 +08:00
Update();
2021-08-13 15:20:26 +02:00
}
2021-11-19 22:08:15 +01:00
if ((Util.GetTickCount64() - Pressed) < 5000 && !Main.MainChat.Focused
#if !NON_INTERACTIVE
&& !Menus.CoopMenu.MenuPool.AreAnyVisible
#endif
)
2021-08-13 15:20:26 +02:00
{
2022-07-20 17:50:01 +08:00
Function.Call(Hash.DRAW_SCALEFORM_MOVIE, _mainScaleform.Handle,
LeftAlign ? LEFT_POSITION : RIGHT_POSITION, 0.3f,
0.28f, 0.6f,
255, 255, 255, 255, 0);
2021-08-13 15:20:26 +02:00
}
}
2022-08-08 17:03:41 +08:00
private static void Update()
2021-07-07 13:36:25 +02:00
{
_lastUpdate = Util.GetTickCount64();
2021-08-13 15:20:26 +02:00
_mainScaleform.CallFunction("SET_DATA_SLOT_EMPTY", 0);
2022-09-06 21:46:35 +08:00
int i = 0;
2022-07-20 17:50:01 +08:00
2022-08-08 17:03:41 +08:00
foreach (var player in Players.Values)
2021-07-07 13:36:25 +02:00
{
2022-09-06 21:46:35 +08:00
_mainScaleform.CallFunction("SET_DATA_SLOT", i++, $"{player.Ping * 1000:N0}ms", player.Username + (player.IsHost ? " (Host)" : ""), 116, 0, i - 1, "", "", 2, "", "", ' ');
2021-07-07 13:36:25 +02:00
}
_mainScaleform.CallFunction("SET_TITLE", "Player list", $"{Players.Count} players");
_mainScaleform.CallFunction("DISPLAY_VIEW");
2021-07-07 13:36:25 +02:00
}
2022-07-20 17:50:01 +08:00
public static void SetPlayer(int id, string username, float latency = 0)
2022-05-22 15:55:26 +08:00
{
Main.Logger.Debug($"{id},{username},{latency}");
2022-09-08 12:41:56 -07:00
if (Players.TryGetValue(id, out Player p))
2022-05-22 15:55:26 +08:00
{
2022-09-06 21:46:35 +08:00
p.Username = username;
p.ID = id;
p._latencyToServer = latency;
2022-05-22 15:55:26 +08:00
}
else
{
2022-09-06 21:46:35 +08:00
p = new Player { ID = id, Username = username, _latencyToServer = latency };
2022-07-20 17:50:01 +08:00
Players.Add(id, p);
2022-06-22 14:18:20 +08:00
}
}
public static void UpdatePlayer(Packets.PlayerInfoUpdate packet)
{
var p = GetPlayer(packet.PedID);
2022-09-06 21:46:35 +08:00
if (p != null)
2022-07-30 12:03:57 +08:00
{
2022-08-08 17:03:41 +08:00
p._latencyToServer = packet.Latency;
p.Position = packet.Position;
2022-09-06 21:46:35 +08:00
p.IsHost = packet.IsHost;
2022-10-09 11:15:09 +08:00
API.QueueAction(() =>
2022-08-08 17:47:09 +08:00
{
2022-09-06 21:46:35 +08:00
if (p.FakeBlip?.Exists() != true)
2022-08-08 18:07:22 +08:00
{
2022-09-06 21:46:35 +08:00
p.FakeBlip = World.CreateBlip(p.Position);
2022-08-08 18:07:22 +08:00
}
2022-08-27 14:17:10 +08:00
if (EntityPool.PedExists(p.ID))
2022-08-08 17:47:09 +08:00
{
2022-08-20 11:08:51 +08:00
p.FakeBlip.DisplayType = BlipDisplayType.NoDisplay;
2022-08-08 17:47:09 +08:00
}
else
{
2022-10-08 23:49:48 +08:00
p.FakeBlip.Color = API.Config.BlipColor;
p.FakeBlip.Scale = API.Config.BlipScale;
p.FakeBlip.Sprite = API.Config.BlipSprite;
2022-08-20 11:08:51 +08:00
p.FakeBlip.DisplayType = BlipDisplayType.Default;
p.FakeBlip.Position = p.Position;
2022-09-06 21:46:35 +08:00
}
2022-08-08 17:47:09 +08:00
});
2022-09-06 21:46:35 +08:00
2022-07-30 12:03:57 +08:00
}
2022-05-22 15:55:26 +08:00
}
2022-08-08 17:03:41 +08:00
public static Player GetPlayer(int id)
2022-05-22 15:55:26 +08:00
{
2022-09-08 12:41:56 -07:00
Players.TryGetValue(id, out Player p);
2022-06-22 14:18:20 +08:00
return p;
}
2022-08-08 17:03:41 +08:00
public static Player GetPlayer(SyncedPed p)
2022-06-22 14:18:20 +08:00
{
var player = GetPlayer(p.ID);
2022-09-06 21:46:35 +08:00
if (player != null)
{
2022-09-06 21:46:35 +08:00
player.Character = p;
}
2022-06-22 14:18:20 +08:00
return player;
2022-05-22 15:55:26 +08:00
}
public static void RemovePlayer(int id)
2022-05-22 15:55:26 +08:00
{
2022-09-06 21:46:35 +08:00
if (Players.TryGetValue(id, out var player))
2022-05-22 15:55:26 +08:00
{
2022-06-22 14:18:20 +08:00
Players.Remove(id);
2022-10-09 11:15:09 +08:00
API.QueueAction(() => player.FakeBlip?.Delete());
2022-05-22 15:55:26 +08:00
}
}
public static void Cleanup()
{
2022-09-06 21:46:35 +08:00
foreach (var p in Players.Values.ToArray())
2022-08-11 15:38:19 +08:00
{
p.FakeBlip?.Delete();
}
2022-09-06 21:46:35 +08:00
Players = new Dictionary<int, Player> { };
}
2021-07-07 13:36:25 +02:00
}
2022-08-27 14:17:10 +08:00
public class Player
{
2022-08-27 14:17:10 +08:00
public byte HolePunchStatus { get; internal set; } = 1;
public bool IsHost { get; internal set; }
public string Username { get; internal set; }
/// <summary>
2022-08-27 14:17:10 +08:00
/// Universal ped ID.
/// </summary>
2022-08-27 14:17:10 +08:00
public int ID
{
get; internal set;
}
2022-08-27 14:17:10 +08:00
public IPEndPoint InternalEndPoint { get; internal set; }
public IPEndPoint ExternalEndPoint { get; internal set; }
internal bool ConnectWhenPunched { get; set; }
public Blip FakeBlip { get; internal set; }
public Vector3 Position { get; internal set; }
public SyncedPed Character { get; internal set; }
/// <summary>
2022-08-27 14:17:10 +08:00
/// Player round-trip time in seconds, will be the rtt to server if not using P2P connection.
/// </summary>
2022-09-06 21:46:35 +08:00
public float Ping => Main.LocalPlayerID == ID ? Networking.Latency * 2 : (HasDirectConnection ? Connection.AverageRoundtripTime : _latencyToServer * 2);
public float PacketTravelTime => HasDirectConnection ? Connection.AverageRoundtripTime / 2 : Networking.Latency + _latencyToServer;
2022-08-27 14:17:10 +08:00
internal float _latencyToServer = 0;
2022-07-09 19:32:11 +08:00
public bool DisplayNameTag { get; set; } = true;
2022-08-27 14:17:10 +08:00
public NetConnection Connection { get; internal set; }
public bool HasDirectConnection => Connection?.Status == NetConnectionStatus.Connected;
}
2021-07-07 13:36:25 +02:00
}