public string JoinGame(string hashedGameId, string hashedPlayerid) { try { GameDAL gdal = new GameDAL(); UserDAL udal = new UserDAL(); CustomCrypto hashing = new CustomCrypto(); int gameId = Convert.ToInt32(hashing.Decrypt(hashedGameId)); int playerid = Convert.ToInt32(hashing.Decrypt(hashedPlayerid)); Game game = gdal.GetGame(gameId); User user = udal.GetUserById(playerid); if (playerid == game.Player1 || playerid == game.Player2 || playerid == game.Player3 || playerid == game.Player4) { //user is already part of the game return(hashing.Encrypt(game.GameId.ToString())); } //user is already part of ANOTHER the game if (user.ActiveGameId != null) { throw new CustomException("You are already a part of another game."); } if (game.Player1 != null && game.Player2 != null && game.Player3 != null && game.Player4 != null) { throw new CustomException("Game room is full."); } int time = 0; if (game.Player1 == null) { game.Player1 = playerid; var secondsElapsed = (DateTime.Now).Subtract(game.StartTime).Seconds; time = 30 - secondsElapsed; } else if (game.Player2 == null) { game.Player2 = playerid; var secondsElapsed = (DateTime.Now).Subtract(game.StartTime).Seconds; time = 30 - secondsElapsed; } else if (game.Player3 == null) { game.Player3 = playerid; var secondsElapsed = (DateTime.Now).Subtract(game.StartTime).Seconds; time = 30 - secondsElapsed; } else if (game.Player4 == null) { game.Player4 = playerid; var secondsElapsed = (DateTime.Now).Subtract(game.StartTime).Seconds; time = 30 - secondsElapsed; } var new_game = gdal.AddPlayer(playerid, game.GameId); udal.UpdateActiveGame(playerid, new_game.GameId); // check if game room is full if (game.Player1 != null && game.Player2 != null && game.Player3 != null && game.Player4 != null) { InitializeGame(game.GameId); WaitForPlayers(time, new_game.GameId); return(hashing.Encrypt(new_game.GameId.ToString())); } WaitForPlayers(time, new_game.GameId); game = gdal.GetGame(game.GameId); if (game.Status != (int)GameStatus.Started) { AbortGame(hashedGameId); throw new CustomException("Timeout. Players insufficient."); } else { return(hashing.Encrypt(new_game.GameId.ToString())); } } catch (CustomException e) { throw new CustomException(e.Message); } catch (Exception e) { logger.Error(e); throw new Exception("Oops! Some error occured."); } }