Beispiel #1
0
        protected static void SendMessage(string messageText, User toUser, Game game, MessageType messageType, BullsAndCowsEntities context)
        {
            MessageState unreadMessageState = context.MessageStates.First(ms => ms.State == MessageStateUnread);
            var message = new Message()
            {
                Text = messageText,
                Game = game,
                User = toUser,
                MessageState = unreadMessageState,
                MessageType = messageType
            };

            context.Messages.Add(message);
        }
Beispiel #2
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();
            }
        }
Beispiel #3
0
        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", "ERR_INV_OP");
                }
                var fullGameStatus = context.GameStatuses.First(st => st.Status == GameStatusFull);
                if (game.GameStatus != fullGameStatus)
                {
                    throw new ServerErrorException("Game cannot be started", "ERR_INV_OP");
                }
                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 message = new Message()
                {
                    Text = string.Format(MessageTextStartedGame, redUser.Nickname, game.Title),
                    Game = game,
                    User = game.BlueUser,
                    MessageState = unreadMessageState,
                    MessageType = gameStartedMessageType
                };
                context.Messages.Add(message);
                context.SaveChanges();
            }
        }