Пример #1
0
        public async Task <ChessGameInfo> JoinGameAsync(string gameId, string playerName)
        {
            var playersClient = proxyFactory.CreateServiceProxy <IChessFabrickPlayersStatefulService>(playerServiceUri, ChessFabrickUtils.NamePartitionKey(playerName));
            var player        = await playersClient.PlayerInfoAsync(playerName);

            var dictNewGames = await GetNewGameDict();

            var dictActiveGames = await GetActiveGameDict();

            ChessGameInfo activeGame;

            using (var tx = StateManager.CreateTransaction())
            {
                var game = await dictNewGames.TryGetValueAsync(tx, gameId);

                if (!game.HasValue)
                {
                    throw new ArgumentException("Game does not exist.");
                }
                if ((game.Value.White ?? game.Value.Black).Name == playerName)
                {
                    throw new ArgumentException("Can't play against yourself");
                }
                activeGame = game.Value.White == null ?
                             new ChessGameInfo(game.Value.GameId, player, game.Value.Black) :
                             new ChessGameInfo(game.Value.GameId, game.Value.White, player);
                await dictNewGames.TryRemoveAsync(tx, gameId);

                await dictActiveGames.AddAsync(tx, gameId, activeGame);

                await playersClient.AddPlayerGameAsync(playerName, gameId);

                await tx.CommitAsync();
            }

            var chessSignalRClient = proxyFactory.CreateServiceProxy <IChessFabrickSignalRService>(chessSignalRUri /*, ChessFabrickUtils.GuidPartitionKey(gameId)*/);

            chessSignalRClient.PlayerJoined(playerName, activeGame);

            return(activeGame);
        }
Пример #2
0
        public async Task <ChessGameInfo> NewGameAsync(string gameId, string playerName, PieceColor playerColor)
        {
            var playersClient = proxyFactory.CreateServiceProxy <IChessFabrickPlayersStatefulService>(playerServiceUri, ChessFabrickUtils.NamePartitionKey(playerName));
            var player        = playerName == ChessFabrickUtils.BOT_NAME ? new ChessPlayer(ChessFabrickUtils.BOT_NAME) : await playersClient.PlayerInfoAsync(playerName);

            var dictGames = await GetNewGameDict();

            ChessGameInfo game;

            using (var tx = StateManager.CreateTransaction())
            {
                game = playerColor == PieceColor.White ?
                       new ChessGameInfo(gameId, player, null) :
                       new ChessGameInfo(gameId, null, player);
                await dictGames.AddAsync(tx, gameId, game);

                await playersClient.AddPlayerGameAsync(playerName, gameId);

                await tx.CommitAsync();
            }

            var chessSignalRClient = proxyFactory.CreateServiceProxy <IChessFabrickSignalRService>(chessSignalRUri /*, ChessFabrickUtils.GuidPartitionKey(gameId)*/);

            chessSignalRClient.GameCreated(game);

            return(game);
        }
        public async Task <IActionResult> GetPlayer(string playerName)
        {
            ServiceEventSource.Current.ServiceMessage(context, $"GetPlayer({playerName})");

            var playersClient = proxyFactory.CreateServiceProxy <IChessFabrickPlayersStatefulService>(playerServiceUri, ChessFabrickUtils.NamePartitionKey(playerName));
            var player        = await playersClient.PlayerInfoAsync(playerName);

            return(Ok(player));
        }
        public async Task <IActionResult> PostNewPlayer([FromBody] AuthenticationModel model)
        {
            ServiceEventSource.Current.ServiceMessage(context, $"PostNewPlayer({model.Name}, {model.Password})");

            if (string.IsNullOrEmpty(model.Name) || model.Name.Length > 30)
            {
                return(BadRequest("Username must not be empty or longer than 30 characters."));
            }
            if (!Regex.IsMatch(model.Name, "^[a-zA-Z0-9_]+$"))
            {
                return(BadRequest("Username can contain only letters, numbers and underscore."));
            }
            //if (!Regex.IsMatch(model.Password, "([a-zA-Z0-9_]+)"))
            //{
            //    return BadRequest("Username can contain only letters, numbers and underscore.");
            //}

            var playersClient = proxyFactory.CreateServiceProxy <IChessFabrickPlayersStatefulService>(playerServiceUri, ChessFabrickUtils.NamePartitionKey(model.Name));
            var player        = await playersClient.NewPlayerAsync(model.Name);

            var user = await userService.Authenticate(model.Name, model.Password);

            return(Ok(user));
        }
        public async Task <IActionResult> GetPlayerGames()
        {
            ServiceEventSource.Current.ServiceMessage(context, $"GetPlayerGames()");
            try
            {
                var playerClient = proxyFactory.CreateServiceProxy <IChessFabrickPlayersStatefulService>(playerServiceUri, ChessFabrickUtils.NamePartitionKey(User.Identity.Name));
                var games        = await playerClient.PlayerGamesAsync(User.Identity.Name);

                return(Ok(games));
            }
            catch (Exception ex)
            {
                ServiceEventSource.Current.ServiceMessage(context, ex.ToString());;
                return(StatusCode(500, ex.Message));
            }
        }
Пример #6
0
        public async Task <UserModel> Authenticate(string userName, string password)
        {
            var userClient = proxyFactory.CreateServiceProxy <IChessFabrickPlayersStatefulService>(userServiceUri, ChessFabrickUtils.NamePartitionKey(userName));
            var player     = await userClient.PlayerInfoAsync(userName);

            if (player == null)
            {
                throw new ArgumentException("Authentication failed.");
            }

            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = Encoding.ASCII.GetBytes(appSettings.Secret);
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.Name, player.Name)
                }),
                Expires            = DateTime.UtcNow.AddDays(7),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };
            var token = tokenHandler.CreateToken(tokenDescriptor);

            var user = new UserModel {
                Player = player, Token = tokenHandler.WriteToken(token)
            };

            return(user);
        }