Files
OldThink/Content.Server/Store/Systems/StoreSystem.cs
Jabak b385386ab4 Upstream gp (#149)
* fix borg (#719)

* Automatic changelog update

* Переводы снаряжения и прочей мелочи в стартовом меню (#720)

* Сумки, мешки и прочее

* Перевод снаряжения

* перевод черт персонажа

* Добавлено ничего

* Automatic changelog update

* Фикс отображения потери мастера для импланта подчинения (#721)

* фикс отображения

* brain damage is real

* я блять запустил райдер ради рефактора одного ифа

* а лучше даже так

* Automatic changelog update

* add coderabbitai config (#722)

* fix (#723)

* Шприц теперь является оружием массового поражения (#724)

* Automatic changelog update

* Пиздец (#725)

Я на это потратил 2 недели

* Automatic changelog update

* Honk FM (#136) (#726)

* Fix Cosmic Temperance и новые песенки в jukebox

* Новая музыка в jucebox x2

Co-authored-by: Vorge7 <vorge228@gmail.com>

* Automatic changelog update

* Флаф (fluff) мне (big_zi_348) (#727)

* Заработал

* brain damage

* fuck (#729)

* Automatic changelog update

* FUCKERS (#732)

* Удаление ненужных суффиксов (#731)

* Перевод захардкукоженной строки (#728)

* Пластырь поможет

* очапятка

* Перевод ревенанта

* Карповый перекат

* Create shakeable-component.ftl

* Криогеника

* Хранилища скафандров

* Update autotranslate-14.ftl

* Update Cyborgs.xml

* Комоды

* Кредиты

* Удалил дубликат

* Информация

* Пластырь миму и клоуну

* Переводы всего

* Перевод аплинка

* Удалил ненужные суффиксы

* Revert "Удалил ненужные суффиксы"

This reverts commit d82f05f30c37ec2c11e5736b91239fe9dd1a4d17.

* Удаление ненужных суффиксов

* Перевод реагентовых слизней

* Перевод аномалий

* Перевод маяков

* Перевод различной мелочи

* Automatic changelog update

* Переводы и правки Гайдов (#730)

* Automatic changelog update

* aaaaa (#733)

* Правка локализации (#737)

* Update ThirstSystem.cs (#736)

* AccessConfiguratorForBorgs (#735)

* Automatic changelog update

* Починил бесконечную сварку (#734)

* Automatic changelog update

* ShowManifestFeature (#738)

* Automatic changelog update

* I LOVE OPENSOURCE

* Изменение размеров милишек (#739)

* Фикс размеров

* Заготовку биты тоже

* Правка

* Automatic changelog update

* Время после взрыва нюки (#740)

* More Fun

* Автоформатирование

* Подкрутка

* Automatic changelog update

* Скальпель в армейские ботинки (#741)

* Automatic changelog update

* DoHeavyAttack stamina check (#742)

* Automatic changelog update

* aaaaaaaaaaaaaaaaaaaaaaaaaaaaa (#743)

* fix retarded code (#744)

* Automatic changelog update

---------

Co-authored-by: RavmorganButOnCocaine <valtos@nextmail.ru>
Co-authored-by: BIGZi0348 <118811750+BIGZi0348@users.noreply.github.com>
Co-authored-by: ThereDrD <88589686+ThereDrD0@users.noreply.github.com>
Co-authored-by: Vorge7 <vorge228@gmail.com>
Co-authored-by: Valtos <valtos@spaces.ru>
Co-authored-by: haiwwkes <49613070+rhailrake@users.noreply.github.com>
2024-10-24 21:36:52 +03:00

233 lines
8.3 KiB
C#

using Content.Server.Store.Components;
using Content.Shared.UserInterface;
using Content.Shared.FixedPoint;
using Content.Shared.Implants.Components;
using Content.Shared.Interaction;
using Content.Shared.Popups;
using Content.Shared.Stacks;
using Content.Shared.Store;
using JetBrains.Annotations;
using Robust.Shared.Prototypes;
using System.Linq;
using Robust.Shared.Utility;
namespace Content.Server.Store.Systems;
/// <summary>
/// Manages general interactions with a store and different entities,
/// getting listings for stores, and interfacing with the store UI.
/// </summary>
public sealed partial class StoreSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<StoreComponent, ActivatableUIOpenAttemptEvent>(OnStoreOpenAttempt);
SubscribeLocalEvent<CurrencyComponent, AfterInteractEvent>(OnAfterInteract);
SubscribeLocalEvent<StoreComponent, BeforeActivatableUIOpenEvent>(BeforeActivatableUiOpen);
SubscribeLocalEvent<StoreComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<StoreComponent, ComponentStartup>(OnStartup);
SubscribeLocalEvent<StoreComponent, ComponentShutdown>(OnShutdown);
SubscribeLocalEvent<StoreComponent, OpenUplinkImplantEvent>(OnImplantActivate);
InitializeUi();
InitializeCommand();
InitializeRefund();
}
private void OnMapInit(EntityUid uid, StoreComponent component, MapInitEvent args)
{
RefreshAllListings(component);
InitializeFromPreset(component.Preset, uid, component);
component.StartingMap = Transform(uid).MapUid;
}
private void OnStartup(EntityUid uid, StoreComponent component, ComponentStartup args)
{
// for traitors, because the StoreComponent for the PDA can be added at any time.
if (MetaData(uid).EntityLifeStage == EntityLifeStage.MapInitialized)
{
RefreshAllListings(component);
InitializeFromPreset(component.Preset, uid, component);
}
var ev = new StoreAddedEvent();
RaiseLocalEvent(uid, ref ev, true);
}
private void OnShutdown(EntityUid uid, StoreComponent component, ComponentShutdown args)
{
var ev = new StoreRemovedEvent();
RaiseLocalEvent(uid, ref ev, true);
}
private void OnStoreOpenAttempt(EntityUid uid, StoreComponent component, ActivatableUIOpenAttemptEvent args)
{
if (!component.OwnerOnly)
return;
component.AccountOwner ??= args.User;
DebugTools.Assert(component.AccountOwner != null);
if (component.AccountOwner == args.User)
return;
_popup.PopupEntity(Loc.GetString("store-not-account-owner", ("store", uid)), uid, args.User);
args.Cancel();
}
private void OnAfterInteract(EntityUid uid, CurrencyComponent component, AfterInteractEvent args)
{
if (args.Handled || !args.CanReach)
return;
if (!TryComp<StoreComponent>(args.Target, out var store))
return;
var ev = new CurrencyInsertAttemptEvent(args.User, args.Target.Value, args.Used, store);
RaiseLocalEvent(args.Target.Value, ev);
if (ev.Cancelled)
return;
if (!TryAddCurrency((uid, component), (args.Target.Value, store)))
return;
args.Handled = true;
var msg = Loc.GetString("store-currency-inserted", ("used", args.Used), ("target", args.Target));
_popup.PopupEntity(msg, args.Target.Value, args.User);
}
private void OnImplantActivate(EntityUid uid, StoreComponent component, OpenUplinkImplantEvent args)
{
ToggleUi(args.Performer, uid, component);
}
/// <summary>
/// Gets the value from an entity's currency component.
/// Scales with stacks.
/// </summary>
/// <param name="uid"></param>
/// <param name="component"></param>
/// <returns>The value of the currency</returns>
public Dictionary<string, FixedPoint2> GetCurrencyValue(EntityUid uid, CurrencyComponent component)
{
var amount = EntityManager.GetComponentOrNull<StackComponent>(uid)?.Count ?? 1;
return component.Price.ToDictionary(v => v.Key, p => p.Value * amount);
}
/// <summary>
/// Tries to add a currency to a store's balance. Note that if successful, this will consume the currency in the process.
/// </summary>
public bool TryAddCurrency(Entity<CurrencyComponent?> currency, Entity<StoreComponent?> store)
{
if (!Resolve(currency.Owner, ref currency.Comp))
return false;
if (!Resolve(store.Owner, ref store.Comp))
return false;
var value = currency.Comp.Price;
if (TryComp(currency.Owner, out StackComponent? stack) && stack.Count != 1)
{
value = currency.Comp.Price
.ToDictionary(v => v.Key, p => p.Value * stack.Count);
}
if (!TryAddCurrency(value, store, store.Comp))
return false;
// Avoid having the currency accidentally be re-used. E.g., if multiple clients try to use the currency in the
// same tick
currency.Comp.Price.Clear();
if (stack != null)
_stack.SetCount(currency.Owner, 0, stack);
QueueDel(currency);
return true;
}
/// <summary>
/// Tries to add a currency to a store's balance
/// </summary>
/// <param name="currency">The value to add to the store</param>
/// <param name="uid"></param>
/// <param name="store">The store to add it to</param>
/// <returns>Whether or not the currency was succesfully added</returns>
public bool TryAddCurrency(Dictionary<string, FixedPoint2> currency, EntityUid uid, StoreComponent? store = null)
{
if (!Resolve(uid, ref store))
return false;
//verify these before values are modified
foreach (var type in currency)
{
if (!store.CurrencyWhitelist.Contains(type.Key))
return false;
}
foreach (var type in currency)
{
if (!store.Balance.TryAdd(type.Key, type.Value))
store.Balance[type.Key] += type.Value;
}
UpdateUserInterface(null, uid, store);
return true;
}
/// <summary>
/// Initializes a store based on a preset ID
/// </summary>
/// <param name="preset">The ID of a store preset prototype</param>
/// <param name="uid"></param>
/// <param name="component">The store being initialized</param>
public void InitializeFromPreset(string? preset, EntityUid uid, StoreComponent component)
{
if (preset == null)
return;
if (!_proto.TryIndex<StorePresetPrototype>(preset, out var proto))
return;
InitializeFromPreset(proto, uid, component);
}
/// <summary>
/// Initializes a store based on a given preset
/// </summary>
/// <param name="preset">The StorePresetPrototype</param>
/// <param name="uid"></param>
/// <param name="component">The store being initialized</param>
public void InitializeFromPreset(StorePresetPrototype preset, EntityUid uid, StoreComponent component)
{
component.Preset = preset.ID;
component.CurrencyWhitelist.UnionWith(preset.CurrencyWhitelist);
component.Categories.UnionWith(preset.Categories);
if (component.Balance == new Dictionary<string, FixedPoint2>() && preset.InitialBalance != null) //if we don't have a value stored, use the preset
TryAddCurrency(preset.InitialBalance, uid, component);
ApplySales(component.Listings, preset); // WD
if (_ui.HasUi(uid, StoreUiKey.Key))
{
_ui.SetUiState(uid, StoreUiKey.Key, new StoreInitializeState(preset.StoreName));
}
}
}
public sealed class CurrencyInsertAttemptEvent : CancellableEntityEventArgs
{
public readonly EntityUid User;
public readonly EntityUid Target;
public readonly EntityUid Used;
public readonly StoreComponent Store;
public CurrencyInsertAttemptEvent(EntityUid user, EntityUid target, EntityUid used, StoreComponent store)
{
User = user;
Target = target;
Used = used;
Store = store;
}
}