<commit_before>﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Shared.Administration.Logs;
using Content.Shared.Database;
using Content.Shared.StatusEffect;
using Content.Shared.Stunnable;
using JetBrains.Annotations;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Dynamics;

namespace Content.Shared.Slippery
{
    [UsedImplicitly]
    public abstract class SharedSlipperySystem : EntitySystem
    {
        [Dependency] private readonly SharedAdminLogSystem _adminLog = default!;
        [Dependency] private readonly SharedStunSystem _stunSystem = default!;
        [Dependency] private readonly StatusEffectsSystem _statusEffectsSystem = default!;

        private readonly List<SlipperyComponent> _slipped = new();

        public override void Initialize()
        {
            base.Initialize();
            SubscribeLocalEvent<SlipperyComponent, StartCollideEvent>(HandleCollide);
            SubscribeLocalEvent<NoSlipComponent, SlipAttemptEvent>(OnNoSlipAttempt);
        }

        private void HandleCollide(EntityUid uid, SlipperyComponent component, StartCollideEvent args)
        {
            var otherUid = args.OtherFixture.Body.OwnerUid;

            if (!CanSlip(component, otherUid)) return;

            if (!_slipped.Contains(component))
                _slipped.Add(component);

            component.Colliding.Add(otherUid);
        }

        private void OnNoSlipAttempt(EntityUid uid, NoSlipComponent component, SlipAttemptEvent args)
        {
            args.Cancel();
        }

        /// <inheritdoc />
        public override void Update(float frameTime)
        {
            for (var i = _slipped.Count - 1; i >= 0; i--)
            {
                var slipperyComp = _slipped[i];
                if (!Update(slipperyComp)) continue;
                _slipped.RemoveAt(i);
            }
        }

        public bool CanSlip(SlipperyComponent component, EntityUid uid)
        {
            if (!component.Slippery
                || component.Owner.IsInContainer()
                || component.Slipped.Contains(uid)
                || !_statusEffectsSystem.CanApplyEffect(uid, "Stun")) //Should be KnockedDown instead?
            {
                return false;
            }

            return true;
        }

        private bool TrySlip(SlipperyComponent component, IPhysBody ourBody, IPhysBody otherBody)
        {
            if (!CanSlip(component, otherBody.OwnerUid)) return false;

            if (otherBody.LinearVelocity.Length < component.RequiredSlipSpeed)
            {
                return false;
            }

            var percentage = otherBody.GetWorldAABB().IntersectPercentage(ourBody.GetWorldAABB());

            if (percentage < component.IntersectPercentage)
            {
                return false;
            }

            var ev = new SlipAttemptEvent();
            RaiseLocalEvent(otherBody.OwnerUid, ev, false);
            if (ev.Cancelled)
                return false;

            otherBody.LinearVelocity *= component.LaunchForwardsMultiplier;

            bool playSound = !_statusEffectsSystem.HasStatusEffect(otherBody.OwnerUid, "KnockedDown");

            _stunSystem.TryParalyze(otherBody.OwnerUid, TimeSpan.FromSeconds(component.ParalyzeTime), true);
            component.Slipped.Add(otherBody.OwnerUid);
            component.Dirty();

            //Preventing from playing the slip sound when you are already knocked down.
            if(playSound)
            {
                PlaySound(component);
            }

            _adminLog.Add(LogType.Slip, LogImpact.Low, $"{component.Owner} slipped on collision with {otherBody.Owner}");

            return true;<commit_msg>Tiny admin log slip message fix. (#5719)<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Shared.Administration.Logs;
using Content.Shared.Database;
using Content.Shared.StatusEffect;
using Content.Shared.Stunnable;
using JetBrains.Annotations;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Dynamics;

namespace Content.Shared.Slippery
{
    [UsedImplicitly]
    public abstract class SharedSlipperySystem : EntitySystem
    {
        [Dependency] private readonly SharedAdminLogSystem _adminLog = default!;
        [Dependency] private readonly SharedStunSystem _stunSystem = default!;
        [Dependency] private readonly StatusEffectsSystem _statusEffectsSystem = default!;

        private readonly List<SlipperyComponent> _slipped = new();

        public override void Initialize()
        {
            base.Initialize();
            SubscribeLocalEvent<SlipperyComponent, StartCollideEvent>(HandleCollide);
            SubscribeLocalEvent<NoSlipComponent, SlipAttemptEvent>(OnNoSlipAttempt);
        }

        private void HandleCollide(EntityUid uid, SlipperyComponent component, StartCollideEvent args)
        {
            var otherUid = args.OtherFixture.Body.OwnerUid;

            if (!CanSlip(component, otherUid)) return;

            if (!_slipped.Contains(component))
                _slipped.Add(component);

            component.Colliding.Add(otherUid);
        }

        private void OnNoSlipAttempt(EntityUid uid, NoSlipComponent component, SlipAttemptEvent args)
        {
            args.Cancel();
        }

        /// <inheritdoc />
        public override void Update(float frameTime)
        {
            for (var i = _slipped.Count - 1; i >= 0; i--)
            {
                var slipperyComp = _slipped[i];
                if (!Update(slipperyComp)) continue;
                _slipped.RemoveAt(i);
            }
        }

        public bool CanSlip(SlipperyComponent component, EntityUid uid)
        {
            if (!component.Slippery
                || component.Owner.IsInContainer()
                || component.Slipped.Contains(uid)
                || !_statusEffectsSystem.CanApplyEffect(uid, "Stun")) //Should be KnockedDown instead?
            {
                return false;
            }

            return true;
        }

        private bool TrySlip(SlipperyComponent component, IPhysBody ourBody, IPhysBody otherBody)
        {
            if (!CanSlip(component, otherBody.OwnerUid)) return false;

            if (otherBody.LinearVelocity.Length < component.RequiredSlipSpeed)
            {
                return false;
            }

            var percentage = otherBody.GetWorldAABB().IntersectPercentage(ourBody.GetWorldAABB());

            if (percentage < component.IntersectPercentage)
            {
                return false;
            }

            var ev = new SlipAttemptEvent();
            RaiseLocalEvent(otherBody.OwnerUid, ev, false);
            if (ev.Cancelled)
                return false;

            otherBody.LinearVelocity *= component.LaunchForwardsMultiplier;

            bool playSound = !_statusEffectsSystem.HasStatusEffect(otherBody.OwnerUid, "KnockedDown");

            _stunSystem.TryParalyze(otherBody.OwnerUid, TimeSpan.FromSeconds(component.ParalyzeTime), true);
            component.Slipped.Add(otherBody.OwnerUid);
            component.Dirty();

            //Preventing from playing the slip sound when you are already knocked down.
            if(playSound)
            {
                PlaySound(component);
            }

            _adminLog.Add(LogType.Slip, LogImpact.Low, $"{otherBody.Owner} slipped on collision with {component.Owner}");

            return true;
{'fraction_non_alphanumeric': 0.08515594668047108, 'fraction_numerical': 0.0010353306587291317, 'mean_word_length': 2.258010118043845, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6030250', 'n_tokens_mistral': 2097, 'n_tokens_neox': 2033, 'n_words': 423}