private async void OnTimerElapsed(object state)
        {
            if (_firstLaunch)
            {
                _firstLaunch = false;
                await ClearPlayersMessages();
            }

            var now = DateTime.UtcNow;

            if (DateHelper.DistributionTimeHasCome(now))
            {
                await SendQuestionToAllUsersAsync();
            }

            if (DateHelper.TeamsGenerationTimeHasCome(now) || _teamsController.IsActiveDisliked)
            {
                var players = await SheetController.GetInstance().GetPlayersReadyToPlay();

                await _teamsController.GenerateNewTeams(players);
                await SendGeneratedTeamsMessageAsync();
            }

            if (DateHelper.GameStarted(now))
            {
                await ClearGameAttrsAsync();

                _teamsController.ClearGeneratedTeams();
            }

            await UpdateTotalPlayersMessagesAsync();
            await UpdateTeamPollMessagesAsync();
            await SetPlayersReadyToPlayBySheet();
        }
        private async Task DetermineIfUserIsReadyToPlayAsync(CallbackQuery callbackQuery)
        {
            var playerSetCallback = new PlayerSetCallback(callbackQuery.Data);

            await ClearInlineKeyboardAsync(callbackQuery.Message.Chat.Id, callbackQuery.Message.MessageId);

            try
            {
                await _client.DeleteMessageWithTokenAsync(callbackQuery.Message.Chat.Id, callbackQuery.Message.MessageId);
            }
            catch (Exception ex)
            {
                _logger.Error(ex, $"Error on deleting message");
            }

            if (IsButtonPressedAfterGame(playerSetCallback.GameDate))
            {
                _logger.Information($"Button pressed after game: now - {DateTime.UtcNow}, game date - {playerSetCallback.GameDate.Date}");
                return;
            }

            var player = await _playerRepository.GetAsync(callbackQuery.From.Id);

            await SheetController.GetInstance().UpdateApproveCellAsync(player.Name, GetApproveCellValue(playerSetCallback.UserAnswer));

            player.IsGoingToPlay            = playerSetCallback.UserAnswer == Constants.YES_ANSWER;
            player.ApprovedPlayersMessageId = await SendApprovedPlayersMessageAsync(callbackQuery.Message.Chat.Id, player);

            await _playerRepository.UpdateAsync(player);
        }
Beispiel #3
0
 public static SheetController GetInstance()
 {
     if (_sheetController == null)
     {
         _sheetController = new SheetController();
     }
     return(_sheetController);
 }
Beispiel #4
0
        public async Task UpdateTotalPlayersMessagesAsync()
        {
            var approvedPlayersMessage = await SheetController.GetInstance().GetApprovedPlayersMessageAsync();

            if (approvedPlayersMessage == _approvedPlayersMessage)
            {
                return;
            }

            _approvedPlayersMessage = approvedPlayersMessage;
            await EditMessageAsync(await PlayerRepository.GetRecievedMessageAsync(), _approvedPlayersMessage, Constants.APPROVED_PLAYERS_MESSAGE_TYPE);
        }
 private async Task ClearGameAttrsAsync()
 {
     try
     {
         await SheetController.GetInstance().ClearApproveCellsAsync();
         await ClearPlayersMessages();
     }
     catch (Exception ex)
     {
         _logger.Error(ex, "Clearing game attrs error");
         await _messageController.SendTextMessageToBotOwnerAsync("Ошибка при очищении полей");
     }
 }
        private async Task SetPlayersReadyToPlayBySheet()
        {
            var playersUpdate         = new List <Player>();
            var playersReadyFromSheet = await SheetController.GetInstance().GetPlayersReadyToPlay();

            var playersRecievedMessages = await _playerRepository.GetRecievedMessageAsync();

            foreach (var player in playersRecievedMessages)
            {
                var isGoingToPlay = playersReadyFromSheet.Contains(player.Name);
                if (player.IsGoingToPlay != isGoingToPlay)
                {
                    player.IsGoingToPlay = isGoingToPlay;
                    playersUpdate.Add(player);
                }
            }

            await _playerRepository.UpdateMultipleAsync(playersUpdate);
        }
        /// <summary>
        /// Sends an approved players message if it wasn't sent yet. Otherwise edits it.
        /// </summary>
        /// <param name="chatId">Player chat id</param>
        /// <param name="player">Player</param>
        /// <returns>Sent message id</returns>
        private async Task <int> SendApprovedPlayersMessageAsync(ChatId chatId, Player player)
        {
            var approvedPlayersMessage = await SheetController.GetInstance().GetApprovedPlayersMessageAsync();

            if (player.ApprovedPlayersMessageId != 0)
            {
                try
                {
                    await _client.EditMessageTextWithTokenAsync(chatId, player.ApprovedPlayersMessageId, approvedPlayersMessage);

                    return(player.ApprovedPlayersMessageId);
                }
                catch (Exception ex) // Telegram API doesn't allow to check if user deleted message
                {
                    _logger.Error(ex, $"Error on editing message for user {player.Name}");
                    return((await _client.SendTextMessageWithTokenAsync(chatId, approvedPlayersMessage)).MessageId);
                }
            }

            return((await _client.SendTextMessageWithTokenAsync(chatId, approvedPlayersMessage)).MessageId);
        }