public void Create(int eventId, NotificationType notificationType)
        {
            var notification = new Notification()
            {
                NotificationType = notificationType,
                EventId = eventId
            };

            this.notifications.Add(notification);
            this.notifications.Save();
        }
 public void ChangeState(Notification notification)
 {
     this.state = notification;
     this.Notify();
 }
        public IHttpActionResult Put(int id, GetNumberDataModel model)
        {
            Validate(model.Number);

            var game = this.data.Games.Find(id);
            if (game == null)
            {
                return BadRequest("Invalid game id!");
            }

            var userId = this.User.Identity.GetUserId();

            if (userId == game.RedId)
            {
                return BadRequest("A game, created by a user, cannot be joined by the same user.");
            }

            game.BlueNumber = model.Number;
            game.BlueId = userId;

            var playerInTurn = rand.Next(1, 2);
            game.GameState = playerInTurn == 1 ? GameState.RedInTurn : GameState.BlueInTurn;

            var currentMessage = string.Format(JOINED_GAME_MESSAGE, game.Name);

            var notification = new Notification
            {
                DateCreated = DateTime.Now,
                GameId = game.ID,
                Type = NotificationType.YourTurn,
                State = NotificationState.Unread,
                Message = string.Format(currentMessage),
            };

            game.Notifications.Add(notification);
            this.data.Games.Update(game);

            var user = this.data.Users.Find(userId);

            user.Notifications.Add(notification);
            this.data.Users.Update(user);

            this.data.SaveChanges();

            return Ok(new
            {
                result = currentMessage
            });
        }
Beispiel #4
0
 public Models.Notification SaveNotification(Models.Notification notification)
 {
     if (notification.Destination.TrueForAll(t => Enum.TryParse(t.Message.TypeNotification, out EventSupport eventSupport)))
     {
         return(_liteDb.Insert(notification));
     }
        public IHttpActionResult Post(int id, [FromBody]GetNumberDataModel model)
        {
            var game = this.data.Games.Find(id);
            if (game == null)
            {
                return BadRequest("Invalid game id!");
            }
            if (game.GameState == GameState.WaitingForOpponent || game.GameState == GameState.Finished)
            {
                return BadRequest("Invalid game state");
            }

            var userId = this.User.Identity.GetUserId();
            if (!(game.BlueId == userId || game.RedId == userId))
            {
                return BadRequest("Wrong user!");
            }

            ApplicationUser playerInTurn;
            ApplicationUser otherPlayer;
            int originalNumber = int.MinValue; ;

            if (game.RedId == userId && game.GameState == GameState.RedInTurn)
            {
                otherPlayer = game.Blue;
                playerInTurn = game.Red;
                originalNumber = game.RedNumber;
            }
            else
            {
                otherPlayer = game.Red;
                playerInTurn = game.Blue;
                originalNumber = (int)game.BlueNumber;
            }
            
            ValidateNumber(model.Number);

            var bullsAndCowsCheck = CheckBullsAndCows(model.Number.ToString(), originalNumber.ToString());
            var bulls = bullsAndCowsCheck[0];
            var cows = bullsAndCowsCheck[1];

            var toOtherPlayerNotification = new Notification
            {
                DateCreated = DateTime.Now,
                GameId = game.ID,
                State = NotificationState.Unread,
                UserId = otherPlayer.Id
            };

            if (bulls == 4)
            {
                game.GameState = GameState.Finished;
                toOtherPlayerNotification.Message = string.Format(BEATEN_MESSAGE, playerInTurn.Email,game.Name);
                toOtherPlayerNotification.Type = NotificationType.GameLost;

                var toCurrentPlayerMessage = new Notification
                {
                    DateCreated = DateTime.Now,
                    GameId = game.ID,
                    State = NotificationState.Unread,
                    Type = NotificationType.GameWon,
                    Message = string.Format(YOU_WON_MESSAGE, otherPlayer.Email, game.Name),
                    UserId = playerInTurn.Id
                };

                this.data.Notifications.Add(toCurrentPlayerMessage);
            }
            else
            {
                toOtherPlayerNotification.Message = string.Format(YOUR_TURN_MESSAGE, game.Name);
                toOtherPlayerNotification.Type = NotificationType.YourTurn;
            }

            this.data.Notifications.Add(toOtherPlayerNotification);

            var guess = new Guess
            {
                BullsCount = bulls,
                CowsCount = cows,
                DateMade = DateTime.Now,
                GameID = game.ID,
                Number = model.Number,
                UserID = userId
            };

            this.data.Guesses.Add(guess);

            var response = this.data.Guesses.All().Where(g => g.ID == guess.ID).Select(GuessDataModel.FromGame);

            this.data.Games.Update(game);
            this.data.SaveChanges();

            return Ok(response);
        }