-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDsuCommands.cs
130 lines (105 loc) · 4.21 KB
/
DsuCommands.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
using System;
using System.Linq;
using System.Threading.Tasks;
using CitizenFX.Core;
using CitizenFX.Core.Native;
using PoliceMP.Core.Server.Commands.Interfaces;
using PoliceMP.Core.Server.Communications.Interfaces;
using PoliceMP.Core.Server.Interfaces.Services;
using PoliceMP.Core.Server.Networking;
using PoliceMP.Core.Shared;
using PoliceMP.Shared.Constants;
namespace PoliceMP.Server.Controllers.Commands
{
public class DsuCommands : Controller
{
private readonly ILogger<DsuCommands> _logger;
private readonly ICommandManager _commands;
private readonly IServerCommunicationsManager _comms;
private readonly INotificationService _notifications;
//private readonly IUserService _userService;
private readonly PlayerList _players;
private readonly float ChatDistance = 50f;
public DsuCommands(ILogger<DsuCommands> logger,
ICommandManager commands,
IServerCommunicationsManager comms,
INotificationService notifications,
PlayerList players)
{
_logger = logger;
_commands = commands;
_comms = comms;
_notifications = notifications;
_players = players;
//_userService = userService;
}
public override Task Started()
{
_comms.On(ServerEvents.SendDogMeCommand, (Player player, string dogName, string emoteText) =>
{
DogMeCommand(player, dogName, emoteText);
});
_comms.On(ServerEvents.SendDogDoCommand, (Player player, string dogName, string emoteText) =>
{
DogDoCommand(player, dogName, emoteText);
});
_comms.OnRequest<string, bool>(ServerEvents.FetchDsuAceAllowed, (player, s) => Task.FromResult(API.IsPlayerAceAllowed(player.Handle, "Police.dogTrained")));
return Task.FromResult(0);
}
public void DogMeCommand(Player player, string dogName, string message)
{
try
{
if (string.IsNullOrEmpty(message)) return;
string formattedString = $"^6* {dogName} {message}";
Vector3 playerPosition = player.Character.Position;
foreach (var target in _players.ToArray())
{
if (target == null || target.Character == null) continue;
Vector3 targetPosition = target.Character.Position;
if (targetPosition == default(Vector3)) continue;
if (targetPosition.Distance(playerPosition) > ChatDistance) continue;
target.TriggerEvent("chat:addMessage", formattedString);
}
}
catch (Exception e)
{
Console.WriteLine(e);
return;
}
}
public void DogDoCommand(Player player, string dogName, string message)
{
try
{
if (string.IsNullOrEmpty(message)) return;
string formattedString = $"^6* {message} (( {dogName} ))";
Vector3 playerPosition = player.Character.Position;
foreach (var target in _players.ToArray())
{
if (target == null || target.Character == null) continue;
Vector3 targetPosition = target.Character.Position;
if (targetPosition == default(Vector3)) continue;
if (targetPosition.Distance(playerPosition) > ChatDistance) continue;
target.TriggerEvent("chat:addMessage", formattedString);
}
}
catch (Exception e)
{
Console.WriteLine(e);
return;
}
}
}
public static class Vector3Extension
{
public static float Distance(this Vector3 position, Vector3 targetPosition)
{
var diffX = position.X - targetPosition.X;
var diffY = position.Y - targetPosition.Y;
var diffZ = position.Z - targetPosition.Z;
var sum = diffX * diffX + diffY * diffY + diffZ * diffZ;
return (float)Math.Sqrt(sum);
}
}
}