Skip to content

Commit

Permalink
feat(3.x.x): initial compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackFlash5 committed Nov 6, 2021
1 parent 4aad84a commit 556519a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 10 deletions.
48 changes: 41 additions & 7 deletions saltychat/SaltyClient/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,17 @@ public EchoEffect(int duration = 100, float rolloff = 0.3f, int delay = 250)
#region Properties
public Vector3 Position { get; set; }
public float Rotation { get; set; }
public float VoiceRange { get; set; }
public bool IsAlive { get; set; }
public EchoEffect Echo { get; set; }
#endregion

#region CTOR
public SelfState(CitizenFX.Core.Vector3 position, float rotation, bool isAlive, bool echo = false)
public SelfState(CitizenFX.Core.Vector3 position, float rotation, float voiceRange, bool isAlive, bool echo = false)
{
this.Position = new Vector3(position.X, position.Y, position.Z);
this.Rotation = rotation;
this.VoiceRange = voiceRange;
this.IsAlive = isAlive;

if (echo)
Expand Down Expand Up @@ -569,6 +571,30 @@ public RadioCommunication(string name, RadioType senderRadioType, RadioType ownR
#endregion
}

/// <summary>
/// User for <see cref="Command.AddRadioChannelMember"/> and <see cref="Command.RemoveRadioChannelMember"/>
/// </summary>
public class RadioChannelMember
{
public string PlayerName { get; set; }
public bool IsPrimaryChannel { get; set; }
}

/// <summary>
/// Used for <see cref="Command.UpdateRadioChannelMembers"/>
/// </summary>
public class RadioChannelMemberUpdate
{
public string[] PlayerNames { get; set; }
public bool IsPrimaryChannel { get; set; }

public RadioChannelMemberUpdate(string[] members, bool isPrimary)
{
this.PlayerNames = members;
this.IsPrimaryChannel = isPrimary;
}
}

/// <summary>
/// Sent by the plugin through <see cref="Command.RadioTrafficState"/>
/// </summary>
Expand Down Expand Up @@ -735,6 +761,10 @@ public enum Command
RadioTowerUpdate = 32,
RadioTrafficState = 33,

AddRadioChannelMember = 37,
UpdateRadioChannelMembers = 38,
RemoveRadioChannelMember = 39,

// Megaphone
MegaphoneCommunicationUpdate = 40,
StopMegaphoneCommunication = 41,
Expand All @@ -744,12 +774,16 @@ public enum Command
#region Error
public enum Error
{
OK,
InvalidJson,
NotConnectedToServer,
AlreadyInGame,
ChannelNotAvailable,
NameNotAvailable
OK = 0,
InvalidJson = 1,
NotConnectedToServer = 2,
AlreadyInGame = 3,
ChannelNotAvailable = 4,
NameNotAvailable = 5,
InvalidValue = 6,

ServerBlacklisted = 100,
ServerUnderlicensed = 101
}
#endregion

Expand Down
26 changes: 25 additions & 1 deletion saltychat/SaltyClient/VoiceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,21 +346,44 @@ private void OnSetRadioChannel(string radioChannel, bool isPrimary)
this.PrimaryRadioChannel = radioChannel;

if (String.IsNullOrEmpty(radioChannel))
{
this.PlaySound("leaveRadioChannel", false, "radio");

this.ExecuteCommand(new PluginCommand(Command.UpdateRadioChannelMembers, this.Configuration.ServerUniqueIdentifier, new RadioChannelMemberUpdate(new string[0], true)));
}
else
{
this.PlaySound("enterRadioChannel", false, "radio");
}
}
else
{
this.SecondaryRadioChannel = radioChannel;

if (String.IsNullOrEmpty(radioChannel))
{
this.PlaySound("leaveRadioChannel", false, "radio");

this.ExecuteCommand(new PluginCommand(Command.UpdateRadioChannelMembers, this.Configuration.ServerUniqueIdentifier, new RadioChannelMemberUpdate(new string[0], false)));
}
else
{
this.PlaySound("enterRadioChannel", false, "radio");
}
}
}

[EventHandler(Event.SaltyChat_RadioChannelMemberUpdated)]
private void OnChannelMembersUpdated(string channelName, List<dynamic> channelMembers)
{
string[] memberArray = channelMembers.Select(m => (string)m).ToArray();

if (this.PrimaryRadioChannel == channelName)
this.ExecuteCommand(new PluginCommand(Command.UpdateRadioChannelMembers, this.Configuration.ServerUniqueIdentifier, new RadioChannelMemberUpdate(memberArray, true)));
else if (this.SecondaryRadioChannel == channelName)
this.ExecuteCommand(new PluginCommand(Command.UpdateRadioChannelMembers, this.Configuration.ServerUniqueIdentifier, new RadioChannelMemberUpdate(memberArray, false)));
}

[EventHandler(Event.SaltyChat_IsSending)]
private void OnPlayerIsSending(string handle, string teamSpeakName, string radioChannel, bool isSending, bool stateChange, dynamic position)
{
Expand Down Expand Up @@ -498,7 +521,7 @@ private void OnUpdateRadioTowers(dynamic towers)
}
#endregion

#region Remote Events(Megaphone)
#region Remote Events (Megaphone)
[EventHandler(Event.SaltyChat_IsUsingMegaphone)]
private void OnIsUsingMegaphone(string handle, string teamSpeakName, float range, bool isSending, dynamic position)
{
Expand Down Expand Up @@ -982,6 +1005,7 @@ private async Task OnStateUpdateTick()
new SelfState(
playerPosition,
API.GetGameplayCamRot(0).Z,
this.VoiceRange,
this.IsAlive
)
)
Expand Down
18 changes: 16 additions & 2 deletions saltychat/SaltyServer/RadioChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class RadioChannel

internal RadioChannelMember[] Members => this._members.ToArray();
private List<RadioChannelMember> _members = new List<RadioChannelMember>();

private object _memberLock = new object();
#endregion

#region CTOR
Expand All @@ -34,14 +36,16 @@ internal bool IsMember(VoiceClient voiceClient)

internal void AddMember(VoiceClient voiceClient, bool isPrimary)
{
lock (this._members)
lock (this._memberLock)
{
if (!this._members.Any(m => m.VoiceClient == voiceClient))
{
this._members.Add(new RadioChannelMember(this, voiceClient, isPrimary));

voiceClient.TriggerEvent(Event.SaltyChat_SetRadioChannel, this.Name, isPrimary);

this.BroadcastEvent(Event.SaltyChat_RadioChannelMemberUpdated, this.Name, this.Members.Select(m => m.VoiceClient.TeamSpeakName));

foreach (RadioChannelMember member in this._members.Where(m => m.IsSending))
{
voiceClient.TriggerEvent(Event.SaltyChat_IsSending, member.VoiceClient.Player.Handle, member.VoiceClient.TeamSpeakName, this.Name, true, false, member.VoiceClient.Player.GetPosition());
Expand All @@ -52,7 +56,7 @@ internal void AddMember(VoiceClient voiceClient, bool isPrimary)

internal void RemoveMember(VoiceClient voiceClient)
{
lock (this._members)
lock (this._memberLock)
{
RadioChannelMember member = this._members.FirstOrDefault(m => m.VoiceClient == voiceClient);

Expand Down Expand Up @@ -86,6 +90,8 @@ internal void RemoveMember(VoiceClient voiceClient)
}

voiceClient.TriggerEvent(Event.SaltyChat_SetRadioChannel, null, member.IsPrimary);

this.BroadcastEvent(Event.SaltyChat_RadioChannelMemberUpdated, this.Name, this.Members.Select(m => m.VoiceClient.TeamSpeakName));
}
}
}
Expand Down Expand Up @@ -169,6 +175,14 @@ internal bool TryGetMember(VoiceClient voiceClient, out RadioChannelMember radio

return radioChannelMember != null;
}

private void BroadcastEvent(string eventName, params object[] eventParams)
{
foreach (RadioChannelMember member in this.Members)
{
member.VoiceClient.TriggerEvent(eventName, eventParams);
}
}
#endregion
}

Expand Down
1 change: 1 addition & 0 deletions saltychat/SaltyShared/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class Event
#endregion

#region Radio
public const string SaltyChat_RadioChannelMemberUpdated = "SaltyChat_RadioChannelMemberUpdated";
public const string SaltyChat_SetRadioSpeaker = "SaltyChat_SetRadioSpeaker";
public const string SaltyChat_ChannelInUse = "SaltyChat_ChannelInUse";
public const string SaltyChat_IsSending = "SaltyChat_IsSending";
Expand Down

0 comments on commit 556519a

Please sign in to comment.