Ejemplo n.º 1
0
        internal async void RenderPoll(HockeyChat chat, int messageId)
        {
            var poll = chat.Polls.FindLast(x => x.MessageId == messageId);

            if (poll == null)
            {
                return;
            }
            _currentPoll = poll;

            var noCnt  = poll.Votes.Count(x => x.Data == "Не");
            var yesCnt = poll.Votes.Count(x => x.Data == "Да");

            try
            {
                await _bot.EditMessageTextAsync(chat.Id, messageId, poll.Report, parseMode : ParseMode.Markdown,
                                                replyMarkup : new InlineKeyboardMarkup(new[]
                {
                    new InlineKeyboardButton
                    {
                        Text         = $"Да – {yesCnt}",
                        CallbackData = "Да"
                    },
                    new InlineKeyboardButton
                    {
                        Text         = $"Не – {noCnt}",
                        CallbackData = "Не"
                    }
                }));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Ejemplo n.º 2
0
        public HockeyPoll GetPollByMessageId(int messageId)
        {
            var cmd = _conn.CreateCommand();

            cmd.CommandText = "SELECT * FROM voting WHERE messageid = " + messageId;

            try
            {
                var reader = cmd.ExecuteReader();

                while (reader.Read() && reader.HasRows)
                {
                    var voting = new HockeyPoll()
                    {
                        Id        = Convert.ToInt32(reader["id"].ToString()),
                        MessageId = messageId,
                        Votes     = null,
                        Question  = reader["question"].ToString()
                    };
                    return(voting);
                }
            }
            catch (SQLiteException ex)
            {
                Console.WriteLine(ex.Message);
            }

            return(null);
        }
Ejemplo n.º 3
0
        internal void UpdatePoll(HockeyChat chat, int msgid, CallbackQuery e)
        {
            var poll = chat.Polls.FindLast(x => x.MessageId == msgid);

            if (poll == null)
            {
                return;
            }
            _currentPoll = poll;

            var user   = e.From;
            var player = _db.GetPlayerByUserid(user.Id);
            var vote   = new Vote(msgid, user.Id, user.Username, player == null ? user.FirstName : player.Name,
                                  player == null ? user.LastName : player.Surname, e.Data);
            var voteDupl = poll.Votes.FindLast(x => x.TelegramUserId == vote.TelegramUserId);

            if (voteDupl != null)
            {
                if (voteDupl.Data == vote.Data)
                {
                    return;
                }

                voteDupl.Data = vote.Data;
                _db.UpdateVoteData(msgid, vote.TelegramUserId, vote.Data);
            }
            else
            {
                AddVoteToPoll(poll, vote);
            }
        }
Ejemplo n.º 4
0
 internal void DeleteVoteFromPoll(HockeyPoll poll, Vote vote)
 {
     poll.Votes.RemoveAll(v => v.MessageId == vote.MessageId &&
                          v.Name == vote.Name &&
                          v.TelegramUserId == vote.TelegramUserId &&
                          v.Data == vote.Data &&
                          v.Surname == vote.Surname &&
                          v.Username == vote.Username);
     _db.DeleteVote(vote);
 }
Ejemplo n.º 5
0
        public void AddPoll(HockeyPoll voting)
        {
            var cmd = _conn.CreateCommand();

            cmd.CommandText = $"INSERT INTO voting (messageid, question) VALUES({voting.MessageId}, '{voting.Question}')";

            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (SQLiteException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Ejemplo n.º 6
0
        private async void AddPoll(HockeyChat chat, string arg)
        {
            chat.VoteMode = false;
            var btnYes = new InlineKeyboardButton
            {
                Text         = "Да",
                CallbackData = "Да"
            };
            var btnNo = new InlineKeyboardButton
            {
                Text         = "Не",
                CallbackData = "Не"
            };
            var btnShow = new InlineKeyboardButton
            {
                Text         = "Показать результаты",
                CallbackData = "Show"
            };
            var keyboard = new InlineKeyboardMarkup(new[] { new [] { btnYes, btnNo }, new[] { btnShow } });

            var msg = await _bot.SendTextMessageAsync(chat.Id, $"{arg}", replyMarkup : keyboard);

            var v    = new List <Vote>();
            var poll = new HockeyPoll()
            {
                MessageId = msg.MessageId, Votes = v, Question = arg
            };

            _currentPoll = poll;

            _db.AddPoll(poll);
            var addedPoll = _db.GetPollByMessageId(poll.MessageId);

            poll.Id = addedPoll.Id;

            chat.Polls.Add(poll);
        }
Ejemplo n.º 7
0
 internal void AddVoteToPoll(HockeyPoll poll, Vote vote)
 {
     poll.Votes.Add(vote);
     _db.AddVote(vote);
 }