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