RAGECOOP-V/RageCoop.Client/PlayerList.cs

170 lines
5.8 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-07-20 17:50:01 +08:00
using RageCoop.Core;
using System.Collections.Generic;
2022-08-08 17:03:41 +08:00
using Lidgren.Network;
using System.Net;
2022-08-11 15:38:19 +08:00
using System.Linq;
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);
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-08-16 19:51:56 +08:00
_mainScaleform.CallFunction("SET_DATA_SLOT", i++, $"{player.Ping * 1000:N0}ms", player.Username, 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-08-08 17:03:41 +08:00
Player p;
2022-07-20 17:50:01 +08:00
if (Players.TryGetValue(id, out p))
2022-05-22 15:55:26 +08:00
{
p.Username=username;
p.PedID=id;
2022-08-08 17:03:41 +08:00
p._latencyToServer=latency;
2022-05-22 15:55:26 +08:00
}
else
{
2022-08-08 17:03:41 +08:00
p = new Player { PedID=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-07-30 12:03:57 +08:00
if (p!=null)
{
2022-08-08 17:03:41 +08:00
p._latencyToServer = packet.Latency;
p.Position = packet.Position;
2022-08-08 17:47:09 +08:00
Main.QueueAction(() =>
{
2022-08-08 18:07:22 +08:00
if (p.FakeBlip?.Exists()!=true)
{
p.FakeBlip=World.CreateBlip(p.Position);
}
2022-08-20 11:08:51 +08:00
if (p.Character?.MainPed != null && p.Character.MainPed.Exists())
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-08-20 11:08:51 +08:00
p.FakeBlip.Color = Scripting.API.Config.BlipColor;
p.FakeBlip.Scale = Scripting.API.Config.BlipScale;
p.FakeBlip.Sprite = Scripting.API.Config.BlipSprite;
p.FakeBlip.DisplayType = BlipDisplayType.Default;
p.FakeBlip.Position = p.Position;
2022-08-08 17:47:09 +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-08-08 17:03:41 +08:00
Player p;
2022-06-22 14:18:20 +08:00
Players.TryGetValue(id, out p);
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);
if (player!=null)
{
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-08-11 15:38:19 +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-08-11 15:38:19 +08:00
Main.QueueAction(() => player.FakeBlip?.Delete());
2022-05-22 15:55:26 +08:00
}
}
public static void Cleanup()
{
2022-08-11 15:38:19 +08:00
foreach(var p in Players.Values.ToArray())
{
p.FakeBlip?.Delete();
}
2022-08-08 17:03:41 +08:00
Players=new Dictionary<int, Player> { };
}
2021-07-07 13:36:25 +02:00
}
2022-08-08 17:03:41 +08:00
internal class Player
{
2022-08-10 20:42:47 +08:00
public byte HolePunchStatus { get; set; } = 1;
public string Username { get; internal set; }
/// <summary>
/// Universal character ID.
/// </summary>
public int PedID
{
get; internal set;
}
2022-08-10 20:42:47 +08:00
public IPEndPoint InternalEndPoint { get; set; }
public IPEndPoint ExternalEndPoint { get; set; }
public bool ConnectWhenPunched { get; set; }
2022-08-08 17:47:09 +08:00
public Blip FakeBlip { get; set; }
2022-08-08 17:03:41 +08:00
public Vector3 Position { get; set; }
2022-06-22 14:18:20 +08:00
public SyncedPed Character { get; set; }
/// <summary>
2022-08-16 19:51:56 +08:00
/// Player round-trip time in seconds, will be the latency to server if not using P2P connection.
/// </summary>
2022-08-16 19:51:56 +08:00
public float Ping => Main.LocalPlayerID==PedID ? Networking.Latency*2 : (HasDirectConnection ? Connection.AverageRoundtripTime : _latencyToServer*2);
2022-08-08 17:03:41 +08:00
public float PacketTravelTime => HasDirectConnection ? Connection.AverageRoundtripTime/2 : Networking.Latency+_latencyToServer;
public float _latencyToServer = 0;
2022-07-09 19:32:11 +08:00
public bool DisplayNameTag { get; set; } = true;
2022-08-08 17:03:41 +08:00
public NetConnection Connection { get; set; }
public bool HasDirectConnection => Connection?.Status==NetConnectionStatus.Connected;
}
2021-07-07 13:36:25 +02:00
}