1
0

SharedPointSystem.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using Content.Shared.FixedPoint;
  2. using Robust.Shared.Network;
  3. using Robust.Shared.Utility;
  4. namespace Content.Shared.Points;
  5. /// <summary>
  6. /// This handles modifying point counts for <see cref="PointManagerComponent"/>
  7. /// </summary>
  8. public abstract class SharedPointSystem : EntitySystem
  9. {
  10. /// <summary>
  11. /// Adds the specified point value to a player.
  12. /// </summary>
  13. public void AdjustPointValue(NetUserId userId, FixedPoint2 value, EntityUid uid, PointManagerComponent? component = null)
  14. {
  15. if (!Resolve(uid, ref component))
  16. return;
  17. if (!component.Points.TryGetValue(userId, out var current))
  18. current = 0;
  19. SetPointValue(userId, current + value, uid, component);
  20. }
  21. /// <summary>
  22. /// Sets the amount of points for a player
  23. /// </summary>
  24. public void SetPointValue(NetUserId userId, FixedPoint2 value, EntityUid uid, PointManagerComponent? component = null)
  25. {
  26. if (!Resolve(uid, ref component))
  27. return;
  28. if (component.Points.TryGetValue(userId, out var current) && current == value)
  29. return;
  30. component.Points[userId] = value;
  31. component.Scoreboard = GetScoreboard(uid, component);
  32. Dirty(uid, component);
  33. var ev = new PlayerPointChangedEvent(userId, value);
  34. RaiseLocalEvent(uid, ref ev, true);
  35. }
  36. /// <summary>
  37. /// Gets the amount of points for a given player
  38. /// </summary>
  39. public FixedPoint2 GetPointValue(NetUserId userId, EntityUid uid, PointManagerComponent? component = null)
  40. {
  41. if (!Resolve(uid, ref component))
  42. return FixedPoint2.Zero;
  43. return component.Points.TryGetValue(userId, out var value)
  44. ? value
  45. : FixedPoint2.Zero;
  46. }
  47. /// <summary>
  48. /// Ensures that a player is being tracked by the PointManager, giving them a default score of 0.
  49. /// </summary>
  50. public void EnsurePlayer(NetUserId userId, EntityUid uid, PointManagerComponent? component = null)
  51. {
  52. if (!Resolve(uid, ref component))
  53. return;
  54. if (component.Points.ContainsKey(userId))
  55. return;
  56. SetPointValue(userId, FixedPoint2.Zero, uid, component);
  57. }
  58. /// <summary>
  59. /// Returns a formatted message containing a ranking of all the currently online players and their scores.
  60. /// </summary>
  61. public virtual FormattedMessage GetScoreboard(EntityUid uid, PointManagerComponent? component = null)
  62. {
  63. return new FormattedMessage();
  64. }
  65. }
  66. /// <summary>
  67. /// Event raised on the point manager entity and broadcasted whenever a player's points change.
  68. /// </summary>
  69. /// <param name="Player"></param>
  70. /// <param name="Points"></param>
  71. [ByRefEvent]
  72. public readonly record struct PlayerPointChangedEvent(NetUserId Player, FixedPoint2 Points);