示例#1
0
 internal CallContext(ApiChat apiChat, Update update, object userState, UserInChat userInChat)
 {
     _apiChat   = apiChat;
     UserInChat = userInChat;
     UserState  = userState;
     Update     = update;
 }
示例#2
0
        public static void InvokeGame(this GameProvider gameProvider, UserInChat userInChat, Action <Game> action,
                                      bool isPrivate = false)
        {
            var game = gameProvider.GetGameForUser(userInChat, isPrivate);

            if (isPrivate
                ) // when writing to private use language if the game chat (i'm not sure this should be done in this manner)
            {
                LocalizedStrings.Language = gameProvider.GetLanguageForChat(game.ChatId);
            }

            lock (game)
            {
                try
                {
                    TelemetryStatic.TelemetryClient.Context.Properties[TelemetryStatic.GameKey] = game.Id.ToString();
                    action(game);
                }
                catch (GameCommandException exception)
                {
                    throw new BrakeFlowCallException(exception.Message, exception);
                }
                gameProvider.SaveGame(game);
            }
        }
        public static IState GetStateForChat(UserInChat chat)
        {
            if (!States.ContainsKey(chat))
            {
                States.Add(chat, new InMemoryState(string.Empty, null));
            }

            return(States[chat]);
        }
示例#4
0
        public void Process(Update update, object userState, out IState state)
        {
            var chat       = update.GetChat().ToChat();
            var apiChat    = new ApiChat(_api, chat, _limiter);
            var userInChat = new UserInChat(chat, update.GetUser());
            var locker     = _userInChatLockers.GetOrCreateLocker(userInChat);

            lock (locker)
            {
                state = _stateProvider.GetStateForUserInChat(userInChat);
#if !DEBUG
                try
                {
#endif
                var callContext = new CallContext(apiChat, update, userState, userInChat);

                try
                {
                    if (Process(callContext, state,
                                update))
                    {
                        state.ClearForward();
                    }
                }
                catch (CommandNotFoundException)
                {
                    if (callContext.IsMessage)
                    {
                        callContext.Echo(LocalizedStrings.FlowEngine_UnrecognizedCommand, update.GetMessageId());
                    }
                    else
                    {
                        throw;  //if it was not message update - it's developer fail that was not supported (don't post buttons if you dont support them) //todo: low substitute exception
                    }
                }
                catch (BrakeFlowCallException exception)
                {
                    if (callContext.IsMessage || callContext.IsQuery)
                    {
                        callContext.ReplyEcho(exception.Message);
                    }
                }
#if !DEBUG
            }
            catch (Exception exception)
            {
                throw new ChatException(apiChat, exception.Message, exception);
            }
#endif
            }
        }
示例#5
0
        public void Ready(UserInChat userInChat, GameProvider gameProvider, CallContext context = null)
        {
            string userName = userInChat.UserName;

            if (userName == null)
            {
                throw new BrakeFlowCallException(LocalizedStrings.PublicEngine_ChangeName);
            }
            Trace.WriteLine(userName);
            gameProvider.InvokeGame(userInChat, game =>
            {
                _gameProvider.CheckUserInTelegram(userInChat.UserId, publicChatId: userInChat.ChatId);
                game.Ready(userInChat.ToUser());
                SayPlayersCount(game, context);
            });
        }
示例#6
0
 public IState GetStateForUserInChat(UserInChat userInChat)
 {
     return(InMemoryRootState.GetStateForChat(userInChat));
 }
        public MGM.Game.Game GetGameForUser(UserInChat userInChat, bool isPrivate)
        {
            return(UsingDb(db =>
            {
                {
                    int gameIdToLoad;
                    if (!isPrivate)
                    {
                        //Retrieve game for this chat
                        var chatId = userInChat.ChatId;
                        var chat = db.ChatInTelegrams.ById(chatId);
                        var thisChatGames = chat.Games;

                        var notFinishedGame =
                            thisChatGames.SingleOrDefault(
                                game =>
                                game.FinishTime == null ||
                                game.FinishTime > DateTime.Now - EndState.EndStateMinMinutesTime);
                        if (notFinishedGame == null)
                        {
                            var locker = _gameCreationLockers.GetOrCreateLocker(userInChat.Chat); //todo low clean
                            lock (locker)
                            {
                                notFinishedGame = new Database.DataModels.Game.Game
                                {
                                    ChatInTelegram = chat,
                                    CreationTime = DateTime.Now,
                                    LastAccessTime = DateTime.Now
                                };
                                db.Add(notFinishedGame);
                                db.SaveChanges();
                                db.Refresh(RefreshMode.OverwriteChangesFromStore, notFinishedGame);


                                var newGameP = new MGM.Game.Game(userInChat.Chat.Title,
                                                                 new GameStateBannerProvider(new ApiChat(Api, userInChat.Chat, _limiter)), notFinishedGame.Id,
                                                                 this, this, chatId, this);
                                newGameP.State = new GameState.NewGameState(newGameP);
                                notFinishedGame.SerializedGame = SerializeGame(newGameP);
                                db.SaveChanges();
                                db.Refresh(RefreshMode.OverwriteChangesFromStore, notFinishedGame);
                            }
                        }

                        gameIdToLoad = notFinishedGame.Id;
                    }
                    else
                    {
                        //looking for games with this player, if many - choose first one that is awaiting for vote
                        var userInTelegram = db.UserInTelegrams.ById(userInChat.UserId);

                        var gameOfThrones = db.Players
                                            .Where(player => player.UserInTelegram == userInTelegram)
                                            .Where(player => player.Game.IsNight)
                                            .Where(player => player.Game.FinishTime == null)
                                            .Where(player => player.PutToVoting != null)
                                            .OrderBy(player => player.PutToVoting) //first is earliest
                                            .FirstOrDefault()
                                            ?.Game;

                        if (gameOfThrones == null)
                        {
//если игра не найдена
                            throw new BrakeFlowCallException(LocalizedStrings.GameProvider_NoGamesYouCanVote);
                        }
                        gameIdToLoad = gameOfThrones.Id;
                    }

                    return InternalLoadFromCacheOrDb(gameIdToLoad);
                }
            }));
        }
示例#8
0
 public static User ToUser(this UserInChat userInChat)
 {
     return(new User(userInChat.UserId, userInChat.UserName));
 }