Files
OldThink/Content.Server/_White/Jukebox/TapeCreatorSystem.cs

179 lines
6.0 KiB
C#
Raw Normal View History

2023-05-04 13:43:03 +06:00
using System.Linq;
using System.Threading.Tasks;
using Content.Server.Popups;
2023-05-04 13:43:03 +06:00
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Interaction;
using Content.Shared.Tag;
using Content.Shared.Verbs;
using Content.Shared._White.Jukebox;
using Content.Shared.Popups;
using Robust.Server.Containers;
2023-05-04 13:43:03 +06:00
using Robust.Shared.Containers;
using Robust.Shared.GameStates;
using Robust.Shared.Utility;
namespace Content.Server._White.Jukebox;
2023-05-04 13:43:03 +06:00
public sealed class TapeCreatorSystem : EntitySystem
{
[Dependency] private readonly ServerJukeboxSongsSyncManager _songsSyncManager = default!;
[Dependency] private readonly PopupSystem _popup = default!;
[Dependency] private readonly ContainerSystem _container = default!;
[Dependency] private readonly SharedHandsSystem _hands = default!;
[Dependency] private readonly TagSystem _tag = default!;
2023-05-04 13:43:03 +06:00
private const string TapeCreatorContainerName = "tape_creator_container";
2023-05-04 13:43:03 +06:00
public override void Initialize()
{
base.Initialize();
SubscribeNetworkEvent<JukeboxSongUploadRequest>(OnSongUploaded);
SubscribeLocalEvent<TapeCreatorComponent, ComponentInit>(OnComponentInit);
SubscribeLocalEvent<TapeCreatorComponent, InteractUsingEvent>(OnInteract);
SubscribeLocalEvent<TapeCreatorComponent, GetVerbsEvent<Verb>>(OnTapeCreatorGetVerb);
SubscribeLocalEvent<TapeCreatorComponent, ComponentGetState>(OnTapeCreatorStateChanged);
SubscribeLocalEvent<TapeComponent, ComponentGetState>(OnTapeStateChanged);
}
private void OnTapeCreatorGetVerb(EntityUid uid, TapeCreatorComponent component, GetVerbsEvent<Verb> ev)
{
if (component.Recording) return;
if (ev.Hands == null) return;
if (component.TapeContainer.ContainedEntities.Count == 0) return;
var removeTapeVerb = new Verb
2023-05-04 13:43:03 +06:00
{
Text = "Вытащить касету",
Priority = 10000,
Icon = new SpriteSpecifier.Texture(new ResPath("/Textures/Interface/VerbIcons/remove_tape.png")),
Act = () =>
{
var tapes = component.TapeContainer.ContainedEntities.ToList();
_container.EmptyContainer(component.TapeContainer, true);
2023-05-04 13:43:03 +06:00
foreach (var tape in tapes)
{
_hands.PickupOrDrop(ev.User, tape);
2023-05-04 13:43:03 +06:00
}
component.InsertedTape = null;
Dirty(uid, component);
2023-05-04 13:43:03 +06:00
}
};
ev.Verbs.Add(removeTapeVerb);
}
private void OnTapeStateChanged(EntityUid uid, TapeComponent component, ref ComponentGetState args)
{
args.State = new TapeComponentState
2023-05-04 13:43:03 +06:00
{
Songs = component.Songs
};
}
private void OnTapeCreatorStateChanged(EntityUid uid, TapeCreatorComponent component, ref ComponentGetState args)
{
args.State = new TapeCreatorComponentState
{
Recording = component.Recording,
CoinBalance = component.CoinBalance,
InsertedTape = component.InsertedTape
};
}
private void OnComponentInit(EntityUid uid, TapeCreatorComponent component, ComponentInit args)
{
component.TapeContainer = _container.EnsureContainer<Container>(uid, TapeCreatorContainerName);
2023-05-04 13:43:03 +06:00
}
private void OnInteract(EntityUid uid, TapeCreatorComponent component, InteractUsingEvent args)
{
if (component.Recording)
{
return;
}
2023-05-04 13:43:03 +06:00
if (HasComp<TapeComponent>(args.Used))
2023-05-04 13:43:03 +06:00
{
var containedEntities = component.TapeContainer.ContainedEntities;
if (containedEntities.Count > 1)
{
var removedTapes = _container.EmptyContainer(component.TapeContainer, true).ToList();
_container.Insert(args.Used, component.TapeContainer);
2023-05-04 13:43:03 +06:00
foreach (var tapes in removedTapes)
{
_hands.PickupOrDrop(args.User, tapes);
2023-05-04 13:43:03 +06:00
}
}
else
{
_container.Insert(args.Used, component.TapeContainer);
2023-05-04 13:43:03 +06:00
}
component.InsertedTape = GetNetEntity(args.Used);
Dirty(uid, component);
2023-05-04 13:43:03 +06:00
return;
}
if (_tag.HasTag(args.Used, "TapeRecorderCoin"))
2023-05-04 13:43:03 +06:00
{
Del(args.Used);
component.CoinBalance += 1;
Dirty(uid, component);
2023-05-04 13:43:03 +06:00
}
}
private void OnSongUploaded(JukeboxSongUploadRequest ev)
{
var tapeCreator = GetEntity(ev.TapeCreatorUid);
if (!TryComp<TapeCreatorComponent>(tapeCreator, out var tapeCreatorComponent))
{
return;
}
2023-05-04 13:43:03 +06:00
if (!tapeCreatorComponent.InsertedTape.HasValue || tapeCreatorComponent.CoinBalance <= 0)
{
_popup.PopupEntity("Т# %ак@ э*^о сdf{ал б2я~b? Запись была прервана.", tapeCreator);
2023-05-04 13:43:03 +06:00
return;
}
tapeCreatorComponent.CoinBalance -= 1;
tapeCreatorComponent.Recording = true;
var insertedTape = GetEntity(tapeCreatorComponent.InsertedTape.Value);
var tapeComponent = Comp<TapeComponent>(insertedTape);
2023-05-04 13:43:03 +06:00
var songData = _songsSyncManager.SyncSongData(ev.SongName, ev.SongBytes);
var song = new JukeboxSong
2023-05-04 13:43:03 +06:00
{
SongName = songData.SongName,
SongPath = songData.Path
2023-05-04 13:43:03 +06:00
};
tapeComponent.Songs.Add(song);
2024-01-13 12:24:00 +03:00
DirtyEntity(GetEntity(ev.TapeCreatorUid));
Dirty(insertedTape, tapeComponent);
2023-05-04 13:43:03 +06:00
2024-08-01 07:08:29 +03:00
Record(tapeCreator, tapeCreatorComponent, _popup, _container);
}
private void Record(
EntityUid uid,
TapeCreatorComponent component,
SharedPopupSystem popupSystem,
SharedContainerSystem containerSystem)
{
containerSystem.EmptyContainer(component.TapeContainer, force: true);
component.Recording = false;
component.InsertedTape = null;
popupSystem.PopupEntity("Запись мозговой активности завершена", uid);
Dirty(uid, component);
2023-05-04 13:43:03 +06:00
}
2024-07-31 07:20:33 +03:00
}