示例#1
0
        /// <summary>
        /// Makes move (and checks for move legality) in session "Valil" game
        /// Flags the game as started in session and sets start DateTime database if it's first move
        /// Makes move in database
        /// Updates palyer ratings if the game is ended
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="promotionPiece"></param>
        /// <returns>Game result: NoResult - the game continues, WhiteWin, BlackWin, Tie</returns>
        private GameResult MakeMove(string from, string to, string promotionPiece = "")
        {
            // check for move legality and update game state in session
            try
            {
                Move myMove = Utils.GetCANMove(ValilGameS, from + to + promotionPiece);
                ValilGameS.Make(myMove);
            }
            catch
            {
                throw new HubException("Invalid move");
            }

            // Start "timer"
            if (!MatchS.IsGameInitialized)
            {
                _startGameCommandHandler.Handle(new StartGameCommand(MatchS.MatchId, MatchS.White.UserName, MatchS.Black.UserName));
                MatchS.GameStartTime     = DateTime.Now;
                MatchS.IsGameInitialized = true;
            }

            // check result
            GameResult result = CalculateAndUpdateResult();

            // add move in database
            _makeMoveCommandHandler.Handle(new MakeMoveCommand(MatchS.MatchId, from, to, promotionPiece, result));

            return(result);
        }
示例#2
0
        /// <summary>
        /// Check if one of the players has a timeout
        /// </summary>
        public bool CheckForTimeout()
        {
            if (IsHavingMatchS)
            {
                lock (MatchS)
                {
                    if (IsHavingMatchS)
                    {
                        var whiteTime = ValilGameS.GetWhiteTimeLeft(MatchS.GameStartTime, MatchS.InitialTime, MatchS.AddedTimePerMove);
                        var blackTime = ValilGameS.GetBlackTimeLeft(MatchS.GameStartTime, MatchS.InitialTime, MatchS.AddedTimePerMove);
                        Clients.Group(MatchS.MatchId).updateTime(whiteTime, blackTime);

                        if (whiteTime.TotalSeconds < 0 || blackTime.TotalMilliseconds < 0)
                        {
                            GameResult result = whiteTime.TotalSeconds <= 0 ? GameResult.BlackWin : GameResult.WhiteWin;
                            Clients.Group(MatchS.MatchId).gameEnded(result.ToString());
                            InitPlayerInfo(MatchS.White.UserName, MatchS.White.ConnectionId, MatchS.White.SessionId);
                            InitPlayerInfo(MatchS.Black.UserName, MatchS.Black.ConnectionId, MatchS.Black.SessionId);
                            CalculateAndUpdateResult(result);
                            EndGameSession();
                            return(true);
                        }
                        return(false);
                    }
                }
            }
            return(false);
        }
示例#3
0
        /// <summary>
        /// Do a promotion move in a game
        /// </summary>
        /// <param name="from">board position, e.g. "c3"</param>
        /// <param name="to">board position, e.g. "c3"</param>
        /// <param name="promotionPiece">board piece, e.g. 'q'</param>
        /// <returns>Return if the move is valid</returns>
        public bool MovePromotion(string from, string to, char promotionPiece)
        {
            if (IsHavingMatchS)
            {
                try
                {
                    var gameResult = MakeMove(from, to, promotionPiece.ToString());
                    Clients.OthersInGroup(MatchS.MatchId).movedPromoted(from, to, promotionPiece);

                    var whiteTime = ValilGameS.GetWhiteTimeLeft(MatchS.GameStartTime, MatchS.InitialTime, MatchS.AddedTimePerMove);
                    var blackTime = ValilGameS.GetBlackTimeLeft(MatchS.GameStartTime, MatchS.InitialTime, MatchS.AddedTimePerMove);
                    Clients.Group(MatchS.MatchId).updateTime(whiteTime, blackTime);

                    if (gameResult != GameResult.NoResult)
                    {
                        Clients.Group(MatchS.MatchId).gameEnded(gameResult.ToString());
                        InitPlayerInfo(MatchS.White.UserName, MatchS.White.ConnectionId, MatchS.White.SessionId);
                        InitPlayerInfo(MatchS.Black.UserName, MatchS.Black.ConnectionId, MatchS.Black.SessionId);
                        EndGameSession();
                    }
                }
                catch
                {
                    ValilGameS.Previous();
                    return(false);
                }
                return(true);
            }
            return(false);
        }
示例#4
0
        /// <summary>
        /// If the Caller has match - try to reconnect to match
        /// </summary>
        public void ReconnectToMatch()
        {
            if (IsHavingMatchS)
            {
                lock (MatchS)
                {
                    if (IsHavingMatchS && ValilGameS.MoveCount >= 3)
                    {
                        try
                        {
                            MatchS.IsDrawOffered = false;
                            Groups.Add(ConnectionId, MatchS.MatchId);
                            Clients.OthersInGroup(MatchS.MatchId).playerReconnected(SessionId);

                            var gameMoves = _getGameMovesQueryHandler.Handle(new GetGameMovesQuery(MatchS.MatchId));
                            var whiteTime = ValilGameS.GetWhiteTimeLeft(MatchS.GameStartTime, MatchS.InitialTime, MatchS.AddedTimePerMove);
                            var blackTime = ValilGameS.GetBlackTimeLeft(MatchS.GameStartTime, MatchS.InitialTime, MatchS.AddedTimePerMove);
                            Clients.Caller.reportMatch(MatchS, gameMoves, whiteTime, blackTime);
                            Clients.OthersInGroup(MatchS.MatchId).drawDenied();
                        }
                        catch (Exception ex)
                        {
                            throw new Microsoft.AspNet.SignalR.Client.HubException("ReconnectToMatch", ex);
                        }
                    }
                    else
                    {
                        EndGameSession();
                    }
                }
            }
        }