| | 1 | | using UnityEngine; |
| | 2 | | using SocialFeaturesAnalytics; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System; |
| | 5 | | using Newtonsoft.Json; |
| | 6 | |
|
| | 7 | | namespace DCL |
| | 8 | | { |
| | 9 | | public class DCLVoiceChatController : MonoBehaviour |
| | 10 | | { |
| | 11 | | [Header("InputActions")] |
| | 12 | | public InputAction_Hold voiceChatAction; |
| | 13 | | public InputAction_Trigger voiceChatToggleAction; |
| | 14 | |
|
| | 15 | | private InputAction_Hold.Started voiceChatStartedDelegate; |
| | 16 | | private InputAction_Hold.Finished voiceChatFinishedDelegate; |
| | 17 | | private InputAction_Trigger.Triggered voiceChatToggleDelegate; |
| | 18 | |
|
| 610 | 19 | | private bool firstTimeVoiceRecorded = true; |
| | 20 | | private ISocialAnalytics socialAnalytics; |
| | 21 | | private UserProfileWebInterfaceBridge userProfileWebInterfaceBridge; |
| | 22 | | private double voiceMessageStartTime = 0; |
| | 23 | | private bool isVoiceChatToggledOn = false; |
| | 24 | |
|
| | 25 | | void Awake() |
| | 26 | | { |
| 596 | 27 | | userProfileWebInterfaceBridge = new UserProfileWebInterfaceBridge(); |
| | 28 | |
|
| 596 | 29 | | voiceChatStartedDelegate = (action) => DataStore.i.voiceChat.isRecording.Set(new KeyValuePair<bool, bool>(tr |
| 596 | 30 | | voiceChatFinishedDelegate = (action) => DataStore.i.voiceChat.isRecording.Set(new KeyValuePair<bool, bool>(f |
| 596 | 31 | | voiceChatToggleDelegate = (action) => ToggleVoiceChatRecording(); |
| 596 | 32 | | voiceChatAction.OnStarted += voiceChatStartedDelegate; |
| 596 | 33 | | voiceChatAction.OnFinished += voiceChatFinishedDelegate; |
| 596 | 34 | | voiceChatToggleAction.OnTriggered += voiceChatToggleDelegate; |
| | 35 | |
|
| 780 | 36 | | KernelConfig.i.EnsureConfigInitialized().Then(config => EnableVoiceChat(config.comms.voiceChatEnabled)); |
| 596 | 37 | | KernelConfig.i.OnChange += OnKernelConfigChanged; |
| 596 | 38 | | DataStore.i.voiceChat.isRecording.OnChange += IsVoiceChatRecordingChanged; |
| 596 | 39 | | } |
| | 40 | |
|
| | 41 | | void OnDestroy() |
| | 42 | | { |
| 596 | 43 | | voiceChatAction.OnStarted -= voiceChatStartedDelegate; |
| 596 | 44 | | voiceChatAction.OnFinished -= voiceChatFinishedDelegate; |
| 596 | 45 | | KernelConfig.i.OnChange -= OnKernelConfigChanged; |
| 596 | 46 | | DataStore.i.voiceChat.isRecording.OnChange -= IsVoiceChatRecordingChanged; |
| 596 | 47 | | } |
| | 48 | |
|
| 0 | 49 | | void OnKernelConfigChanged(KernelConfigModel current, KernelConfigModel previous) { EnableVoiceChat(current.comm |
| | 50 | |
|
| 368 | 51 | | void EnableVoiceChat(bool enable) { CommonScriptableObjects.voiceChatDisabled.Set(!enable); } |
| | 52 | |
|
| | 53 | | public void VoiceChatStatus(string voiceChatStatusPayload) |
| | 54 | | { |
| 0 | 55 | | VoiceChatStatusPayload voiceChatStatus = JsonConvert.DeserializeObject<VoiceChatStatusPayload>(voiceChatStat |
| 0 | 56 | | DataStore.i.voiceChat.isJoinedToVoiceChat.Set(voiceChatStatus.isConnected); |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | private void IsVoiceChatRecordingChanged(KeyValuePair<bool, bool> current, KeyValuePair<bool, bool> previous) |
| | 60 | | { |
| 0 | 61 | | if (!DataStore.i.voiceChat.isJoinedToVoiceChat.Get()) |
| 0 | 62 | | return; |
| | 63 | |
|
| 0 | 64 | | CreateSocialAnalyticsIfNeeded(); |
| | 65 | |
|
| 0 | 66 | | if (current.Key) |
| | 67 | | { |
| 0 | 68 | | if (!isVoiceChatToggledOn) |
| | 69 | | { |
| 0 | 70 | | Interface.WebInterface.SendSetVoiceChatRecording(true); |
| 0 | 71 | | SendFirstTimeMetricIfNeeded(); |
| 0 | 72 | | voiceMessageStartTime = Time.realtimeSinceStartup; |
| | 73 | | } |
| 0 | 74 | | } |
| | 75 | | else |
| | 76 | | { |
| 0 | 77 | | Interface.WebInterface.SendSetVoiceChatRecording(false); |
| | 78 | |
|
| 0 | 79 | | socialAnalytics.SendVoiceMessage( |
| | 80 | | Time.realtimeSinceStartup - voiceMessageStartTime, |
| | 81 | | (current.Value || isVoiceChatToggledOn) ? VoiceMessageSource.Shortcut : VoiceMessageSource.Button, |
| | 82 | | userProfileWebInterfaceBridge.GetOwn().userId); |
| | 83 | |
|
| 0 | 84 | | isVoiceChatToggledOn = false; |
| | 85 | | } |
| 0 | 86 | | } |
| | 87 | |
|
| | 88 | | private void ToggleVoiceChatRecording() |
| | 89 | | { |
| 0 | 90 | | if (!DataStore.i.voiceChat.isJoinedToVoiceChat.Get()) |
| 0 | 91 | | return; |
| | 92 | |
|
| 0 | 93 | | Interface.WebInterface.ToggleVoiceChatRecording(); |
| 0 | 94 | | isVoiceChatToggledOn = !isVoiceChatToggledOn; |
| | 95 | |
|
| 0 | 96 | | if (isVoiceChatToggledOn) |
| | 97 | | { |
| 0 | 98 | | SendFirstTimeMetricIfNeeded(); |
| 0 | 99 | | voiceMessageStartTime = Time.realtimeSinceStartup; |
| 0 | 100 | | } |
| | 101 | | else |
| | 102 | | { |
| 0 | 103 | | socialAnalytics.SendVoiceMessage( |
| | 104 | | Time.realtimeSinceStartup - voiceMessageStartTime, |
| | 105 | | VoiceMessageSource.Shortcut, |
| | 106 | | userProfileWebInterfaceBridge.GetOwn().userId); |
| | 107 | | } |
| 0 | 108 | | } |
| | 109 | |
|
| | 110 | | private void CreateSocialAnalyticsIfNeeded() |
| | 111 | | { |
| 0 | 112 | | if (socialAnalytics != null) |
| 0 | 113 | | return; |
| | 114 | |
|
| 0 | 115 | | socialAnalytics = new SocialAnalytics( |
| | 116 | | Environment.i.platform.serviceProviders.analytics, |
| | 117 | | userProfileWebInterfaceBridge); |
| 0 | 118 | | } |
| | 119 | |
|
| | 120 | | private void SendFirstTimeMetricIfNeeded() |
| | 121 | | { |
| 0 | 122 | | if (firstTimeVoiceRecorded) |
| | 123 | | { |
| 0 | 124 | | CreateSocialAnalyticsIfNeeded(); |
| 0 | 125 | | socialAnalytics.SendVoiceMessageStartedByFirstTime(); |
| 0 | 126 | | firstTimeVoiceRecorded = false; |
| | 127 | | } |
| 0 | 128 | | } |
| | 129 | | } |
| | 130 | |
|
| | 131 | | [Serializable] |
| | 132 | | public class VoiceChatStatusPayload |
| | 133 | | { |
| | 134 | | public bool isConnected; |
| | 135 | | } |
| | 136 | | } |