PointSystem.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System.Linq;
  2. using Content.Shared.FixedPoint;
  3. using Content.Shared.Points;
  4. using JetBrains.Annotations;
  5. using Robust.Server.GameStates;
  6. using Robust.Server.Player;
  7. using Robust.Shared.Player;
  8. using Robust.Shared.Utility;
  9. namespace Content.Server.Points;
  10. /// <inheritdoc/>
  11. public sealed class PointSystem : SharedPointSystem
  12. {
  13. [Dependency] private readonly IPlayerManager _player = default!;
  14. [Dependency] private readonly PvsOverrideSystem _pvsOverride = default!;
  15. /// <inheritdoc/>
  16. public override void Initialize()
  17. {
  18. base.Initialize();
  19. SubscribeLocalEvent<PointManagerComponent, ComponentStartup>(OnStartup);
  20. }
  21. private void OnStartup(EntityUid uid, PointManagerComponent component, ComponentStartup args)
  22. {
  23. _pvsOverride.AddGlobalOverride(GetNetEntity(uid));
  24. }
  25. /// <summary>
  26. /// Adds the specified point value to a player.
  27. /// </summary>
  28. [PublicAPI]
  29. public void AdjustPointValue(EntityUid user, FixedPoint2 value, EntityUid uid, PointManagerComponent? component, ActorComponent? actor = null)
  30. {
  31. if (!Resolve(uid, ref component) || !Resolve(user, ref actor, false))
  32. return;
  33. AdjustPointValue(actor.PlayerSession.UserId, value, uid, component);
  34. }
  35. /// <summary>
  36. /// Sets the amount of points for a player
  37. /// </summary>
  38. [PublicAPI]
  39. public void SetPointValue(EntityUid user, FixedPoint2 value, EntityUid uid, PointManagerComponent? component, ActorComponent? actor = null)
  40. {
  41. if (!Resolve(uid, ref component) || !Resolve(user, ref actor, false))
  42. return;
  43. SetPointValue(actor.PlayerSession.UserId, value, uid, component);
  44. }
  45. /// <summary>
  46. /// Gets the amount of points for a given player
  47. /// </summary>
  48. [PublicAPI]
  49. public FixedPoint2 GetPointValue(EntityUid user, EntityUid uid, PointManagerComponent? component, ActorComponent? actor = null)
  50. {
  51. if (!Resolve(uid, ref component) || !Resolve(user, ref actor, false))
  52. return FixedPoint2.Zero;
  53. return GetPointValue(actor.PlayerSession.UserId, uid, component);
  54. }
  55. /// <inheritdoc/>
  56. public override FormattedMessage GetScoreboard(EntityUid uid, PointManagerComponent? component = null)
  57. {
  58. var msg = new FormattedMessage();
  59. if (!Resolve(uid, ref component))
  60. return msg;
  61. var orderedPlayers = component.Points.OrderByDescending(p => p.Value).ToList();
  62. var place = 1;
  63. foreach (var (id, points) in orderedPlayers)
  64. {
  65. if (!_player.TryGetPlayerData(id, out var data))
  66. continue;
  67. msg.AddMarkupOrThrow(Loc.GetString("point-scoreboard-list",
  68. ("place", place),
  69. ("name", data.UserName),
  70. ("points", points.Int())));
  71. msg.PushNewline();
  72. place++;
  73. }
  74. return msg;
  75. }
  76. }