diff --git a/JetKarmaBot/Commands/StatusCommand.cs b/JetKarmaBot/Commands/StatusCommand.cs new file mode 100644 index 0000000..1ff8114 --- /dev/null +++ b/JetKarmaBot/Commands/StatusCommand.cs @@ -0,0 +1,50 @@ +using System; +using System.Linq; +using System.Collections.Generic; +using System.Text; +using Perfusion; +using Telegram.Bot; +using Telegram.Bot.Args; +using Telegram.Bot.Types; + +namespace JetKarmaBot.Commands +{ + class StatusCommand : IChatCommand + { + public IReadOnlyCollection Names => new[] { "status" }; + + public bool Execute(CommandString cmd, MessageEventArgs args) + { + var asker = args.Message.From; + var awardTypeName = cmd.Parameters.FirstOrDefault(); + + string response; + + if (string.IsNullOrWhiteSpace(awardTypeName)) + { + var awards = Db.CountAllUserAwards(asker.Id); + + response = "Your badges report:\n" + + string.Join("\n", awards.Select(a => $" - {Db.AwardTypes[a.AwardTypeId].Symbol} {a.Amount}")); + + } + else + { + var awardTypeId = Db.GetAwardTypeId(cmd.Parameters.FirstOrDefault()); + var awardType = Db.AwardTypes[awardTypeId]; + + response = $"You are at {Db.CountUserAwards(asker.Id, awardTypeId)}{awardType.Symbol} now."; + } + + Client.SendTextMessageAsync( + args.Message.Chat.Id, + response, + replyToMessageId: args.Message.MessageId); + return true; + } + + [Inject(true)] Db Db { get; set; } + [Inject(true)] TelegramBotClient Client { get; set; } + + } +} diff --git a/JetKarmaBot/JetKarmaBot.cs b/JetKarmaBot/JetKarmaBot.cs index 9cc1c85..9196c9e 100644 --- a/JetKarmaBot/JetKarmaBot.cs +++ b/JetKarmaBot/JetKarmaBot.cs @@ -62,6 +62,7 @@ namespace JetKarmaBot Commands = new ChatCommandRouter(Me); Commands.Add(c.ResolveObject(new StartCommand())); Commands.Add(c.ResolveObject(new AwardCommand(Me))); + Commands.Add(c.ResolveObject(new StatusCommand())); } #endregion diff --git a/JetKarmaBot/Services/Db.cs b/JetKarmaBot/Services/Db.cs index 41d16b5..26016a2 100644 --- a/JetKarmaBot/Services/Db.cs +++ b/JetKarmaBot/Services/Db.cs @@ -46,7 +46,7 @@ namespace JetKarmaBot public IReadOnlyDictionary AwardTypes => m_AwardTypes; public IReadOnlyDictionary AwardTypesByCommandName { get; private set; } - public int CountAwards(long userId, byte awardTypeId) + public int CountUserAwards(long userId, byte awardTypeId) { return Conn.QuerySingle ( @@ -55,6 +55,27 @@ namespace JetKarmaBot ) ?? 0; } + public struct UserAwardsReport + { + public int Amount { get; private set; } + public byte AwardTypeId { get; private set; } + } + + public IEnumerable CountAllUserAwards(long userId) + { + return Conn.Query + ( + @"SELECT SUM(amount) AS amount, t.awardtypeid + FROM award a + JOIN awardtype t on a.awardtypeid = t.awardtypeid + WHERE toid = @userId + GROUP BY awardtypeid;", + new { userId } + ); + } + + + public byte GetAwardTypeId(string name) => AwardTypesByCommandName.GetOrDefault(name)?.AwardTypeId ?? DefaultAwardTypeId;