Ejemplo n.º 1
0
        /// <summary>
        /// Moves the selected checker
        /// </summary>
        /// <param name="user">user name</param>
        /// <param name="opponent">opponent name</param>
        /// <param name="currIndex">index of selected checker</param>
        /// <param name="number">number  the dice</param>
        public void MoveChecker(string user, string opponent, int currIndex, int number)
        {
            //lock (padlock)
            //{
            if (!string.IsNullOrEmpty(user) && !string.IsNullOrEmpty(opponent))
            {
                var game = GameMng.GetGame(user, opponent);
                if (game == null)
                {
                    return;
                }

                var  callback = OperationContext.Current.GetCallbackChannel <IClientCallback>();
                bool result   = false;

                if (_callback.ContainsKey(user))
                {
                    try
                    {
                        var doubleDice = game.DoubleDice;
                        var diffDice   = game.DifferentDice;

                        try
                        {
                            result = GameMng.CheckersMoovingLogic(user, opponent, currIndex, number);
                        }
                        catch (Exception ex)
                        {
                            _callback[user].RecieveGameError(ex.Message);
                            _callback[user].MoveEnableFromServer(user, opponent);
                            if (_callback.ContainsKey(opponent))
                            {
                                _callback[opponent].SetWaiterFromServer(opponent, user);
                            }
                            return;
                        }
                        if (result)
                        {
                            var gameTable = GameMng.GetGameTableForSend(user, opponent);

                            var whiteCheckers = gameTable[0];
                            var blackCheckers = gameTable[1];
                            var barCheckers   = gameTable[2];
                            _callback[user].UpdateTable(user, opponent, whiteCheckers, blackCheckers, barCheckers);
                            if (opponent != Player.autoPlayer.ToString())
                            {
                                _callback[opponent].UpdateTable(opponent, user, whiteCheckers, blackCheckers, barCheckers);
                            }

                            if (doubleDice != null && doubleDice.IsMoovingFinish())
                            {
                                // GameMng.DeleteDoubleDice(user, opponent);
                                if (opponent != Player.autoPlayer.ToString())
                                {
                                    _callback[opponent].SetMasterFromServer(opponent, user);
                                }
                                _callback[user].SetWaiterFromServer(user, opponent);
                                //turn computer moving
                                if (opponent == Player.autoPlayer.ToString())
                                {
                                    MoveCheckerAuto(opponent, user);
                                }
                            }
                            else if (diffDice != null && diffDice.IsMoovingFinish())
                            {
                                // GameMng.DeleteDifferentDice(user, opponent);
                                if (opponent != Player.autoPlayer.ToString())
                                {
                                    _callback[opponent].SetMasterFromServer(opponent, user);
                                }
                                _callback[user].SetWaiterFromServer(user, opponent);
                                //turn computer moving
                                if (opponent == Player.autoPlayer.ToString())
                                {
                                    MoveCheckerAuto(opponent, user);
                                }
                            }

                            if (GameMng.IsGameOver(user, opponent))
                            {
                                _callback[user].GameOverFromServer(user, opponent, true);
                                if (opponent != Player.autoPlayer.ToString())
                                {
                                    _callback[opponent].GameOverFromServer(opponent, user, false);
                                }
                                GameMng.DeleteGame(user, opponent);
                            }
                        }
                        else
                        {
                            if (_callback.ContainsKey(opponent))
                            {
                                _callback[user].SetWaiterFromServer(user, opponent);
                            }

                            if (_callback.ContainsKey(opponent))
                            {
                                _callback[opponent].SetMasterFromServer(opponent, user);
                            }
                            else
                            {
                                MoveCheckerAuto(opponent, user);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        callback.RecieveGameError(ex.Message);
                        if (_callback.ContainsKey(opponent))
                        {
                            _callback[opponent].RecieveGameError(ex.Message);
                        }
                    }
                }
            }
            //}
        }