Пример #1
0
        /// <summary>
        /// Save new score
        /// </summary>
        /// <param name="model">game model</param>
        /// <param name="playerName">player name</param>
        /// <returns>score model</returns>
        public Score SaveScore([FromBody] T model, string playerName)
        {
            Logger.Logger.Instance.Info($"Saving score for player {playerName}.");

            if (playerName == null)
            {
                Logger.Logger.Instance.Error("Player's name is null.");
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                                            $"Parameter {nameof(playerName)} is required"));
            }

            Validate(model);
            if (!ModelState.IsValid)
            {
                Logger.Logger.Instance.Error(ModelState.GetErrors());
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }

            if (!model.IsGameCompleted)
            {
                Logger.Logger.Instance.Error("Game is not ended yet to save results.");
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                                            "Game is not ended yet to save results!"));
            }

            Guid id    = Guid.NewGuid();
            var  score = new Score(id, playerName, _gameName, model.GameStats.TimeSpent, model.GameStats.TurnsNumber);

            _scoresRepository.AddScore(score);

            Logger.Logger.Instance.Info($"New score with id: {id} has been saved.");
            return(score);
        }
Пример #2
0
        public void UpdateAmount(int amount)
        {
            var user = _usersBusiness.Get(_userId);

            user.Total += amount;
            _usersBusiness.Update(user);

            _leaderboardSet.AddScore(user.Name, user.Total);
            _historyList.AddToList(amount);
            _historyList.LimitList(5);
        }
Пример #3
0
        public void GetScoresTest()
        {
            PlayersRepository playersRepository = new PlayersRepository(ConnectionString);
            GamesRepository   gamesRepository   = new GamesRepository(ConnectionString);
            ScoresRepository  scoresRepository  = new ScoresRepository(ConnectionString);

            string playerName = GetRandomName(10);
            string gameName   = GetRandomName(10);

            playersRepository.AddPlayer(new Player(playerName));
            _playersList.Add(playerName);

            gamesRepository.AddGame(new Game(gameName));
            _gamesList.Add(gameName);

            Guid scoreId1 = Guid.NewGuid(), scoreId2 = Guid.NewGuid();

            _scoresList.Add(scoreId1);
            _scoresList.Add(scoreId2);

            Score score = new Score(scoreId1, playerName, gameName, scores: 5);

            scoresRepository.AddScore(score);
            score = new Score(scoreId2, playerName, gameName, scores: 15);
            scoresRepository.AddScore(score);

            List <Score> scores = scoresRepository.GetScores(new Game(gameName), new Player(playerName)).ToList();

            if (scores.Find(x => x.Id == scoreId1) == null)
            {
                Assert.Fail($"Expected score id is {scoreId1}");
            }
            if (scores.Find(x => x.Id == scoreId2) == null)
            {
                Assert.Fail($"Expected score id is {scoreId1}");
            }
        }
Пример #4
0
        public void AddGetScoreTest()
        {
            PlayersRepository playersRepository = new PlayersRepository(ConnectionString);
            GamesRepository   gamesRepository   = new GamesRepository(ConnectionString);
            ScoresRepository  scoresRepository  = new ScoresRepository(ConnectionString);

            string playerName = GetRandomName(10);
            string gameName   = GetRandomName(10);

            playersRepository.AddPlayer(new Player(playerName));
            _playersList.Add(playerName);

            gamesRepository.AddGame(new Game(gameName));
            _gamesList.Add(gameName);

            Guid scoreId = Guid.NewGuid();

            _scoresList.Add(scoreId);
            Score score = new Score(scoreId, playerName, gameName, scores: 5);

            scoresRepository.AddScore(score);

            Assert.AreEqual(scoreId, scoresRepository.GetScore(scoreId).Id);
        }