2020-09-13 14:23:52 +02:00
using System.Collections.Generic ;
2020-10-30 16:06:48 +01:00
using Content.Server.Administration ;
2020-09-13 14:23:52 +02:00
using Content.Server.GameObjects.Components.MachineLinking ;
2020-11-10 00:30:19 -08:00
using Content.Server.GameObjects.EntitySystems.Click ;
2020-10-30 16:06:48 +01:00
using Content.Shared.Administration ;
2020-08-29 03:33:42 -07:00
using Robust.Server.Interfaces.Console ;
using Robust.Server.Interfaces.Player ;
using Robust.Shared.GameObjects ;
using Robust.Shared.GameObjects.Systems ;
using Robust.Shared.Input ;
using Robust.Shared.Input.Binding ;
using Robust.Shared.Interfaces.GameObjects ;
using Robust.Shared.IoC ;
using Robust.Shared.Map ;
using Robust.Shared.Network ;
using Robust.Shared.Players ;
namespace Content.Server.GameObjects.EntitySystems
{
public class SignalLinkerSystem : EntitySystem
{
2020-09-29 14:26:00 +02:00
private Dictionary < NetUserId , SignalTransmitterComponent > _transmitters ;
2020-08-29 03:33:42 -07:00
public override void Initialize ( )
{
base . Initialize ( ) ;
2020-09-29 14:26:00 +02:00
_transmitters = new Dictionary < NetUserId , SignalTransmitterComponent > ( ) ;
2020-08-29 03:33:42 -07:00
}
2020-11-05 03:57:49 -08:00
public bool SignalLinkerKeybind ( NetUserId id , bool? enable )
2020-08-29 03:33:42 -07:00
{
if ( enable = = null )
{
enable = ! _transmitters . ContainsKey ( id ) ;
}
if ( enable = = true )
{
if ( _transmitters . ContainsKey ( id ) )
{
2020-11-05 03:57:49 -08:00
return true ;
2020-08-29 03:33:42 -07:00
}
if ( _transmitters . Count = = 0 )
{
CommandBinds . Builder
2020-11-10 00:30:19 -08:00
. BindBefore ( EngineKeyFunctions . Use , new PointerInputCmdHandler ( HandleUse ) , typeof ( InteractionSystem ) )
2020-08-29 03:33:42 -07:00
. Register < SignalLinkerSystem > ( ) ;
}
_transmitters . Add ( id , null ) ;
}
else if ( enable = = false )
{
if ( ! _transmitters . ContainsKey ( id ) )
{
2020-11-05 03:57:49 -08:00
return false ;
2020-08-29 03:33:42 -07:00
}
_transmitters . Remove ( id ) ;
if ( _transmitters . Count = = 0 )
{
CommandBinds . Unregister < SignalLinkerSystem > ( ) ;
}
}
2020-11-05 03:57:49 -08:00
return enable = = true ;
2020-08-29 03:33:42 -07:00
}
2020-09-06 16:11:53 +02:00
private bool HandleUse ( ICommonSession session , EntityCoordinates coords , EntityUid uid )
2020-08-29 03:33:42 -07:00
{
2020-09-29 14:26:00 +02:00
if ( ! _transmitters . TryGetValue ( session . UserId , out var signalTransmitter ) )
2020-08-29 03:33:42 -07:00
{
return false ;
}
if ( ! EntityManager . TryGetEntity ( uid , out var entity ) )
{
return false ;
}
if ( entity . TryGetComponent < SignalReceiverComponent > ( out var signalReceiver ) )
{
if ( signalReceiver . Interact ( session . AttachedEntity , signalTransmitter ) )
{
return true ;
}
}
if ( entity . TryGetComponent < SignalTransmitterComponent > ( out var transmitter ) )
{
2020-09-29 14:26:00 +02:00
_transmitters [ session . UserId ] = transmitter . GetSignal ( session . AttachedEntity ) ;
2020-08-29 03:33:42 -07:00
return true ;
}
return false ;
}
}
2020-10-30 16:06:48 +01:00
[AdminCommand(AdminFlags.Debug)]
2020-08-29 03:33:42 -07:00
public class SignalLinkerCommand : IClientCommand
{
public string Command = > "signallink" ;
public string Description = > "Turns on signal linker mode. Click a transmitter to tune that signal and then click on each receiver to tune them to the transmitter signal." ;
public string Help = > "signallink (on/off)" ;
public void Execute ( IConsoleShell shell , IPlayerSession player , string [ ] args )
{
bool? enable = null ;
if ( args . Length > 0 )
{
if ( args [ 0 ] = = "on" )
enable = true ;
else if ( args [ 0 ] = = "off" )
enable = false ;
else if ( bool . TryParse ( args [ 0 ] , out var boolean ) )
enable = boolean ;
else if ( int . TryParse ( args [ 0 ] , out var num ) )
{
if ( num = = 1 )
enable = true ;
else if ( num = = 0 )
enable = false ;
}
}
if ( ! IoCManager . Resolve < IEntitySystemManager > ( ) . TryGetEntitySystem < SignalLinkerSystem > ( out var system ) )
{
return ;
}
2020-11-05 03:57:49 -08:00
var ret = system . SignalLinkerKeybind ( player . UserId , enable ) ;
shell . SendText ( player , ret ? "Enabled" : "Disabled" ) ;
2020-08-29 03:33:42 -07:00
}
}
}