Пример #1
0
        public async Task JoinGame(string gameId, string userName)
        {
            if (!_groupStore.GroupExists(gameId))
            {
                throw new ArgumentException($"There is no game '{gameId}'.");
            }

            if (!_gameEngineService.CanJoinGame(gameId))
            {
                throw new InvalidOperationException($"Game {gameId} has already started.");
            }

            var addResult = _gameEngineService.AddPlayer(gameId, userName);
            var player    = new PlayerDetails
            {
                Id       = addResult.Id,
                Color    = addResult.Color,
                UserName = userName
            };

            _playerStore.AddPlayer(Context.ConnectionId, player);

            await Groups.AddToGroupAsync(Context.ConnectionId, gameId);

            await Clients.Client(_groupStore.GetGroupHost(gameId)).SendAsync(ClientMethods.OnPlayerJoined, userName);

            await Clients.Caller.SendAsync(ClientMethods.OnColourSet, player.Color);
        }