using JetBrains.Annotations; using Content.Shared.Interaction; using Content.Shared.Tabletop; using Content.Shared.Tabletop.Components; using Content.Shared.Tabletop.Events; using Robust.Server.GameObjects; using Robust.Shared.Enums; using Robust.Shared.Player; namespace Content.Server.Tabletop; [UsedImplicitly] public sealed partial class TabletopSystem : SharedTabletopSystem { [Dependency] private EyeSystem _eye = default!; [Dependency] private SharedMapSystem _map = default!; [Dependency] private ViewSubscriberSystem _viewSubscriberSystem = default!; public override void Initialize() { base.Initialize(); SubscribeNetworkEvent(OnStopPlaying); SubscribeLocalEvent(OnTabletopActivate); SubscribeLocalEvent(OnGameShutdown); SubscribeLocalEvent(OnPlayerDetached); SubscribeLocalEvent(OnGamerShutdown); InitializeMap(); } protected override void OnTabletopMove(TabletopMoveEvent msg, EntitySessionEventArgs args) { if (args.SenderSession is not { } playerSession) return; if (!TryComp(GetEntity(msg.TableUid), out TabletopGameComponent? tabletop) || tabletop.Session is not { } session) return; // Check if player is actually playing at this table. if (!session.Players.ContainsKey(playerSession)) return; base.OnTabletopMove(msg, args); } private void OnTabletopActivate(Entity ent, ref ActivateInWorldEvent args) { if (args.Handled || !args.Complex) return; // Check that a player is attached to the entity. if (!TryComp(args.User, out ActorComponent? actor)) return; OpenSessionFor(actor.PlayerSession, ent.Owner); } private void OnGameShutdown(Entity ent, ref ComponentShutdown args) { CleanupSession(ent.Owner); } private void OnStopPlaying(TabletopStopPlayingEvent msg, EntitySessionEventArgs args) { CloseSessionFor(args.SenderSession, GetEntity(msg.TableUid)); } private void OnPlayerDetached(Entity ent, ref PlayerDetachedEvent args) { if (ent.Comp.Tabletop.IsValid()) CloseSessionFor(args.Player, ent.Comp.Tabletop); } private void OnGamerShutdown(Entity ent, ref ComponentShutdown args) { if (!TryComp(ent.Owner, out ActorComponent? actor)) return; if (ent.Comp.Tabletop.IsValid()) CloseSessionFor(actor.PlayerSession, ent.Comp.Tabletop); } public override void Update(float frameTime) { base.Update(frameTime); var query = EntityQueryEnumerator(); while (query.MoveNext(out var uid, out var gamer)) { if (!Exists(gamer.Tabletop)) continue; if (!TryComp(uid, out ActorComponent? actor)) { RemCompDeferred(uid); continue; } if (actor.PlayerSession.Status != SessionStatus.InGame || !CanSeeTable(uid, gamer.Tabletop)) CloseSessionFor(actor.PlayerSession, gamer.Tabletop); } } }