Beispiel #1
0
        /// <summary>
        /// Sends an undo request to the server, after undo button is clicked.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UndoButton_Click(object sender, EventArgs e)
        {
            // Sending undo request to server via JSon
            UndoRequest r       = new UndoRequest("undo");
            string      request = JsonConvert.SerializeObject(r) + "\n";

            NC.SendData(request);
        }
Beispiel #2
0
        public async Task <UndoRequest> UndoRequest(ulong channel, ulong player, int amount, Stream stream, Action <ChessMatch> onTimeout = null)
        {
            return(await Task.Run(async() =>
            {
                var match = await GetMatch(channel, player);

                if (match == null)
                {
                    throw new ChessException("You are not in a game.");
                }

                var historyCount = match.History.Count();
                if (historyCount < amount)
                {
                    amount = historyCount;
                }

                var moveToRevert = match.History.OrderByDescending(x => x.MoveDate).Skip(amount - 1).FirstOrDefault();
                if (moveToRevert == null)
                {
                    throw new ChessException("Nothing to undo.");
                }

                var userWhoseTurn = match.Game.WhoseTurn == Player.White ? match.Challenged : match.Challenger;

                if (userWhoseTurn != player)
                {
                    throw new ChessException("You can't undo another players turn.");
                }

                if (match.UndoRequest != null)
                {
                    throw new ChessException("Undo request is already in process.");
                }

                var undoRequest = new UndoRequest {
                    CreatedDate = DateTime.UtcNow, CreatedBy = player, Amount = amount
                };

                match.UndoRequest = undoRequest;

                RemoveUndoRequest(match, onTimeout);

                var previousMove = match.History.OrderByDescending(x => x.MoveDate).Skip(amount).FirstOrDefault();

                await WriteBoard(previousMove, new ChessGame(new GameCreationData {
                    Board = moveToRevert.PreviousBoardState, WhoseTurn = moveToRevert.PreviousWhoseTurn
                }), stream);

                return await Task.FromResult(undoRequest);
            }));
        }
        public async Task <UndoRequest> UndoRequest(ulong channel, ulong player, Action <ChessMatch> onTimeout = null)
        {
            var match = await GetMatch(channel, player);

            if (match == null)
            {
                throw new ChessException("You are not in a game.");
            }

            if (!match.History.Any())
            {
                throw new ChessException("Nothing to undo.");
            }

            var userWhoseTurn = match.Game.WhoseTurn == Player.White ? match.Challenged : match.Challenger;

            if (userWhoseTurn != player)
            {
                throw new ChessException("You can't undo another players turn.");
            }

            if (match.UndoRequest != null)
            {
                throw new ChessException("Undo request is already in process.");
            }

            var undoRequest = new UndoRequest {
                CreatedDate = DateTime.UtcNow, CreatedBy = player
            };

            match.UndoRequest = undoRequest;

            RemoveUndoRequest(match, onTimeout);

            return(await Task.FromResult(undoRequest));
        }