Add some client API

This commit is contained in:
sardelka9515
2022-08-27 14:17:10 +08:00
parent accdbbcbc6
commit f44558cd3b
6 changed files with 60 additions and 78 deletions

View File

@ -29,7 +29,7 @@ namespace RageCoop.Client
{
if (p.InternalEndPoint!=null && p.ExternalEndPoint!=null && (p.Connection==null || p.Connection.Status==NetConnectionStatus.Disconnected))
{
Main.Logger.Trace($"Sending HolePunch message to {p.InternalEndPoint},{p.ExternalEndPoint}. {p.Username}:{p.PedID}");
Main.Logger.Trace($"Sending HolePunch message to {p.InternalEndPoint},{p.ExternalEndPoint}. {p.Username}:{p.ID}");
var msg = Networking.Peer.CreateMessage();
new Packets.HolePunch
{
@ -69,7 +69,7 @@ namespace RageCoop.Client
puncher.HolePunchStatus=(byte)(p.Status+1);
if (p.Status>=3)
{
Main.Logger.Debug("HolePunch sucess: "+from+", "+puncher.PedID);
Main.Logger.Debug("HolePunch sucess: "+from+", "+puncher.ID);
if (puncher.ConnectWhenPunched && (puncher.Connection==null || puncher.Connection.Status==NetConnectionStatus.Disconnected))
{
Main.Logger.Debug("Connecting to peer: "+from);

View File

@ -141,7 +141,7 @@ namespace RageCoop.Client
{
var p = new Player
{
PedID = packet.PedID,
ID = packet.PedID,
Username= packet.Username,
};
PlayerList.SetPlayer(packet.PedID, packet.Username);

View File

@ -67,12 +67,12 @@ namespace RageCoop.Client
if (Players.TryGetValue(id, out p))
{
p.Username=username;
p.PedID=id;
p.ID=id;
p._latencyToServer=latency;
}
else
{
p = new Player { PedID=id, Username=username, _latencyToServer=latency };
p = new Player { ID=id, Username=username, _latencyToServer=latency };
Players.Add(id, p);
}
}
@ -90,7 +90,7 @@ namespace RageCoop.Client
{
p.FakeBlip=World.CreateBlip(p.Position);
}
if (EntityPool.PedExists(p.PedID))
if (EntityPool.PedExists(p.ID))
{
p.FakeBlip.DisplayType = BlipDisplayType.NoDisplay;
}
@ -139,32 +139,32 @@ namespace RageCoop.Client
}
}
internal class Player
public class Player
{
public byte HolePunchStatus { get; set; } = 1;
public bool IsHost;
public byte HolePunchStatus { get; internal set; } = 1;
public bool IsHost { get; internal set; }
public string Username { get; internal set; }
/// <summary>
/// Universal character ID.
/// Universal ped ID.
/// </summary>
public int PedID
public int ID
{
get; internal set;
}
public IPEndPoint InternalEndPoint { get; set; }
public IPEndPoint ExternalEndPoint { get; set; }
public bool ConnectWhenPunched { get; set; }
public Blip FakeBlip { get; set; }
public Vector3 Position { get; set; }
public SyncedPed Character { get; set; }
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>
/// Player round-trip time in seconds, will be the latency to server if not using P2P connection.
/// Player round-trip time in seconds, will be the rtt to server if not using P2P connection.
/// </summary>
public float Ping => Main.LocalPlayerID==PedID ? Networking.Latency*2 : (HasDirectConnection ? Connection.AverageRoundtripTime : _latencyToServer*2);
public float Ping => Main.LocalPlayerID==ID ? Networking.Latency*2 : (HasDirectConnection ? Connection.AverageRoundtripTime : _latencyToServer*2);
public float PacketTravelTime => HasDirectConnection ? Connection.AverageRoundtripTime/2 : Networking.Latency+_latencyToServer;
public float _latencyToServer = 0;
internal float _latencyToServer = 0;
public bool DisplayNameTag { get; set; } = true;
public NetConnection Connection { get; set; }
public bool HasDirectConnection => Connection?.Status==NetConnectionStatus.Connected;
public NetConnection Connection { get; internal set; }
public bool HasDirectConnection => Connection?.Status == NetConnectionStatus.Connected;
}
}

View File

@ -4,6 +4,7 @@ using RageCoop.Core;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Newtonsoft.Json;
namespace RageCoop.Client.Scripting
{
@ -212,13 +213,12 @@ namespace RageCoop.Client.Scripting
/// Get a <see cref="Core.Logger"/> that RAGECOOP is currently using.
/// </summary>
/// <returns></returns>
public static Logger Logger
{
get
{
return Main.Logger;
}
}
public static Logger Logger => Main.Logger;
/// <summary>
/// Get all players indexed by their ID
/// </summary>
public static Dictionary<int,Player> Players => new Dictionary<int, Player>(PlayerList.Players);
#endregion
#region FUNCTIONS
@ -245,6 +245,15 @@ namespace RageCoop.Client.Scripting
Networking.ToggleConnection(null);
}
}
/// <summary>
/// List all servers from master server address
/// </summary>
/// <returns></returns>
public static List<ServerInfo> ListServers()
{
return JsonConvert.DeserializeObject<List<ServerInfo>>(HttpHelper.DownloadString(Main.Settings.MasterServer));
}
/// <summary>
/// Send a local chat message to this player

View File

@ -114,64 +114,34 @@ namespace RageCoop.Client
Function.Call(Hash.SET_NUMBER_OF_PARKED_VEHICLES, 0);
Function.Call(Hash.SET_DISTANT_CARS_ENABLED, false);
Function.Call(Hash.DISABLE_VEHICLE_DISTANTLIGHTS, true);
if (Networking.IsOnServer)
foreach (Ped ped in World.GetAllPeds())
{
foreach (Ped ped in World.GetAllPeds())
SyncedPed c = EntityPool.GetPedByHandle(ped.Handle);
if ((c == null) || (c.IsLocal && (ped.Handle != Game.Player.Character.Handle) && ped.PopulationType != EntityPopulationType.Mission))
{
SyncedPed c = EntityPool.GetPedByHandle(ped.Handle);
if ((c == null) || (c.IsLocal && (ped.Handle != Game.Player.Character.Handle) && ped.PopulationType != EntityPopulationType.Mission))
{
if (ped.Handle == Game.Player.Character.Handle) { continue; }
// Main.Logger.Trace($"Removing ped {ped.Handle}. Reason:RemoveTraffic");
ped.CurrentVehicle?.Delete();
ped.Kill();
ped.Delete();
}
if (ped.Handle == Game.Player.Character.Handle) { continue; }
// Main.Logger.Trace($"Removing ped {ped.Handle}. Reason:RemoveTraffic");
ped.CurrentVehicle?.Delete();
ped.Kill();
ped.Delete();
}
foreach (Vehicle veh in World.GetAllVehicles())
{
SyncedVehicle v = veh.GetSyncEntity();
if (v.MainVehicle == Game.Player.LastVehicle || v.MainVehicle==Game.Player.Character.CurrentVehicle)
{
// Don't delete player's vehicle
continue;
}
if ((v == null) || (v.IsLocal && veh.PopulationType != EntityPopulationType.Mission))
{
// Main.Logger.Debug($"Removing Vehicle {veh.Handle}. Reason:ClearTraffic");
veh.Delete();
}
}
}
else
foreach (Vehicle veh in World.GetAllVehicles())
{
foreach (Ped ped in World.GetAllPeds())
SyncedVehicle v = veh.GetSyncEntity();
if (v.MainVehicle == Game.Player.LastVehicle || v.MainVehicle == Game.Player.Character.CurrentVehicle)
{
if ((ped != Game.Player.Character) && (ped.PopulationType != EntityPopulationType.Mission))
{
// Main.Logger.Trace($"Removing ped {ped.Handle}. Reason:RemoveTraffic");
ped.CurrentVehicle?.Delete();
ped.Kill();
ped.Delete();
}
// Don't delete player's vehicle
continue;
}
var last = Game.Player.Character.LastVehicle;
var current = Game.Player.Character.CurrentVehicle;
foreach (Vehicle veh in World.GetAllVehicles())
if ((v == null) || (v.IsLocal && veh.PopulationType != EntityPopulationType.Mission))
{
if (veh.PopulationType != EntityPopulationType.Mission && veh != last && veh!=current)
{
// Main.Logger.Debug($"Removing Vehicle {veh.Handle}. Reason:ClearTraffic");
// Main.Logger.Debug($"Removing Vehicle {veh.Handle}. Reason:ClearTraffic");
veh.Delete();
}
veh.Delete();
}
}
}

View File

@ -5,9 +5,12 @@ using Newtonsoft.Json;
namespace RageCoop.Core
{
internal class ServerInfo
/// <summary>
/// A json object representing a server's information as annouced to master server.
/// </summary>
public class ServerInfo
{
#pragma warning disable 1591
public string address { get; set; }
public string port { get; set; }
public string name { get; set; }