-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerInfoController.cs
187 lines (157 loc) · 6.96 KB
/
PlayerInfoController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Timers;
using CitizenFX.Core;
using CitizenFX.Core.Native;
using Microsoft.EntityFrameworkCore.Internal;
using PoliceMP.Client.Services.Interfaces;
using PoliceMP.Core.Server.Commands.Interfaces;
using PoliceMP.Core.Server.Communications.Interfaces;
using PoliceMP.Core.Server.Networking;
using PoliceMP.Core.Shared;
using PoliceMP.Core.Shared.Models;
using PoliceMP.Server.Services.Interfaces;
using PoliceMP.Shared.Constants;
using PoliceMP.Shared.Constants.States;
using PoliceMP.Shared.Models;
using Debug = System.Diagnostics.Debug;
using Vector3 = PoliceMP.Core.Shared.Models.Vector3;
namespace PoliceMP.Server.Controllers
{
public class PlayerInfoController : Controller
{
private readonly IServerCommunicationsManager _comms;
private readonly PlayerList _playerList;
private readonly ICommandManager _command;
private readonly ILogger<PlayerInfoController> _logger;
private readonly IBucketService _buckets;
private readonly IPermissionService _permissionService;
private readonly Timer _timer = new Timer(10000)
{
AutoReset = true,
Enabled = true
};
private List<PlayerInfo> _cachedPlayerInfo = new List<PlayerInfo>();
public PlayerInfoController(IServerCommunicationsManager comms, PlayerList playerList, ICommandManager command, ILogger<PlayerInfoController> logger, IBucketService buckets, IPermissionService permissionService)
{
_comms = comms;
_playerList = playerList;
_command = command;
_logger = logger;
_buckets = buckets;
_permissionService = permissionService;
_timer.Elapsed += TimerElapsed;
}
/// <summary>
/// Updates the cached player list every 10 seconds to stop spamming it everytime it is requested. When requested they are sent the cache.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void TimerElapsed(object sender, ElapsedEventArgs e)
{
if (!_playerList.Any()) return;
await UpdateCachedPlayers();
_comms.ToClient(ServerEvents.SendAllPlayersToClient, _cachedPlayerInfo);
}
public async Task UpdateCachedPlayers()
{
try
{
var playerList = new List<PlayerInfo>();
foreach (var targetPlayer in _playerList)
{
var userRole = await _permissionService.GetUserRole(targetPlayer);
if(targetPlayer.Character == null) continue;
lock (playerList)
{
var tryParse = int.TryParse(targetPlayer.Handle, out int targetHandle);
if (!tryParse) continue;
var playerInfo = new PlayerInfo
{
Name = targetPlayer.Name,
Index = _playerList.IndexOf(targetPlayer),
RoutingBucket = _buckets.GetPlayerBucket(targetPlayer),
VehicleNetworkId = 0,
CallSign = (string)targetPlayer.State.Get("PMPCallsign"),
ActiveBranch = userRole.Branch,
ActiveDivision = userRole.Division
};
playerInfo.ServerHandle = targetHandle;
var ped = targetPlayer.Character;
if (ped != null)
{
playerInfo.NetworkId = ped.NetworkId;
if (ped.Position != default)
{
playerInfo.Position = new Vector3(ped.Position.X, ped.Position.Y, ped.Position.Z);
}
if (ped.Rotation != default)
{
playerInfo.Rotation = new Vector3(ped.Rotation.X, ped.Rotation.Y, ped.Rotation.Z);
}
}
playerList.Add(playerInfo);
}
}
lock (_cachedPlayerInfo)
{
_cachedPlayerInfo = playerList;
}
}
catch (Exception e)
{
Debug.WriteLine(e);
return;
}
}
public override Task Started()
{
_comms.OnRequest<int, PlayerInfo>(ServerEvents.RequestPlayerInfo, FetchPlayerInfoFromNetworkId);
_comms.OnRequest(ServerEvents.FetchAllPlayersFromServer, FetchAllPlayersFromServer);
_comms.On<string>(ServerEvents.OnReceiveCallSign, (player, callsign) =>
{
player.State.Set("PMPCallsign", callsign, true);
});
return Task.FromResult(0);
}
private async Task<List<PlayerInfo>> FetchAllPlayersFromServer(Player player)
{
return _cachedPlayerInfo;
}
private async Task<PlayerInfo> FetchPlayerInfoFromNetworkId(Player player, int networkId)
{
var targetPlayer = _playerList.FirstOrDefault(x => x.Character?.NetworkId == networkId);
if (targetPlayer == null || targetPlayer.Character == null) return null;
var ped = targetPlayer.Character;
var userRole = await _permissionService.GetUserRole(player);
var playerInfo = new PlayerInfo
{
Name = targetPlayer.Name,
Index = _playerList.IndexOf(targetPlayer),
RoutingBucket = _buckets.GetPlayerBucket(targetPlayer),
NetworkId = ped.NetworkId,
Position = new Vector3(ped.Position.X, ped.Position.Y, ped.Position.Z),
Rotation = new Vector3(ped.Rotation.X, ped.Rotation.Y, ped.Rotation.Z),
VehicleNetworkId = 0,
CallSign = targetPlayer.State.Get("PMPCallsign"),
ActiveBranch = userRole.Branch,
ActiveDivision = userRole.Division
};
var tryParse = int.TryParse(targetPlayer.Handle, out int targetHandle);
if (tryParse)
{
playerInfo.ServerHandle = targetHandle;
}
var pedVehicleId = API.GetVehiclePedIsIn(ped.Handle, false);
if (pedVehicleId == 0) return playerInfo;
var pedVehicle = (Vehicle)Entity.FromHandle(pedVehicleId);
if (pedVehicle != null)
{
playerInfo.VehicleNetworkId = pedVehicle.NetworkId;
}
return playerInfo;
}
}
}