1
0

CargoGiftsRule.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System.Linq;
  2. using Content.Server.Cargo.Components;
  3. using Content.Server.Cargo.Systems;
  4. using Content.Server.GameTicking;
  5. using Content.Server.Station.Components;
  6. using Content.Server.StationEvents.Components;
  7. using Content.Shared.GameTicking.Components;
  8. using Robust.Shared.Prototypes;
  9. namespace Content.Server.StationEvents.Events;
  10. public sealed class CargoGiftsRule : StationEventSystem<CargoGiftsRuleComponent>
  11. {
  12. [Dependency] private readonly CargoSystem _cargoSystem = default!;
  13. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  14. [Dependency] private readonly GameTicker _ticker = default!;
  15. protected override void Added(EntityUid uid, CargoGiftsRuleComponent component, GameRuleComponent gameRule, GameRuleAddedEvent args)
  16. {
  17. if (!TryComp<StationEventComponent>(uid, out var stationEvent))
  18. return;
  19. var str = Loc.GetString(component.Announce,
  20. ("sender", Loc.GetString(component.Sender)), ("description", Loc.GetString(component.Description)), ("dest", Loc.GetString(component.Dest)));
  21. stationEvent.StartAnnouncement = str;
  22. base.Added(uid, component, gameRule, args);
  23. }
  24. /// <summary>
  25. /// Called on an active gamerule entity in the Update function
  26. /// </summary>
  27. protected override void ActiveTick(EntityUid uid, CargoGiftsRuleComponent component, GameRuleComponent gameRule, float frameTime)
  28. {
  29. if (component.Gifts.Count == 0)
  30. return;
  31. if (component.TimeUntilNextGifts > 0)
  32. {
  33. component.TimeUntilNextGifts -= frameTime;
  34. return;
  35. }
  36. component.TimeUntilNextGifts += 30f;
  37. if (!TryGetRandomStation(out var station, HasComp<StationCargoOrderDatabaseComponent>) ||
  38. !TryComp<StationDataComponent>(station, out var stationData))
  39. return;
  40. if (!TryComp<StationCargoOrderDatabaseComponent>(station, out var cargoDb))
  41. {
  42. return;
  43. }
  44. // Add some presents
  45. var outstanding = CargoSystem.GetOutstandingOrderCount(cargoDb);
  46. while (outstanding < cargoDb.Capacity - component.OrderSpaceToLeave && component.Gifts.Count > 0)
  47. {
  48. // I wish there was a nice way to pop this
  49. var (productId, qty) = component.Gifts.First();
  50. component.Gifts.Remove(productId);
  51. var product = _prototypeManager.Index(productId);
  52. if (!_cargoSystem.AddAndApproveOrder(
  53. station!.Value,
  54. product.Product,
  55. product.Name,
  56. product.Cost,
  57. qty,
  58. Loc.GetString(component.Sender),
  59. Loc.GetString(component.Description),
  60. Loc.GetString(component.Dest),
  61. cargoDb,
  62. (station.Value, stationData)
  63. ))
  64. {
  65. break;
  66. }
  67. }
  68. if (component.Gifts.Count == 0)
  69. {
  70. // We're done here!
  71. _ticker.EndGameRule(uid, gameRule);
  72. }
  73. }
  74. }