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