RAGECOOP-V/RageCoop.Client/PlayerList.cs

131 lines
3.9 KiB
C#
Raw Normal View History

2022-03-28 14:49:26 +02:00
using System.Collections.Generic;
2022-05-22 15:55:26 +08:00
using System.Linq;
2021-07-07 13:36:25 +02:00
using GTA;
using GTA.Math;
2022-06-22 14:18:20 +08:00
using RageCoop.Core;
2021-07-07 13:36:25 +02:00
using GTA.Native;
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-06-22 14:18:20 +08:00
public static Dictionary<int,PlayerData> Players=new Dictionary<int, PlayerData> { };
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-05-22 15:55:26 +08:00
Update( Main.Settings.Username);
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
{
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
}
}
private static void Update( string localUsername)
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);
_mainScaleform.CallFunction("SET_DATA_SLOT", 0, $"{Networking.Latency * 1000:N0}ms", localUsername, 116, 0, 0, "", "", 2, "", "", ' ');
2021-07-07 13:36:25 +02:00
int i = 1;
2021-08-13 15:20:26 +02:00
2022-05-22 15:55:26 +08:00
foreach (var player in Players)
2021-07-07 13:36:25 +02:00
{
2022-06-22 14:18:20 +08:00
_mainScaleform.CallFunction("SET_DATA_SLOT", i++, $"{player.Value.Latency * 1000:N0}ms", player.Value.Username, 116, 0, i - 1, "", "", 2, "", "", ' ');
2021-07-07 13:36:25 +02:00
}
2022-05-30 11:11:40 +08:00
_mainScaleform.CallFunction("SET_TITLE", "Player list", $"{Players.Count+1} players");
_mainScaleform.CallFunction("DISPLAY_VIEW");
2021-07-07 13:36:25 +02:00
}
public static void SetPlayer(int id, string username,float latency=0)
2022-05-22 15:55:26 +08:00
{
2022-06-22 14:18:20 +08:00
PlayerData p;
if (Players.TryGetValue(id,out p))
2022-05-22 15:55:26 +08:00
{
p.Username=username;
p.PedID=id;
p.Latency=latency;
2022-05-22 15:55:26 +08:00
}
else
{
2022-06-22 14:18:20 +08:00
p = new PlayerData { PedID=id, Username=username,Latency=latency };
Players.Add(id,p);
}
}
public static void UpdatePlayer(Packets.PlayerInfoUpdate packet)
{
var p = GetPlayer(packet.PedID);
if(p?.Character != null)
{
p.Latency= packet.Latency;
2022-05-22 15:55:26 +08:00
}
}
public static PlayerData GetPlayer(int id)
2022-05-22 15:55:26 +08:00
{
2022-06-22 14:18:20 +08:00
PlayerData p;
Players.TryGetValue(id, out p);
return p;
}
public static PlayerData GetPlayer(SyncedPed p)
{
var player = GetPlayer(p.ID);
player.Character=p;
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-06-22 14:18:20 +08:00
if (Players.ContainsKey(id))
2022-05-22 15:55:26 +08:00
{
2022-06-22 14:18:20 +08:00
Players.Remove(id);
2022-05-22 15:55:26 +08:00
}
}
public static void Cleanup()
{
2022-06-22 14:18:20 +08:00
Players=new Dictionary<int, PlayerData>{ };
}
2022-05-22 15:55:26 +08:00
2021-07-07 13:36:25 +02:00
}
2022-06-22 14:18:20 +08:00
internal class PlayerData
{
public string Username { get; internal set; }
/// <summary>
/// Universal character ID.
/// </summary>
public int PedID
{
get; internal set;
}
2022-06-22 14:18:20 +08:00
public SyncedPed Character { get; set; }
/// <summary>
/// Player Latency in second.
/// </summary>
public float Latency { get; internal set; }
2022-06-22 14:18:20 +08:00
}
2021-07-07 13:36:25 +02:00
}