Exemplo n.º 1
0
        /// <summary>
        /// Register positions
        /// </summary>
        /// <param name="pBattleFieldsPositions">Positions</param>
        public void RegisterPositions(List <DML.BattleField> pBattleFieldsPositions)
        {
            if (pBattleFieldsPositions == null)
            {
                throw new ArgumentNullException(paramName: nameof(pBattleFieldsPositions), "Battlefield positions cannot be null");
            }
            else if (pBattleFieldsPositions.Any())
            {
                BattleFieldList list = new BattleFieldList();

                list.BattleFields.AddRange(pBattleFieldsPositions);

                if (!IBoPlayer.PlayerExists(list.BattleFields.First().Player))
                {
                    throw new Exception("Player do not exists");
                }

                Match.DML.Match currentMatch = IBoMatch.CurrentMatch(list.BattleFields.First().Player);
                if (currentMatch == null)
                {
                    throw new Exception("The player does not have any match");
                }
                else if (currentMatch.ID != list.BattleFields.First().MatchID)
                {
                    throw new Exception("The current match of the player is another");
                }

                list.CheckData();

                try
                {
                    IDispatcherBattleField.BeginTransaction();
                    foreach (DML.BattleField battleField in list.BattleFields)
                    {
                        battleField.Attacked = 0;
                        IDispatcherBattleField.RegisterPosition(battleField);
                    }
                    IDispatcherBattleField.Commit();
                }
                catch (Exception ex)
                {
                    IDispatcherBattleField.Rollback();
                    throw new Exception($"Error on register positions. Original error: {ex.Message}");
                }
            }
            else
            {
                throw new Exception("Battlefield positions count is 0");
            }
        }
Exemplo n.º 2
0
        public int AttackPositions(List <DML.BattleField> pBattleFieldsPositions, int?pSpecialPowerId, out bool enemyDefeated)
        {
            if (pBattleFieldsPositions == null)
            {
                throw new ArgumentNullException(paramName: nameof(pBattleFieldsPositions), "Battlefield positions cannot be null");
            }
            else if (pBattleFieldsPositions.Any())
            {
                BattleFieldList list = new BattleFieldList();

                list.BattleFields.AddRange(pBattleFieldsPositions);

                List <MatchAttacks.DML.MatchAttacks> matchAttacks = new List <MatchAttacks.DML.MatchAttacks>();

                int targetHited = 0;

                if (!IBoPlayer.PlayerExists(list.BattleFields.First().Player))
                {
                    throw new Exception("Player do not exists");
                }

                Match.DML.Match currentMatch = IBoMatch.CurrentMatch(list.BattleFields.First().Player);
                if (currentMatch == null)
                {
                    throw new Exception("The player does not have any match");
                }
                else if (currentMatch.ID != list.BattleFields.First().MatchID)
                {
                    throw new Exception("The current match of the player is another");
                }

                list.CheckData();

                try
                {
                    IDispatcherBattleField.BeginTransaction();
                    foreach (DML.BattleField battleField in list.BattleFields)
                    {
                        if (targetHited == 1)
                        {
                            IDispatcherBattleField.AttackPosition(battleField);
                            matchAttacks.Add(new MatchAttacks.DML.MatchAttacks()
                            {
                                MatchId = battleField.MatchID,
                                PosX    = battleField.PosX,
                                PosY    = battleField.PosY,
                                /*O alvo não é quem está atacando*/
                                Target = currentMatch.Player1 == battleField.Player ? currentMatch.Player2 : currentMatch.Player1
                            });
                        }
                        else
                        {
                            targetHited = IDispatcherBattleField.AttackPosition(battleField);
                            matchAttacks.Add(new MatchAttacks.DML.MatchAttacks()
                            {
                                MatchId = battleField.MatchID,
                                PosX    = battleField.PosX,
                                PosY    = battleField.PosY,
                                /*O alvo não é quem está atacando*/
                                Target = currentMatch.Player1 == battleField.Player ? currentMatch.Player2 : currentMatch.Player1
                            });
                        }
                    }

                    IBoMatchAttacks.RegisterMatchAttacks(matchAttacks);
                    if (pSpecialPowerId != null)
                    {
                        IBoMatchSpecialPower.RegisterUseOfSpecialPower(list.BattleFields.First().MatchID, list.BattleFields.First().Player, Convert.ToInt32(pSpecialPowerId));
                    }

                    enemyDefeated = PlayerDefeated(currentMatch.ID, currentMatch.CurrentPlayer == currentMatch.Player1 ? currentMatch.Player2 : currentMatch.Player1);
                    if (enemyDefeated)
                    {
                        IBoMatch.CloseMatch(currentMatch.ID);
                    }
                    else if (targetHited == 0)
                    {
                        IBoMatch.ChangeCurrentPlayer(currentMatch.ID, currentMatch.Player1 == list.BattleFields.First().Player ? currentMatch.Player2 : currentMatch.Player1);
                    }

                    IDispatcherBattleField.Commit();

                    return(targetHited);
                }
                catch (Exception ex)
                {
                    IDispatcherBattleField.Rollback();
                    throw new Exception($"Error on register positions. Original error: {ex.Message}");
                }
            }
            else
            {
                throw new Exception("Battlefield positions count is 0");
            }
        }