RAGECOOP-V/RageCoop.Client/PlayerList.cs

122 lines
3.7 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;
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;
public static List<PlayerData> Players=new List<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-05-22 15:55:26 +08:00
_mainScaleform.CallFunction("SET_DATA_SLOT", i++, $"{player.Latency * 1000:N0}ms", player.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
{
var toset = Players.Where(x => x.PedID==id);
if (toset.Any())
{
var p=toset.First();
p.Username=username;
p.PedID=id;
p.Latency=latency;
2022-05-22 15:55:26 +08:00
}
else
{
PlayerData p = new PlayerData { PedID=id, Username=username,Latency=latency };
2022-05-22 15:55:26 +08:00
Players.Add(p);
}
}
public static PlayerData GetPlayer(int id)
2022-05-22 15:55:26 +08:00
{
return Players.Find(x => x.PedID==id);
}
public static void RemovePlayer(int id)
2022-05-22 15:55:26 +08:00
{
var p = Players.Where(x => x.PedID==id);
if (p.Any())
{
Players.Remove(p.First());
}
}
public static void Cleanup()
{
Players=new List<PlayerData> { };
}
2022-05-22 15:55:26 +08:00
2021-07-07 13:36:25 +02:00
}
public class PlayerData
{
public string Username { get; internal set; }
/// <summary>
/// Universal character ID.
/// </summary>
public int PedID
{
get; internal set;
}
/// <summary>
/// The ID of player's last vehicle.
/// </summary>
public int VehicleID { get; internal set; }
public Vector3 Position { get; internal set; }
/// <summary>
/// Player Latency in second.
/// </summary>
public float Latency { get; internal set; }
public int Health { get; internal set; }
}
2021-07-07 13:36:25 +02:00
}