Пример #1
0
        public async Task <ActionResult> AddPlayerToRoom(int roomId, string playerId, string playerName)
        {
            var result = await roomService.AddPlayerToRoom(roomId, playerId, playerName);

            var message = result ? $"Add to room: {roomId}" : $"Can not add to room: {roomId}";

            return(Ok(message));
        }
        public async Task AddPlayerToRoom_ShouldNotAddAsync()
        {
            int notExistId = 100;

            _roomService.AddPlayerToRoom(notExistId, Arg.Any <string>(), Arg.Any <string>()).Returns(false);
            _controller = CreateController();

            ActionResult <List <Room> > actionResult = await _controller.AddPlayerToRoom(notExistId, "player1", "Palyer1Name");

            Assert.IsInstanceOf <OkObjectResult>(actionResult.Result);
            OkObjectResult okObjectResult = actionResult.Result as OkObjectResult;

            Assert.IsInstanceOf <string>(okObjectResult.Value);
            string model = okObjectResult.Value as string;

            Assert.IsNotNull(model);
            Assert.AreEqual($"Can not add to room: {notExistId}", model);
        }
Пример #3
0
        public async Task AddToGameGroup(string gameId, string playerId, string playerName)
        {
            try
            {
                await _roomService.AddPlayerToRoom(Convert.ToInt32(gameId), playerId, playerName);

                await Groups.AddToGroupAsync(Context.ConnectionId, gameId);
                await SendToGameGroup(gameId, "Send", $"{playerName} has joined the game.");
            }
            catch (Exception)
            { }
        }
Пример #4
0
        public async Task Connect(string roomId, string player)
        {
            PlayerInfo playerInfo = _playerService.GetPlayerByName(player);

            // If we don't have a player create one
            if (playerInfo == null)
            {
                playerInfo = new PlayerInfo
                {
                    Name         = player,
                    Address      = Context.Request.HttpContext.Connection.RemoteIpAddress.ToString(),
                    ConnectionId = Context.ConnectionId
                };

                _playerService.AddPlayer(playerInfo);
            }

            await _roomService.AddPlayerToRoom(playerInfo, roomId);
        }
Пример #5
0
        public async Task AddToGameGroup(string gameId, string playerId, string playerName)
        {
            try
            {
                var result = await _roomService.AddPlayerToRoom(Convert.ToInt32(gameId), playerId, playerName);

                if (result)
                {
                    await Groups.AddToGroupAsync(Context.ConnectionId, gameId);

                    await SendToGameGroup(gameId, "Send", $"{playerName} has joined the game.");
                }
                else
                {
                    await Clients.Client(Context.ConnectionId).SendAsync("Error", "Couldn't join to game.");
                }
            }
            catch (Exception)
            { }
        }