public IHttpActionResult JoinGame(int id, GameJoinModel item)
        {
            // Get the game
            var currentGame = this.data.Games.Find(id);

            if (currentGame == null)
            {
                return(this.BadRequest(string.Format(INVALID_ITEM_FORMAT, "game")));
            }

            // Check if game can be played
            if (currentGame.GameState != GameState.WaitingForOpponent)
            {
                return(this.BadRequest("This game is in progress. You can't join!"));
            }

            // Get the user
            var userId      = this.userIdProvider.GetUserId();
            var currentUser = this.data.Users.Find(userId);

            if (currentUser == null)
            {
                return(this.BadRequest(string.Format(INVALID_ITEM_FORMAT, "user")));
            }

            // Check if not same player
            if (currentUser.UserName == currentGame.Red.UserName)
            {
                return(this.BadRequest("You can't join your own game"));
            }

            if (!this.IsValidNumber(item.Number))
            {
                return(this.BadRequest("Invalid Number"));
            }

            currentGame.Blue             = currentUser;
            currentGame.GameState        = GameState.InProgress;
            currentGame.PlayerInTurn     = (PlayerInTurn)RandomNumber.Next(0, 2);
            currentGame.BluePlayerNumber = item.Number;

            // Create Notification
            var notification = new Notification()
            {
                Message     = string.Format("{0} joined your game \"{1} by {2}\"", currentUser.UserName, currentGame.Name, currentGame.Red.UserName),
                Game        = currentGame,
                Type        = NotificationType.GameJoined,
                User        = currentGame.Red,
                DateCreated = DateTime.Now,
                State       = NotificationState.Unread
            };

            this.data.Notifications.Add(notification);

            this.data.SaveChanges();

            var result = string.Format("result: You joined game \"{0}\"", currentGame.Name);

            return(this.Ok(result));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> Join(int id, [FromBody] GameJoinModel model, CancellationToken cancellationToken = default)
        {
            var user = await GetUserAsync(cancellationToken : cancellationToken);

            model.UserId = user.Id;

            await _gameService.AddUserToGameAsync(id, model, cancellationToken : cancellationToken);

            return(Ok());
        }
        public async Task JoinAsync_Unauthorized()
        {
            await RunAsync(async factory =>
            {
                var game = new GameJoinModel
                {
                    DeckId = IdDeck + 1,
                };
                var client = factory.CreateClient();

                var response = await client.PostJsonAsync($"api/games/{ IdGame + 1 }/join", game);

                Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
            });
        }
        public async Task JoinAsync_InvalidGame_ReturnsNotFound()
        {
            await RunAsync(async factory =>
            {
                var game = new GameJoinModel
                {
                    DeckId = IdDeck + 1,
                };
                var client = factory.CreateClientWithAuth();

                var response = await client.PostJsonAsync($"api/games/{ IdGame + 1 }/join", game);
                var model    = await response.Content.ReadAsAsync <ErrorViewModel>();

                //TODO: Replace with NotFound
                Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
                Assert.Equal($"Game { IdGame + 1 } does not exist.", model.Message);
            });
        }
Ejemplo n.º 5
0
 Task IGameService.AddUserToGameAsync(int id, GameJoinModel join, CancellationToken cancellationToken)
 {
     return(AddUserToGameInternalAsync(id, join.UserId, join.DeckId, cancellationToken: cancellationToken));
 }
        public async Task JoinAsync_DeckNotEnoughCards_ReturnsBadRequest()
        {
            await RunAsync(async factory =>
            {
                await factory.AddDataAsync(
                    new CardModel
                {
                    Id = IdCard + 1,
                }
                    );

                await factory.AddDataAsync(
                    new DeckData
                {
                    Id       = IdDeck + 1,
                    MaxCards = 5,
                    UserId   = 1,
                    Cards    = new DeckCardData[]
                    {
                        new DeckCardData
                        {
                            CardCollectionId = IdCardCollection + 1,
                            CardId           = IdCard + 1,
                        },
                        new DeckCardData
                        {
                            CardCollectionId = IdCardCollection + 2,
                            CardId           = IdCard + 1,
                        },
                    }
                }
                    );

                await factory.AddDataAsync(
                    new GameData
                {
                    Columns    = 3,
                    Id         = IdGame + 1,
                    MaxPlayers = 2,
                    Rows       = 3,
                }
                    );

                await factory.AddDataAsync(
                    new GameUserData
                {
                    GameId = IdGame + 1,
                    UserId = 2,
                }
                    );

                var game = new GameJoinModel
                {
                    DeckId = IdDeck + 1,
                };
                var client = factory.CreateClientWithAuth();

                var response = await client.PostJsonAsync($"api/games/{ IdGame + 1 }/join", game);
                var model    = await response.Content.ReadAsAsync <ErrorViewModel>();

                Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
                Assert.Equal($"Deck { IdDeck + 1 } needs 5 cards. Currently only has 2.", model.Message);
            });
        }
        public async Task JoinAsync_ValidDeckGameStarts_ReturnsSuccess()
        {
            await RunAsync(async factory =>
            {
                await factory.AddDataAsync(
                    new CardModel
                {
                    Id = IdCard + 1,
                }
                    );

                await factory.AddDataAsync(
                    new DeckData
                {
                    Id       = IdDeck + 1,
                    MaxCards = 5,
                    UserId   = 1,
                    Cards    = new DeckCardData[]
                    {
                        new DeckCardData
                        {
                            CardCollectionId = IdCardCollection + 1,
                            CardId           = IdCard + 1,
                        },
                        new DeckCardData
                        {
                            CardCollectionId = IdCardCollection + 2,
                            CardId           = IdCard + 1,
                        },
                        new DeckCardData
                        {
                            CardCollectionId = IdCardCollection + 3,
                            CardId           = IdCard + 1,
                        },
                        new DeckCardData
                        {
                            CardCollectionId = IdCardCollection + 4,
                            CardId           = IdCard + 1,
                        },
                        new DeckCardData
                        {
                            CardCollectionId = IdCardCollection + 5,
                            CardId           = IdCard + 1,
                        },
                    }
                }
                    );

                await factory.AddDataAsync(
                    new GameData
                {
                    Columns    = 3,
                    Id         = IdGame + 1,
                    MaxPlayers = 2,
                    Rows       = 3,
                }
                    );

                await factory.AddDataAsync(
                    new GameUserData
                {
                    GameId = IdGame + 1,
                    UserId = 2,
                }
                    );

                var game = new GameJoinModel
                {
                    DeckId = IdDeck + 1,
                };
                var client = factory.CreateClientWithAuth();

                var response = await client.PostJsonAsync($"api/games/{ IdGame + 1 }/join", game);
                response.EnsureSuccessStatusCode();

                response  = await client.GetAsync($"api/games/{ IdGame + 1 }");
                var model = await response.Content.ReadAsAsync <GameModel>();

                Assert.NotNull(model);
                Assert.NotNull(model.CurrentUserId);
            });
        }
Ejemplo n.º 8
0
 Task IGameService.AddUserToGameAsync(int id, GameJoinModel join, CancellationToken cancellationToken)
 {
     return(_addUserToGameHandler.HandleAsync(id, join.UserId, join.DeckId, cancellationToken: cancellationToken));
 }