public async Task <ChessGameState> MovePieceAsync(string gameId, string playerName, string from, string to) { var dictActiveGames = await GetActiveGameDict(); var dictCompletedGames = await GetCompletedGameDict(); ChessGameState newGameState; var board = new Board(); using (var tx = StateManager.CreateTransaction()) { var game = await GetActiveGameAsync(tx, gameId); if (playerName != game.White.Name && playerName != game.Black.Name) { throw new ArgumentException("Player not in the game."); } board.PerformMoves(game.MoveHistory); var fromField = ChessGameUtils.FieldFromString(from); var toField = ChessGameUtils.FieldFromString(to); var piece = board[fromField.Item1, fromField.Item2]; if (!piece.MoveTo(toField.Item1, toField.Item2)) { throw new ArgumentException("Illegal move."); } var newGameInfo = new ChessGameInfo(game.GameId, game.White, game.Black, board.ToMovesString()); if (board.IsCheckmate || board.IsDraw) { await dictActiveGames.TryRemoveAsync(tx, gameId); await dictCompletedGames.TryAddAsync(tx, gameId, newGameInfo); } else { await dictActiveGames.TryUpdateAsync(tx, gameId, newGameInfo, game); } await tx.CommitAsync(); newGameState = new ChessGameState(newGameInfo); } var chessSignalRClient = proxyFactory.CreateServiceProxy <IChessFabrickSignalRService>(chessSignalRUri /*, ChessFabrickUtils.GuidPartitionKey(gameId)*/); chessSignalRClient.PieceMovedAsync(playerName, from, to, newGameState); if (newGameState.GetCurrentPlayer().IsBot() && !board.IsCheckmate && !board.IsDraw) { var actor = ActorProxy.Create <IChessFabrickActor>(new ActorId(gameId), chessActorUri); actor.PerformMove(); } return(newGameState); }
private async void btnJoin_Click(object sender, EventArgs e) { btnJoin.Enabled = false; ChessGameInfo gameInfo = null; try { if (tabControl.SelectedTab == tabNewGames) { gameInfo = await PostJoinGameAsync(lbNewGames.SelectedItem.ToString()); } else if (tabControl.SelectedTab == tabActiveGames) { var game = await GetGameStateAsync(lbActiveGames.SelectedItem.ToString()); gameInfo = game.GameInfo; } else if (tabControl.SelectedTab == tabYourGames) { var game = await GetGameStateAsync(lbYourGames.SelectedItem.ToString()); gameInfo = game.GameInfo; } else if (tabControl.SelectedTab == tabSearchedGames) { var game = await GetGameStateAsync(lbSearchedGames.SelectedItem.ToString()); gameInfo = game.GameInfo; if ((gameInfo.Black == null && gameInfo.White?.Name != user.Player.Name) || (gameInfo.White == null && gameInfo.Black?.Name != user.Player.Name)) { gameInfo = await PostJoinGameAsync(gameInfo.GameId); } } new ChessOnlineForm(user, host, gameInfo).Show(); } catch (Exception ex) { Console.Error.WriteLine(ex); txbMessages.Text = ex.Message; } btnJoin.Enabled = true; }
public ChessGameState(ChessGameInfo gameInfo) { GameInfo = gameInfo; var board = new Board(); board.PerformMoves(gameInfo.MoveHistory); var sb = new StringBuilder(); for (int i = 0; i < Board.SIZE; ++i) { for (int j = 0; j < Board.SIZE; ++j) { if (board[i, j] != null) { sb.Append(board[i, j].ToChar()); } sb.Append(','); } sb.Remove(sb.Length - 1, 1).Append(';'); } ChessBoard = sb.Remove(sb.Length - 1, 1).ToString(); CapturedPieces = new List <char>(); foreach (var piece in board.GetCaptured()) { CapturedPieces.Add(piece.ToChar()); } CheckingPieces = new List <string>(); foreach (var piece in board.CheckingPieces) { CheckingPieces.Add(ChessGameUtils.FieldToString(piece.X, piece.Y)); } IsDraw = board.IsDraw; IsCheckmate = board.IsCheckmate; OnTurn = board.TurnColor; }
public ChessOnlineForm(UserModel user, Uri host, ChessGameInfo gameInfo) { this.user = user; this.host = host; this.gameId = gameInfo.GameId; if (gameInfo.White?.Name == user.Player.Name) { playerColor = PieceColor.White; } else if (gameInfo.Black?.Name == user.Player.Name) { playerColor = PieceColor.Black; } if (gameInfo.White != null && gameInfo.Black != null) { gameState = new ChessGameState(gameInfo); } InitializeComponent(); InitializeBoard(); InitHttpClient(); InitHubConnection(); Icon = Icon.FromHandle(Properties.Resources.knight_white.GetHicon()); Text = $"Chess - {user.Player.Name}"; if (playerColor == null) { grbPlayerColor.Text = "Spectating"; cfbPlayerColor.Image = Properties.Resources.eye; } else { grbPlayerColor.Text = "Your color"; cfbPlayerColor.Image = PieceImageUtils.Pawn(playerColor.Value); } }
public async Task PlayerJoined(string playerName, ChessGameInfo game) { await gameHubContext.Clients.Group(ChessFabrickUtils.GameGroupName(game.GameId)).SendAsync("OnPlayerJoined", game, playerName); }
public async Task GameCreated(ChessGameInfo game) { await gameHubContext.Clients.All.SendAsync("OnGameCreated", game); }