<commit_before>﻿using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using SpaceInvaders.Command;
using SpaceInvaders.Core;
using SpaceInvaders.Exceptions;
using SpaceInvaders.Factories;

namespace SpaceInvaders.Entities
{
    public class Ship : Entity
    {
        [JsonConstructor]
        public Ship(int id, int playerNumber, int x, int y, int width, int height, bool alive, ShipCommand command,
            string commandFeedback)
            : base(id, playerNumber, x, y, width, height, alive, EntityType.Ship)
        {
            OnDestroyedEvent += OnDestroy;
            Command = command;
            CommandFeedback = commandFeedback;
        }

        public Ship(int playerNumber)
            : base(playerNumber, 3, 1, EntityType.Ship)
        {
            OnDestroyedEvent += OnDestroy;
            Command = ShipCommand.Nothing;
            CommandFeedback = "Did nothing.";
        }

        private Ship(Ship ship) : base(ship)
        {
            OnDestroyedEvent += OnDestroy;
            Command = ship.Command;
            CommandFeedback = ship.CommandFeedback;
        }

        [JsonConverter(typeof (StringEnumConverter))]
        public ShipCommand Command { get; set; }

        [JsonIgnore]
        public ShipCommand LastCommand { get; set; }

        public string CommandFeedback { get; set; }

        public static Ship CopyAndFlip(Ship ship, CoordinateFlipper flipper, Dictionary<int, Entity> flippedEntities)
        {
            if (flippedEntities.ContainsKey(ship.Id)) return (Ship) flippedEntities[ship.Id];

            var copy = new Ship(ship)
            {
                PlayerNumber = ship.PlayerNumber == 1 ? 2 : 1,
                X = flipper.CalculateFlippedX(ship.X + (ship.Width - 1)),
                Y = flipper.CalculateFlippedY(ship.Y)
            };

            flippedEntities.Add(copy.Id, copy);
            return copy;
        }

        public void Shoot()
        {
            var player = GetPlayer();

            if (player.Missiles.Count >= player.MissileLimit)
            {
                CommandFeedback = "Shot failed - too many missiles in flight.";
                return;
            }

            var missileX = X + 1; // center of the ship
            var missileY = (PlayerNumber == 1) ? Y - 1 : Y + 1;

            var missile = new Missile(PlayerNumber)
            {
                X = missileX,
                Y = missileY
            };

            player.Missiles.Add(missile);
            missile.OnDestroyedEvent += OnMissileDestroyed;

            try
            {
                GetMap().AddEntity(missile);
            }
            catch (CollisionException ex)
            {
                missile.Destroy();
                missile.ScoreKill(ex.Entity);
                ex.Entity.Destroy();
            }
        }

        public void OnMissileDestroyed(Object entity, EventArgs arguments)<commit_msg>No need to add a missile to a player's missiles before it gets added to a map. In actual fact, it might get destroyed before it gets added to the map.<commit_after>﻿using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using SpaceInvaders.Command;
using SpaceInvaders.Core;
using SpaceInvaders.Exceptions;
using SpaceInvaders.Factories;

namespace SpaceInvaders.Entities
{
    public class Ship : Entity
    {
        [JsonConstructor]
        public Ship(int id, int playerNumber, int x, int y, int width, int height, bool alive, ShipCommand command,
            string commandFeedback)
            : base(id, playerNumber, x, y, width, height, alive, EntityType.Ship)
        {
            OnDestroyedEvent += OnDestroy;
            Command = command;
            CommandFeedback = commandFeedback;
        }

        public Ship(int playerNumber)
            : base(playerNumber, 3, 1, EntityType.Ship)
        {
            OnDestroyedEvent += OnDestroy;
            Command = ShipCommand.Nothing;
            CommandFeedback = "Did nothing.";
        }

        private Ship(Ship ship) : base(ship)
        {
            OnDestroyedEvent += OnDestroy;
            Command = ship.Command;
            CommandFeedback = ship.CommandFeedback;
        }

        [JsonConverter(typeof (StringEnumConverter))]
        public ShipCommand Command { get; set; }

        [JsonIgnore]
        public ShipCommand LastCommand { get; set; }

        public string CommandFeedback { get; set; }

        public static Ship CopyAndFlip(Ship ship, CoordinateFlipper flipper, Dictionary<int, Entity> flippedEntities)
        {
            if (flippedEntities.ContainsKey(ship.Id)) return (Ship) flippedEntities[ship.Id];

            var copy = new Ship(ship)
            {
                PlayerNumber = ship.PlayerNumber == 1 ? 2 : 1,
                X = flipper.CalculateFlippedX(ship.X + (ship.Width - 1)),
                Y = flipper.CalculateFlippedY(ship.Y)
            };

            flippedEntities.Add(copy.Id, copy);
            return copy;
        }

        public void Shoot()
        {
            var player = GetPlayer();

            if (player.Missiles.Count >= player.MissileLimit)
            {
                CommandFeedback = "Shot failed - too many missiles in flight.";
                return;
            }

            var missileX = X + 1; // center of the ship
            var missileY = (PlayerNumber == 1) ? Y - 1 : Y + 1;

            var missile = new Missile(PlayerNumber)
            {
                X = missileX,
                Y = missileY
            };            

            try
            {
                GetMap().AddEntity(missile);
                player.Missiles.Add(missile);
                missile.OnDestroyedEvent += OnMissileDestroyed;
            }
            catch (CollisionException ex)
            {
                missile.ScoreKill(ex.Entity);
                ex.Entity.Destroy();
            }
        }

        public void OnMissileDestroyed(Object entity, EventArgs arguments)
{'fraction_non_alphanumeric': 0.0852188274153592, 'fraction_numerical': 0.0033030553261767133, 'mean_word_length': 1.7156950672645739, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '4600740', 'n_tokens_mistral': 1625, 'n_tokens_neox': 1556, 'n_words': 410}