public IHttpActionResult JoinGame(int id, JoinGameDataModel gameModel)
        {
            var gameToJoin = this.Data.Games
                             .All()
                             .FirstOrDefault(g => g.Id == id);

            if (gameToJoin == null)
            {
                return(NotFound());
            }

            if (!this.IsValidNumber(gameModel.Number))
            {
                return(BadRequest("Number is not valid"));
            }

            var userId = this.GetCurrentUserId();
            var user   = this.GetCurrentUser(userId);

            gameToJoin.SecondUserSecretNumber = gameModel.Number;
            gameToJoin.SecondUserId           = userId;
            gameToJoin.SecondUser             = user;

            // randomly decide which player is in turn
            // TODO: Provide IRandomProvider if you have time
            gameToJoin.GameState = randomProvider.GetRandomInt(0, 1) == 0 ? GameState.BlueInTurn : GameState.RedInTurn;

            this.Data.Games.Update(gameToJoin);
            this.Data.SaveChanges();

            return(Ok(new { result = string.Format(SuccessfullJoin, gameToJoin.Name) }));
        }
Example #2
0
        public ApplicationEnum.Enum.NewsThemeType GetCurrentTheme()
        {
            var themeCount   = Enum.GetNames(typeof(ApplicationEnum.Enum.NewsThemeType)).Length;
            var currentTheme = _randomProvider.GetRandomInt(themeCount);
            var parseResult  = Enum.TryParse(typeof(ApplicationEnum.Enum.NewsThemeType), currentTheme.ToString(), out object theme);

            if (!parseResult || (ApplicationEnum.Enum.NewsThemeType)theme == default)//if parse fail or enum is None set first enum
            {
                theme = ApplicationEnum.Enum.NewsThemeType.Beard;
            }
            return((ApplicationEnum.Enum.NewsThemeType)theme);
        }