public IActionResult GameRefresh([FromBody] GameRefreshData data) { string userID = _signInManager.UserManager.GetUserId(User); bool IsMyTurn = _GameRepo.IsActivePlayer(data.GameID, userID); bool isChecked = false; ChessboardPartialViewModel game = null; var gamestate = _GameRepo.GetGamestate(data.GameID); bool isactive = gamestate.IsActive; if (IsMyTurn) { var changedTiles = _GameRepo.GetChangedTiles(data.GameID); char userColor = _GameRepo.GetPlayerColor(data.GameID, userID); if (userColor == 'w') { isChecked = _chessLogicManager.IsWhiteChecked(gamestate.Board); } else { isChecked = _chessLogicManager.IsBlackChecked(gamestate.Board); } game = new ChessboardPartialViewModel { GameState = gamestate, SelectedTile = null, CanMoveToAndTakeTiles = new List <string>(), CanMoveToTiles = new List <string>(), GameId = data.GameID, PlayerColor = userColor, ChangedTiles = changedTiles.ToList() }; } // Game will be null if not users turn. return(Json(new GameViewData { Game = game, MoveCount = gamestate.MovesCounter, IsActive = isactive, IsMyTurn = IsMyTurn, IsChecked = isChecked })); }
public IActionResult ArchivedGames(string gameId) { if (!_signInManager.IsSignedIn(User)) { return(RedirectToAction("Login", "Account")); } RefreshUser(User); var uid = _signInManager.UserManager.GetUserId(User); var games = _GameRepo.GetArchivedGameList(uid); if (games.Count() == 0) { //has no archived games return(RedirectToAction("Index", "Manage")); } if (gameId == "default") { gameId = games.First().GameID; } var userId = _signInManager.UserManager.GetUserId(User); var gamestate = _GameRepo.GetGamestate(gameId); var emptyStringList = new List <string>(); List <string> changedTiles = new List <string>(); ChessboardPartialViewModel board = new ChessboardPartialViewModel { GameState = gamestate, SelectedTile = null, CanMoveToAndTakeTiles = emptyStringList, CanMoveToTiles = emptyStringList, GameId = gameId, PlayerColor = _GameRepo.GetPlayerColor(gameId, userId), ChangedTiles = changedTiles }; ArchivedGamesViewModel ag = new ArchivedGamesViewModel { ArchivedGames = games, Board = board }; return(View(ag)); }
public IActionResult Index(string gameId) { if (!_signInManager.IsSignedIn(User)) { return(RedirectToAction("Login", "Account")); } RefreshUser(User); var userId = _signInManager.UserManager.GetUserId(User); GameStateDTO gamestate; try { gamestate = _GameRepo.GetGamestate(gameId); } catch (ArgumentException e) { return(RedirectToAction("Index", "Home")); } var friends = _GameRepo.GetFriendList(userId); var chat = _GameRepo.GetMessageLog(gameId); var games = _GameRepo.GetAllGames(userId); var emptyStringList = new List <string>(); List <string> changedTiles = new List <string>(); foreach (char f in Enumerable.Range('a', 8)) { foreach (char r in Enumerable.Range('1', 8)) { changedTiles.Add(f.ToString() + r.ToString()); } } FriendsPartialViewModel friendsAndGames = new FriendsPartialViewModel { UserFriends = friends, UserGames = _GameRepo.GetUserGameList(userId), ActiveGame = gameId }; ChessboardPartialViewModel board = new ChessboardPartialViewModel { GameState = gamestate, SelectedTile = null, CanMoveToAndTakeTiles = emptyStringList, CanMoveToTiles = emptyStringList, GameId = gameId, PlayerColor = _GameRepo.GetPlayerColor(gameId, userId), ChangedTiles = changedTiles }; ChatPartialViewModel chatSystem = new ChatPartialViewModel { GameID = gameId, ChatMessages = chat }; string opponent; try { opponent = games.Where(e => e.GameID.Equals(gameId)).FirstOrDefault().OpponentName; } catch (NullReferenceException e) { return(RedirectToAction("Index", "Home")); } return(View(new GameViewModel { FriendsAndGames = friendsAndGames, Board = board, Chat = chatSystem, OpponentName = opponent })); }
// public IActionResult ClickedTile(string clickedTile, string gameId, string selectedTile, List<string> canMove, List<string> canTake) public IActionResult ClickedTile([FromBody] TileClick data) { var gameId = data.GameID; var clickedTile = data.ClickedTile; var selectedTile = data.SelectedTile; var canMove = data.CanMove; var canTake = data.CanTake; var didMove = false; List <string> changedTiles = new List <string>(); changedTiles.AddRange(canMove); changedTiles.AddRange(canTake); string userId = _signInManager.UserManager.GetUserId(User); RefreshUser(User); GameStateDTO gamestate = _GameRepo.GetGamestate(gameId); char userColor = _GameRepo.GetPlayerColor(gameId, userId); if (!_GameRepo.IsActivePlayer(gameId, userId)) { // Not players turn return(Json("")); } else if (selectedTile == clickedTile) { // Clicked already selected tile. Deselect it. changedTiles.Add(selectedTile); selectedTile = null; canMove = new List <string>(); canTake = new List <string>(); } else if (_chessLogicManager.PositionIsColor(gamestate.Board, clickedTile, userColor)) { // Clicked own piece. Get possible moves. SelectedPieceDTO selectedPiece = new SelectedPieceDTO { Board = gamestate.Board, PlayerColor = userColor, Selected = clickedTile }; var moves = _chessLogicManager.GetPossibleMoves(selectedPiece); canMove = moves.PositionsPieceCanMoveTo; canTake = moves.PositionsPieceCanKillAt; changedTiles.AddRange(canMove); changedTiles.AddRange(canTake); if (selectedTile != null) { changedTiles.Add(selectedTile); } changedTiles.Add(clickedTile); selectedTile = clickedTile; } else if (selectedTile != null) { //a tile is already selected, and clicked an enemy or empty tile //Can the selected piece move there? MovePlanDTO movePlan = new MovePlanDTO { Board = gamestate.Board, From = selectedTile, To = clickedTile, PlayerColor = userColor }; changedTiles.Add(clickedTile); changedTiles.Add(selectedTile); if (_chessLogicManager.IsValidMove(movePlan)) { //yes, the selected piece can move there //Apply the game logic, save the new board state, and log the move. string[,] newBoard = _chessLogicManager.DoMove(movePlan); NewMoveDTO newMove = new NewMoveDTO { NewBoard = newBoard, From = selectedTile, To = clickedTile, GameID = gameId }; _GameRepo.AddNewMove(newMove); _GameRepo.SetChangedTiles(gameId, new List <string> { clickedTile, selectedTile }); didMove = true; if (_chessLogicManager.IsBlackCheckMate(newBoard) || _chessLogicManager.IsWhiteCheckMate(newBoard)) { _GameRepo.SetGameToFinished(gameId); _GameRepo.SetGameToFinished(gameId); } } // Either a move was made, or the tile was deselected by clicking // a tile where a move wasn't possible. Remove highlights. canMove = new List <string>(); canTake = new List <string>(); selectedTile = null; } gamestate = _GameRepo.GetGamestate(gameId); ChessboardPartialViewModel board = new ChessboardPartialViewModel { GameState = gamestate, SelectedTile = selectedTile, CanMoveToAndTakeTiles = canTake, CanMoveToTiles = canMove, GameId = gameId, PlayerColor = userColor, ChangedTiles = changedTiles, DidMove = didMove }; return(Json(board)); }