Beispiel #1
0
        public async Task Connect(string myConnectionId, string partnerCode, string myCode)
        {
            var player        = playerObject.FindPlayerByCode(myCode);
            var partnerPlayer = playerObject.FindPlayerByCode(partnerCode);

            if (!BondAlreadyExists(partnerCode, myCode))
            {
                connections.PlayersBinded.Add(new PlayerBinded(myCode, partnerCode));
            }

            var partnerConnectionId = GetConnectionId(partnerCode);

            var currentMatchPlayer  = matchObject.CurrentMatch(player.Login);
            var currentMatchPartner = matchObject.CurrentMatch(partnerPlayer.Login);

            var matchId = currentMatchPartner?.ID;

            if (currentMatchPlayer is null || currentMatchPartner is null || !PlayersAlreadyInSameMatch(currentMatchPlayer, currentMatchPartner))
            {
                matchId = matchObject.CreateMatch(new Match()
                {
                    Player1       = player.ID,
                    Player2       = partnerPlayer.ID,
                    CurrentPlayer = partnerPlayer.ID
                });
            }

            ChangePlayerReady(partnerCode, isReady: false);
            ChangePlayerReady(myCode, isReady: false);

            await Clients.Caller.SendAsync("Connected", matchId, player, partnerPlayer);

            await Clients.Client(partnerConnectionId).SendAsync("Connected", matchId, partnerPlayer, player);
        }
Beispiel #2
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");
            }
        }
        public OutAttackPlayerVM AttackPositions(InAttackPositionsVm pModel)
        {
            OutAttackPlayerVM outAttackPlayerVM = new OutAttackPlayerVM();

            if (pModel.attackPositions.Any())
            {
                BattleshipApi.Player.DML.Player player = IBoPlayer.FindPlayerByUserName(User.Claims.GetJWTUserName());

                if (player == null || player.ID <= 0)
                {
                    outAttackPlayerVM.Message    = "Player not found";
                    outAttackPlayerVM.HttpStatus = StatusCodes.Status400BadRequest;
                }
                else
                {
                    Match currentMatch = IBoMatch.CurrentMatch(player.ID);

                    if (currentMatch == null || currentMatch.ID <= 0)
                    {
                        outAttackPlayerVM.Message    = "Match not found";
                        outAttackPlayerVM.HttpStatus = StatusCodes.Status400BadRequest;
                    }
                    else
                    {
                        outAttackPlayerVM.HitTarget = IBoBattleField.AttackPositions(pModel.attackPositions.Select(c => new BattleshipApi.BattleField.DML.BattleField()
                        {
                            Attacked       = 0,
                            MatchID        = currentMatch.ID,
                            Player         = player.ID,
                            PositionObject = new BattleshipApi.BattleField.DML.BattleFieldPosition()
                            {
                                X = c.PosX,
                                Y = c.PosY
                            }
                        }).ToList(), pModel.specialPower, out bool enemyDefeated) == 1;
                        outAttackPlayerVM.EnemyDefeated     = enemyDefeated;
                        outAttackPlayerVM.PositionsAttacked = IBoMatchAttacks.PositionsAttacked(currentMatch.ID, currentMatch.CurrentPlayer == currentMatch.Player1 ? currentMatch.Player2 : currentMatch.Player1);
                    }
                }
            }
            else
            {
                outAttackPlayerVM.Message    = "No attack positions";
                outAttackPlayerVM.HttpStatus = StatusCodes.Status400BadRequest;
            }

            return(outAttackPlayerVM);
        }
        public OutCurrentMatchVM CurrentMatch()
        {
            OutCurrentMatchVM outCurrentMatchVM = new OutCurrentMatchVM();

            try
            {
                string userName = User.Claims.GetJWTUserName();

                outCurrentMatchVM.Match      = IBoMatch.CurrentMatch(userName);
                outCurrentMatchVM.HttpStatus = StatusCodes.Status200OK;
                outCurrentMatchVM.Message    = "OK";
            }
            catch (Exception ex)
            {
                outCurrentMatchVM.HttpStatus = StatusCodes.Status400BadRequest;
                outCurrentMatchVM.Message    = $"Error while getting current match! {ex.Message}";
                outCurrentMatchVM.Match      = null;
            }

            return(outCurrentMatchVM);
        }