예제 #1
0
        public static IEnumerable<MessageModel> GetUnreadMessages(int userId)
        {
            var context = new BullsAndCowsEntities();
            using (context)
            {
                var user = context.Users.Include("Messages").FirstOrDefault(u => u.Id == userId);
                if (user == null)
                {
                    throw new ServerErrorException("Invalid user", "ERR_INV_USR");
                }

                var unreadMessageState = context.MessageStates.First(ms => ms.State == MessageStateUnread);
                var readMessageState = context.MessageStates.First(ms => ms.State == MessageStateRead);

                IEnumerable<Message> unreadMessages = user.Messages.Where(msg => msg.MessageState == unreadMessageState);
                var messageModels = ParseMessagesToModels(unreadMessages).Reverse();

                foreach (var msg in unreadMessages)
                {
                    msg.MessageState = readMessageState;
                }
                context.SaveChanges();
                return messageModels;
            }
        }
        /* public members */
        public static void CreateUser(string username, string nickname, string authCode)
        {
            ValidateUsername(username);
            ValidateNickname(nickname);
            ValidateAuthCode(authCode);
            using (BullsAndCowsEntities context = new BullsAndCowsEntities())
            {
                var usernameToLower = username.ToLower();
                var nicknameToLower = nickname.ToLower();

                var dbUser = context.Users.FirstOrDefault(u => u.Username.ToLower() == usernameToLower || u.Nickname.ToLower() == nicknameToLower);

                if (dbUser != null)
                {
                    if (dbUser.Username.ToLower() == usernameToLower)
                    {
                        throw new ServerErrorException("Username already exists", "ERR_DUP_USR");
                    }
                    else
                    {
                        throw new ServerErrorException("Nickname already exists", "ERR_DUP_NICK");
                    }
                }

                dbUser = new User()
                {
                    Username = usernameToLower,
                    Nickname = nickname,
                    AuthCode = authCode
                };
                context.Users.Add(dbUser);
                context.SaveChanges();
            }
        }
        /* public members */
        public static void CreateGame(int userId, CreateGameModel gameModel)
        {
            ValidateGameTitle(gameModel.Title);
            ValidateGamePassword(gameModel.Password);
            ValidateUserNumber(gameModel.Number);

            var context = new BullsAndCowsEntities();
            using (context)
            {
                var redUser = GetUser(userId, context);

                var gameOpenStatus = context.GameStatuses.First(st => st.Status == GameStatusOpen);

                var game = new Game()
                {
                    Title = gameModel.Title,
                    Password = gameModel.Password,
                    RedUser = redUser,
                    RedUserNumber = gameModel.Number,
                    GameStatus = gameOpenStatus
                };

                context.Games.Add(game);
                context.SaveChanges();
            }
        }
        /* public members */
        public static void MakeGuess(int userId, GuessModel guessModel)
        {
            ValidateUserNumber(guessModel.Number);
            var context = new BullsAndCowsEntities();
            using (context)
            {
                var user = GetUser(userId, context);
                var game = GetGame(guessModel.GameId, context);

                if (game.GameStatus != context.GameStatuses.First((st) => st.Status == GameStatusInProgress))
                {
                    throw new ServerErrorException("Game is not in progress", "INV_OP_GAME_STAT");
                }

                ValidateUserInGame(game, user);

                var guessNumber = guessModel.Number.ToString();
                string otherUserNumber = GetOtherUserNumber(game, user);

                int bullsCount, cowsCount;
                CountBullsAndCows(guessNumber, otherUserNumber, out bullsCount, out cowsCount);

                var guess = new Guess()
                {
                    Bulls = bullsCount,
                    Cows = cowsCount,
                    Game = game,
                    Number = guessModel.Number,
                    User = user
                };
                context.Guesses.Add(guess);

                var otherUser = (game.RedUser == user) ? game.BlueUser : game.RedUser;

                if (bullsCount == UserNumberLength)
                {
                    FinishGame(user, otherUser, game, context);
                }
                else
                {
                    game.UserInTurn = otherUser.Id;

                    MessageType guessMadeMessageType = context.MessageTypes.First(mt => mt.Type == MessageTypeGuessMade);
                    string messageText = string.Format(MessageTextGuessMade, user.Nickname, guessNumber, game.Title);
                    SendMessage(messageText, otherUser, game, guessMadeMessageType, context);
                }
                context.SaveChanges();
            }
        }
        public static void StartGame(int userId, int gameId)
        {
            var context = new BullsAndCowsEntities();
            using (context)
            {
                var redUser = GetUser(userId, context);
                var game = GetGame(gameId, context);

                if (game.RedUser != redUser)
                {
                    throw new ServerErrorException("You cannot start the game", "INV_OP_GAME_OWNER");
                }
                var fullGameStatus = context.GameStatuses.First(st => st.Status == GameStatusFull);
                if (game.GameStatus != fullGameStatus)
                {
                    throw new ServerErrorException("Game cannot be started", "INV_OP_GAME_STAT");
                }
                var inProgressGameStatus = context.GameStatuses.First(st => st.Status == GameStatusInProgress);

                game.GameStatus = inProgressGameStatus;
                game.UserInTurn = rand.Next(2) == 0 ? game.RedUser.Id : game.BlueUser.Id;

                //MessageState unreadMessageState = context.MessageStates.First(ms => ms.State == MessageStateUnread);
                MessageType gameStartedMessageType = context.MessageTypes.First(mt => mt.Type == MessageTypeGameStarted);
                var msgText = string.Format(MessageTextStartedGame, redUser.Nickname, game.Title);
                SendMessage(msgText, game.BlueUser, game, gameStartedMessageType, context);
                context.SaveChanges();
            }
        }
        public static void JoinGame(int userId, JoinGameModel gameModel)
        {
            ValidateGamePassword(gameModel.Password);
            ValidateUserNumber(gameModel.Number);

            var context = new BullsAndCowsEntities();
            using (context)
            {
                var game = GetGame(gameModel.Id, context);

                ValidateOpenGameStatus(game.GameStatus);

                if (game.Password != gameModel.Password)
                {
                    throw new ServerErrorException("Incorrect game password", "INV_GAME_AUTH");
                }

                var blueUser = GetUser(userId, context);
                game.BlueUser = blueUser;
                game.BlueUserNumber = gameModel.Number;
                var fullGameStatus = context.GameStatuses.First(st => st.Status == GameStatusFull);
                game.GameStatus = fullGameStatus;

                MessageType gameJoinedMessageType = context.MessageTypes.First(mt => mt.Type == MessageTypeGameJoined);

                var msgText = string.Format(MessageTextJoinedGame, blueUser.Nickname, game.Title);

                SendMessage(msgText, game.RedUser, game, gameJoinedMessageType, context);

                context.SaveChanges();
            }
        }
        public static string LoginUser(string username, string authCode, out string nickname)
        {
            ValidateUsername(username);
            ValidateAuthCode(authCode);
            var context = new BullsAndCowsEntities();
            using (context)
            {
                var usernameToLower = username.ToLower();
                var user = context.Users.FirstOrDefault(u => u.Username.ToLower() == usernameToLower && u.AuthCode == authCode);
                if (user == null)
                {
                    throw new ServerErrorException("Invalid user authentication", "INV_USR_AUTH");
                }

                var sessionKey = GenerateSessionKey((int)user.Id);
                user.SessionKey = sessionKey;
                nickname = user.Nickname;
                context.SaveChanges();
                return sessionKey;
            }
        }
 public static void LogoutUser(string sessionKey)
 {
     ValidateSessionKey(sessionKey);
     var context = new BullsAndCowsEntities();
     using (context)
     {
         var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey);
         if (user == null)
         {
             throw new ServerErrorException("Invalid user authentication", "INV_USR_AUTH");
         }
         user.SessionKey = null;
         context.SaveChanges();
     }
 }
예제 #9
0
        public static void JoinGame(int userId, JoinGameModel gameModel)
        {
            ValidateGamePassword(gameModel.Password);
            ValidateUserNumber(gameModel.Number);

            var context = new BullsAndCowsEntities();
            using (context)
            {
                var game = GetGame(gameModel.Id, context);

                if (game.GameStatus.Status != GameStatusOpen)
                {
                    throw new ServerErrorException("Game is unavailable", "ERR_INV_GAME");
                }

                if (game.Password != gameModel.Password)
                {
                    throw new ServerErrorException("Incorrect game password", "ERR_BAD_PASS");
                }

                var blueUser = GetUser(userId, context);
                game.BlueUser = blueUser;
                game.BlueUserNumber = gameModel.Number;
                var fullGameStatus = context.GameStatuses.First(st => st.Status == GameStatusFull);
                game.GameStatus = fullGameStatus;

                MessageState unreadMessageState = context.MessageStates.First(ms => ms.State == MessageStateUnread);
                MessageType gameJoinedMessageType = context.MessageTypes.First(mt => mt.Type == MessageTypeGameJoined);
                var message = new Message()
                {
                    Text = string.Format(MessageTextJoinedGame, blueUser.Nickname, game.Title),
                    Game = game,
                    User = game.RedUser,
                    MessageState = unreadMessageState,
                    MessageType = gameJoinedMessageType
                };
                context.Messages.Add(message);
                context.SaveChanges();
            }
        }
예제 #10
0
        public static void MakeGuess(int userId, GuessModel guessModel)
        {
            ValidateUserNumber(guessModel.Number);
            var context = new BullsAndCowsEntities();
            using (context)
            {
                var user = GetUser(userId, context);
                var game = GetGame(guessModel.GameId, context);

                if (game.GameStatus != context.GameStatuses.First((st) => st.Status == GameStatusInProgress))
                {
                    throw new ServerErrorException("Game is not in progress", "ERR_INV_OP");
                }

                ValidateUserInGame(game, user);

                string otherUserNumber = GetOtherUserNumber(game, user);
                var guessNumber = guessModel.Number.ToString();

                int bullsCount, cowsCount;
                CountBullsAndCows(guessNumber, otherUserNumber, out bullsCount, out cowsCount);

                var guess = new Guess()
                {
                    Bulls = bullsCount,
                    Cows = cowsCount,
                    Game = game,
                    Number = guessModel.Number,
                    User = user
                };
                context.Guesses.Add(guess);

                var otherUser = (game.RedUser == user) ? game.BlueUser : game.RedUser;
                if (bullsCount == UserNumberLength)
                {
                    //game finished
                    game.GameStatus = context.GameStatuses.First(st => st.Status == GameStatusFinished);

                    //set score
                    var guessesMadeCount = game.Guesses.Count(g => g.User == user);
                    var score = new Score()
                    {
                        User = user,
                        Value = 1000 / guessesMadeCount
                    };

                    context.Scores.Add(score);

                    //send msg
                    MessageType gameFinishedMessageType = context.MessageTypes.First(mt => mt.Type == MessageTypeGameFinished);
                    string messageTextWinner = string.Format(MessageTextGameFinishedWinner, game.Title, otherUser.Nickname, guessesMadeCount);
                    string messageTextLoser = string.Format(MessageTextGameFinishedLoser, user.Nickname, game.Title);

                    SendMessage(context, messageTextWinner, game, user, gameFinishedMessageType);
                    SendMessage(context, messageTextLoser, game, otherUser, gameFinishedMessageType);
                }
                else
                {
                    game.UserInTurn = otherUser.Id;

                    MessageType guessMadeMessageType = context.MessageTypes.First(mt => mt.Type == MessageTypeGuessMade);
                    string messageText = string.Format(MessageTextGuessMade, user.Nickname, guessNumber, game.Title);
                    SendMessage(context, messageText, game, otherUser, guessMadeMessageType);
                }
                context.SaveChanges();
            }
        }