Ejemplo n.º 1
0
        /// <summary>
        /// Возвращает сообщение с результатом выстрела.
        /// </summary>
        /// <param name="ship">Корабль по которому попали.</param>
        /// <param name="location">Позизия выстрела.</param>
        public static string GetShotResultMessage(Ship ship, Location location)
        {
            SeaBattleClassLibrary.DataProvider.ShotResult.ShotResultType type = SeaBattleClassLibrary.DataProvider.ShotResult.ShotResultType.Miss;

            if (ship != null)
            {
                if (ship.IsDead)
                {
                    type = SeaBattleClassLibrary.DataProvider.ShotResult.ShotResultType.Kill;
                }
                else
                {
                    type = SeaBattleClassLibrary.DataProvider.ShotResult.ShotResultType.Damage;
                }
            }

            ship = type == SeaBattleClassLibrary.DataProvider.ShotResult.ShotResultType.Kill ? ship : null;

            JObject jObject = new JObject();

            jObject.Add(JsonStructInfo.Type, Answer.EnumTypeToString(Answer.AnswerTypes.ShotResult));
            jObject.Add(JsonStructInfo.Result, SeaBattleClassLibrary.DataProvider.ShotResult.EnumTypeToString(type));
            jObject.Add(JsonStructInfo.AdditionalContent, Serializer <Ship> .Serialize(ship));
            jObject.Add(JsonStructInfo.Content, Serializer <Location> .Serialize(location));

            return(jObject.ToString() + JsonStructInfo.EndOfMessage);
        }
        /// <summary>
        /// Удар по врагу.
        /// </summary>
        private static void ReceiveCallbackMyShot(IAsyncResult ar)
        {
            try
            {
                StateObject state  = (StateObject)ar.AsyncState;
                Socket      client = state.workSocket;

                int bytesRead = client.EndReceive(ar);

                state.sb.Append(Encoding.UTF8.GetString(state.buffer, 0, bytesRead));

                if (state.sb.Length > 1)
                {
                    response = state.sb.ToString();
                }

                response = response.Remove(response.LastIndexOf(JsonStructInfo.EndOfMessage));

                JObject jObject = null;

                Answer.AnswerTypes type = Answer.AnswerTypes.ShotOfTheEnemy;
                SeaBattleClassLibrary.DataProvider.ShotResult.ShotResultType result = SeaBattleClassLibrary.DataProvider.ShotResult.ShotResultType.Miss;
                Ship ship = null;

                try
                {
                    jObject = JObject.Parse(response);
                }
                catch (JsonReaderException e)
                {
                    Console.WriteLine(e);
                }

                type   = Answer.JsonTypeToEnum((string)jObject[JsonStructInfo.Type]);
                result = SeaBattleClassLibrary.DataProvider.ShotResult.JsonTypeToEnum((string)jObject[JsonStructInfo.Result]);
                ship   = result == SeaBattleClassLibrary.DataProvider.ShotResult.ShotResultType.Kill ?
                         Serializer <Ship> .Deserialize((string)jObject[JsonStructInfo.AdditionalContent]) : null;

                Location location = Serializer <Location> .Deserialize((string)jObject[JsonStructInfo.Content]);

                Game.ShotResult shotResult = Game.ShotResult.Miss;

                if (result != SeaBattleClassLibrary.DataProvider.ShotResult.ShotResultType.Miss)
                {
                    shotResult = result == SeaBattleClassLibrary.DataProvider.ShotResult.ShotResultType.Damage ?
                                 Game.ShotResult.Damage : Game.ShotResult.Kill;
                }

                EnemyGameField.Shot(location, shotResult, ship);
                pingDone.Reset();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }