2022-10-16 13:44:50 -04:00
using Content.Server.Explosion.Components ;
2022-11-15 17:09:27 +13:00
using Content.Server.Speech ;
using Content.Server.Speech.Components ;
using Content.Shared.Database ;
2022-10-16 13:44:50 -04:00
using Content.Shared.Examine ;
using Content.Shared.Verbs ;
using Robust.Shared.Player ;
namespace Content.Server.Explosion.EntitySystems
{
public sealed partial class TriggerSystem
{
private void InitializeVoice ( )
{
2022-11-15 17:09:27 +13:00
SubscribeLocalEvent < TriggerOnVoiceComponent , ComponentInit > ( OnVoiceInit ) ;
2022-10-16 13:44:50 -04:00
SubscribeLocalEvent < TriggerOnVoiceComponent , ExaminedEvent > ( OnVoiceExamine ) ;
SubscribeLocalEvent < TriggerOnVoiceComponent , GetVerbsEvent < AlternativeVerb > > ( OnVoiceGetAltVerbs ) ;
2022-11-15 17:09:27 +13:00
SubscribeLocalEvent < TriggerOnVoiceComponent , ListenEvent > ( OnListen ) ;
}
private void OnVoiceInit ( EntityUid uid , TriggerOnVoiceComponent component , ComponentInit args )
{
if ( component . IsListening )
EnsureComp < ActiveListenerComponent > ( uid ) . Range = component . ListenRange ;
else
2022-11-27 03:01:17 -05:00
RemCompDeferred < ActiveListenerComponent > ( uid ) ;
2022-11-15 17:09:27 +13:00
}
private void OnListen ( EntityUid uid , TriggerOnVoiceComponent component , ListenEvent args )
{
var message = args . Message . Trim ( ) ;
if ( component . IsRecording )
{
if ( message . Length > = component . MinLength | | message . Length < = component . MaxLength )
FinishRecording ( component , args . Source , args . Message ) ;
return ;
}
if ( ! string . IsNullOrWhiteSpace ( component . KeyPhrase ) & & message . Contains ( component . KeyPhrase , StringComparison . InvariantCultureIgnoreCase ) )
{
_adminLogger . Add ( LogType . Trigger , LogImpact . High ,
$"A voice-trigger on {ToPrettyString(uid):entity} was triggered by {ToPrettyString(args.Source):speaker} speaking the key-phrase {component.KeyPhrase}." ) ;
2023-02-10 17:45:38 -06:00
Trigger ( uid , args . Source ) ;
2022-11-15 17:09:27 +13:00
}
2022-10-16 13:44:50 -04:00
}
private void OnVoiceGetAltVerbs ( EntityUid uid , TriggerOnVoiceComponent component , GetVerbsEvent < AlternativeVerb > args )
{
if ( ! args . CanInteract | | ! args . CanAccess )
return ;
args . Verbs . Add ( new AlternativeVerb ( )
{
2022-11-15 17:09:27 +13:00
Text = Loc . GetString ( component . IsRecording ? "verb-trigger-voice-record-stop" : "verb-trigger-voice-record" ) ,
Act = ( ) = >
{
if ( component . IsRecording )
StopRecording ( component ) ;
else
StartRecording ( component , args . User ) ;
} ,
2022-10-16 13:44:50 -04:00
Priority = 1
} ) ;
2022-11-15 17:09:27 +13:00
if ( string . IsNullOrWhiteSpace ( component . KeyPhrase ) )
return ;
args . Verbs . Add ( new AlternativeVerb ( )
{
Text = Loc . GetString ( "verb-trigger-voice-clear" ) ,
Act = ( ) = >
{
component . KeyPhrase = null ;
component . IsRecording = false ;
RemComp < ActiveListenerComponent > ( uid ) ;
}
} ) ;
2022-10-16 13:44:50 -04:00
}
2022-11-15 17:09:27 +13:00
public void StartRecording ( TriggerOnVoiceComponent component , EntityUid user )
2022-10-16 13:44:50 -04:00
{
2022-11-15 17:09:27 +13:00
component . IsRecording = true ;
EnsureComp < ActiveListenerComponent > ( component . Owner ) . Range = component . ListenRange ;
2022-10-16 13:44:50 -04:00
2022-11-15 17:09:27 +13:00
_adminLogger . Add ( LogType . Trigger , LogImpact . Low ,
$"A voice-trigger on {ToPrettyString(component.Owner):entity} has started recording. User: {ToPrettyString(user):user}" ) ;
2022-12-19 10:41:47 +13:00
_popupSystem . PopupEntity ( Loc . GetString ( "popup-trigger-voice-start-recording" ) , component . Owner ) ;
2022-11-15 17:09:27 +13:00
}
public void StopRecording ( TriggerOnVoiceComponent component )
{
component . IsRecording = false ;
if ( string . IsNullOrWhiteSpace ( component . KeyPhrase ) )
RemComp < ActiveListenerComponent > ( component . Owner ) ;
2022-12-19 10:41:47 +13:00
_popupSystem . PopupEntity ( Loc . GetString ( "popup-trigger-voice-stop-recording" ) , component . Owner ) ;
2022-11-15 17:09:27 +13:00
}
public void FinishRecording ( TriggerOnVoiceComponent component , EntityUid source , string message )
{
component . KeyPhrase = message ;
component . IsRecording = false ;
_adminLogger . Add ( LogType . Trigger , LogImpact . Low ,
$"A voice-trigger on {ToPrettyString(component.Owner):entity} has recorded a new keyphrase: '{component.KeyPhrase}'. Recorded from {ToPrettyString(source):speaker}" ) ;
2022-12-19 10:41:47 +13:00
_popupSystem . PopupEntity ( Loc . GetString ( "popup-trigger-voice-recorded" , ( "keyphrase" , component . KeyPhrase ! ) ) , component . Owner ) ;
2022-10-16 13:44:50 -04:00
}
private void OnVoiceExamine ( EntityUid uid , TriggerOnVoiceComponent component , ExaminedEvent args )
{
if ( args . IsInDetailsRange )
2022-11-15 17:09:27 +13:00
{
args . PushText ( string . IsNullOrWhiteSpace ( component . KeyPhrase )
2022-11-27 03:01:17 -05:00
? Loc . GetString ( "trigger-voice-uninitialized" )
: Loc . GetString ( "examine-trigger-voice" , ( "keyphrase" , component . KeyPhrase ) ) ) ;
2022-11-15 17:09:27 +13:00
}
2022-10-16 13:44:50 -04:00
}
}
}